<%--
  - FileName   : ContBudget.jsp
  - Author(s)  : lee mi kyoung
  - Date       : 2017.1.25
  -
  - Description: 구매외주 예산등록 
  --%>
  


<%@ 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="kr.co.udapsoft.common.code.CommCodeManager" %>                                                                                                                                                                                                       
<%@ 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();
    
    RowSet rowSet = storage.getRowSet("ROWSET");
   	
	String temp1="";
	String temp2="";
	for(int i=0;i<rowSet.size();i++){
		if(i==rowSet.size()-1){
		    temp1 +=(rowSet.getColumn(i,"ESTDESC").toString().substring(8)) ;
		    temp2 +=(rowSet.getColumn(i,"PAYITEM").toString().substring(8)) ;
		}else{
		    temp1 =temp1+(rowSet.getColumn(i,"ESTDESC").toString().substring(8)+"|") ;
			temp2 =temp2+(rowSet.getColumn(i,"PAYITEM").toString().substring(8)+"|") ; 			
		}
    }
	//2017.04.04 외주별도처리
	temp1 = temp1 + "|(T01)Engineering|(T02)Construction";
	temp2 = temp2 + "|T01|T02";
	
%>

<script type="text/javascript" src="/js/creditEvaluationPop.js"></script>
<script language="javascript">
	/*예약함수*/
	var sheet1 = new ibsheetObject();
	var sheet1Row;
	var sheet1PageNo = 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:msHeaderOnly, Page:100};
		init_sheet.Cols = [
		       			{Header:'Del',					Type:"DelCheck",		SaveName:"DEL_CHECK", 				Width:50, 		Align:"Center" },
		    			{Header:'Status',				Type:"Status", 			SaveName:"STATUS", 					Width:0, 		Align:"Center", 	Edit:"true", Hidden:"True"},
		    			{Header:'DEPT_CD',				Type:"Text", 			SaveName:"DEPT_CD", 			    Width:120, 		Align:"Left", 	    Edit:"true", Hidden:"True"},
		    			{Header:'부서명',				    Type:"Text", 			SaveName:"DEPT_NM", 			    Width:180, 		Align:"Left", 		Edit:"true", Hidden:"True"},
		    			{Header:'정렬순서',				Type:"Int", 		    SaveName:"ORDER_SEQ", 			    Width:40, 		Align:"Center", 	Edit:"true"},
		    			{Header:'구매/외주',				Type:"Combo",			SaveName:"KIND_GB", 			    Width:40, 		Align:"Left", 	    Edit:"true", ComboText:"구매|외주",  ComboCode:"P|O"},
		    			{Header:'PAY_ITEM',			    Type:"Combo", 	    	SaveName:"PAY_ITEM", 			    Width:60, 		Align:"Left", 		Edit:"true", ComboText:"<%=temp1%>",  ComboCode:"<%=temp2%>"},
		    			{Header:'PAY_ITEM_NAME',        Type:"Text", 		    SaveName:"PAY_ITEM_NAME",			Width:80, 		Align:"Left", 		Edit:"true", Hidden:"False"},
		    			{Header:'PAY_ITEM_SEQ',			Type:"Text", 		    SaveName:"PAY_ITEM_SEQ",			Width:60, 		Align:"Left", 		Edit:"true", Hidden:"True"},
		    			{Header:'품목',				    Type:"Text", 		    SaveName:"ITEM_NAME", 			    Width:100,		Align:"Left", 		Edit:"true"},
		    			{Header:'실행예산',			    Type:"Int", 		    SaveName:"EXEAMT", 			        Width:60,		Align:"Right", 		Edit:"true", Format:"#,##0"},
		    			{Header:'FILE_GB',				Type:"Text", 			SaveName:"FILE_GB", 			    Width:70, 		Align:"Left",    	Edit:"true", Hidden:"True"},
		    			{Header:'수정일자',				Type:"Text", 		    SaveName:"UPD_DATE", 			    Width:60, 		Align:"Center", 	Edit:"true"}		    			
		    			
		];

		init_sheet.HeaderMode = {Sort:1};
		IBS_InitSheet(sheetName, init_sheet);
		sheetName.SetDataAutoTrim(0);  
		var info = [{StdCol:3, SumCols:"EXEAMT",  CaptionCol:9}
	               ,{StdCol:5, SumCols:"EXEAMT",  CaptionCol:9}
	               ,{StdCol:6, SumCols:"EXEAMT",  CaptionCol:9} ];
				
		sheetName.ShowSubSum(info);
		setGridSize();
	}
	
	
	$(function(){
		
		$("#btn_search").click(function(){
		    $.searchList();
		});
		
		$("#btn_add").click(function(){
			var f = document.form1;
			if ($("#p_dept_cd").val() == "") {
				alert("현장을 선택하십시요.");
				return ;
			}
						
			sheet1.DataAutoTrim = false;
			if (sheet1.LastRow() == 0){
				//선급공사원가 통합
				var f= document.form1;
			    $("#fevent").val("PRE_WORK"); 
			    submitXmlRequest("/cont/purchase/ContBudget_form.screen", "PRE_WORK", f, "ROWSET");
				//sheet1.DataInsert(0);
			}
			else{
				var rowIdx = sheet1.DataCopy();
				sheet1.SetCellValue(rowIdx, "ITEM_NAME", '');
				sheet1.SetCellValue(rowIdx, "EXEAMT", 0);
				sheet1.SetCellValue(rowIdx, "UPD_DATE", '');
				sheet1.SetCellValue(rowIdx, "ORDER_SEQ", 0);
				changeList(sheet1.GetCellValue(rowIdx, "KIND_GB"), rowIdx);
			}
			
		});
		
		$("#btn_save").click(function(){
			jQuery("#fevent").val("BUDGET_SAVE");
			var url = Modular.model.PageContext.CONTEXT_ROOT + "/cont/purchase/ContBudget_sheet.screen";
			var result = Modular.view.IBSheet7.doSave(sheet1, url, jQuery("#form1"), false);		
		});
		
		$("#btn_excel").click(function(){
	        var params = {FileName:"예산현황.xls",SheetName:"예산현황",Merge:1,SheetDesign:1, HiddenColumn:1};
	        sheet1.Down2Excel (params);
	    });
		
		
		$("#btn_file").click(function(){
			var f = document.form1;
			var v_dept = $("#p_dept_cd").val();
			if (v_dept == "") {
				alert("현장을 선택하십시요.");
				return ;
			}
			else if(v_dept != ''){
				var upLoadFlg = "Y";
				viewFileListNew1('CONT_BCS_'+v_dept, v_dept, upLoadFlg, "cont/bcs/"+v_dept) ; 
			} 
	    });
		
		$.searchList = function()
		{
			if ($("#p_dept_cd").val() == "") {
				alert("현장을 선택하십시요.");
				return ;
			}
			
			$("#fevent").val("BUDGET_CONTROL");
			
			var url = Modular.model.PageContext.CONTEXT_ROOT + "/cont/purchase/ContBudget_sheet.screen";
			Modular.view.IBSheet7.doSearch(sheet1, url, $("#form1"), 30, "");
		} 
		 
	});
	
	
	$(document).ready(function(){
		
	    $.gridInit(sheet1);
		
		autoResize();  
		<%if(user.get("right_name").equals("OUTSIDE")){%>
			$("#P_KIND_GB").val("O");
		<%}else if(user.get("right_name").equals("PURCHASE")){%>
			$("#P_KIND_GB").val("P");
		<%}%>
			
	});
	 
	function doResult(resultObj) {
		try {
			if ( resultObj.getRequestEvent() == "PRE_WORK" ) {
				document.form1.isSubmit = false;
				$.searchList();
				alert("최초 작성시 선급공사원가 확인하였습니다.");
				if (sheet1.LastRow() == 0){ 
					sheet1.DataInsert(0);
					sheet1.SetCellValue(1, "KIND_GB",$("#P_KIND_GB").val() );
					changeList($("#P_KIND_GB").val(), 1);
				}
			} 
		}
		catch(errorObject) {
			showErrorDlg("doResult()", errorObject);
		}
	}
	
	function setGridSize() 
	{
		sheet1.FitColWidth();
		autoResize();
	}
	 
	
	function autoResize(){
		var t1_height = document.getElementById("search-table").scrollHeight + document.getElementById("title").scrollHeight + 54;
		var iframeHeight= document.body.scrollHeight - t1_height;   
		var iframeWidth= document.body.scrollWidth;   
		
		sheet1.SetSheetHeight(iframeHeight);
	}		
	
	function sheet1_OnSmartResize(Width, Height) { 
		sheet1.FitColWidth();
		autoResize();
	}
	
	function sheet1_OnSaveEnd(code, msg) {
	    if(code >= 0) {
	    	alert("저장되었습니다.");
	    	$.searchList();
	    } else {
	    	alert("저장 실패");
	    }	
	}
 
	function viewFileListNew1(fileGB, id, isModify, isDrectory, iframeID, user_filename) {
		var win_id = "_viewFileList";
		var link = "/common/file/FileListNew.screen";
		    link += "?pop_win_id="+win_id;
		    link += "&extFunction=viewFileListClose1";
		    link += "&file_gb=" + fileGB;
		    link += "&id=" + id;
		    
		    if( iframeID != null )
		    	link += "&iframeID=" + iframeID;
		    
			if( isModify != null )
				link += "&is_modify=" + isModify;
			if( isDrectory != null )
				link += "&sys_gb=" + isDrectory;	    
			if( user_filename != null )
				link += "&user_filename=" + user_filename;	 
		
		fLayerPop(win_id, link, 750,430, '', '', 'NO', '200');
	}
	
	function sheet1_OnChange(Row, Col, Value, OldValue, RaiseFlag) 
	{ 
		//2017.04.04 외주 구분 두개로 고정 
		if (Col == "5"){
			changeList(sheet1.GetCellValue(Row,5), Row);
	    }
		if (Col == "6"){
			var vText =  sheet1.GetCellText(Row,6);
			sheet1.SetCellText(Row, 7, vText.substring(5,vText.length));
		}
	}
	 
	function sheet1_OnSearchEnd(code, message) {
	    if(code == 0) { 
	    	for(var i=1; i <= sheet1.RowCount()-1; i++){
	    		changeList(sheet1.GetCellValue(i, "KIND_GB"), i);
  	    	} 
		} else {
			//조회 에러 수행
			alert(message);
		}
	}
	
	
	
	function changeList(payGB, Row){
		if (payGB == "O"){
			var data_lc = "(T01)Engineering|(T02)Construction"; 
	    	var data_lc_value = "T01|T02";
			var info = {Header:'PAY_ITEM',  Type:"Combo", SaveName:"PAY_ITEM", Width:100,  Align:"Left", Edit:"true" , ComboText:data_lc, ComboCode:data_lc_value, ColMerge:"False"};               
	    	sheet1.InitCellProperty(Row, "PAY_ITEM", info); 
		}else{ 
			var info = {Header:'PAY_ITEM',  Type:"Combo", SaveName:"PAY_ITEM", Width:100,  Align:"Left", Edit:"true" , ComboText:"<%=temp1%>", ComboCode:"<%=temp2%>", ColMerge:"False"};               
	    	sheet1.InitCellProperty(Row, "PAY_ITEM", 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="materialgroup" id="materialgroup" value=""/>
 
<div align="left" id="title">
	<ul>
    	<li>예산등록</li> 
   	</ul>
</div> 
<!-- 검색조건 시작 -->
<table id="search-table" class="search-form-table">
	<colgroup>
		<col width="100" />
		<col width="400" />
		<col width="100" />
		<col width="*" />
		<col width="300" />
	</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>
  		<%=CommCodeManager.getSelectCodeList("JOB_DIV","P_KIND_GB","height=18;width:80px;","<option value=''>--전체--</option>",params.get("P_KIND_GB")) %>
	</td>
  	<td class="button" >
		<span class="btnSearch center" id="btn_search"><a href="javascript:void(0);">검색</a></span>
		<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>
		<span class="btnSearch center" id="btn_excel"><a href="javascript:void(0);" >엑셀</a></span>
		<span class="btnSearch center" id="btn_file"><a href="javascript:void(0);" >첨부파일</a></span>
	</td>	
  </tr>   
</table>
</form>
<img src="/img/com/blank.gif" height="5px" />
   <div align="right" id="bugase">
    	<ul>
        단위(원)
    	</ul>
  	</div>
<div id="sheet1_div" style="padding-top:5px; height: auto; min-height: 400px; overflow: auto;">
	<script>createIBSheet('sheet1','100%', '400px');</script>
</div>