<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="ui" uri="http://egovframework.gov/ctl/ui"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%   
response.setHeader("Cache-Control","no-store");   
response.setHeader("Pragma","no-cache");   
response.setDateHeader("Expires",0);   
if (request.getProtocol().equals("HTTP/1.1")) response.setHeader("Cache-Control", "no-cache"); 
%> 
<!DOCTYPE html>
<html lang="ko">

<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge" />
	<title>납품장비물러오기</title>
	<link rel="stylesheet" href="/direct/css/font.css">
	<link rel="stylesheet" href="/direct/css/reset.css">
	<link rel="stylesheet" href="/direct/css/enroll_popup.css">
	<link rel="stylesheet" href="/direct/css/nice-select.css">
	<!--[if IE]>
    <link rel="stylesheet" type="text/css" href="/direct/css/ie_popup.css" />
    <![endif]-->
	<script src="/direct/js/jquery-1.11.3.min.js"></script>
	<script src="/direct/js/jquery-ui.min.js"></script>
	<script src="/direct/js/jquery.nice-select.js"></script>
	<script src="/direct/js/script.js"></script>
	<script src="/direct/js/popup_open_2.js"></script>
	<script type="text/javascript" src="/js/audit/jquery.form.min.js"></script>
	<script>
		$(document).ready(function() {
			$('select').niceSelect();
		});
		
		function total_check(object){
			if($(object).is(':checked')){
				$('#companyAssetsList').find('input[type=checkbox]').prop("checked", true);
			}else{
				$('#companyAssetsList').find('input[type=checkbox]').prop("checked", false);
			}
		}
		
		function searchAssets(){
			var searchKeyword = $('#searchKeyword').val();
			var companyId = $('#companyId').val();
			$.ajax({
			    type: "POST",
			    url: "/uss/itsm/company/selectCompanyAssetsAjax.do",
			    data:{  "searchKeyword": searchKeyword, "companyId":companyId },
			    dataType:'jsonp',
			    cache: false,
			    async: false,
			    success: function (returnData, status) {
			    	if(status == 'success'){
			        	if(returnData.result == 'fail'){
			        		alert("생성에 실패하였습니다.");
			        	}else if(returnData.result == 'auth_fail'){
			        		alert("세션이 종료되었습니다.");
			        	}else if(returnData.result =='success'){

			        		var searchAssetsInfo =  returnData.companyAssetsList;
			        		$("#companyAssetsList > tr").remove();

			        		if(searchAssetsInfo.length == 0){
			        			var tempTr = "<tr class='noSerchTr'>";
								tempTr += "<td class='noSerch'>검색 내역이 없습니다.</td>";
								tempTr += "</tr>";
								$("#companyAssetsList").append(tempTr);
								
							}else{
								
								for(var i=0; i < searchAssetsInfo.length; i++){
									
									var tableIndex = Number(i)+1;
									var tempCompanyAssetsTable = $('#temp_company_assets_table').clone();
		 			        		tempCompanyAssetsTable = tempCompanyAssetsTable.find('tr') ;
		 			        		tempCompanyAssetsTable.addClass('item_td_temp') ; //추가 행에 class 추가
		 			        		$("#companyAssetsList").append(tempCompanyAssetsTable);
		 			        		
		 			        		$('.item_td_temp').find("td").each(function(index, item){  //추가한 행에 id 세팅함.
		 	 		        			if($(this).attr('id') == 'temp_company_assets_checkbox'){
		 	 		        				var check="<input type='checkbox' name='assets'  id='ast_"+tableIndex+"' value='"+searchAssetsInfo[i].astId+"'>"
		 	 		        				check+="<label for='ast_"+tableIndex+"'></label>"
		 	 		        				$(this).html(check);
		 	 		        			}
		 	 		        			if($(this).attr('id') == 'temp_company_assets_mak'){
		 	 		        				$(this).text(searchAssetsInfo[i].companyAstMak);	
		 	 		        			}
										if($(this).attr('id') == 'temp_company_assets_model'){
											$(this).text(searchAssetsInfo[i].companyAstModel);
										}
										if($(this).attr('id') == 'temp_company_assets_cnt'){
											$(this).text(searchAssetsInfo[i].companyAstCnt);
										}
										if($(this).attr('id') == 'temp_company_assets_acqDate'){
											$(this).text(searchAssetsInfo[i].companyAstAcqDate);
										}
										if($(this).attr('id') == 'temp_company_assets_acqPrice'){
											$(this).text(searchAssetsInfo[i].companyAstAcqPrice);
										}
										if($(this).attr('id') == 'temp_company_assets_dc'){
											$(this).text(searchAssetsInfo[i].companyAstDc);
										}
		 			        		});
		 			        		$('.item_td_temp').attr('class' , searchAssetsInfo[i].astId) ; //td 클래스 이름변경
								}
							}
			        	}
			        }else{
			        	alert("생성에 실패하였습니다.");
			        }
			    },
			    error: function (e) {
			        alert("생성에 실패하였습니다.");
			    }
			});
		}
		
		/* 등록버튼 클릭시 임시파일들 N으로 업데이트 */
		function insertAssets(){
			if($("input[name=assets]:checked").length == 0){
				alert("선택된 항목이 없습니다.");
				return;
			}
			if(!confirm("등록하시겠습니까?")){
				return false;
			}
			var frm = document.listForm;
			var data = new FormData(frm);
			$.ajax({
			    type: "POST",
			    url: "/uss/itsm/company/insertCompanyAssetsAjax.do",
			    data: data,
			    dataType:'jsonp',
			    async: false,
			    processData: false,
			    contentType: false,
			    cache: false,
			    timeout: 600000,
			    success: function (returnData, status) {
			    	if(status == 'success'){
			        	if(returnData.result == 'fail'){
			        		alert("등록에 실패하였습니다.");
			        	}else if(returnData.result == 'auth_fail'){
			        		alert("세션이 종료되었습니다.");
			        		if(opener !=null){
			        			opener.location.reload();
			        		}
			        		window.self.close() ;
			        	}else if(returnData.result =='success'){
			        		alert("등록되었습니다.");
			        		if(opener != null){
			        			opener.self_reload();
			        		}
			        		window.self.close() ;
			        	}
			        }else{
			        	alert("등록에 실패하였습니다.");
			        }
			    },
			    error: function (e) {
			        alert("등록에 실패하였습니다.");
			    }
			});
		}
		
		function linkPage(pageNo){
			var searchForm = document.searchForm ;
			searchForm.pageIndex.value = pageNo ;
			searchForm.searchKeyword.value = $('#searchKeyword').val();
			searchForm.submit();
		}
	</script>
	<style type="text/css">
		 _:-ms-input-placeholder, :root .enterprise_right_table table thead { width: 733px !important; }
		 _:-ms-input-placeholder, :root .enterprise_right_table table tbody.eq_List { width: 725px !important; }
		 .enroll_popup.enterprise_call_popup{height:540px;}
		 .enroll_popup.enterprise_call_popup .main1_btn_div{padding:3px 0;}
	</style>
</head>

<body>
	<form name="listForm" id="listForm" method="post">
		<input type="hidden" name="companyId" id = "companyId" value="${companyMngVO.companyId}"/>
		<div class="enroll_popup enterprise_call_popup">
			<div class="enroll_popup_title asset_popup_title">
				<img src="/direct/img/enroll_popup_title_bg.png" alt=""> 납품장비 불러오기
				<!-- <div class="enroll_popup_title_btns">
					<ul>
						<li><img src="/direct/img/popup_little_icon.png" alt=""></li>
						<li><img src="/direct/img/popup_full_icon.png" alt=""></li>
						<li><img src="/direct/img/popup_close_icon.png" alt=""></li>
					</ul>
				</div> -->
			</div>
			<div class="enroll_input_left asset_exposure_list">
				<ul>
					<li>
						검&emsp;&emsp;색
						<input type="text" class="business_call_serch" id="searchKeyword" name="searchKeyword" title="검색" style="width:590px;" value="${companyMngVO.searchKeyword}"/>
						<input type="button" value="검색" class="serch_btn" onclick="searchAssets()">
					</li>
					<li>
						<div class="enterprise_right_table eq_right_pp">
							<table>
								<thead class="eq_tList">
									<tr>
										<th>
											<input type="checkbox" id="all" onclick="total_check(this);">
											<label for="all"></label>
										</th>
										<th class="eqm">제조사</th>
										<th class="eqt">모델명</th>
										<th class="eqn">수량</th>
										<th class="eqd">도입일자</th>
										<th class="eqp">도입금액</th>
										<th class="eqe">비      고</th>
									</tr>
								</thead>
								<tbody id="companyAssetsList" class="eq_List">
									<c:forEach var="companyAssetsInfo" items="${companyAssetsList}" varStatus="status">
										<tr>
											<td>
												<input type="checkbox" name="assets"  id="bus_${status.index}" value="${companyAssetsInfo.astId}"
												
												<c:if test="${companyAssetsInfo.selectFlag eq 'Y' }">
												checked="checked" disabled="disabled"
												</c:if>
												>
												
												<label for="bus_${status.index}"></label>
											</td>
											<td class="eqm"><span class="eqListEqm">${companyAssetsInfo.companyAstMak}</span></td>
											<td class="eqt"><span class="eqListEqt">${companyAssetsInfo.companyAstModel}</span></td>
											<td class="eqn"><span class="eqListEqn">${companyAssetsInfo.companyAstCnt}</span></td>
											<td class="eqd"><span class="eqListEqd">${companyAssetsInfo.companyAstAcqDate}</span></td>
											<td class="eqp"><span class="eqListEqp">${companyAssetsInfo.companyAstAcqPrice}</span></td>
											<td class="eqe"><span class="eqListEqe">${companyAssetsInfo.companyAstDc}</span></td>
										</tr>
									</c:forEach>
								</tbody>
							</table>
						</div>
					</li>
				</ul>
			</div>
			<div class="page">
	            <ul>
	                <ui:pagination paginationInfo = "${paginationInfo}"  type="image" jsFunction="linkPage" />
	            </ul>
			</div>
			<div class="main1_btn_div">
				<input type="button" class="save_btn" value="저장" onclick="insertAssets()">
				<input type="button" class="close_btn" value="닫기" onclick="window.self.close()"/>
			</div>
		</div>
		<input type="hidden" name="asd">
	</form>
	<table id="temp_company_assets_table" style="display: none">
		<tr>
			<td id="temp_company_assets_checkbox"></td>
			<td class="eqm" id="temp_company_assets_mak"></td>
			<td class="eqt" id="temp_company_assets_model"></td>
			<td class="eqn" id="temp_company_assets_cnt"></td>
			<td class="eqd" id="temp_company_assets_acqDate"></td>
			<td class="eqp" id="temp_company_assets_acqPrice"></td>
			<td class="eqe" id="temp_company_assets_dc"></td>
		</tr>	
	</table>
	<form name="searchForm" method="get" action="<c:url value='/uss/itsm/company/CompanyAssetsView.do'/>">
		<input name="pageIndex" type="hidden" value="1" />
		<input name="searchCondition" type="hidden" />
		<input name="searchKeyword" type="hidden" />
		<input name="pageUnit" type="hidden" value="10" />
	</form>	
</body>
</html>