<%--
  - FileName   : chgItemPopup.jsp
  - Author(s)  : Kim Myung HO
  - Date       : 2014.10.28
  - 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.ui.HTMLMaker" %>
<%@ page import="com.udapsoft.waf.system.HandlerStorage" %>
<%@ page import="com.udapsoft.waf.common.util.*" %>

<%@ 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();
    
    ValueObject cont = storage.getValueObject("cont");
    ValueObject ContInfo = storage.getValueObject("ContInfo");
    RowSet chgItemList = storage.getRowSet("chgItemList");
%>

<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_SELECT");
        	var url = Modular.model.PageContext.CONTEXT_ROOT + "/cont/purchase/chgItemPopup_sheet.screen";
        	Modular.view.IBSheet7.doSearch(sheet1, url, jQuery("#form1"), 30, "");
		});

		jQuery("#btnSave").click(function() {
			if(!chgSaveChk()){
				return;
			}		
			
			if(!sheet1.IsDataModified()) {
			    alert("저장할 내역이 없습니다.");
				return;
			}
			
			if (confirm("변경 내역을 저장 하시겠습니까?")){			
				jQuery("#fevent").val("SHEET_SAVE");
				var url = Modular.model.PageContext.CONTEXT_ROOT + "/cont/purchase/chgItemPopup_sheet.screen";
				var result = Modular.view.IBSheet7.doSave(sheet1, url, jQuery("#form1"), false);		
			}
		});

		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:"ITEM_QTY",		Width:80, 	Align:"Right", Edit:"false",PointCount:2},
			    			{Header:'변경전|단가',		Type:"Float",		SaveName:"ITEM_PRICE",		Width:100, 	Align:"Right", Edit:"false", PointCount:'<%=cont.get("POINTCOUNT")%>'},
			    			{Header:'변경전|금액',		Type:"AutoSum",	SaveName:"ITEM_AMT",		Width:150, 	Align:"Right", Edit:"false", PointCount:'<%=cont.get("POINTCOUNT")%>'},
			    			{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:'<%=cont.get("POINTCOUNT")%>'},
			    			{Header:'변경후|금액',		Type:"AutoSum",	SaveName:"CONT_AMT",	Width:150, 	Align:"Right", Edit:"false", PointCount:'<%=cont.get("POINTCOUNT")%>'},
			    			{Header:'발주수량|발주수량',	Type:"Int",		SaveName:"DEMANDED_QTY",	Width:80, 	Align:"Right", Edit:"false"},
			    			{Header:'입고수량|입고수량',	Type:"Int",		SaveName:"STOCK_QTY",		Width:80, 	Align:"Right", Edit:"false"},
			    			{Header:'상태|상태',		Type:"Text",	SaveName:"CHG_MOD_CD",		Width:80, 	Align:"Left", Edit:"false",	Hidden:"True"},
			    			{Header:'Status|Status',Type:"Status", 	SaveName:"STATUS", 			Width:50, 	Align:"Center",	Hidden:"True"},
			    			{Header:'INFLAG|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();
	    	for(var i=1;i <= sheet1.RowCount();i++){
	    		if(sheet1.GetCellValue(i,"CHG_MOD_CD") == "DELETE"){
	    			sheet1.SetRowEditable(i,0);
	    		}
	    	}		    	
		} else {
			//조회 에러 수행
			alert(message);
		}
	}

	function sheet1_OnSaveEnd(code, msg)
	{
		var event = $("#fevent").val();
		if(code>= "0"){
			if(event == "SHEET_SAVE"){
				alert("저장되었습니다.");
				parent.window.location.reload();
			}
		}else{
			alert("저장실패.");
		}
	}

	/**
	 *	IBSheet Change Event
	 */
	 function sheet1_OnChange(Row, Col, Value, OldValue, RaiseFlag) { 
		 if (sheet1.ColSaveName(Col) == "CONT_QTY") {
			 if(RaiseFlag==0){
				 if((sheet1.GetCellValue(Row,"CONT_QTY") < sheet1.GetCellValue(Row,"STOCK_QTY")) || (sheet1.GetCellValue(Row,"CONT_QTY") > sheet1.GetCellValue(Row,"DEMANDED_QTY"))){
						 alert("변경 수량은 발주 수량보다 많을수 없으며 입고수량보다 적을수 없습니다.");
						 sheet1.SetCellValue(Row, Col, OldValue);
				//		 return false;
					 }
		 	}
				 
		 }
		 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 chgSaveChk(){
		for(var i=2; i<=sheet1.RowCount()+1; i++){
			if(sheet1.GetCellValue(i,"CHG_MOD_CD")!="DELETE"){
	    		/*if((sheet1.GetCellValue(i,"CONT_AMT")=="") ){
	    			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="post" 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="pDemID" value="<%=params.get("pDemID")%>" />
	<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("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: 330px; overflow: auto;">
		<script>createIBSheet('sheet1','100%', '330px');</script>
	</div>
	
</form>
<%//상품 목록 %>
<%@ include file="/jsp/ko_KR/buyer/cont/purchase/chgItemList.jsp" %>


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