<%@ page contentType="text/html;charset=UTF-8" %>
<%@ page import="java.util.Iterator" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.Collection" %>
<%@ page import="net.sz600.basicdata.*" %>
<%@ page import="net.sz600.tool.SzConvert" %>
<%@ page import="net.sz600.tool.SzDateHelp" %>
<%@ page import="net.sz600.system.*" %>
<%@ page import="net.sz600.flow.*" %>
<%@ page import="net.sz600.online.*" %>
<%@ page import="net.sz600.formEngine2.*" %>
<%@ page import="net.sz600.weboffice.*" %>
<%@ include file="/sessionInfo.jsp"%>
<head>
<%@ include file="/share/oa_title.jsp"%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="<%=strSkin_SESSION%>/css2007.css" rel="stylesheet" type="text/css">
</head>

<% 
	 String sKEY =  SzConvert.toZeroStr(request.getParameter("KEY"));
	 int iFormID = SzConvert.toInt(request.getParameter("FormID"));	
	 String sWeboffice = SzConvert.toZeroStr(request.getParameter("WebOffice"));
	 String sOPERATOR = SzConvert.toSpaceStr(request.getParameter("OPERATOR"));
	 String sRecordId = SzConvert.toSpaceStr(request.getParameter("RecordId"));
	 String sCreate = SzConvert.toSpaceStr(request.getParameter("Create"));
	 
	 String sEditType = "0";
	 BdFormControl formControl = new BdFormControl();
	 boolean hasFlow = formControl.getHasFlow(iFormID);
	 //2016-04-15增加
	 String assistant = formControl.getAssistant(iFormID);
	 boolean hasOfficeMult = false;
	 if ((";" + assistant + ";").indexOf(";" + BdFormHelp.Assistant_Office_Mult + ";")>0) {
		 hasOfficeMult = true;
	 }
	 
	 boolean canSave = true;
		if (sOPERATOR.startsWith("add") || (sOPERATOR.startsWith("update") && hasFlow)) {
			sEditType = "1";
		} else if (sOPERATOR.startsWith("update") && !hasFlow) {
			sEditType = "2";
		} else if (sOPERATOR.startsWith("transact")) {
			sEditType = "3";
		} else {
			canSave = false;
		}
		//System.out.println("==sWeboffice==" + sWeboffice);
		
		String FileType   = sWeboffice;
		String ModuleType = "formEngine";
		int    ModuleID   = iFormID;		
		OfficeRecordID officeRecordID = new OfficeRecordID();
		
	if (canSave && hasFlow) {
		//判断是否有修改权限：2016-03-16增加
		LcFlowControl lcFlowControl = new LcFlowControl();
		LcFlowPopedomInfo lcFlowPopedomInfo = lcFlowControl.getFlowPopedomInfo(iFormID, SzConvert.toInt(sKEY), iDLID_SESSION);
		if (!lcFlowPopedomInfo.isWord() && !sOPERATOR.equals("update") && !sOPERATOR.equals("add")) {
			canSave = false;
		} 
	}
%>
<%		
   session.setAttribute("SzModuleType", "");
   session.setAttribute("SzModuleID", "");
   session.setAttribute("SzInfoID", "");   
   if (!sWeboffice.equals("")) {
		
		String RecordID  = "";
		if (sKEY.equals("0")) {
			RecordID  = sRecordId;
		} else if (!sCreate.equals("1") && sRecordId.equals("")) {
		  //取得RecordID：这个是第一次进入正文，取到第一个默认显示		    
		  RecordID  = officeRecordID.loadRecordID(ModuleType,ModuleID,SzConvert.toInt(sKEY));			
		} else if (!sCreate.equals("1") && !sRecordId.equals("")) {
			//非新建，有recordId，检查正文是否存在，不存在就关闭退出。
			boolean flag = officeRecordID.existRecordID(sRecordId);
			if (!flag) {
				out.println(SzPageHelp.jsAlert("该文已不存在"));
				out.println(SzPageHelp.jsCloseWindow());
			} else {
				RecordID  = sRecordId;
			}
				
		} else {
			RecordID  = sRecordId;
		}
		

	  session.setAttribute("SzModuleType", ModuleType);
	  session.setAttribute("SzModuleID", Integer.toString(ModuleID));
	  session.setAttribute("SzInfoID", sKEY);
	    
%>
<form name="formUpdate" method="post">
<input type="hidden" name="create" value="<%=sCreate %>">
<input type="hidden" name="hasOfficeMult" value="<%=hasOfficeMult%>">
<table width="100%">
  <tr>
    <td>
    	<%
    	if (canSave) {
    		out.println( SzPageHelp.saveButton() );
    		if (hasOfficeMult && !sKEY.equals("0") && !RecordID.equals("")) {
    		  out.println( SzPageHelp.button("新增WORD","javascript:OfficeDetailWin('.doc', '', 1);") );
    		  out.println( SzPageHelp.button("新增EXCEL","javascript:OfficeDetailWin('.xls', '', 1);") );
    		  out.println( SzPageHelp.button("删除","javascript:OfficeDetailDelete();") );
    		}
    	}
    	out.println( SzPageHelp.closeButton() );      
      %>         
    </td>
  </tr>
  <tr>
  	<td>
  	<%
  		ArrayList list = officeRecordID.getRecordIDList(ModuleType, ModuleID, SzConvert.toInt(sKEY));
  	  String str_record[] = null;
  	  //2016-03-16增加：当只有1个时，不显示。
  	  for (int i = 0; i<list.size() && list.size() > 1 ; i++) {
  		  str_record = (String[])list.get(i);
  		  if (str_record != null) {  			  
  			  if (str_record[1].equals(RecordID)) {
  				  out.println("<span class='greyClass'>");
  			  } else {
  				  out.println("<span>");
  			  }
  			  out.println("<a href='#' onClick=\"javascript:OfficeDetailWin('" + str_record[2] + "', '" + str_record[1] + "', 0);\">");
  			  out.println("【");
  			  if (str_record[2].equals(".doc")) {
  			  	out.println("<img src='../../images/share2/word.gif'>");
  			  } else if (str_record[2].equals(".xls")) {
  					out.println("<img src='../../images/share2/excel.gif'>");
  			  }
  			  out.println(str_record[0]);
  			  out.println("】");
  			  out.println("</a></span>");
  		  }
  	  }
  	%>
  	</td>
  </tr>
</table>

<table width="100%" border="0" cellpadding="3" cellspacing="0">
  <tr>
    <td>
      <iframe id="webOffice" width="100%" height="1380" 
              frameborder="0"
              src="../../share/weboffice/DocumentEdit.jsp?FileType=<%=FileType%>&InfoID=<%=sKEY%>&ModuleType=<%=ModuleType %>&ModuleID=<%=ModuleID %>&RecordID=<%=RecordID%>&EditType=<%=sEditType%>&Create=<%=sCreate%>">
      </iframe>
    </td>
  </tr>
</table>
<% } %>
</form>
<script language="javascript">
  function FormSave(formName, url, vOPERATOR) {
	  //var oWindow = window.dialogArguments;
	  var oWindow = window.opener;
	  //var create = formName.create.value;
	  if (oWindow && oWindow.formUpdate.RecordID) {
	    var vRecordID = webOffice.webform.RecordID.value; 
      oWindow.formUpdate.RecordID.value = vRecordID;
    }
    
    webOffice.webform.save.click();
           
    var hasOfficeMult = formName.hasOfficeMult.value;
    if (hasOfficeMult == "true") {
    	alert("保存成功！请刷新已打开的正文窗口");
    } else {
    	alert("保存成功！");
    }
    window.close();
  }  

  //2015-10-19新增加  
  function OfficeDetailWin(webOffice, recordId, create) {
    var vUrl = "documentEdit.jsp?OPERATOR=<%=sOPERATOR%>"
    				 + "&WebOffice=" + webOffice + "&FormID=<%=iFormID%>"
    				 + "&KEY=<%=sKEY%>&RecordId=" + recordId + "&Create=" + create;
  	window.open(vUrl,"","left=100,top=100,scrollbars=yes,resizable=yes,status=yes,width=950,height=800");
  	//window.close();
  } 
  
  function OfficeDetailDelete() {	   
		if (confirm("请确定是否要删除正文")) {  
      webOffice.Delete();
      window.close();
    }
  }     
</script>
