<%--
  - Name   : WPRList.jsp
  - Author(s)  : gyeongmo an
  - Date       : 2014.08.28
  - Copyright  : Copyright (c) 2014 www.udapsoft.co.kr, Inc.
  - Description: WPR
  --%>
  
<%@ 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="kr.co.udapsoft.common.code.CommCodeManager" %>
<%@ page import="com.udapsoft.waf.common.util.DateUtil" %>

<%@ 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 fevent = storage.getEvent();
    
    String today = DateUtil.formatDateTime(DateUtil.getToday(), "yyyyMMdd", "yyyyMMdd");
%>

<SCRIPT language="JavaScript">
/*예약함수*/
var sheet1;
var sheet1Row;
var sheet1PageNo = 1;
var sheet2;
var sheet2Row;
var sheet2PageNo = 1;
var pageLastRowIndex = 1;
var validationResult = false;
var filterShow = false;
var selectRow = 0;


function _onReady(){
	gridInit(sheet1);
	gridInit(sheet2);

	jQuery("#btnSearch").click(function() {
		selectRow = 2;
		jQuery("#fevent").val("GET_WPR_LIST");
		var url = Modular.model.PageContext.CONTEXT_ROOT + "/process/process/WPRList_sheet.screen";
		Modular.view.IBSheet7.doSearch(sheet1, url, jQuery("#form1"), 10, "");
	});
	
	jQuery("#btnSave").click(function() {
		if(confirm("저장하시겠습니까?")) {
			var weightSum=0;
			 for(var i=2; i<sheet2.RowCount()+2; i++) {
				 weightSum += Number(sheet2.GetCellValue(i, "weight_factor"));
			 }
			 
			 if(weightSum != 100) {
				 alert("W/F의 합은 100이여야 합니다.");
				 return;
			 }
			
			
			jQuery("#fevent").val("SAVE_WPR_LIST");
			var url = Modular.model.PageContext.CONTEXT_ROOT + "/process/process/WPRList_sheet.screen";
			var result = Modular.view.IBSheet7.doSave(sheet2, url, jQuery("#form1"), false);	
		}
		 
	});
	
	
	jQuery("#btnExcel").click(function() {
		if(sheet1.RowCount() == 0) {
			alert("내역이 없습니다.");
			return;
		}
		var params = { FileName : "WPRList.xls",  SheetName : "WPRList", SheetDesign:0, Merge:1, DownCols:"discipline_nm|mr_no|ord_doc_no|cst_doc_name|vendor_name|wf_progress"
				, TitleText:"Work Progress Status", UserMerge:"0,0,1,6", AutoSizeColumn:1  } ;
		sheet1.Down2Excel(params);
	});
	
	sheet1.SetActionMenu("ExcelDown");
	
	autoResize();
}


function gridInit(sheetName){
	var init_sheet = {};
	init_sheet.Cfg = {SearchMode:2, Page:10, MergeSheet:msPrevColumnMerge + msHeaderOnly};
	
	if(sheetName.id == "sheet1") {
		init_sheet.Cols = [
			       			{Header:'status|status',	Type:"Status",	SaveName:"status",		Width:100, 	Align:"Center", Edit:"false", Hidden:"True"},
			       			{Header:'Discipline|Discipline',	Type:"Text",	SaveName:"discipline_nm",		Width:100, 	Align:"Center", Edit:"false"},
			    			{Header:'MR Number|MR Number',	Type:"Text",	SaveName:"mr_no",		Width:90, 	Align:"Left", Edit:"false"},
			    			{Header:'PO Number|PO Number',	Type:"Text",	SaveName:"ord_doc_no",		Width:170, 	Align:"Left", Edit:"false"},
			       			{Header:'PO Title|PO Title',	Type:"Text",	SaveName:"cst_doc_name",		Width:250, 	Align:"Left", Edit:"false"},
			       			{Header:'Vendor|Vendor',	Type:"Text",	SaveName:"vendor_name",		Width:100, 	Align:"Left", Edit:"false"},
			       			{Header:'Progress|Progress',	Type:"Text",	SaveName:"wf_progress",		Width:60, 	Align:"Center", Edit:"false"},
			       			{Header:'discipline|discipline',	Type:"Text",	SaveName:"discipline",		Width:100, 	Align:"Center", Edit:"false", Hidden:"True"},
			       			{Header:'psr_id|psr_id',	Type:"Text",	SaveName:"psr_id",		Width:100, 	Align:"Center", Edit:"false", Hidden:"True"},
			       			{Header:'dept_cd|dept_cd',	Type:"Text",	SaveName:"dept_cd",		Width:100, 	Align:"Center", Edit:"false", Hidden:"True"},
			       			{Header:'flag_yn|flag_yn',	Type:"Text",	SaveName:"flag_yn",		Width:100, 	Align:"Center", Edit:"false", Hidden:"True"}
		];
	} else {
		init_sheet.Cols = [
			       			{Header:'status',	Type:"Status",	SaveName:"status",		Width:100, 	Align:"Center", Edit:"false", Hidden:"True"},
			       			{Header:'Activity|Activity',	Type:"Text",	SaveName:"psr_activity_nm",		Width:150, 	Align:"Left", Edit:"false"},
			    			{Header:'W/F|W/F',	Type:"Int",	SaveName:"weight_factor",		Width:50, 	Align:"Center", PointCount:0, EditLen:2},
			    			{Header:'Schedule|Plan Date',	Type:"Date",	SaveName:"plan_dt",		Width:120, 	Align:"Center"},
			    			{Header:'Schedule|Forecasting\nDate',	Type:"Date",	SaveName:"forecasting_dt",		Width:120, 	Align:"Center"},
			    			{Header:'Schedule|Actual\nDate',	Type:"Date",	SaveName:"actual_dt",		Width:120, 	Align:"Center"},
			    			{Header:'psr_activity_cd|psr_activity_cd',	Type:"Text",	SaveName:"psr_activity_cd",		Width:100, 	Align:"Center", Edit:"false", Hidden:"True"},
			    			{Header:'psr_id|psr_id',	Type:"Text",	SaveName:"psr_id",		Width:100, 	Align:"Center", Edit:"false", Hidden:"True"},
			    			{Header:'mr_no|mr_no',	Type:"Text",	SaveName:"mr_no",		Width:100, 	Align:"Center", Edit:"false", Hidden:"True"},
			    			{Header:'demanded_id|demanded_id',	Type:"Text",	SaveName:"demanded_id",		Width:100, 	Align:"Center", Edit:"false", Hidden:"True"},
			    			{Header:'pur_cont_id|pur_cont_id',	Type:"Text",	SaveName:"pur_cont_id",		Width:100, 	Align:"Center", Edit:"false", Hidden:"True"},
			    			{Header:'dept_cd|dept_cd',	Type:"Text",	SaveName:"dept_cd",		Width:100, 	Align:"Center", Edit:"false", Hidden:"True"}
		];
	}

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

	function setGridSize(sheetName) {
	sheetName.FitColWidth();
}


function sheet1_OnSearchEnd(code, message) {
    if(code == 0) {
    	//setGridSize(sheet1);
    	for(var i=0; i<sheet1.RowCount(); i++) {
    		if(sheet1.GetCellValue(i+2, "flag_yn") == "Y") {
    			sheet1.SetCellFontColor(i+2, "mr_no", "#CD1039");
    		}
    	}
    	
    	sheet1.SetSelectRow(selectRow);

    	if(sheet1.RowCount() > 0) { 
    		sheet1_OnClick( sheet1.GetSelectRow() ); 
    	}
	} else {
		//조회 에러 수행
		alert(message);
	}
}
function sheet2_OnSearchEnd(code, message) {
    if(code == 0) {
    	//setGridSize(sheet2);

    	for(var i=0; i<sheet2.RowCount(); i++) {
    		if( sheet2.GetCellValue(i+2, "plan_dt") != "" ) {
    			if('<%=today%>' > sheet2.GetCellValue(i+2, "plan_dt") && sheet2.GetCellValue(i+2, "actual_dt") == "") {
      				sheet2.SetCellFontColor(i+2, "plan_dt", "#CD1039");
      			}	
    		}
    	}
	} else {
		//조회 에러 수행
		alert(message);
	}
}

	function sheet1_OnClick(Row, Col, Value, CellX, CellY, CellW, CellH) {
		if(Row < 2) { return; }
		
		selectRow = sheet1.GetSelectRow();
		jQuery("#fevent").val("GET_WPR_DETAIL");
		jQuery("#psr_id").val(sheet1.GetCellValue(Row, "psr_id"));
		
		var url = Modular.model.PageContext.CONTEXT_ROOT + "/process/process/WPRList_sheet.screen";
		Modular.view.IBSheet7.doSearch(sheet2, url, jQuery("#form1"), 10, "");
	}
	

	 function sheet2_OnSaveEnd(Code, Msg, StCode, StMsg) {
		 if(Code >= 0) {
	    	alert("저장 성공");
	    } else {
	    	alert("저장실패");
	    }
		 
		 jQuery("#fevent").val("GET_WPR_LIST");
			var url = Modular.model.PageContext.CONTEXT_ROOT + "/process/process/WPRList_sheet.screen";
			Modular.view.IBSheet7.doSearch(sheet1, url, jQuery("#form1"), 10, "");
	 }

		function clearDiscipline() {
			var f= document.form1;
			f.p_discipline.value = "";
		}
		
		function sheet1_OnSelectMenu(Text, Code) {
			switch(Text) {
				case "ExcelDown":
					jQuery("#btnExcel").click();
				break;
			}
		}
	
	 function sheet1_OnSmartResize(Width, Height) { 
			sheet1.FitColWidth();
			autoResize();
			
		}
	 function sheet2_OnSmartResize(Width, Height) { 
			sheet2.FitColWidth();
			autoResize();
			
		}
	

	function autoResize(){
		var t1_height = document.getElementById("search-table").scrollHeight + document.getElementById("title").scrollHeight + document.getElementById("sheet_title").scrollHeight + 35;
		var iframeHeight= document.body.scrollHeight - t1_height;   
		var iframeWidth= document.body.scrollWidth;   
		
		
		sheet1.SetSheetHeight(iframeHeight); 
		sheet2.SetSheetHeight(iframeHeight);
	}	

	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" style="width:100%;height:100%;">
<INPUT type="hidden" name="fevent" id="fevent">
<INPUT type="hidden" name="psr_id" id="psr_id">



<div align="left" id="title">
    <ul>
     <li>Work Progress Report</li>
    </ul>
  </div> 
	<!-- 검색조건 시작 -->
	<table id="search-table" class="search-form-table">
		<colgroup>
			<col width="80" />
			<col width="350" />
			<col width="80" />
			<col width="350" />
			<col width="80" />
			<col width="*" />
			<col width="250" />
		</colgroup>
		<tr>
			<th>Project</th>
			<td>
		   		<%=InputText.getSiteCodeNew("dept_cd", "dept_nm", "", user, params.get("dept_cd"), params.get("dept_nm"))%>
			</td>
			<th>Vendor</th>
			<td>
				<%= InputText.get("corp_name", params.get("corp_name"), 90, 50, "hangule, desc=corp name, PerSize") %>
			</td>
			<th>Status</th>
			<td>
				<select id="endYn" style="width:100px" >
					<OPTION value='all'>전체</OPTION>
					<OPTION value='ing'>진행중</OPTION>
					<OPTION value='end'>진행완료</OPTION>
				</select>
			</td>
			<td class="button" rowspan="2">
				<span class="btnSearch center"><a href="javascript:void(0);" id="btnSearch">검색</a></span>
				<span class="btnSearch center"><a href="javascript:void(0);" id="btnSave">저장</a></span>
				<span class="btnSearch center"><a href="javascript:void(0);" id="btnExcel">엑셀</a></span>
			</td>
		</tr>
		<tr>
			<th>Discipline</th>
			<td>
				<%= CommCodeManager.getSelectCodeList("BAS_LVL02", "p_discipline", "width:85%;height=18px;", "<OPTION value=''>-- 전체 --</OPTION>", params.get("discipline"), "") %>
				<IMG id='_btn_clear' onClick="clearDiscipline();" src='/img/buyer/button/bt_eraser1.gif' border='0' align='absmiddle' alt='[초기화]' style='cursor:pointer;'>
			</td>
			<th>MR No</th>
			<td colspan="3">
				<%= InputText.get("p_mr_no", "", 97, 50, "hangule, desc=MR No, PerSize") %>
			</td>
		</tr>
	</table>
	
	<TABLE id="sheet_title" border="0" cellspacing="0" cellpadding="0" width="100%">
		<tr>
			<td width="*">
				<div class="application-info magT10">
						<div class="application-sub-title">
							<span class="title">Work Progress List</span>
						</div>
						<div class="application-location">
						</div>
					</div>
			</td>
			<td width="400px">
				<div class="application-info magT10">
					<div class="application-sub-title">
						<span class="title">Detail Status</span>
					</div>
					<div class="application-location">
					</div>
				</div>
			</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>
	

	
	
	
	