<%--
  - FileName   : ShippingTargetSearchPopup.jsp
  - Author(s)  : gyeongmo an
  - Date       : 2014.08.13
  - Description: 선적 대상 조회
  --%>
<%@ page contentType="text/html; charset=utf-8" %>
<%@ page import="kr.co.hsnc.common.sql.RowSet" %>
<%@ page import="kr.co.hsnc.common.util.ValueObject" %>
<%@ page import="com.udapsoft.waf.system.*" %>
<%@ page import="com.udapsoft.waf.common.*" %>
<%@ page import="kr.co.udapsoft.common.*" %>
<%@ page import="com.udapsoft.waf.common.ui.HTMLMaker" %>
<%@ page import="com.udapsoft.waf.system.HandlerStorage" %>
<%@ page import="kr.co.hsnc.common.sql.*" %>
<%@ page import="kr.co.hsnc.common.util.*"%>
<%@ page import="com.udapsoft.waf.common.ui.*" %>
<%@page import="java.net.URLDecoder"%>
<%@ include file="/jsp/work/taglib.jsp" %>
<jsp:useBean id="ctx" class="com.udapsoft.waf.system.context.SessionContext" scope="session" />
<%
    ctx.init(request, response);

    HandlerStorage storage = ctx.getHandlerStorage();
    ValueObject user       = ctx.getUser();
    ValueObject params     = storage.getParams();
    String fevent           = storage.getEvent();
%>

<SCRIPT language="JavaScript">
var sheet1;
var sheet1Row;
var sheet1PageNo = 1;
var pageLastRowIndex = 1;
var validationResult = false;
var filterShow = false;

var sheet2;
var sheet2Row;
var sheet2PageNo = 1;

// 마지막으로 선택된 행의 번호를 기억하고 있음
var CurMasterRow = 0;
	
    function _onReady(){
		gridInit(sheet1);
		gridInit(sheet2);

		//autoResize();
		
		jQuery("#btnSearch").click(function() {
        	jQuery("#fevent").val("GET_SHIPPING_TARGET1");
        	var url = Modular.model.PageContext.CONTEXT_ROOT + "/process/process/ShippingTargetSearch_sheet.screen";
        	Modular.view.IBSheet7.doSearch(sheet1, url, jQuery("#form1"), 30, "");
		});
		
		jQuery("#btnChoice").click(function() {
			var temp = "N";
			var material_cd		= new Array;	// 자재코드
	     	var material_name 	= new Array;	// 자재명
	     	var material_spec = new Array;
	     	var unit_kind_name 		= new Array;	// 단위명
	     	var jan_qty 		= new Array;	
	     	var ship_qty = new Array;
	     	var shp_unit_cost = new Array;
	     	var shp_amt = new Array;
	     	var pur_cont_id = new Array;
	     	var ord_doc_no = new Array;
	     	var cst_doc_name = new Array;
	     	var currency_gb = new Array;
	     	var oversea_gb = new Array;
	     	var corp_id = new Array;
	     	var material_gb  = new Array;
				
        	if(sheet2.LastRow() >= 1){
           		var idx = 0;
               	for(var i=1; i<=sheet2.LastRow(); i++){
              		var tmpArray = sheet2.GetCellValue(i,"check");
         	 		if(tmpArray=="1"){
         	 			temp = "Y";
         	 			material_cd[idx] 		= sheet2.GetCellValue(i,"material_cd");
         	 			material_name[idx] 		= sheet2.GetCellValue(i,"material_name"); 
         	 			material_spec[idx] 		= sheet2.GetCellValue(i,"material_spec"); 
         	 			unit_kind_name[idx] 	= sheet2.GetCellValue(i,"item_unit");    	
         	 			ship_qty[idx] 	= sheet2.GetCellValue(i,"ship_qty");   	  
         	 			shp_unit_cost[idx] 	= sheet2.GetCellValue(i,"shp_unit_cost");   
         	 			shp_amt[idx] 	= sheet2.GetCellValue(i,"shp_amt");   	  
         	 			pur_cont_id[idx] 	= sheet2.GetCellValue(i,"pur_cont_id");   	  
         	 			ord_doc_no[idx] 	= sheet2.GetCellValue(i,"ord_doc_no");   	  
         	 			cst_doc_name[idx] 	= sheet2.GetCellValue(i,"cst_doc_name");   	  
         	 			currency_gb[idx] 	= sheet2.GetCellValue(i,"currency_gb");   	  
         	 			oversea_gb[idx] 	= sheet1.GetCellValue(1,"oversea_gb");   	  
         	 			corp_id[idx] 	= sheet1.GetCellValue(1,"corp_id");   	  
         	 			material_gb[idx] 	= sheet2.GetCellValue(i,"material_gb");   	  
			     		idx++; 
			     		 
	                } 
  	          	}
               	if(temp == "N") {
               		alert("자재를 선택해 주세요.");
               		return;
               	}
    	    }

	        var returnValue = new Array(material_cd, material_name, material_spec, unit_kind_name, ship_qty, shp_unit_cost, shp_amt, pur_cont_id, ord_doc_no, cst_doc_name, currency_gb, oversea_gb, corp_id, material_gb);
			setTimeout(function(){parent.SearchShippingTargetSearch(returnValue);},100);
		});
		
		jQuery("#btnClose").click(function() {
			setTimeout(function(){parent.SearchShippingTargetSearch(null);},100);
		});
		
		jQuery("#btnSearch").click();
		
	}
    
    function gridInit(sheetName){
		var init_sheet = {};
		init_sheet.Cfg = {SearchMode:2, Page:30};
		
		if(sheetName.id == "sheet1") {
			init_sheet.Cols = [
				    			{Header:'PO No',	Type:"Text",	SaveName:"ord_doc_no",		Width:130, 	Align:"Center", Edit:"false"},
				    			{Header:'PO Title',	Type:"Text",	SaveName:"cst_doc_name",		Width:200, 	Align:"Left", Edit:"false"},
				       			{Header:'계약ID',	Type:"Text",	SaveName:"pur_cont_id",		Width:100, 	Align:"Center", Edit:"false", Hidden:"True"},
				       			{Header:'국내외구분',	Type:"Text",	SaveName:"oversea_gb",		Width:100, 	Align:"Center", Edit:"false", Hidden:"True"},
				       			{Header:'Vendor',	Type:"Text",	SaveName:"vendor_name",		Width:100, 	Align:"Left", Edit:"false"},
				       			{Header:'업체코드',	Type:"Text",	SaveName:"corp_id",		Width:100, 	Align:"Left", Edit:"false", Hidden:"True"}
			];
		} else {
			init_sheet.Cols = [
				       			{Header:'check',	Type:"CheckBox",	SaveName:"check",		Width:60, 	Align:"Center"},
				    			{Header:'Material Code',	Type:"Text",	SaveName:"material_cd",		Width:130, 	Align:"Left", Edit:"false"},
				    			{Header:'Material Name',	Type:"Text",	SaveName:"material_name",		Width:280, 	Align:"Left", Edit:"false"},
				    			{Header:'Material Spec',	Type:"Text",	SaveName:"material_spec",		Width:150, 	Align:"Left", Edit:"false"},
				    			{Header:'Material Unit',	Type:"Text",	SaveName:"item_unit",		Width:50, 	Align:"Center", Edit:"false"},
				    			{Header:'PO Qty',	Type:"Float",	SaveName:"ship_qty",		Width:80, 	Align:"Right", Edit:"false", PointCount:2},
				    			{Header:'PO Price',	Type:"Float",	SaveName:"shp_unit_cost",		Width:80, 	Align:"Right", Edit:"false", PointCount:2},
				    			{Header:'PO Amount',	Type:"Float",	SaveName:"shp_amt",		Width:80, 	Align:"Right", Edit:"false", PointCount:2},
				    			{Header:'계약ID',	Type:"Text",	SaveName:"pur_cont_id",		Width:100, 	Align:"Center", Edit:"false", Hidden:"True"},
				    			{Header:'계약번호',	Type:"Text",	SaveName:"ord_doc_no",		Width:100, 	Align:"Center", Edit:"false", Hidden:"True"},
				    			{Header:'계약명',	Type:"Text",	SaveName:"cst_doc_name",		Width:100, 	Align:"Left", Edit:"false", Hidden:"True"},
				    			{Header:'통화구분',	Type:"Text",	SaveName:"currency_gb",		Width:100, 	Align:"Left", Edit:"false", Hidden:"True"},
				    			{Header:'자재분류코드',	Type:"Text",	SaveName:"material_gb",		Width:100, 	Align:"Left", Edit:"false", Hidden:"True"}
			];
		}

		init_sheet.HeaderMode = {Sort:1};
		IBS_InitSheet(sheetName, init_sheet);
		sheetName.SetDataAutoTrim(0);
		setGridSize(sheetName);
	}
 
 	function setGridSize(sheetName) {
		sheetName.FitColWidth();
	}
 
 	
 	
 	function fistRowSearch(){
    	var f = document.form1;
    	f.pur_cont_id.value = sheet1.GetCellValue(1, "pur_cont_id");
    	jQuery("#fevent").val("GET_SHIPPING_TARGET2");
    	var url = Modular.model.PageContext.CONTEXT_ROOT + "/process/process/ShippingTargetSearch_sheet.screen";
    	Modular.view.IBSheet7.doSearch(sheet2, url, jQuery("#form1"), 30, "");
 	}

    function sheet1_OnSearchEnd(code, message) {
	    if(code == 0) {
	    	setGridSize(sheet1);
	    	fistRowSearch();
		} else {
			//조회 에러 수행
			alert(message);
		}
	}
    function sheet2_OnSearchEnd(code, message) {
	    if(code == 0) {
	    	setGridSize(sheet2);
		} else {
			//조회 에러 수행
			alert(message);
		}
	}
    
    function sheet1_OnClick(Row, Col, Value, CellX, CellY, CellW, CellH) {
    	var f = document.form1;
    	f.pur_cont_id.value = sheet1.GetCellValue(Row, "pur_cont_id");
    	jQuery("#fevent").val("GET_SHIPPING_TARGET2");
    	var url = Modular.model.PageContext.CONTEXT_ROOT + "/process/process/ShippingTargetSearch_sheet.screen";
    	Modular.view.IBSheet7.doSearch(sheet2, url, jQuery("#form1"), 30, "");
    }
</SCRIPT>

<modular:search id="form1">
	<modular:key id="fevent" />
</modular:search>

<form id="form1" name="form1" method="post" modular:type="search" style="width:100%;height:100%;">
<INPUT type="hidden" name="fevent" id="fevent" value="">
<INPUT type="hidden" name="pur_cont_id">
	<!-- 검색조건 시작 -->
	<table id="search-table" class="search-form-table">
		<colgroup>
			<col width="80" />
			<col width="300" />
			<col width="90" />
			<col width="*" />
			<col width="200" />
		</colgroup>
		<tr>
			<th>Project</th>
			<td>
		   		<%= InputText.get("dept_cd", params.get("dept_cd"), 10, 50, "hangule, desc=현장코드, readOnly, PerSize") %>
		   		<%= InputText.get("dept_nm", URLDecoder.decode(params.get("dept_nm"),"UTF-8"), 70, 50, "hangule, desc=현장명, readOnly, PerSize") %> 				
			</td>
			<th>Vendor</th>
			<td>
				<%= InputText.get("vendor_name", URLDecoder.decode(params.get("vendor_name"),"UTF-8") , 80, 50, "hangule, desc=계약명, PerSize") %>
			</td>
			<td class="button">
				<span class="btnSearch center"><a href="javascript:void(0);" id="btnSearch">Search</a></span>
				<span class="btnSearch center"><a href="javascript:void(0);" id="btnChoice">Select</a></span>
				<span class="btnSearch center"><a href="javascript:void(0);" id="btnClose">Close</a></span>
			</td>
		</tr>
	</table>
	
	<TABLE border="0" cellspacing="0" cellpadding="0" width="100%">
		<tr>
			<td width="35%">
				<div id="sheet1_div" style="padding-top:5px; height: auto; min-height: 320px; overflow: auto;">
					<script>createIBSheet('sheet1','95%', '320px');</script>
				</div>
			</td>
			<td width="65%">
				<div id="sheet2_div" style="padding-top:5px; height: auto; min-height: 320px; overflow: auto;">
					<script>createIBSheet('sheet2','100%', '320px');</script>
				</div>
			</td>
		</tr>
	</TABLE>
	
	<!-- 검색조건 종료 -->
	
	
	
	
</form>
