<%--
  - FileName   : LCList.jsp
  - Author(s)  : lee mi kyoung
  - Date       : 2016.11.01
  -
  - Description: LC관리 
  --%>
  


<%@ page contentType="text/html; charset=utf-8" %>
<%@ page import="com.udapsoft.waf.system.HandlerStorage" %>
<%@ page import="com.udapsoft.waf.common.ui.*" %>
<%@ page import="kr.co.hsnc.common.util.*"%>
<%@ page import="kr.co.hsnc.common.sql.*" %>
<%@ page import = "blowfishj.*"%>   
<%@ page import = "java.text.SimpleDateFormat"%>    
<%@ page import="com.udapsoft.waf.common.util.DateUtil"%>                                                                                                                                                                                                         
<%@ page import = "java.util.Date"%>                                                                                                                                                                                                                         
<%@ page import = "java.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 params = storage.getParams();
    ValueObject user = storage.getUser();
    String event = storage.getEvent();
    
    String selectText 	    = (String)storage.getObject("selectText");    
	String selectValue     = (String)storage.getObject("selectValue");
	String selectLC        = (String)storage.getObject("selectLC");
	
	//검색조건 작성일
	String  initSearchDateStart = params.get("p_from_dt");
    String  initSearchDateEnd = params.get("p_to_dt");

	ValueObject defaultDate = DateUtil.getDefaultPeriod();
	if("".equals(initSearchDateStart)){
		initSearchDateStart = DateUtil.getAddedDate("-12", "yyyy-mm-dd");//defaultDate.get("START_DATE");
	}
	if("".equals(initSearchDateEnd)){
		initSearchDateEnd = defaultDate.get("END_DATE"); 
	} 
   
%>



<script type="text/javascript" src="/js/creditEvaluationPop.js"></script>
<script language="javascript">
	/*예약함수*/
	var sheet1 = new ibsheetObject();
	var sheet1Row;
	var sheet1PageNo = 1;
	var sheet2 = new ibsheetObject();
	var sheet2Row;
	var sheet2PageNo = 1;
	var pageLastRowIndex = 1;
	var validationResult = false;
	var filterShow = false;
	
	
	$(document).on("keyup", "input:text", function(e) {
	    if($(this).attr("numberOnly") == "true")
        {
	        $(this).val( $(this).val().replace(/[^0-9]/gi,"") );
        }
	    
	    if (e.keyCode == 13)
        {
	        $.searchList();	        
        }
	    
	});
	
	
	$.gridInit = function(sheetName)
	{
		var init_sheet = {};
		init_sheet.Cfg = {SearchMode:2, MergeSheet:msAll, Page:100};
		if(sheetName.id == "sheet1") {
			init_sheet.Cols = [
			       			{Header:'Del',					 Type:"DelCheck",		SaveName:"DEL_CHECK", 				Width:50, 		Align:"Center",		Hidden:"False",ColMerge:"False"},
			    			{Header:'Status',				 Type:"Status", 		SaveName:"STATUS", 					Width:0, 		Align:"Center", 	Edit:"false", Hidden:"True"},
			    			{Header:'PUR_CONT_ID',		     Type:"Text", 		    SaveName:"PUR_CONT_ID", 			Width:0, 		Align:"Center", 	Edit:"false", Hidden:"True" },
			    			{Header:'SEQ',		             Type:"Text", 		    SaveName:"SEQ", 		         	Width:20, 		Align:"Center", 	Edit:"True",  Hidden:"True" },
			    			{Header:'구매담당자|구매담당자',     Type:"Text", 		    SaveName:"CON_CHARGE_NAME",			Width:60, 		Align:"Center", 	Edit:"false"},
			    			{Header:'PO No|PO No',			 Type:"Text", 		    SaveName:"ORD_DOC_NO",				Width:180, 		Align:"Left", 		Edit:"false"},
			    			{Header:'☞Description|☞Description', Type:"Text", 		    SaveName:"CST_DOC_NAME", 			Width:180,		Align:"Left", 		Edit:"false",ColMerge:"False",	Cursor:"pointer"},
			    			{Header:'Vendor|Vendor',	     Type:"Text", 		    SaveName:"CORP_KOR", 			    Width:50,		Align:"Left", 		Edit:"false"},
			    			{Header:'Vendor|국가',			 Type:"Text", 		    SaveName:"NATION", 			        Width:50,		Align:"Left", 		Edit:"false"},
			    			{Header:'화폐|화폐',				 Type:"Text", 			SaveName:"CURRENCY_GB", 			Width:40, 		Align:"Center", 	Edit:"false", ColMerge:"False"},
			    			{Header:'금액|금액',				 Type:"Float",			SaveName:"ORD_AMT", 			    Width:60, 		Align:"Right", 	    Edit:"false", ColMerge:"False"},          
			    			{Header:'지급조건|지급조건',		 Type:"Text", 		    SaveName:"PAY_CONDI", 			    Width:60, 		Align:"Left", 		Edit:"true", ColMerge:"False"},          
			    			{Header:'LC Open|금액',			 Type:"AutoSum", 		SaveName:"LC_AMT", 			        Width:50, 		Align:"Right",      Edit:"true", ColMerge:"False"},          
			    			{Header:'LC Open|비율',			 Type:"Int", 			SaveName:"RATE", 		            Width:40, 		Align:"Right",		Edit:"true", Format:"#,##0", CalcLogic:"(|LC_AMT|/|ORD_AMT|)*100", ColMerge:"False"},          
			    			{Header:'LC Open|Open Date',	 Type:"Date", 			SaveName:"OPEN_DATE", 			    Width:90, 		Align:"Left",		Edit:"true" , ColMerge:"False"},               
			    			{Header:'LC Open|Expiry date',	 Type:"Date", 			SaveName:"EXPIRE_DATE", 			Width:90, 		Align:"Left",		Edit:"true" , ColMerge:"False"},               
			    			{Header:'LC Open|은행',			 Type:"Combo", 			SaveName:"BANK_CD", 		   	    Width:100, 		Align:"Left",		Edit:"true", ComboText:"<%=selectValue%>", ComboCode:"<%=selectText%>" , ColMerge:"False"},               
			    			{Header:'LC Open|LC번호',		 Type:"Text", 			SaveName:"LC_NO", 			        Width:200, 		Align:"Left",		Edit:"true" , ColMerge:"False"},               
			    			{Header:'LC Open|LC잔여',		 Type:"Float", 			SaveName:"LC_REMAIN",		        Width:50, 		Align:"Right",		Edit:"false", ColMerge:"False"},               
			    			{Header:'Project|Project',		 Type:"Text", 			SaveName:"DEPT_NAME", 			    Width:180, 		Align:"Left", 		Edit:"false", ColMerge:"False"},
			    			];
		}else{
			init_sheet.Cols = [
				       			{Header:'Del',			 Type:"DelCheck",		SaveName:"DEL_CHECK", 				Width:40, 		Align:"Center",		Hidden:"False",ColMerge:"False"},
				    			{Header:'Status',		 Type:"Status", 		SaveName:"STATUS", 					Width:0, 		Align:"Center", 	Edit:"false", Hidden:"True"},
				    			{Header:'PUR_CONT_ID',	 Type:"Text", 			SaveName:"PUR_CONT_ID", 	        Width:40, 		Align:"Right",		Edit:"true", Hidden:"True"},          
				    			{Header:'LC_AMT',	     Type:"Int", 			SaveName:"LC_AMT", 	                Width:40, 		Align:"Right",		Edit:"true", Hidden:"True"},          
				    			{Header:'SEQ',	         Type:"Text", 			SaveName:"SEQ", 	                Width:40, 		Align:"Right",		Edit:"true" , ColMerge:"False", Hidden:"True"},          
				    			{Header:'LC번호',		 Type:"Combo", 			SaveName:"LC_NO", 			        Width:100, 		Align:"Left",		Edit:"true" , ComboText:"",ComboCode:"",  ColMerge:"False"},               
				    			{Header:'금액',			 Type:"AutoSum", 		SaveName:"PAY_AMT", 			    Width:50, 		Align:"Right",      Edit:"true" , ColMerge:"False"},          
				    			{Header:'비율',			 Type:"Int", 			SaveName:"RATE", 		            Width:40, 		Align:"Right",		Edit:"true" , Format:"#,##0.00", CalcLogic:"(|PAY_AMT|/|LC_AMT|)*100", ColMerge:"False" },          
				    			{Header:'Nego Date',	 Type:"Date", 			SaveName:"NEGO_DATE", 			    Width:90, 		Align:"Center",		Edit:"true" , ColMerge:"False"},               
				    			{Header:'지불일',	         Type:"Date", 			SaveName:"PAY_DATE", 			    Width:90, 		Align:"Center",		Edit:"true" , ColMerge:"False"},               
				    			{Header:'USANCE이자',	 Type:"Float", 			SaveName:"USANCE_INT", 		   	    Width:90, 		Align:"Right",		Edit:"true" , ColMerge:"False"},               
				    			];
		}
		
		init_sheet.HeaderMode = {Sort:1};
		IBS_InitSheet(sheetName, init_sheet);
		sheetName.SetDataAutoTrim(0);
		//setGridSize();
	}
	
	
	$(function(){
		
		$("#btn_search").click(function(){
		    $.searchList();
		});
		
		$("#btn_excel").click(function(){
	        var params = {FileName:"LC현황.xls",SheetName:"LC현황",Merge:1,SheetDesign:1, HiddenColumn:1};
	        sheet1.Down2Excel (params);
	    });
		
		$("#btn_add").click(function() {
			var workSheet = document.form1.work_sheet.value;

			if(workSheet == '1') {
				var rowIdx = sheet1.DataCopy();
				sheet1.SetCellValue(rowIdx, "SEQ", 0);
				sheet1.SetCellValue(rowIdx, "LC_AMT", 0);
				sheet1.SetCellValue(rowIdx, "RATE", 0);
				sheet1.SetCellValue(rowIdx, "OPEN_DATE", "");
				sheet1.SetCellValue(rowIdx, "EXPIRE_DATE", "");
				sheet1.SetCellValue(rowIdx, "BANK_CD", "");      
				sheet1.SetCellValue(rowIdx, "LC_NO", "");
			}else {
				if (jQuery("#pur_cont_id").val().length < 1)
				{
				   alert("작성할 계약목록을 선택하십시요.");
				   return;
				}
				
				var rowIdx = 0;
				if (sheet2.LastRow() == 1){
					rowIdx = sheet2.DataInsert(0);
					
					var data_lc = document.form1.GET_LC.value;
					var data_lc_value = document.form1.GET_LC_VALUE.value;
					var info = {Header:'LC번호',  Type:"Combo", SaveName:"LC_NO", Width:100,  Align:"Left", Edit:"true" , ComboText:data_lc, ComboCode:data_lc_value, ColMerge:"False"};               
					
					sheet2.InitCellProperty(rowIdx, "LC_NO", info); 
				}else{
					var rowIdx = sheet2.DataCopy();
				}
				sheet2.SetCellValue(rowIdx, "PUR_CONT_ID", jQuery("#pur_cont_id").val());
				sheet2.SetCellValue(rowIdx, "SEQ", '');
				sheet2.SetCellValue(rowIdx, "PAY_AMT", 0);
				sheet2.SetCellValue(rowIdx, "RATE", 0);
				sheet2.SetCellValue(rowIdx, "NEGO_DATE", '');
				sheet2.SetCellValue(rowIdx, "PAY_DATE", '');
				sheet2.SetCellValue(rowIdx, "USANCE_INT", '');
				     
			}
		});
		
		
		$("#btn_save").click(function() {
			if (confirm("저장하시겠습니까?")) {
				 for (var i = 1; i < sheet2.LastRow(); i++ ){
					if (sheet2.GetCellValue(i, "LC_NO") == ""){
						alert("LC번호 지정은 필수입니다.");
						return ;
					}
				}
				jQuery("#fevent").val("SAVE_LC_LIST");
				var url = Modular.model.PageContext.CONTEXT_ROOT + "/payment/purchase/LCList_sheet.screen";
				var result = Modular.view.IBSheet7.doSave(sheet1, url, jQuery("#form1"), false);
			}
		}); 
		
		$.searchList = function()
		{
			$("#fevent").val("GET_LIST");
			var url = Modular.model.PageContext.CONTEXT_ROOT + "/payment/purchase/LCList_sheet.screen";
			Modular.view.IBSheet7.doSearch(sheet1, url, $("#form1"), 30, "");
		} 
		 
	});
		
	
	
	$(document).ready(function(){
		
	    $.gridInit(sheet1);
	    $.gridInit(sheet2);
		
		autoResize(); 
	});
	 
	function calendar(thisDayObj){
		dlgCalendar(thisDayObj);
	}
	
	function setGridSize() 
	{
		sheet1.FitColWidth();
		sheet2.FitColWidth();
	}
	
	function sheet1_OnSmartResize(Width, Height) { 
		autoResize();
		
	}

    function sheet2_OnSmartResize(Width, Height) { 
		autoResize();
		
	}
	
	function autoResize(){
		var t1_height = document.getElementById("search-table").scrollHeight + document.getElementById("title").scrollHeight + 24;
		var iframeHeight= document.body.scrollHeight - t1_height;    
		
		sheet1.SetSheetHeight(iframeHeight); 
		sheet2.SetSheetHeight(iframeHeight); 		 
	}		
 
	function doResult(resultObj) {
		try {
			if ( resultObj.getRequestEvent() == "GET_LC" ) {
				var rowSet	= resultObj.getRowSet();
				var getData = "";
				var getDataValue = "";
				for ( var i = 0; i < rowSet.getRowSize(); i++ ){
					getData      = getData + rowSet.getColValue( i, 1 ) + "|";
					getDataValue = getDataValue + rowSet.getColValue( i, 0 ) + "|";
				}
		 
				$("#GET_LC").val(getData);
				$("#GET_LC_VALUE").val(getDataValue);
				document.form1.isSubmit = false;
				
				jQuery("#fevent").val("GET_DETAIL");
				var url = Modular.model.PageContext.CONTEXT_ROOT + "/payment/purchase/LCList_sheet.screen";
				Modular.view.IBSheet7.doSearch(sheet2, url, jQuery("#form1"), 10, "");
			} 
		}
		catch(errorObject) {
			showErrorDlg("doResult()", errorObject);
		}
	}
 
	function sheet1_OnSearchEnd(code, message) {
	    if(code == 0) {
	    	sheet1.SetColFontColor("CST_DOC_NAME", "#0000ff");
			var f= document.form1;
	    	jQuery("#pur_cont_id").val('');
	    	$("#fevent").val("GET_LC");
			submitXmlRequest("/payment/purchase/LCList_form.screen", "GET_LC", f, "ROWSET");
		} else {
			//조회 에러 수행
			alert(message);
		} 
	}
	
	function sheet1_OnClick(Row, Col, Value, CellX, CellY, CellW, CellH) {
		
		var f= document.form1;
		f.work_sheet.value = '1';	
		
		if(Row < 2) return;  //CST_DOC_NAME 
		if(sheet1.ColSaveName(0, Col) == "CST_DOC_NAME" ){ 
			document.form1.isSubmit = false;
			jQuery("#pur_cont_id").val(sheet1.GetCellValue(Row, "PUR_CONT_ID"));
			$("#fevent").val("GET_LC");			
		    submitXmlRequest("/payment/purchase/LCList_form.screen", "GET_LC", f, "ROWSET");
		} 
	}
	
	function sheet1_OnDblClick(Row, Col, Value, CellX, CellY, CellW, CellH) {
		var f= document.form1;
		if(Row < 2) return;  //CST_DOC_NAME 
		jQuery("#pur_cont_id").val(sheet1.GetCellValue(Row, "PUR_CONT_ID"));
		$("#fevent").val("GET_LC");			
		submitXmlRequest("/payment/purchase/LCList_form.screen", "GET_LC", f, "ROWSET");
	}
	
	function sheet2_OnClick(Row, Col, Value, CellX, CellY, CellW, CellH) {
		var f= document.form1;
		f.work_sheet.value = '2';
	}
	function sheet2_OnMouseDown(Button, Shift, X, Y) { 
		var f= document.form1;
		f.work_sheet.value = '2'; 
	}
	
	function sheet1_OnSaveEnd(code, msg) {
	    if(code >= 0) {
	    	jQuery("#fevent").val("SAVE_LC_DETAIL");
			var url = Modular.model.PageContext.CONTEXT_ROOT + "/payment/purchase/LCList_sheet.screen";
			var result = Modular.view.IBSheet7.doSave(sheet2, url, jQuery("#form1"), false);
	    } else {
	    	alert("저장 실패");
	    }	
	}
	
	function sheet2_OnSaveEnd(code, msg) {
	    if(code >= 0) {
	    	alert("저장 성공");
	    	//$.searchList();
	    	var f= document.form1;
	    	$("#fevent").val("GET_LC");			
			submitXmlRequest("/payment/purchase/LCList_form.screen", "GET_LC", f, "ROWSET");
	    	
	    } else {
	    	alert("저장 실패");
	    }	
	}
	
	function sheet2_OnSearchEnd(code, msg) {
		var data_lc = document.form1.GET_LC.value;
		var data_lc_value = document.form1.GET_LC_VALUE.value;
		var info = {Header:'LC번호',  Type:"Combo", SaveName:"LC_NO", Width:100,  Align:"Left", Edit:"true" , ComboText:data_lc, ComboCode:data_lc_value, ColMerge:"False"};               
		
		var rowcnt = sheet2.RowCount();
    	if(rowcnt > 0){
			for(var r=1; r < rowcnt+1; r++)
			{
				sheet2.InitCellProperty(r, "LC_NO", info); 
			}
		}
	}
	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_id" id="pur_cont_id" value=""/>
<input type="hidden" name="work_sheet" value="">
<input type="hidden" name="GET_LC" value="">
<input type="hidden" name="GET_LC_VALUE" value="">

<div align="left" id="title">
	<ul>
    	<li>LC관리</li> 
   	</ul>
</div> 
<!-- 검색조건 시작 -->
<table id="search-table" class="search-form-table">
	<colgroup>
		<col width="100" />
		<col width="480" />
		<col width="100" />
		<col width="*" />
		<col width="150" />
	</colgroup>
  <tr height="25"> 
  	<th>현장</th>
   	<td>
  		<%=InputText.getSiteCodeNew("p_dept_cd", "p_dept_nm", "", user, params.get("p_dept_cd"), params.get("p_dept_nm"))%>
	</td>
  	<th>계약명</th>
   	<td>
   		<INPUT type="text" name="p_cst_doc_name" class="input" value="<%= params.get("p_cst_doc_name") %>" style="width:90%"> 
	</td>
	<td class="button" >
		<span class="btnSearch center" id="btn_search"><a href="javascript:void(0);">검색</a></span>
		<span class="btnSearch center" id="btn_excel"><a href="javascript:void(0);">엑셀</a></span>
	</td>
  </tr>
  <tr>
	<th>계약일</th>
    <td> 
		<%= InputText.get("p_from_dt", initSearchDateStart, 15, 50, "Center, date, PerSize, readonly", "date", "") %>
		<a href="JavaScript:calendar('p_from_dt');"><img src="/img/buyer/button/bt_calender.gif" alt="달력" border="0" align="absmiddle" /></a>
		&nbsp;~&nbsp;
		<%= InputText.get("p_to_dt", initSearchDateEnd, 15, 50, "Center, date, PerSize, readonly", "date", "") %>
		<a href="JavaScript:calendar('p_to_dt');"><img src="/img/buyer/button/bt_calender.gif" alt="달력" border="0" align="absmiddle" /></a>
    </td>
    <th>업체명</th>
    <td> 
		<INPUT type="text" name="p_member_name" class="input" value="<%= params.get("p_member_name") %>" style="width:90%">
    </td>
    <td class="button" >
		<span class="btnSearch center" id="btn_add"><a href="javascript:void(0);">추가</a></span> 
		<span class="btnSearch center" id="btn_save"><a href="javascript:void(0);">저장</a></span>
	</td>	
  </tr>
   
</table>
<TABLE border="0" cellspacing="0" cellpadding="0" width="100%">
		<tr>
			<td width="*">
				<div id="sheet1_div" style="padding-top:5px; height: auto; min-height: 300px; overflow: auto;">
					<script>createIBSheet('sheet1','99%', '300px');</script>
				</div>			
			</td>
			<td width="400px">
				<div id="sheet2_div" style="padding-top:5px; height: auto; min-height: 300px; overflow: auto;">
					<script>createIBSheet('sheet2','100%', '300px');</script>
				</div>
			</td>
		</tr>
	</TABLE>
</form>