<%--
  - FileName   : matlcodeselectallpopup.jsp
  - Author     : gyeongmo an
  - Date       : 2014.07.23
  - Copyright  : Copyright (c) 2014 www.udapsoft.co.kr, Inc.
  - All rights reserved.
  - Description: 자재선택 팝업
--%>
<%@ page contentType="text/html; charset=utf-8" %>
<%@ page import="kr.co.hsnc.common.sql.RowSet" %>
<%@ page import="kr.co.hsnc.common.sql.RowSetImpl" %>
<%@ page import="kr.co.hsnc.common.util.ValueObject" %>
<%@ page import="kr.co.udapsoft.common.*" %>
<%@ page import="kr.co.udapsoft.common.code.CommCodeManager" %>
<%@ page import="com.udapsoft.waf.system.*" %>
<%@ page import="com.udapsoft.waf.common.*" %>
<%@ page import="com.udapsoft.waf.common.ui.HTMLMaker" %>
<%@ page import="com.udapsoft.waf.system.HandlerStorage" %>
<%@ page import="com.udapsoft.waf.common.ui.InitCombo" %>
<%@ page import="com.udapsoft.waf.common.ui.InputText" %>

<%@ 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 params     = storage.getParams();
	String fevent           = storage.getEvent();
	
	RowSet materialKindCodeLv1 = storage.getRowSet("materialKindCodeLv1");
	RowSet materialKindCodeLv2 = new RowSetImpl();
	RowSet materialKindCodeLv3 = new RowSetImpl();
	
	InitCombo ic = new InitCombo();

	ic.makeComboData(CommCodeManager.getCodeList("STOCK_FLAG"), "CODE", "CODE_NAME_KOR");
	String stockFCD = ic.getCode();
	String stockFNM = ic.getCodeName();
	
%>
<SCRIPT language="JavaScript">
/*예약함수*/
var sheet1;
var sheet1Row;
var sheet1PageNo = 1;
var pageLastRowIndex = 1;
var validationResult = false;
var filterShow = false;

function _onReady(){
	gridInit(sheet1);

	jQuery("#btnSearch").click(function() {
		var f= document.form1;
		if(f.material_Kind_Code_Lv1.value == "" && f.material_Kind_Code_Lv2.value == "" && f.material_Kind_Code_Lv3.value == "") {
			if(f.search_material_cd.value == "" && f.search_material_name.value == "" ) {
				alert("자재코드나 자재명칭을 입력하시거나 \n 또는 자재분류를 선택해 주세요.");
				return;
			} 
		}
		
		//var deadflg_value 		= $(':radio[name="deadflg"]:checked').val();
		jQuery("#fevent").val("GET_MATL_CODE_LIST");
		var url = Modular.model.PageContext.CONTEXT_ROOT + "/lib/dlgSearchMaterialCd_sheet.screen";
		Modular.view.IBSheet7.doSearch(sheet1, url, jQuery("#form1"), 30, "");
	});
	
	jQuery("#btnClose").click(function() {
	 	setTimeout(function(){
	 		parent.doSearchMaterialCodeClose(null);
	 		},100);
	});
	
	jQuery("#btnChoice").click(function() {
		if(sheet1.RowCount() < 1) {
			alert("검색을 먼저 해주세요.");
			return; 
		}
		var temp = "N";
		var material_cd		= new Array;	// 자재코드
     	var material_name 	= new Array;	// 자재명
     	var material_spec = new Array;
     	var unit_kind 		= new Array;	// 단위코드
     	var unit_kind_name 		= new Array;	// 단위명
     	var stock_flag 		= new Array;	// 중요자재여부
     	var stock_plan_flag= new Array;	// 입고계획여부
			
        	if( sheet1.LastRow() >= 1 ){
           		var idx = 0;
               	for(var i=1; i <= sheet1.LastRow(); i++){
              		var tmpArray = sheet1.GetCellValue(i,"chk"); 
         	 		if(tmpArray=="1"){
         	 			temp = "Y";
         	 			material_cd[idx] 		= sheet1.GetCellValue(i,"material_cd");
         	 			material_name[idx] 		= sheet1.GetCellValue(i,"material_name"); 
         	 			material_spec[idx] 		= sheet1.GetCellValue(i,"material_spec"); 
         	 			unit_kind[idx] 	    	= sheet1.GetCellValue(i,"unit_kind");    	
         	 			unit_kind_name[idx]		= sheet1.GetCellValue(i,"unit_kind_name");    	
         	 			stock_flag[idx] 		= sheet1.GetCellValue(i,"stock_flag"); 	
         	 			stock_plan_flag[idx] 	= sheet1.GetCellValue(i,"stock_plan_flag");   	  
			     		idx++;
	                } 
  	          	}
               	if(idx > 1) {
               		alert("자재를 하나만 선택해 주세요.");
               		return;
               	}
               	
               	if(temp == "N") {
               		alert("자재를 선택해 주세요.");
               		return;
               	}
    	    }

     	var returnValue = new Array(material_cd, material_name, material_spec, unit_kind, unit_kind_name, stock_flag, stock_plan_flag);
     	setTimeout(function(){
	 		parent.doSearchMaterialCodeClose(returnValue);
	 		},100);
	});
	
	
	doChangeMaterialKind();
}

function gridInit(sheetName){
	var init_sheet = {};
	init_sheet.Cfg = {SearchMode:2, Page:30};
	init_sheet.Cols = [
		       			{Header:'선택',		Type:"CheckBox",	SaveName:"chk",		Width:70, 	Align:"Center", Hidden:"True"},
		       			{Header:'자재코드',		Type:"Text",	SaveName:"material_cd",		Width:100, 	Align:"Center", Edit:"false"},
		       			{Header:'대분류',		Type:"Text",	SaveName:"kind_big",		Width:0, 	Align:"Center", Edit:"false", Hidden:"True"},
		       			{Header:'중분류',		Type:"Text",	SaveName:"kind_mid",		Width:0, 	Align:"Center", Edit:"false", Hidden:"True"},
		       			{Header:'소분류',		Type:"Text",	SaveName:"kind_small",		Width:0, 	Align:"Center", Edit:"false", Hidden:"True"},
		       			{Header:'자재명',		Type:"Text",	SaveName:"material_name",		Width:300, 	Align:"Left", Edit:"false"},
		       			{Header:'규격',		Type:"Text",	SaveName:"material_spec",		Width:150, 	Align:"Left", Edit:"false"},
		    			{Header:'UNIT_KIND',			Type:"Text",	SaveName:"unit_kind",		Width:0, 	Align:"Left", Edit:"false", Hidden:"True"},
		    			{Header:'단위',		Type:"Text", 	SaveName:"unit_kind_name", 			Width:80, 	Align:"Center", Edit:"false"},
		    			{Header:'사용여부',			Type:"Text", 	SaveName:"is_use", 			Width:0,	Align:"Left", Edit:"false", Hidden:"True"},
		    			{Header:'중요자재여부',		Type:"Combo", 	SaveName:"stock_flag", 			Width:90, 	Align:"Center", Edit:"false", ComboText:"즉시출고|재고관리", ComboCode:"1|2"},    
		    			{Header:'입고계획여부',		Type:"Combo",	SaveName:"stock_plan_flag", 			Width:90, 	Align:"Center", Edit:"false", ComboText:"사용|미사용", ComboCode:"Y|N"}
	];

	init_sheet.HeaderMode = {Sort:1};
	IBS_InitSheet(sheetName, init_sheet);
	sheetName.SetDataAutoTrim(0);
	setGridSize();
}

function sheet1_OnSearchEnd(code, message) {
	setGridSize();	
}

function setGridSize() {
	sheet1.FitColWidth();
}

	function doResult(resultObj) {
		var f = document.form1;
		document.form1.isSubmit = false;
		try {
			if ( resultObj.getRequestEvent() == "GET_MATL_KIND_CODE_LV2" ) {
				if ( resultObj.getSuccess() == true ) {
					var vo = resultObj.getValueObject();
					injectSelect(vo, document.form1.material_Kind_Code_Lv2,"materialgroupname","materialgroup");
					document.form1.isSubmit = false;
					//doChangeMaterialKind2();
					if(f.material_Kind_Code_Lv1.value == "") {
						while(document.form1.material_Kind_Code_Lv2.options.length > 1) {
							document.form1.material_Kind_Code_Lv2.remove(1);
						}
					}
					
					// 소분류 select box clear
					while(document.form1.material_Kind_Code_Lv3.options.length > 1) {
						document.form1.material_Kind_Code_Lv3.remove(1);
					}
				} else {
                    dlgErrorMsg(resultObj);
                }
			} else if ( resultObj.getRequestEvent() == "GET_MATL_KIND_CODE_LV3" ) {
				if ( resultObj.getSuccess() == true ) {
					var vo = resultObj.getValueObject();
					injectSelect(vo, document.form1.material_Kind_Code_Lv3,"materialgroupname","materialgroup");
					
					if(f.material_Kind_Code_Lv2.value == "") {
						while(document.form1.material_Kind_Code_Lv3.options.length > 1) {
							document.form1.material_Kind_Code_Lv3.remove(1);
						}
					}
					
					document.form1.isSubmit = false;
				} else {
                    dlgErrorMsg(resultObj);
                }
			}
		} catch (errorObject) {
			dlgErrorDetailAlert("doResult()", errorObject);
		}
	}

	function doClose() {
		returnvalue = null;
		parent.doSearchMaterialCodeClose(null);
	}

	// 자재분류 첫번째 SELECT BOX onChange 이벤트
	function doChangeMaterialKind() {
		document.form1.materiallevel.value = "2";
		document.form1.materialgroup.value = document.form1.material_Kind_Code_Lv1.value;
		submitXmlRequest("/lib/dlgSearchMaterialCd_form.screen", "GET_MATL_KIND_CODE_LV2", document.form1, "VALUEOBJECT");
	}
	
	// 자재분류 두번째 SELECT BOX onChange 이벤트
	function doChangeMaterialKind2() {
		document.form1.materiallevel.value = "3";
		document.form1.materialgroup.value = document.form1.material_Kind_Code_Lv2.value;
		submitXmlRequest("/lib/dlgSearchMaterialCd_form.screen", "GET_MATL_KIND_CODE_LV3", document.form1, "VALUEOBJECT");
	}
	
	function injectSelect(vo, selectObj, code, value){
		var Code = vo.get(code);
		var Value = vo.get(value);
		var arrCd = Code.split('|');
		var arrValue = Value.split('|');

		while(selectObj.options.length){
			selectObj.remove(0);
		}
		
		var newOpt;
		newOpt = document.createElement('option');
		newOpt.text = "-- 전체 --";
		newOpt.value = "";
		selectObj.options.add(newOpt);
		
		if (arrCd == '') {
			selectObj.length = 1;
			
		} else {		
			for(var i = 0; i < arrCd.length; i++){
				newOpt = document.createElement('option');
				newOpt.text = arrValue[i] + " : " + arrCd[i];
				newOpt.value = arrValue[i];
				selectObj.options.add(newOpt);
			}
		}
	}
	

	function prcKeyPress(){
      	if( event.keyCode == 13 )
      	{
      		jQuery("#btnSearch").click();
    	}
    }
	
	function sheet1_OnDblClick(Row, Col, Value, CellX, CellY, CellW, CellH) { 
		var material_cd 		= sheet1.GetCellValue(Row,"material_cd");
		var material_name 		= sheet1.GetCellValue(Row,"material_name"); 
		var material_spec 		= sheet1.GetCellValue(Row,"material_spec"); 
		var unit_kind 	    	= sheet1.GetCellValue(Row,"unit_kind");    	
		var unit_kind_name		= sheet1.GetCellValue(Row,"unit_kind_name");    	
		var stock_flag 		= sheet1.GetCellValue(Row,"stock_flag"); 	
		var stock_plan_flag 	= sheet1.GetCellValue(Row,"stock_plan_flag");  
		
     	var returnValue = new Array(material_cd, material_name, material_spec, unit_kind, unit_kind_name, stock_flag, stock_plan_flag);
     	setTimeout(function(){
	 		parent.doSearchMaterialCodeClose(returnValue);
	 		},100);
	}

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

<form id="form1" name="form1" method="post" modular:type="search">
	<input type="text" name="fevent" style="display:none;">
	<input type="hidden" id="materialgroup" name="materialgroup" value="">
	<input type="hidden" id="materiallevel" name="materiallevel" value="">

	<!-- 검색조건 시작 -->
	<table id="search-table" class="search-form-table">
		<colgroup>
			<col width="100" />
			<col width="200" />
			<col width="100" />
			<col width="*" />
			<col width="200" />
		</colgroup>
		<tr>
			<th>자재분류</th>
			<td colspan="3">
					<%= HTMLMaker.getSelectTag(materialKindCodeLv1, "materialgroup", "materialgroupname" , "material_Kind_Code_Lv1", "style='width:30%;height=18;' onChange='javascript:doChangeMaterialKind();'", "<OPTION value=''>-- 전체 --</OPTION>", "", 0) %>
					<%= HTMLMaker.getSelectTag(materialKindCodeLv2, "materialgroup", "materialgroupname" , "material_Kind_Code_Lv2", "style='width:30%;height=18;' onChange='javascript:doChangeMaterialKind2();'", "<OPTION value=''>-- 전체 --</OPTION>", "", 0) %>
					<%= HTMLMaker.getSelectTag(materialKindCodeLv3, "materialgroup", "materialgroupname" , "material_Kind_Code_Lv3", "style='width:30%;height=18;'", "<OPTION value=''>-- 전체 --</OPTION>", "", 0) %>
			</td>
			<td class="button" rowspan="2">
				<span class="btnSearch center"><a href="javascript:void(0);" id="btnSearch">검색</a></span>
				<!-- <span class="btnSearch center"><a href="javascript:void(0);" id="btnChoice">선택</a></span> -->
				<span class="btnSearch center"><a href="javascript:void(0);" id="btnClose">닫기</a></span>
			</td>
		</tr>
		<tr>
			<th>자재코드</th>
			<td>
				<%= InputText.get("search_material_cd", "", 90, 255, "class=input byteChk, perSize, left", "", "onkeyup=\"prcKeyPress();\"") %>
			</td>
			<th>자재명칭</th>
			<td>
				<%= InputText.get("search_material_name", "", 91, 255, "class=input byteChk, perSize, left", "", "onkeyup=\"prcKeyPress();\"") %>
			</td>
		</tr>
	</table>
	<!-- 검색조건 종료 -->
	
 	<div id="sheet1_div" style="padding-top:5px; height: auto; min-height: 320px; overflow: auto;">
		<script>createIBSheet('sheet1','100%', '320px');</script>
	</div>
	
	
	
</form>





