<%--
  - FileName   : ItemPopup.jsp
  - Author(s)  : Kim Myung HO
  - Date       : 2014.11.21
  - Description: ITEM 변경 내역
  --%>
<%@ 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.util.*" %>
<%@ page import="kr.co.hsnc.common.util.*"%>
<%@ page import="kr.co.hsnc.common.util.Formatter"%>
<%@ page import="com.udapsoft.waf.common.ui.HTMLMaker" %>
<%@ page import="com.udapsoft.waf.system.HandlerStorage" %>
<%@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 event           = storage.getEvent();
    
    System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
    System.out.println(params);
    System.out.println("#################################################");
    ValueObject ContInfo = new ValueObject();
    ContInfo.set("DIV_PO_YN", params.get("DIV_PO_YN"));

    request.setCharacterEncoding("utf-8");
    String curr_std_nm = URLDecoder.decode(request.getParameter("curr_std_nm"),"UTF-8");
    /*계약 구매 내역 목록*/
    RowSet itemList = storage.getRowSet("itemList");
%>

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

	
    function _onReady(){
		gridInit(sheet1);

		//autoResize();
		
		jQuery("#btnSearch").click(function() {
        	jQuery("#fevent").val("SHEET_ITEM_SELECT");
        	var url = Modular.model.PageContext.CONTEXT_ROOT + "/cont/purchase/ItemPopup_sheet.screen";
        	Modular.view.IBSheet7.doSearch(sheet1, url, jQuery("#form1"), 30, "");
		});

		jQuery("#btnSave").click(function() {
			if(!SaveChk()){
				return;
			}
			
			if(!sheet1.IsDataModified()) {
			    alert("저장할 내역이 없습니다.");
				return;
			}
			
			if (confirm("수정된 내역을 저장 하시겠습니까?")){			
				jQuery("#fevent").val("SHEET_ITEM_SAVE");
				var url = Modular.model.PageContext.CONTEXT_ROOT + "/cont/purchase/ItemPopup_sheet.screen";
				var result = Modular.view.IBSheet7.doSave(sheet1, url, jQuery("#form1"), false);		
			}
		});
		
		jQuery("#btnAddItem").click(function() {		
			var win_id = "ItemAddPop";			
			var editYN = "N";
			var link = "/cont/purchase/ItemAddPopup.screen";
			link += "?pop_win_id="+win_id+"&PUR_CONT_MOD_ID="+$("#PUR_CONT_MOD_ID").val()+"&PUR_CONT_ID="+$("#PUR_CONT_ID").val()+"&pDemID="+$("#pDemID").val()+"&editYN="+ editYN;;
			fLayerPop(win_id, link, 500,200, '','', 'NO');
		});	
		
		jQuery("#btnSearch").click();
	}
    
    function ItemAddClose(){
		setTimeout(function(){fLayerPopClose('ItemAddPop');},100);
		jQuery("#btnSearch").click();
	}
    
    function gridInit(sheetName){
		var init_sheet = {};
		init_sheet.Cfg = {SearchMode:2, Page:5000, MergeSheet:msPrevColumnMerge + msHeaderOnly};
		init_sheet.Cols = [
			       			{Header:'자재코드',		Type:"Text",	SaveName:"MATERIAL_CD",		Width:100, 	Align:"Left", 	Edit:"false"},
			    			{Header:'자재명',		Type:"Text",	SaveName:"ITEM_NAME",		Width:150, 	Align:"Left", 	Edit:"false"},
			       			{Header:'규격',		Type:"Text",	SaveName:"ITEM_SPEC",		Width:80, 	Align:"center", Edit:"false"},
			    			{Header:'단위',		Type:"Text",	SaveName:"ITEM_UNIT",		Width:60, 	Align:"center", Edit:"false"},
			       			{Header:'수량',		Type:"Float",		SaveName:"CONT_QTY",		Width:80, 	Align:"Right", 	Edit:"true", PointCount:2},
			    			{Header:'단가',		Type:"Float",		SaveName:"CONT_PRICE",		Width:100, 	Align:"Right", 	Edit:"true", PointCount:<%=params.get("digit")%>},
			    			{Header:'금액',		Type:"AutoSum",	SaveName:"CONT_AMT",		Width:150, 	Align:"Right", 	Edit:"false", PointCount:<%=params.get("digit")%>},
			    			{Header:'Status',	Type:"Status", 	SaveName:"STATUS", 			Width:50, 	Align:"Center",	Hidden:"True"},
			    			{Header:'INFLAG',	Type:"Text", 	SaveName:"INFLAG", 			Width:50, 	Align:"Center",	Hidden:"true"},
			    			
		];

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

    function sheet1_OnSearchEnd(code, message) {
	    if(code == 0) {
	    	setGridSize();	    	
		} else {
			//조회 에러 수행
			alert(message);
		}
	}

	function sheet1_OnSaveEnd(code, msg)
	{
		var event = $("#fevent").val();
		if(code>= "0"){
			if(event == "SHEET_ITEM_SAVE"){
				alert("저장되었습니다.");
				jQuery("#isClose").val("Y");
				jQuery("#fevent").val("");
				document.form1.submit();
			}
		}else{
			alert("저장실패.");
		}
	}

	/**
	 *	IBSheet Change Event
	 */
	 function sheet1_OnChange(Row, Col, Value, OldValue, RaiseFlag) { 

		 if ((sheet1.ColSaveName(Col) == "CONT_QTY") || (sheet1.ColSaveName(Col) == "CONT_PRICE")) {
			//alert(sheet1.GetCellValue(Row,"CONT_QTY") * sheet1.GetCellValue(Row,"CONT_PRICE"));
			sheet1.SetCellValue(Row, "CONT_AMT", sheet1.GetCellValue(Row,"CONT_QTY") * sheet1.GetCellValue(Row,"CONT_PRICE"));			
		}
	}
    
    function sheet1_OnSmartResize(Width, Height) { 
		sheet1.FitColWidth();
		//autoResize();
	}
    
   /**
    *  enter click 시 자동으로 검색이되도록 조
    */ 
	function check_Enter(){
		if (event.keyCode==13) 
			jQuery("#btnSearch").click();
	}
   
	function SaveChk(){
		for(var i=1; i<=sheet1.RowCount()+1; i++){
    		if((sheet1.GetCellValue(i,"CONT_AMT")=="") || (sheet1.GetCellValue(i,"CONT_AMT")=="0")){
    			alert(i-1 + "번째 " + sheet1.GetCellValue(i,"ITEM_NAME") + " 내역에 대해 수랑 또는 단가 입력이 안되었습니다.");
    			
    			return false;
    		}			
    		sheet1.SetCellValue(i, "INFLAG", "Y");				
		}

		return true;
	}   

 	function autoResize(){
		var t1_height = document.getElementById("search-table").scrollHeight + document.getElementById("title_pop").scrollHeight + 20;
		var iframeHeight= document.body.scrollHeight - t1_height;   
		var iframeWidth= document.body.scrollWidth;   

		sheet1.SetSheetHeight(iframeHeight); 
	} 
    document.onkeydown 		= closePopupWindow;
    
    //document.body.onresize = addEvent( document.body.onresize, "autoResize()");
</SCRIPT>

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

<FORM id="form1" name="form1" method="get" modular:type="search">
	<INPUT type="hidden" name="fevent" id="fevent" value="">
	<INPUT	type="hidden" name="PUR_CONT_MOD_ID" value="<%=params.get("PUR_CONT_MOD_ID")%>" />
	<INPUT	type="hidden" name="PUR_CONT_ID" value="<%=params.get("PUR_CONT_ID")%>" />
	<INPUT	type="hidden" name="pop_win_id" value="<%=params.get("pop_win_id")%>" />
	<INPUT	type="hidden" name="editYN" value="<%=params.get("editYN")%>" />
	<INPUT	type="hidden" name="pDemID" value="<%=params.get("pDemID")%>" />	
	<INPUT type="hidden" name="DIV_PO_YN" value="<%=params.get("DIV_PO_YN") %>">
	<INPUT type="hidden" name="TEMPLATE_CD" value="<%=params.get("TEMPLATE_CD") %>">
	<INPUT type="hidden" name="isClose" value="<%=params.get("isClose") %>">
	<INPUT type="hidden" name="digit" value="<%=params.get("digit") %>">
	<INPUT type="hidden" name="curr_std_nm" value="<%=curr_std_nm%>">
	<table id="search-table" class="search-form-table">
		<colgroup>
			<col width="*" />
		</colgroup>
		<tr>
			<td class="button" style="text-align:right; padding-right:10px;">
				<span class="btnSearch center" id="btnSearch" style="display:;"><a href="javascript:void(0);">검색</a></span>
	<%if (params.get("TEMPLATE_CD").equals("4") || params.get("TEMPLATE_CD").equals("13")){%>
				<span class="btnSearch center" id="btnAddItem"><a href="javascript:void(0);">품목추가</a></span>
	<%} %>				
	<%if (params.get("editYN").equals("Y")){%>
				<span class="btnSearch center" id="btnSave"><a href="javascript:void(0);">저장</a></span>
	<%} %>
			</td>
		</tr>
	</table>
	<!-- 검색조건 종료 -->
	<div id="sheet1_div" style="padding-top:5px; height: auto; min-height: 450px; overflow: auto;">
		<script>createIBSheet('sheet1','100%', '450px');</script>
	</div>
	
</form>
<%//상품 목록 %>
<%@ include file="/jsp/ko_KR/buyer/cont/purchase/itemList2.jsp" %>


<%
if (params.get("isClose").equals("Y")) {
%>
<script>
	winClose();
</script>
<%
}
%>