<%@ page contentType="text/html;charset=UTF-8" %>
<%@ page import="java.util.Iterator" %>
<%@ 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.formEngine2.*" %>
<%@ page import="net.sz600.weboffice.*" %>
<%@ include file="/sessionInfo.jsp"%>
<% 
  	String strLevel = "../../"; 
	//获取提交的参数
	String printTemp = SzConvert.strWriteData(request.getParameter("printTemp"));
	String printTempRelate = SzConvert.strWriteData(request.getParameter("printTempRelate"));
	String sKEY =  SzConvert.toZeroStr(request.getParameter("KEY"));
	String sOPERATOR = SzConvert.toZeroStr(request.getParameter("OPERATOR"));
	String sType =  SzConvert.toZeroStr(request.getParameter("type"));

	int iFormID = SzConvert.toInt(request.getParameter("FormID"));
	String sPKEY = SzConvert.toZeroStr(request.getParameter("PKEY"));
	String sPFORM = SzConvert.toZeroStr(request.getParameter("PFORM"));  
	
	//2015-06-26增加日志：打印浏览
	BdFormControl formControl = new BdFormControl();
	String sContent = formControl.getFormDesc(iFormID);
	String sIp = SzConvert.toSpaceStr( request.getRemoteHost()); 
	SzLog.getInstance().printLog("" + iDLID_SESSION, sDLXM_SESSION, sIp
			,"" + iFormID, sKEY, sContent, "0");
	
%> 
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="<%=strSkin_SESSION%>/css2007.css" rel="stylesheet" type="text/css">
</head>
<form name="form">
<table width="90%" align="center">
    <tr>
      <td align="center"><b><font size="3">打印文件清单</font></b><br>
      <input type="checkbox" name="chkCheckAll2" checked OnClick="javascript:CheckAll(form, chkCheckAll2, 'filePath');">全选
      <input type="button" class="btnClass" value="打印" OnClick="javascript:PrintAll();">
      
      <input type="button" class="btnClass" value="重新生成" OnClick="javascript:ReGen();">
      <input type="button" class="btnClass" value="关闭" OnClick="javascript:window.close();">
      </td>
    </tr>
    <tr>
      <td>
        <table width="100%" border="1" bordercolorlight="#C0C0C0" bordercolordark="#FFFFFF" class="tableClass" cellpadding="1" cellspacing="0" align="center">
<%
  
    /*
    //取得表单的信息
    String moduleName = "";
    String formName = "";
	  LcForm lcForm = new LcForm();
	  LcFormInfo lcFormInfo = null;    
    if (iFormID == 0) {
    	moduleName = SzConvert.toSpaceStr(request.getParameter("moduleName"));
    	formName = SzConvert.toSpaceStr(request.getParameter("formName")); 
    	//
    	iFormID = lcForm.getKeyByCond(moduleName, formName); 
    } else {
    	lcFormInfo = (LcFormInfo)lcForm.getInfoByKey(iFormID);    
    	if (lcFormInfo != null) {
    		moduleName = lcFormInfo.getBDLJ();
    		formName = lcFormInfo.getBDBY1();  
    	}
    } 
    //System.out.println("moduleName=" + moduleName);
    //System.out.println("formName=" + formName);
    if (moduleName.equals("") || formName.equals("")) {
    	out.println("还没有设置相关表单信息！");
    	return;
    } 
    */
    //
	//根据打印模板名称，获取打印模板，并且生成word，返回一个word文件，显示打印
	OfficeHelp officeHelp = new OfficeHelp();	
	//	String printTemp_path = officeHelp.getPrintWord(SzConvert.toInt(sKEY), printTemp, iFormID);
	String printTemp_path = "";
	Collection coll = null;
	
	if (sOPERATOR.equals("ReGen")) {
		String temp = printTemp + "," + printTempRelate;
		temp = temp.replaceAll(";", ",");
		temp = temp.replaceAll("，", ",");
		temp = temp.replaceAll("；", ",");
		temp = SzConvert.removeRepeatTokenStr(temp, ",");	
		//System.out.println(temp + "==" + sKEY + "==" + iFormID + "==" + sPKEY + "==" + sPFORM);
		if (sType.equals("0")) {
			//officeHelp.genPrintWord(SzConvert.toInt(sKEY), temp, iFormID, SzConvert.toInt(sPKEY), SzConvert.toInt(sPFORM));
			officeHelp.getPrintWordCollection(SzConvert.toInt(sKEY), temp, iFormID, SzConvert.toInt(sPKEY), SzConvert.toInt(sPFORM), 1);			
		} else if (sType.equals("1")) {
			officeHelp.getPrintWordCollectionRelate(SzConvert.toInt(sKEY), temp, iFormID, SzConvert.toInt(sPKEY), SzConvert.toInt(sPFORM), 1);	
		}
		//关闭
%>
<script language="javascript">
	window.close();
</script>
<%		
		return;
	} 
	
	if (sType.equals("0")) {
		coll = officeHelp.getPrintWordCollection(SzConvert.toInt(sKEY), printTemp, iFormID, SzConvert.toInt(sPKEY), SzConvert.toInt(sPFORM));
	} else if (sType.equals("1")) {
		coll = officeHelp.getPrintWordCollectionRelate(SzConvert.toInt(sKEY), printTempRelate, iFormID, SzConvert.toInt(sPKEY), SzConvert.toInt(sPFORM));
	}

	Iterator iter = null;
	if (coll != null) {
		iter = coll.iterator();
	}
	String s[] = new String[2];
	String formDesc = "";
	String formDescTemp = "";
	int i = 0;
	BdFormControl bdFormControl = new BdFormControl();
	while (iter != null && iter.hasNext()) {
	   	s = (String[])iter.next();
	   	if (s == null || s[1].equals("")) {
			continue;
		}
	   	i ++;
	   	printTemp_path = s[1];
	   	formDesc = bdFormControl.getFormDesc(SzConvert.toInt(s[2]));
	   	
	   	if (!formDescTemp.equals(formDesc)) {
%>
  <tr>
    <td align="center" colspan="3"><b><%=formDesc%></b></td>    
  </tr>
<%	   		
	   	}
%>

  <tr>
    <td align="right" width="5"><input type="checkbox" name="filePath" checked value="<%=s[1]%>" title="<%=i%>"></td>
    <td align="left"><a href="../../<%=s[1]%>"><%=s[0]%></a></td>
    <td align="left" width="50%"><%=s[3]%>&nbsp;</td>     
  </tr>
  
<%		
		formDescTemp = formDesc;
	}//while ends
%>

        </table>
     </td>
  </tr>
  <!-- 
  <tr>
    <td align="center">
      <input type="checkbox" name="chkCheckAll" checked OnClick="javascript:CheckAll(form, chkCheckAll, 'filePath');">全选
      <input type="button" class="btnClass" value="打印" OnClick="javascript:PrintAll();">
      <input type="button" class="btnClass" value="重新生成" OnClick="javascript:ReGen();">
      <input type="button" class="btnClass" value="关闭" OnClick="javascript:window.close();">      
    </td>
  </tr>
   -->
</table>
<!--
  doc文件没法指定到iframe里显示 
<iframe id="iFramePrint" src="" width="100%" height="100%" frameborder="1" ></iframe>
 -->

<%	
	
	//如果只有一个就直接打开
	if (i == 1) {
	  if (printTemp_path.equals("")) {
		//提示失败，自动关闭
		out.println(SzPageHelp.jsAlert("要打印的文件不存在"));		
	  } else if (printTemp_path.endsWith(".doc")) {
		  out.println("<script language='javascript'>");		
		  out.println(" var url ='../../" + printTemp_path + "';");
		  out.println("window.open(url,'','left=100,top=100,scrollbars=yes,resizable=yes,status=yes,width=800,height=600');");
		  //out.println(" document.getElementById('iFramePrint').src = url;");
		  out.println("</script>");

	  } else if (printTemp_path.endsWith(".html")) {
			out.println("<script language='javascript'>");		
			out.println(" var url ='../../" + printTemp_path + "';");
			out.println("window.open(url,'','left=100,top=100,scrollbars=yes,resizable=yes,status=yes,menubar=yes,width=800,height=600');");	
			//out.println(" document.getElementById('iFramePrint').src = url;");
			out.println("</script>");
	  }
	  out.println(SzPageHelp.jsCloseWindow());
	}
%>
</form>
<script language="javascript">
  function ReGen() {
  	document.location.href= "printTemp.jsp?OPERATOR=ReGen&FormID=<%=iFormID%>&KEY=<%=sKEY%>&type=<%=sType%>&printTemp=<%=printTemp%>&printTempRelate=<%=printTempRelate%>&PKEY=<%=sPKEY%>&PFORM=<%=sPFORM%>";
  }
  
  function PrintAll() {
  	//获取选中的文件
    var v = "";
    var vCheck = 0;
    var vLen = 0;  	
    var vUrl = "";
  	var oChk  = form.filePath
    if (oChk == null) {
      return "";
    }
    
    var vUrl_base = "http://<%=sServer_SESSION%>:<%=sPort_SESSION%>/<%=sContext_SESSION%>/";
    
    if (oChk.checked == true) {
      v = oChk.value;
      vUrl = vUrl_base + v;
      print(vUrl);
    } else {
      vLen = oChk.length;
    }
    for (var i=0; i<vLen; i++) {
      if (oChk[i].checked == true) {
        v = oChk[i].value;
        //打开，调用打印后关闭
        vUrl = vUrl_base + v;
   			print(vUrl);      	 	       
      }
    }      	
  }
  
  function print(vUrl) {
  	 var wdapp;
     wdapp=new ActiveXObject("Word.Application");
     wdapp.documents.open(vUrl);
     wdapp.Application.Visible = true;
     wdapp.Application.Printout();
     wdapp.Quit();    
  }
  
  //第一个表示对象，第二个表示对象，第三个表示字符串
  function CheckAll(oFormList, oChkCheckAll, vChkId) {
    var e;
    for (var i=0; i<oFormList.elements.length; i++) {
      e = oFormList.elements[i];
      if (e.name == vChkId) {
        e.checked = oChkCheckAll.checked;
      }
    }
  }  
</script>
