<%--
  - FileName   : InspectionDetail.jsp
  - Author     : gyeongmo an
  - Date       : 2014.09.02
  - Copyright  : Copyright (c) www.udapsoft.co.kr, Inc.
  - All rights reserved.
--%>
 
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ page import="com.udapsoft.waf.system.HandlerStorage" %>
<%@ page import="kr.co.hsnc.common.sql.*" %>
<%@ page import="kr.co.hsnc.common.util.*"%>
<%@ page import="kr.co.udapsoft.ebid.buyer.common.file.FileManager"%>
<%@ page import="com.udapsoft.waf.common.ui.*" %>
<%@ page import="com.udapsoft.waf.common.util.DateUtil" %>
<%@ page import="kr.co.udapsoft.common.code.CommCodeManager" %>
<%@ page import="java.text.*" %>
<%@ 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);
	String fevent = ctx.get("fevent");
	HandlerStorage storage = ctx.getHandlerStorage();

	ValueObject params = storage.getParams();
	ValueObject user = storage.getUser();
	
	ValueObject vo = storage.getValueObject("inspectionDetail");
	// 첨부파일
	ValueObject vo_file = storage.getValueObject("INSPECTION_DETAIL");

%>

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

function _onReady(){
	gridInit(sheet1);

	jQuery("#btnDelete").click(function() {
		if(confirm("삭제하시겠습니까?")){
			var f = document.form1;
			submitXmlRequest("/process/process/InspectionDetail_form.screen", "DELETE_INSPECTION_DETAIL", f, "SUCCESS");
		}
	});
	
	jQuery("#btnSave").click(function() {
		var isModify = <%= (vo.get("inspection_no")!="")?"true":"false"%>;
		var f = document.form1;
		
		if ( stringByteSize4UTF($("#remarks").val()) > 256 ) {
	    	alert("특기사항은 256자까지 입력하실 수 있습니다.");
	    	return;
	    }
		
		
		if( $("#inspector_name").val() == "" ) {
			alert("Inspector는 필수입력 항목 입니다.");
			$("#inspector_name").focus();
			return;
		}
		
		if( $("#inspectionDate").val() == "" ) {
			alert("Inspection Date는 필수입력 항목 입니다.");
			$("#inspectionDate").focus();
			return;
		}
		
		if(confirm("저장하시겠습니까?")){
			
			if(isModify == false){
				//저장
		    	submitXmlRequest("/process/process/InspectionDetail_form.screen", "SAVE_INSPECTION_DETAIL", f, "SUCCESS");
			}else{
				//수정
				submitXmlRequest("/process/process/InspectionDetail_form.screen", "UPDATE_INSPECTION_DETAIL", f, "SUCCESS");
			}
		}
		
	});
	
	jQuery("#btnList").click(function() {
		jQuery("#fevent").val("");
		jQuery("#form1").attr("action", "/process/process/InspectionList.screen");
		jQuery("#form1").attr("target", "_self");
		jQuery("#form1").attr("method", "post");
		jQuery("#form1").submit();	
		
	});
	
	$("[name=date_img]").click(function(){
		var id = $(this).attr("id").substring(0, $(this).attr("id").lastIndexOf("_"));
		dlgCalendar(id);
	});

	get_inspection_detail_sheet();
	
	autoResize();
}

function gridInit(sheetName){
	var init_sheet = {};
	init_sheet.Cfg = {SearchMode:2, Page:10};
	init_sheet.Cols = [
	       			{Header:'상태',					Type:"Status",		SaveName:"status", 		Width:70, 	Align:"Center", Hidden:"True"},
	       			{Header:'check',					Type:"CheckBox",		SaveName:"inspection_yn", 		Width:30, 	Align:"Center", TrueValue:"Y", FalseValue:"N"},
	       			{Header:'Inspection Code',					Type:"Text",			SaveName:"inspection_item_cd", 		Width:70, 	Align:"Center", Edit:"false"},
	       			{Header:'Inspection Description',				Type:"Text",			SaveName:"inspection_desc", 		Width:400, 	Align:"Left", Edit:"false"},
	       			{Header:'inspection_no',				Type:"Text",			SaveName:"inspection_no", 		Width:0, 	Align:"Center", Edit:"false", Hidden:"True"}
	    		];
	init_sheet.HeaderMode = {Sort:1};
	IBS_InitSheet(sheetName, init_sheet);
	sheetName.SetDataAutoTrim(0);
	
	setGridSize();
}

function sheet1_OnSearchEnd(code, message) {
    if(code == 0) {
    	setGridSize();
	} else {
		//조회 에러 수행
		//alert(message);
		alert("조회 실패");
	}
}

function sheet1_OnSaveEnd(code, msg) {
	
    if(code >= 0) {
    	alert("저장 성공");
    } else {
    	alert("저장실패");
    }
    	reloading();
}

/**
 *	IBSheet size setting
 */
function setGridSize() {
	sheet1.FitColWidth();
}
	
function save_inspection_detail_sheet() {
	<%	if( fevent.equals("REG_INSPECTION_DETAIL") ) { %>
		for(var i=1; i<sheet1.RowCount()+1; i++) {
			sheet1.SetCellValue(i, "status", "I");
		}
	<% } else { %>
		for(var i=1; i<sheet1.RowCount()+1; i++) {
			sheet1.SetCellValue(i, "status", "U");
		}
	<% } %>
	
	
	jQuery("#fevent").val("SAVE_INSPECTION_DETAIL_SHEET");
	var url = Modular.model.PageContext.CONTEXT_ROOT + "/process/process/InspectionDetail_sheet.screen";
	var result = Modular.view.IBSheet7.doSave(sheet1, url, jQuery("#form1"), false);	
}


function reloading() {
	var f = document.form1;
	f.inspection_no.value = '<%=params.get("inspection_no")%>';
	
	f.fevent.value ="GET_INSPECTION_DETAIL";
    f.action = "/process/process/InspectionDetail.screen";
    f.target = "_self";
    f.method = "post";  
    f.submit();
}

function doResult(resultObj) {
	try {
		if( resultObj.getRequestEvent() == "SAVE_INSPECTION_DETAIL" ) {
			if( resultObj.getSuccess() ) {
				save_inspection_detail_sheet();
			}
			else {
				dlgErrorMsg(resultObj);
			}
		} else if ( resultObj.getRequestEvent() == "UPDATE_INSPECTION_DETAIL" ) {
			if( resultObj.getSuccess() ) {
				save_inspection_detail_sheet();
			}
			else {
				dlgErrorMsg(resultObj);
			}
		} else if ( resultObj.getRequestEvent() == "DELETE_INSPECTION_DETAIL" ) {
			if( resultObj.getSuccess() ) {
				alert("삭제완료");
				jQuery("#btnList").click();
			}
			else {
				dlgErrorMsg(resultObj);
			}
		}
	}
	catch(errorObject) {
		showErrorDlg("doResult()", errorObject);
	}
}

//첨부파일   
function doAttachFile(obj) {
	 if(obj==''){
		alert("저장을 한 후 파일을 첨부할 수 있습니다.");
	}
	else if(obj != ''){ 
		viewFileListNew('INSPECTION_DETAIL', obj.value, document.form1.p_AttachFile, "Y", "progress/inspection");
	} 
}

function doAttachFileDiv(obj) {
	 if(obj==''){
		alert("저장을 한 후 파일을 첨부할 수 있습니다.");
	}
	else if(obj != ''){ 
		viewFileListNew('NOTI_DETAIL', obj.value, document.getElementById("attach_file_view"), "N", "progress/inspection");
	} 
}

function fileDown(file_id){
		var url="/common/file/FileDownloadServlet";
 		url += "?file_id=" + file_id;
 		
 		window.open(url, "_fileDown", "toolbar=no, width=400, height=100, top=500, left=400, toolbar=no,directories=no,status=no,scrollbars=yes,resize=no,menubar=no,target=new")
}

function get_inspection_detail_sheet() {
	<%	if( fevent.equals("REG_INSPECTION_DETAIL") ) { %>
		jQuery("#fevent").val("REG_INSPECTION_DETAIL_SHEET");
	<%
	} else {
	%>
		jQuery("#fevent").val("GET_INSPECTION_DETAIL_SHEET");
	<%
		}
	%>
	
	var url = Modular.model.PageContext.CONTEXT_ROOT + "/process/process/InspectionDetail_sheet.screen";
	Modular.view.IBSheet7.doSearch(sheet1, url, jQuery("#form1"), 10, "");
	
}

function SearchUser() {
	var win_id = "_searchUserPopup";
	var link = "/lib/dlgSearchUserPop.screen";
	    link += "?pop_win_id="+win_id;
	fLayerPop(win_id, link, 700,400, '', '', 'NO');	
}

function SearchUserClose(result){
	var f= document.form1;
	f.inspector_name.value = result[1];
	f.inspector.value = result[0];

	fLayerPopClose('_searchUserPopup');
}

function sheet1_OnSmartResize(Width, Height) { 
	sheet1.FitColWidth();
	autoResize();
}

function autoResize(){
	var t1_height = document.getElementById("f-table").scrollHeight + document.getElementById("title").scrollHeight + 24 + 45;
	var iframeHeight= document.body.scrollHeight - t1_height;   
	var iframeWidth= document.body.scrollWidth;   
	
	
	sheet1.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">

<INPUT type="hidden" name="fevent" value="<%= params.get("fevent") %>">
<INPUT type="hidden" name="inspection_no" value="<%= params.get("inspection_no") %>">
<INPUT type="hidden" name="psr_id" value="<%= params.get("psr_id") %>">
<INPUT type="hidden" name="psr_activity_cd" value="<%= params.get("psr_activity_cd") %>">

<INPUT type="hidden" name="mr_no" value="<%= params.get("mr_no") %>">
<INPUT type="hidden" name="corp_name" value="<%= params.get("corp_name") %>">
<INPUT type="hidden" name="po_no" value="<%= params.get("po_no") %>">
<INPUT type="hidden" name="po_description" value="<%= params.get("po_description") %>">
<INPUT type="hidden" name="pur_cont_id" value="<%= params.get("pur_cont_id") %>">
<INPUT type="hidden" name="demanded_id" value="<%= params.get("demanded_id") %>">
<INPUT type="hidden" name="dept_cd2" value="<%= params.get("dept_cd2") %>">
<INPUT type="hidden" name="dept_cd" value="<%= params.get("dept_cd") %>">
<INPUT type="hidden" name="dept_nm" value="<%= params.get("dept_nm") %>">
<INPUT type="hidden" name="sYmdSt" value="<%= params.get("sYmdSt") %>">
<INPUT type="hidden" name="sYmdEd" value="<%= params.get("sYmdEd") %>">
<INPUT type="hidden" name="p_discipline" value="<%= params.get("p_discipline") %>">
<INPUT type="hidden" name="p_mr_no" value="<%= params.get("p_mr_no") %>">
<INPUT type="hidden" name="p_corp_name" value="<%= params.get("p_corp_name") %>">

	<!-- 제목 및 버튼 -->
   	<table width="100%">
		<colgroup>
			<col width="500" />
			<col width="*" />
		</colgroup>
	<tr>
	   	<th>
		   <div align="left" id="title">
		    <ul>
		     <li>Inspection Detail</li>
		    </ul>
		   </div>
	   </th>
	   <td align="right">
			<span class="btnSearch center"><a href="javascript:void(0);" id="btnSave">저장</a></span>
			<span class="btnSearch center"><a href="javascript:void(0);" id="btnDelete">삭제</a></span>
			<span class="btnSearch center"><a href="javascript:void(0);" id="btnList">목록</a></span>
	   </td>
   	</tr>
   	</table>


<!-- 데이터 영역 : 시작 -->
<TABLE id="f-table" class="form-table">
		<colgroup>
			<col width="10%" />
			<col width="35%" />
			<col width="13%" />
			<col width="*" />
		</colgroup>	
  <TR height="25"> 
    <th> MR No</th>
	<TD>
		<%=params.get("mr_no") %>
    </TD>
	<th> Vendor</th>
	<TD>
		<%=params.get("corp_name") %>
	</TD>
  </TR>		
  <TR height="25"> 
	<th>PO No</th>
	<TD> 
		<%=params.get("po_no") %>
	</TD>
	<th> PO Title</th>
	<TD>
		<%=params.get("po_description") %>
 	</TD>
  </TR>  
  <TR height="25"> 
    <th class="required">Inspector</th>
	<TD>
		<%= InputText.get("inspector_name", vo.get("inspector_name"), 17, 10, "desc=,readOnly") %>
		<INPUT type='hidden' name="inspector" desc="" value="<%=vo.get("inspector")%>">
		<IMG src="/img/buyer/button/bt_search_right.gif" alt="담당자" border="0" align="absmiddle" style="cursor:pointer;" onClick="JavaScript:SearchUser();">
    </TD>
	<th class="required">Inspection Date</th>
	<TD>
		<%= InputText.get("inspectionDate", DateUtil.formatDateTime(vo.get("inspection_date"), "yyyy-MM-dd", "yyyy-MM-dd"), 15, 50, "readOnly, persize, center") %>
		<IMG name="inspectionDate_date_img" src="/img/buyer/button/bt_calender.gif" border="0" align="absmiddle" onClick="JavaScript:dlgCalendar('inspectionDate');">
	</TD>
  </TR>
  <TR height="25"> 
	<th class="required">Inspection Kind</th>
	<TD>
		<%	if( fevent.equals("REG_INSPECTION_DETAIL") ) { %>
			<%= CommCodeManager.getSelectCodeList("INSPECTION_KIND", "inspection_kind", "width:99%;height=18px;", "", "45", "") %>
		<% } else { %>
			<%= CommCodeManager.getSelectCodeList("INSPECTION_KIND", "inspection_kind", "width:99%;height=18px;", "", vo.get("inspection_kind"), "") %>
		<% } %>
	</TD>
	<th>IRN Date</th>
	<TD>
		<%= InputText.get("irnDate", DateUtil.formatDateTime(vo.get("irn_date"), "yyyy-MM-dd", "yyyy-MM-dd"), 15, 8, "persize, center, date") %>
		<IMG name="irnDate_date_img" src="/img/buyer/button/bt_calender.gif" border="0" align="absmiddle" onClick="JavaScript:dlgCalendar('irnDate');">
	</TD>
  </TR>
   <TR height="25"> 
      <th>Final</th>
      <TD>
      	<%= CommCodeManager.getSelectCodeList("YES_NO", "finalFlag", "width:33%;height=18px;", "", vo.get("final_flag"), "") %>
      </TD>      
      <th>Confirm</th>
      <TD>
      	<%= CommCodeManager.getSelectCodeList("YES_NO", "inspectionEndFlag", "width:21%;height=18px;", "", vo.get("inspection_end_flag"), "") %>
      </TD>
   </TR>
   <TR height="25">
      <th>Remarks</th>
      <TD>
      	<TEXTAREA name="remarks" style="width:97%" rows="6" class="input"><%=vo.get("remarks") %></TEXTAREA>
      </TD>
      <th>Attachment</th>
      <TD>
        	<TEXTAREA name="p_AttachFile" style="width:83%" rows="6" class="input" readOnly><%=vo_file.get("INSPECTION_DETAIL") %></TEXTAREA>
			<!-- <IMG src="/img/buyer/icon/icon_plus.gif" alt="adjust size" border="0" align="top" onClick="JavaScript:toggleTextarea(this, document.form1.p_AttachFile, 1, 7);" style="cursor:hand;"> -->
	        <IMG id='search_spot_button' onClick="doAttachFile(document.form1.inspection_no);" src='/img/buyer/button/bt_change.gif' border='0'  align="top" align='absmiddle' alt='[Attach File]' style='cursor:hand;'>
      </TD>
   </TR>
 </TABLE>
 
  <div class="application-info magT10">
	<div class="application-sub-title">
		<span class="title">Inspection Item</span>
	</div>
	<div class="application-location">
	</div>
</div>
 
<div id="sheet1_div" style="padding-top:5px; height: auto; min-height: 195px; overflow: auto;">
	<script>createIBSheet('sheet1','100%', '195px');</script>
</div>
</FORM>