<%@ 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.*" %>
<%@ include file="/jsp/work/taglib.jsp" %>

<jsp:useBean id="ctx" class="com.udapsoft.waf.system.context.SessionContext" scope="session" />
<%
	ctx.init(request, response);
	ValueObject user = ctx.getUser();
	String USER_ID = user.get("USER_ID");

	HandlerStorage storage = ctx.getHandlerStorage();
	ValueObject params = storage.getParams();
	String systemSelectTag = (String) storage.getObject("SystemSelectTag");
	
	String depth =  (String) params.get("depth");
	String item_cd =  (String) params.get("item_cd");
	
	String item_name = (String) storage.getObject("ITEM_NAME");
	
%>
<script type="text/javascript">

/*예약함수*/
var sheet1 = new ibsheetObject();
var sheet1Row;
var sheet1PageNo = 1;
var pageLastRowIndex = 1;
var validationResult = false;
var filterShow = false;


$(function(){
    
    $.gridInit = function(sheetName)
	{
	    var init_sheet = {};
		
		init_sheet.Cfg = {SearchMode:2, Page:30, MergeSheet:msHeaderOnly};
		init_sheet.Cols = [
									{Header:"Status",				Type:"Status", 		    SaveName:"STATUS", 			Width:0, 	Align:"Center",  Hidden:"True"},
									{Header:"삭제",					Type:"DelCheck", 		SaveName:"DEL_CHECK", 		Width:30, 	Align:"Center",  Hidden:"True"},
									{Header:"No.",					Type:"Seq", 			SaveName:"NO",				Width:20, 	Align:"Center"},
									{Header:"공종코드",				Type:"Text", 			SaveName:"ITEM_CD",			Width:40, 	Align:"Center", Edit:"false"},
									{Header:"공종명",					Type:"Text", 			SaveName:"ITEM_NAME",		Width:140, 	Align:"Left"},
									{Header:"대분류",					Type:"Text", 			SaveName:"KIND_BIG",		Width:140, 	Align:"Left",  Hidden:"True"},
									{Header:"중분류",					Type:"Text", 			SaveName:"KIND_MID",		Width:140, 	Align:"Left",  Hidden:"True"},
									{Header:"DEPTH",				Type:"Text", 			SaveName:"DEPTH",   		Width:140, 	Align:"Left",  Hidden:"True"},
									{Header:"PARENT_LEVEL",			Type:"Text", 			SaveName:"PARENT_LEVEL",	Width:140, 	Align:"Left",  Hidden:"True"},
									
						   ];
		
		
		init_sheet.HeaderMode = {Sort:1};
		IBS_InitSheet(sheetName, init_sheet);
		sheetName.SetDataAutoTrim(0);
		sheetName.SetEditable(true);
		sheetName.FitColWidth();
	}
    
	$(document).on("keyup", "input:text", function(e) {
	    
	    if (e.keyCode == 13)
        {
	        $.searchList();
        }
	    
	});
    
	$.searchList = function()
	{
		$("#fevent").val("GET_CODE_LIST");
		
		var url = Modular.model.PageContext.CONTEXT_ROOT + "/common/CommGongCodeList_sheet.screen";
		Modular.view.IBSheet7.doSearch(sheet1, url, $("#form1"), 30, "");
	}
    
    $("#btn_search").click(function(){
        $.searchList();
    });
    
    $("#btn_add").click(function(){
        var depth = "<%=depth%>";
		var item_cd = "";
		var item_cd1 = "";
		var seq = "";
		
		if(depth == "2")
		{
		    if (sheet1.SearchRows() == 0 &&  (sheet1.RowCount("I") == 0)) 
			{
		    	sheet1.SetDataAutoTrim(0);
				var rowIndex = sheet1.DataInsert(-1);
				item_cd = "<%=item_cd%>";
				sheet1.SetCellValue(rowIndex, "ITEM_CD", item_cd + leadingZeros("01",2));
				sheet1.SetCellValue(rowIndex, "KIND_BIG", item_cd.substring(0,2));   
				sheet1.SetCellValue(rowIndex, "KIND_MID", item_cd.substring(0,4));
				sheet1.SetCellValue(rowIndex, "DEPTH", '3');
				sheet1.SetCellValue(rowIndex, "PARENT_LEVEL", item_cd.substring(0,4));
				
			}
			else
			{
				sheet1.SetDataAutoTrim(0);
				var rowIndex = sheet1.DataInsert(-1);
				item_cd = sheet1.GetCellValue(rowIndex -1, "ITEM_CD");
				item_cd1 = item_cd.substring(0, item_cd.length -2);
				seq = Number(item_cd.substring(item_cd.length -2, item_cd.length)) + 1;
				sheet1.SetCellValue(rowIndex, "ITEM_CD", item_cd1 + leadingZeros(seq,2)); 
				sheet1.SetCellValue(rowIndex, "KIND_BIG", item_cd.substring(0,2));   
				sheet1.SetCellValue(rowIndex, "KIND_MID", item_cd.substring(0,4));
				sheet1.SetCellValue(rowIndex, "DEPTH", '3');
				sheet1.SetCellValue(rowIndex, "PARENT_LEVEL", item_cd.substring(0,4));
			}
		}else{
			alert("소분류를 선택하세요.");
		}
		
    });
    
    
    $("#btn_save").click(function(){
        
        $("#fevent").val("SAVE_CODE_LIST");
        
        var url = Modular.model.PageContext.CONTEXT_ROOT + "/common/CommGongCodeList_sheet.screen";
		var result = Modular.view.IBSheet7.doSave(sheet1, url, $("#form1"), false);
		
    });
    
    
    $("#btn_category_reg").click(function(){
        var depth = "<%=depth%>";
		var item_cd = "<%=item_cd%>";
		var item_name = "<%=item_name%>";
		if (depth == "1")  //1, 01 
		{
			var win_id = "_categoryReg";
		    var url = "/common/GongCategoryAddPopup.screen?depth=2"+"&item_cd=" + item_cd+"&pop_win_id="+win_id;
		}
		else if (depth == "2")
		{
			alert("상위구분을 선택하고 분류코드를 등록하십시요.");
			return ;
		}
		else{
			var win_id = "_categoryReg";
		    var url = "/common/GongCategoryAddPopup.screen?depth=1"+"&item_cd=" + "&pop_win_id="+win_id;
		}
	    		
		fLayerPop(win_id, url, 550, 120, '', '', 'NO');
			
    });
   
   
    
    $("#btn_excel").click(function(){
    	if(sheet1.RowCount() == 0) {
			alert("내역이 없습니다.");
			return;
		}
    	
		var params = { FileName : "CommGongCodeList.xls",  SheetName : "CommGongCodeList", SheetDesign:0, Merge:1, DownCols:"2|3|4|5", TitleText:"외주공종코드", UserMerge:"0,0,1,3", AutoSizeColumn:1  };

		sheet1.Down2Excel(params);
    });
    
    
});

function doResult(resultObj) {
	try {
	
	}
	catch(errorObject) {
        showErrorDlg("doResult()", errorObject);
	}
}


	$(document).ready(function(){
		
	    $.gridInit(sheet1);
		$.searchList();
		
		autoResize();
		
	});
    
    
    
	function autoResize(){
		var t1_height = document.getElementById("search-table").scrollHeight + document.getElementById("title").scrollHeight + 24;
		var iframeHeight= document.body.scrollHeight - t1_height;   
		var iframeWidth= document.body.scrollWidth;   
		
		
		sheet1.SetSheetHeight(iframeHeight); 
	}	   
	    
	    
	
	function leadingZeros(n, digits) {
	  var zero = '';
	  n = n.toString();
	
	  if (n.length < digits) {
	    for (var i = 0; i < digits - n.length; i++)
	      zero += '0';
	  }
	  return zero + n;
	}
	
	function sheet1_OnSearchEnd(code, message)
	{
	    sheet1.FitColWidth();
	}
	
	
	function sheet1_OnSaveEnd(code, msg)
	{
	    var event = $("#fevent").val();
		var f = document.form1;		
		
		document.form1.isSubmit = false;
		
	    try {
	        if(event == "SAVE_CODE_LIST")
			{
	            if(code >= "0")
				{
	                alert("저장되었습니다.");
				}
	            else
	            {
	                alert(msg);
	            }
			}
	    }
	    catch(errorObject) {
	        showErrorDlg("doResult()", errorObject);
		}
	}
	
	
</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="USER_ID" value="<%=USER_ID%>"/>
	<input type="hidden" name="depth" value = "<%=depth %>" />
	
	
	<input type="hidden" name="item_cd" value = "<%=item_cd %>" />
	<input type="hidden" name="item_name" value = "<%=item_name %>" />


<div align="left" id="title">
	<ul>
    	<li>외주공종코드</li>
   	</ul>
</div> 
 
 
<table border="0" cellspacing="0" cellpadding="0" width="98%" id="search-table">
	<!-- 검색조건 영역 : 시작 -->
	<tr>
		<td height="1">
			<table cellspacing="1" cellpadding="0" class="search-form-table">
				<col width="10%">
				<col width="30%">
				<col width="60%">

				<tr height="25"> 
    			<!-- 검색조건 타이틀 1 -->
    				<th>
    					공종코드/명
    				</th>
   					<td>
  						<input type='text' name="pgm_name2" class="input" style="width:90%" />
					</td>
				    <td rowspan="2" class="search_button" align="right"> 
				    	<span class="btnSearch center"><a href="javascript:void(0);" id="btn_search">검색</a></span>
       					<span class="btnSearch center"><a href="javascript:void(0);" id="btn_add">추가</a></span>
       					<span class="btnSearch center"><a href="javascript:void(0);" id="btn_save">저장</a></span>
       					<span class="btnSearch center"><a href="javascript:void(0);" id="btn_category_reg">분류코드</a></span>
       					<span class="btnSearch center"><a href="javascript:void(0);" id="btn_excel">엑셀다운</a></span>
    				</td>
  				</tr>
			</table>
		</td>
	</tr>
	<!-- 검색조건 영역 : 종료 -->

	<!-- 검색조건 영역, 테이터 영역 구분 여백 : 시작 -->
	<tr>
		<td height="1">
		</td>
	</tr>
	<!-- 검색조건 영역, 테이터 영역 구분 여백 : 종료 -->
</table>

<div id="sheet1_div" style="padding-top:5px; width:98%; height: 500px">
	<script>createIBSheet("sheet1","100%", "100%");</script>
</div>

</form>
