
<%    /**
     * **********************************************************
     * @@ Program Name  : report_list_excel.jsp
     * @@ Description	: /집계자/보고문서 목록 공통
     * @@ Author	: 강원중
     * @@ Create Date	: 2004.10.11
     * @@ History	: 2014.10.01 Update By KWON,HAN
     * @@***********************************************************
     */
%>
<%@ page contentType="text/html; charset=UTF-8"
         import="java.util.Date"
         import="java.util.List"
         import="java.util.Map"
         import="java.util.HashMap"
         import="java.text.SimpleDateFormat"
         import="java.net.URLEncoder"
         import="java.net.URLDecoder"
         import="kr.co.kihyun.beans.user.HttpSSOLogin"
         import="kr.co.kihyun.util.MDate"
         import="kr.co.kihyun.lang.MString"
         import="kr.co.kihyun.lang.MInteger"
         import="kr.co.kihyun.lang.MLong"
         import="kr.co.kihyun.lang.Encoder"
         import="kr.co.kihyun.moumi.Moumi"
         import="kr.co.kihyun.moumi.MoumiConfig"
         import="kr.co.kihyun.moumi.doc.MDoc"
         import="kr.co.kihyun.moumi.report.MReport"
         import="kr.co.kihyun.beans.user.User"
         import="kr.co.kihyun.beans.user.Dept"
         import="kr.co.kihyun.beans.totsys.report.Report"
         import="kr.co.kihyun.beans.totsys.doc.Doc"
         import="javax.jdo.PersistenceManager"
         import="javax.jdo.Transaction"
         import="kr.co.kihyun.beans.entity.util.*"
         import="kr.co.kihyun.beans.entity.*"
         import="org.slf4j.Logger"
         import="org.slf4j.LoggerFactory"
         import="kr.co.kihyun.service.PrssReportsService"
         import="kr.co.kihyun.service.vo.PrssReportsVO"
         import="javax.jdo.Query"%>
<%@ include file="/totsys/common/inc/sec/secure.inc.jsp"%>
<% request.setCharacterEncoding("UTF-8");%>
<%
    PersistenceManager pm = new MPersistenceManager(PMF.get().getPersistenceManager());
    Transaction tx = pm.currentTransaction();
    try {
        Logger LOG = LoggerFactory.getLogger(this.getClass());
        /**
         * ******** session내의 userId *********
         */
        String usID = HttpSSOLogin.getLoginID(request);
        int sysAuth = HttpSSOLogin.getSysAuth(request);
        String dpID = HttpSSOLogin.getDeptID(request);

        /**
         * **** Parameter *****
         */
        Long docID = MLong.parseLong(request.getParameter("docID"), null);
        String accAuth = clearXSS(request.getParameter("accAuth"), "");
        String strPage = clearXSS(request.getParameter("strPage"), "");
        String findOption = clearXSS(request.getParameter("findOption"), "");
        //String findWord = clearXSS(Encoder.toJava(request.getParameter("findWord")),"");
        String findWord = clearXSS(URLDecoder.decode(request.getParameter("findWord"), "UTF-16"), "");
        int listType = MInteger.parseInt(request.getParameter("listType"));
        int prssENG = MInteger.parseInt(request.getParameter("prssENG"));
        int recog = MInteger.parseInt(request.getParameter("recog"), -1);
        String sort = clearXSS(request.getParameter("sort"), "");
        String howSort = clearXSS(request.getParameter("howSort"), "");

//        System.out.println(" 넘어온 howSort = " + howSort);

        tx.begin();

        TotDoc totDoc = pm.getObjectById(TotDoc.class, docID);
        String docName = totDoc.getName();



        /**
         * **** all, send report *****
         */
        int allReportCount = 0;
        int sendReportCount = 0;
        int notReportCount = 0;
        int noteReportCount = 0;
        Report report = new Report();
        allReportCount = report.getAllCount(docID);    //전체레포트카운트 구해오는 쿼리 불러오는 함수
        sendReportCount = report.getSendCount(docID);  //제출레포트카운트 구해오는 쿼리 불러오는 함수
        notReportCount = report.getNotCount(docID);    //해당없음카운트 구해오는 쿼리 불러오는 함수
        noteReportCount = report.getNoteCount(docID);  //자료회수 요청 횟수 카운터 가져오기


/*
                //List<TotReport> reportList = totDoc.getPrssReports(pm, listType, findOption, findWord,null);
        List<TotReport> reportList = null;
        //if(findWord == null)
        if (sort.equals("1")) {
            reportList = totDoc.getPrssReports(pm, listType, findOption, findWord, null);

        } else if (sort.equals("2")) {
            reportList = totDoc.getPrssFoundReports(pm, listType, findOption, findWord, null);
        } else {
            reportList = totDoc.getPrssReports(pm, listType, findOption, findWord, null);
        }

        Query q = null;
        if (recog != -1) {
            q = pm.newQuery(TotReport.class, reportList);
            StringBuilder filter = new StringBuilder(150).append("this.recog.ordinal() == recog");
            q.setFilter(filter.toString());
            LOG.debug("filter: {}", filter.toString());
            q.declareParameters("int recog");
            q.setOrdering("id DESC");
            reportList = (List<TotReport>) q.execute(recog);
        }
*/

       //======================================================================
        int count = 0;

        PrssReportsService prssService = new PrssReportsService(pm);
        //검색조건파라미터
        Map params = new HashMap();
        params.put("docId",         docID);
        params.put("listType",      listType);
        params.put("findOption",    findOption);
        params.put("filterValue",   findWord);
        params.put("howSort",       howSort);
        params.put("sort",          sort);

        //검색메인쿼리와 레코드갯수조회쿼리를 생성 후 멤버변수로 저장
        prssService.createPrssReportListSearchQuery(params);

        List<PrssReportsVO> list = null;

        //만족하는 레코드가 존재하면 조회
        list = prssService.executeQuery(PrssReportsVO.class);

        count = list.size();
        //======================================================================

        tx.rollback();

        response.setHeader("Accept-Ranges", "bytes");
                //response.setHeader("Content-Disposition","attachment; filename="+(URLEncoder.encode(docName, "UTF-8")).replaceAll(":","_").replace("+","%20" )+".xls;");
        //LJH
        response.setHeader("Content-Disposition", "attachment; filename=" + (URLEncoder.encode(docName, "UTF-8")).replaceAll(":", "_").replace("+", " ") + ".xls;");
%>
<html xmlns:o="urn:schemas-microsoft-com:office:office"
      xmlns:x="urn:schemas-microsoft-com:office:excel"
      xmlns="http://www.w3.org/TR/REC-html40">
    <head>
        <!--//<meta http-equiv="Content-Type"
        content="text/html; charset=UTF-8">//-->
        <meta http-equiv="Content-Type" content="text/html; charset=MS949">
            <meta name="ProgId" content="Excel.Sheet">
                <meta name="Generator" content="Microsoft Excel 10">
    </head>
                    <body>
                        <table width="100%" border="1" cellpadding="0" cellspacing="0">
                            <tr>
                                <td height="30" valign="bottom"><!----  목록 ---->
                                    <table width="100%" border="1" cellpadding="0" cellspacing="0">
                                        <tr>
                                            <td height="30" valign="bottom" colspan="<%if (MoumiConfig.is2Step() && MoumiConfig.getUseRecog()) {%>
                                                10<%} else if (MoumiConfig.is2Step() || MoumiConfig.getUseRecog()) {%>9<%} else {%>8<%}%>">
                                                <font color="009999"><%=TotReportProcess.ALL%>: </font><%= allReportCount%>&nbsp;
                                                <font color="009999"><%=TotReportProcess.COMP%>: </font><%= sendReportCount%>&nbsp;
                                                <font color="009999"><%=TotReportProcess.NOT%>: </font><%= notReportCount%>&nbsp;
                                                <font color="009999"><%=TotReportProcess.NOT_REPORT%>: </font><%= (allReportCount - sendReportCount - notReportCount)%>&nbsp;
                                                ( <font color="009999"><%=TotReportProcess.NOTE%>: </font><%= noteReportCount%> )
                                            </td>
                                        </tr>
                                    </table>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <table border="1" width="100%" cellpadding="0" cellspacing="0">
                                        <tr style="padding-top: 3">
                                            <!--<td align="center" valign="middle" class="kecttep_tdmenu" rowspan="2">선택</td>-->
                                            <td width="40" align="center" valign="middle" class="kecttep_tdmenu"><%=MoumiConfig.getMessageBundle().getString("moumi.message.tot_doc.number")%></td>
                                            <td align="center" valign="middle" nowrap class="kecttep_sub_tdmenu"><%=MoumiConfig.getMessageBundle().getString("moumi.message.tot_doc.submit")%><%=MoumiConfig.getMessageBundle().getString("moumi.message.tot_doc.coporation")%></td>
                                            <td align="center" valign="middle" nowrap class="kecttep_sub_tdmenu"><%=MoumiConfig.getMessageBundle().getString("moumi.message.grade")%></td>
                                            <td align="center" valign="middle" nowrap class="kecttep_sub_tdmenu"><%=MoumiConfig.getMessageBundle().getString("moumi.message.tot_doc.submit")%><%=MoumiConfig.getMessageBundle().getString("moumi.message.tot_doc.devision")%></td>
                                            <td width="90" align="center" valign="middle" nowrap  class="kecttep_sub_tdmenu"><%=MoumiConfig.getMessageBundle().getString("moumi.message.tot_doc.submit")%><%=MoumiConfig.getMessageBundle().getString("moumi.message.tot_doc.person")%></td>
                                            <td align="center" valign="middle" nowrap class="kecttep_tdmenu" ><%=MoumiConfig.getMessageBundle().getString("moumi.message.tot_doc.telNumber")%> (<%=MoumiConfig.getMessageBundle().getString("moumi.message.tot_report.depart")%>/<%=MoumiConfig.getMessageBundle().getString("moumi.message.tot_report.person")%>)</td>
                                            <td align="center" valign="middle" nowrap class="kecttep_tdmenu" ><%=TotDocProcess.SUBMIT%><%=MoumiConfig.getMessageBundle().getString("moumi.message.tot_doc.dateTime")%></td>
                                            <td width="90" align="center" valign="middle" nowrap class="kecttep_tdmenu" ><%=MoumiConfig.getMessageBundle().getString("moumi.message.tot_report.progressStatus")%></td>
                                            <%if (MoumiConfig.is2Step()) {%>
                                            <td width="60" align="center" valign="middle" nowrap class="kecttep_sub_tdmenu"><%= MoumiConfig.getMessageBundle().getString("moumi.message.tot_doc.subdoc")%></td>
                                            <%}%>
                                            <%if (MoumiConfig.getUseRecog()) {%>
                                            <td width="60" align="center" valign="middle" nowrap class="kecttep_sub_tdmenu"><%=MoumiConfig.getMessageBundle().getString("moumi.message.tot_doc.approval")%></td>
                                            <%}%>
                                            <td width="60" align="center" valign="middle" nowrap class="kecttep_sub_tdmenu"><%=MoumiConfig.getMessageBundle().getString("moumi.message.tot_doc.opinion")%></td>
                                            <td width="60" align="center" valign="middle" nowrap class="kecttep_last_tdmenu"><%=MoumiConfig.getMessageBundle().getString("moumi.message.tot_doc.add")%></td>
                                        </tr>

                                        <%
                                            //tx.begin();
                                            //for (int index = 0; index < reportList.size(); index++) {

            int index = 0;     //배열에서 값을 꺼내오는 키값

            if( count > 0 ) {
                int listCount = list.size();

                for (int i = 0; i < listCount; i++) {
                    index = i;

                    PrssReportsVO prssVO = (PrssReportsVO)list.get(i);
                    //TotReportProcess prss = (TotReportProcess) prssVO.getProcess();

                    TotDoc slaveTotDoc = prssVO.getSlaveTotDoc(pm);

                    //Map<String, List<Byte>> attachmentsMap = prssVO.getAttachmentsMap(pm);
                                        %>
                                        <tr style="padding-top: 3" valign="middle"
                                            onmouseover="this.style.backgroundColor = 'F9F9F9'"
                                            onmouseout="this.style.backgroundColor = 'white'" bgColor=#ffffff>
                                            <td height="25" align="center" valign="middle" nowrap class="kecttep_graytd">&nbsp;<%= index + 1%></td>
                                            <td align="left" valign="middle" nowrap class="kecttep_graytd">&nbsp;<%=prssVO.getOrganName() == null ? prssVO.getDeptName() : prssVO.getOrganName()%></td>
                                            <td align="left" valign="middle" nowrap class="kecttep_graytd">&nbsp;<%=MString.checkNull(prssVO.getUpperDeptName())%></td>
                                            <td align="left" valign="middle" nowrap class="kecttep_graytd">&nbsp;<%=MString.checkNull(prssVO.getDeptName())%></td>
                                            <td align="center" valign="middle" nowrap class="kecttep_graytd">&nbsp;<%= MString.checkNull(prssVO.getOwnerName())%><%if (prssVO.getUserId() != null) {%>(<%= MString.checkNull(prssVO.getUserId())%>) <%}%>&nbsp;</td>
                                            <td align="center" valign="middle" nowrap class="kecttep_graytd">&nbsp;<%= MString.checkNull(prssVO.getDeptPhone())%>/<%= MString.checkNull(prssVO.getUserPhone())%></td>
                                            <td align="center" valign="middle" nowrap class="kecttep_graytd">&nbsp;
                                                <%if (prssVO.getProcess() == TotReportProcess.RETURN || prssVO.getProcess() == TotReportProcess.COMP || prssVO.getProcess() == TotReportProcess.NOT) {%>
                                                <%= prssVO.getSubmitDate() == null ? "" : new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(prssVO.getSubmitDate())%>
                                                <%}%>
                                            </td>
                                            <td align="center" valign="middle" nowrap class="kecttep_graytd">
                                                <%
                                    if (TotReportProcess.ACCEPT == prssVO.getProcess() && TotReportType.NOT_REPORT_REQ == prssVO.getReportType()) {%>
                                                <img src="images/bt_not_report.gif" border="0"> <%} else {
                                                        if (prssVO.isDeleted()) {
                                                            out.write("보고" + MoumiConfig.getMessageBundle().getString("moumi.message.doc") + " 삭제");
                                                        } else {
                                                            out.write(prssVO.getProcess().toString());
                                                        }
                                                    }
                                                    %>
                                            </td>
                                            <%if (MoumiConfig.is2Step()) {%>
                                            <td align="center" valign="middle" nowrap class="kecttep_graytd">
                                                <%if (slaveTotDoc == null) {%> &nbsp; <%} else {%>
                                                연계문서 <%}%>
                                            </td>
                                            <%}%>

                                            <%if (MoumiConfig.getUseRecog()) {%>
                                            <td align="center" valign="middle" nowrap class="kecttep_graytd">
                                                <%if (prssVO.getProcess() == TotReportProcess.COMP || prssVO.getProcess() == TotReportProcess.TEMP || prssVO.getProcess() == TotReportProcess.APPROVE) {%>
                                                <%= prssVO.getRecogType().toString()%> <%} else {%> &nbsp; <%}%>
                                            </td>
                                            <%}%>
                                            <td align="center" valign="middle" nowrap class="kecttep_graytd">
                                                <%if (prssVO.getProcess() != TotReportProcess.NOTE) {%> &nbsp; <%} else {%>
                                                회수요청존재
                                                <%}%>
                                            </td>
                                            <td align="center" valign="middle" nowrap class="kecttep_last_graytd">&nbsp;
                                                <%if ("Y".equals(prssVO.getIsAttachments()) || (null != prssVO.getAttachmentsPath() && !"".equals(prssVO.getAttachmentsPath()))) { %>

                                                <!-- 2014.10.08 Update by KWON,HAN : 제출완료 -> 제출완료, 회수요청 -->
                                                	<%if (prssVO.getProcess() == TotReportProcess.COMP || prssVO.getProcess() == TotReportProcess.NOTE) {%>
                                                	첨부파일존재
                                                	<%} else {%> &nbsp;<%}%>
                                                <%}else{%>
                                                &nbsp;
                                                <%}%>
                                            </td>
                                        </tr>
                                        <%

                }  //end of for list
            }   //end of if( count > 0)
                                            //tx.rollback();
                                        %>
                                    </table>
                                </td>
                            </tr>
                            <%if (count <= 0) {%>
                            <tr>
                                <td class="kecttep_none_tdmenu" height="25" align="center">해당 <%= MoumiConfig.getMessageBundle().getString("moumi.message.doc")%>가 없습니다.</td>
                            </tr>
                            <%}%>
                        </table>
                    </body>
<%
    } catch (Exception ex) {
        ex.printStackTrace();
        out.println(kr.co.kihyun.text.html.ServletUtil.getJavaScript("location='/servlet/kr.co.kihyun.beans.user.HttpSSOLogin?mode=logout';"));
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
%>
