<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c"  uri="http://java.sun.com/jsp/jstl/core" %>
<script src="IDC/js/setToken.js"></script>
<script type="text/javascript">
//전역변수
var ul;
$(function()
{
	ul = $("#codeToggle");
	//ajax로 가져온 list 저장 변수
	var arr;
	//처음에 선택된것이 없으니까 2번 셀렉트박스와 3번 입력값이 없어야 한다.
	$("#specDiv,#cpuNameDiv").hide();
	//1번 셀렉트 박스 눌렀을 때 할 일.
	$(document).on("click","#firMenu > li > a", function()
	{
		var val = arr[$(this).attr("value")].a[0];
		var len = val.cpuname.length;
		
		//선택한 것으로 process 데이터 준다..
		$("#process").val($(this).html());
		$("#process").prev().val($(this).html());
		//2번 선택박스 보여준다.
		$("#specDiv").show();
		
		//3번 cpuname 가려준다.
		$("#cpuNameDiv").hide();
		
		//4번 2번 선택박스 그려주기 위한 이전 정보 날려주기
		$("#secMenu").empty();
		
		//5번 cpuname value값 날려버린다.
		$("#spec,#cpuname").val("");
		
		for(var i=0; i<len ;i++)
		{
			var li = $("<li>");
			var a = $("<a>");
			a.val(val.cpuname[i]).addClass("btn btn-default").html(val.spec[i]).attr("price",val.cost[i]);
			li.append(a);
			$("#secMenu").append(li);
		}
	});
	//2번 셀렉트 박스의 데이터 눌렀을 경우 할일
	$(document).on("click","#secMenu > li > a", function()
	{
			$("#spec").val($(this).html());
			$("#spec").prev().val($(this).html());
			
			$("#cpuNameDiv").show();
			
			$("#cpuname").val($(this).val());
			$("#cpuname").prev().val($(this).val());
			$("#server_cost").val($(this).attr("price"));
	});
	//Ajax로 JSON데이터 가져오기
	$.get("IdcServerListJSON.jso",function(data)
	{
		//전역변수 arr
		arr = data;
		for(var i=0;i<arr.length;i++)
		{
			var li = $("<li>");
			var a = $("<a>");
			a.attr("value",i).addClass("btn btn-default").html(arr[i].id);
			
			li.append(a);
			$("#firMenu").append(li);
		}
	});
	
	//유효성 검사하기.
	$("#sendForm").click(function()
	{
		var check = false;
		//사양 선택 되었는가?
		$("#process,#spec").each(function()
		{
			if($(this).val()!="")
				check=true;
			else
			{
				check=false;
				alert("서버사양을 선택하여 주십시오.");
				return;
			}
		});
		//장비명이 중복되지 않았는가?
		$("#codeToggle > li > a").each(function()
		{
			if($(this).html()==$("#server_code").val().toLocaleUpperCase())
			{
				check=false;
				alert("이미 등록되어있는 장비명 입니다.");
				return;
			}
		});
		
		//하드 및 램 입력 확인
		var server_ram1 = $("#server_ram1").val();
		var server_ram2 = $("#server_ram2").val();
		var server_ram3 = $("#server_ram3").val();
		var server_ram4 = $("#server_ram4").val();
		var server_ram5 = $("#server_ram5").val();
		
		if(server_ram1 == "" && server_ram2 == "" && server_ram3 == "" && server_ram4 == "" && server_ram5 == "" ){
			alert("램을 입력해 주세요");
			return;
		}
		
		var server_hdd1 = $("#server_hdd1").val();
		var server_hdd2 = $("#server_hdd2").val();
		var server_hdd3 = $("#server_hdd3").val();
		var server_hdd4 = $("#server_hdd4").val();
		var server_hdd5 = $("#server_hdd5").val();
				
		if(server_hdd1 == "" && server_hdd2 == "" && server_hdd3 == "" && server_hdd4 == "" && server_hdd5 == "" ){
			alert("하드를 입력해 주세요");
			return;
		}
		
		if(check) $("#form1").attr("action","serverWrite.ser").submit();
	});
	
	$("#server_code").bind("input",function()
	{
		checkServerCode($(this).val());
		$("#codeToggle").show();
	});
	$(document).click(function()
	{
		$("#codeToggle").hide();
	});
	//가용 여부 바뀔 시
	$("#useSelect").change(function()
	{
		if($(this).val()=="y")
		{
			$("#server_fomat_date").val(null);
		}
	});
	//서버 포맷일 바뀔경우 체크
	$("#server_fomat_date").change(function()
	{
		if($(this).val()=="")
			$("#useSelect").val("y");
		else
			$("#useSelect").val("n");
	});
	
	function check_ram(){
		//alert("check_Ram");	
		var server_ram1 = $("#server_ram1").val();
		var server_ram2 = $("#server_ram2").val();
		var server_ram3 = $("#server_ram3").val();
		var server_ram4 = $("#server_ram4").val();
		var server_ram5 = $("#server_ram5").val();
		
		if(server_ram1 == "" && server_ram2 == "" && server_ram3 == "" && server_ram4 == "" && server_ram5 == "" ){
			alert("램을 입력해 주세요");
			return;
		}
		
		var server_hdd1 = $("#server_hdd1").val();
		var server_hdd2 = $("#server_hdd2").val();
		var server_hdd3 = $("#server_hdd3").val();
		var server_hdd4 = $("#server_hdd4").val();
		var server_hdd5 = $("#server_hdd5").val();
				
		if(server_hdd1 == "" && server_hdd2 == "" && server_hdd3 == "" && server_hdd4 == "" && server_hdd5 == "" ){
			alert("하드를 입력해 주세요");
			return;
		}
		return true;
	}
	
});
//서버코드 뿌려주기.
var checkServerCode = function(code)
{
	ul.empty();
	var str = "server_code="+code;
	$.ajax(
	{
		url:"IdcGetServerCodeJSON.jso",
		type:"get",
		data : str,
		dataType:"json",
		timeout:"30000",
		cache:false,
		success : function(data)
		{
			var data = data[0].code;
			for(var i=0;i<data.length;i++)
			{
				var li=$("<li>");
				var a=$("<a>");
				a.addClass("btn btn-default").html(data[i]);
				li.append(a);
				ul.append(li);
			}
		},
		error : function()
		{
			alert("오류");
		}
	});	
}
</script>
<!-- 여기가 본 페이지이다 -->
<div class="row">
	<div class="col-lg-12" style="max-width: 1800px">
		<div class="panel panel-default">
			<div class="panel-heading">
				<h4><i class="fa fa-desktop fa-fw"></i> 새 서버 등록</h4>
			</div>
			<div class="panel-body">
				<div class="row">
					<div class="col-lg-12">
					</div>
				</div>
				<!-- row -->
				<form id="form1" method="post">
		   			<input type="hidden" id="token" name="token">
		   			<div class="table-responsive">
						<table class="table table-bordered table-hover table-striped">
							<thead>
							</thead>
							<tbody>
								<tr>
									<td style="min-width: 85px">가용 여부</td>
									<td colspan="4">										
										<select name="server_use_status" id="useSelect">
											<option value="y">가용가능</option>
											<option value="n">가용불가</option>																	
										</select>
									</td>				
								</tr>
								<tr>
									<td>장비명</td>
									<td colspan="4">
										<input type="text" id="server_code" name="server_code" maxlength="50" size="50" value="${server_code}">
										<div class="input-group-btn">
											<ul class="dropdown-menu" role="menu" id="codeToggle">
											</ul>
										</div>
									</td>
								</tr>
								<tr>
									<td>사양</td>
									<td colspan="4">
										<div class="col-lg-3" style="padding:0px; padding-right:35px">
											<div class="input-group">
												<div class="input-group-btn">
												<input type="hidden" name="server_process">
												<input type="button" id="process" class="form-control" value="" readOnly="readOnly" >
													<ul class="dropdown-menu" role="menu" id="firMenu">
													</ul>
													<button type="button" class="btn btn-default" id="firToggle" data-toggle="dropdown"><span class="caret"></span></button>
												</div>
											</div>
										</div>
										<div class="col-lg-3" style="padding:0px; padding-right:35px" id="specDiv">
											<div class="input-group">
												<div class="input-group-btn">
													<input type="hidden" name="server_spec">
													<input type="button" id="spec"  class="form-control" value="" readOnly="readOnly" >
													<ul class="dropdown-menu" role="menu" id="secMenu">
													</ul>
													<button type="button" class="btn btn-default" id="secToggle" data-toggle="dropdown"><span class="caret"></span></button>
												</div>
											</div>
										</div>
										<div class="col-lg-3" style="padding:0px; padding-right:35px" id="cpuNameDiv">
											<div class="input-group">
												<div class="input-group-btn">
													<input type="hidden" name="server_cpuname">
													<input type="button" id="cpuname" class="form-control" value="" readOnly="readOnly" >
												</div>
											</div>
										</div>
										<div class="col-lg-3">
											<div class="input-group">
												<input type="text" class="form-control" id="server_cost" name="server_cost">
											</div>
										</div>
									</td>
								</tr>
								<tr>
									<td>ram</td>
									<td colspan="2">										
										<select name="server_ram1" id="server_ram1">
											<option value="">없음</option>
											<option value="4g">4g</option>
											<option value="8g">8g</option>
											<option value="16g">16g</option>
											<option value="24g">24g</option>
											<option value="32g">32g</option>
											<option value="64g">64g</option>
											<option value="128g">128g</option>
											<option value="256g">256g</option>																												
										</select>
										<select name="server_ram2" id="server_ram2">
											<option value="">없음</option>
											<option value="4g">4g</option>
											<option value="8g">8g</option>
											<option value="16g">16g</option>
											<option value="24g">24g</option>
											<option value="32g">32g</option>
											<option value="64g">64g</option>
											<option value="128g">128g</option>
											<option value="256g">256g</option>																												
										</select>
										<select name="server_ram3" id="server_ram3">
											<option value="">없음</option>
											<option value="4g">4g</option>
											<option value="8g">8g</option>
											<option value="16g">16g</option>
											<option value="24g">24g</option>
											<option value="32g">32g</option>
											<option value="64g">64g</option>
											<option value="128g">128g</option>
											<option value="256g">256g</option>																												
										</select>
										<select name="server_ram4" id="server_ram4">
											<option value="">없음</option>
											<option value="4g">4g</option>
											<option value="8g">8g</option>
											<option value="16g">16g</option>
											<option value="24g">24g</option>
											<option value="32g">32g</option>
											<option value="64g">64g</option>
											<option value="128g">128g</option>
											<option value="256g">256g</option>																												
										</select>										
									</td>
									<td>직접입력: <Input type="text" name="server_ram5" id="server_ram5"/></td>									
								</tr>
								<tr>
									<td>hdd</td>
									<td colspan="2">
										<select name="server_hdd1" id="server_hdd1">
											<option value="">없음</option>
											<option value="SATA 300G">SATA 300G</option>
											<option value="SATA 500G">SATA 500G</option>
											<option value="SSD 128G">SSD 128G</option>
											<option value="SSD 240G">SSD 240G</option>
											<option value="SSD 480G">SSD 480G</option>
											<option value="SSD 960G">SSD 960G</option>
										</select>
										<select name="server_hdd2" id="server_hdd2">
											<option value="">없음</option>
											<option value="SATA 300G">SATA 300G</option>
											<option value="SATA 500G">SATA 500G</option>
											<option value="SSD 128G">SSD 128G</option>
											<option value="SSD 240G">SSD 240G</option>
											<option value="SSD 480G">SSD 480G</option>
											<option value="SSD 960G">SSD 960G</option>
										</select>
										<select name="server_hdd3" id="server_hdd3">
											<option value="">없음</option>
											<option value="SATA 300G">SATA 300G</option>
											<option value="SATA 500G">SATA 500G</option>
											<option value="SSD 128G">SSD 128G</option>
											<option value="SSD 240G">SSD 240G</option>
											<option value="SSD 480G">SSD 480G</option>
											<option value="SSD 960G">SSD 960G</option>
										</select>
										<select name="server_hdd4" id="server_hdd4">
											<option value="">없음</option>
											<option value="SATA 300G">SATA 300G</option>
											<option value="SATA 500G">SATA 500G</option>
											<option value="SSD 128G">SSD 128G</option>
											<option value="SSD 240G">SSD 240G</option>
											<option value="SSD 480G">SSD 480G</option>
											<option value="SSD 960G">SSD 960G</option>
										</select>
									</td>
									<td>직접입력: <Input type="text" name="server_hdd5" id="server_hdd5"/></td>
								</tr>
								<tr>
									<td>브랜드</td>
									<td colspan="4">										
										<select name="server_brand">
											<option value="HP">HP</option>
											<option value="IBM">IBM</option>
											<option value="Dell">Dell</option>
											<option value="Fujitsu">Fujitsu</option>
											<option value="Cisco">Cisco</option>
											<option value="SuperMicro">SuperMicro</option>																													
										</select>
									</td>									
								</tr>
								<tr>
									<td>위치</td>
									<td colspan="4">
										<select name="server_location">
											<option value="chiba">Irvine</option>
										</select>
									</td>
								</tr>
								<tr>
									<td>포맷 보류</td>
									<td colspan="4">
										<input type="date" id="server_fomat_date" name="server_fomat_date" maxlength="50" size="50">
									</td>
								</tr>
								<tr>
									<td>입고일</td>
									<td colspan="4">
										<input type="date" name="server_recieve_date" maxlength="50" size="50" value="${curdate}">
									</td>
								</tr>
								<tr>
									<td>출고일</td>
									<td colspan="4">
										<input type="date" id="server_release_date" name="server_release_date"  value="${curdate}">
									</td>
								</tr>
								<tr>
									<td>비고</td>
									<td colspan="4">
										<textarea name="server_note" rows="8" cols="100"></textarea>
									</td>
								</tr>
							</tbody>
							<tfoot>
								<tr>
									<td colspan="5" align="right">
										<Button id="sendForm"class="btn btn-outline btn-primary " type="button">서버 등록</Button>
										<a class="btn btn-outline btn-default " href="serverList.ser">취소</a>		
									</td>
								</tr>
							</tfoot>
						</table>
					</div>
					<!-- table-responsive -->
				</form>
			</div>
			<!-- panel-body -->
		</div>
		<!-- panel panel-default -->
	</div>
	<!-- col-lg-12 -->
</div>
<!-- row -->
<!-- /#page-wrapper -->
