
<%/**
     * **********************************************************
     * @@ Program Name : doc_view.jsp
     * @@ Description	: /집계자/진행함/집계현황
     * @@ Author	: 기현테크
     * @@ Create Date	: 2010.11.24
     * @@ History	:
     * @@***********************************************************
     */%>

<%@ page contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8"
         import="java.text.SimpleDateFormat"
         import="java.net.URLEncoder"
         import="java.net.URLDecoder"
         import="java.util.List"
         import="java.util.Map"
         import="java.util.HashMap"
         import="java.util.StringTokenizer"
         import="kr.co.kihyun.text.excel.Excel"
         import="kr.co.kihyun.text.html.TagFilter"
         import="kr.co.kihyun.text.hml.Hwp"
         import="kr.co.kihyun.text.html.HtmlUtil"
         import="kr.co.kihyun.lang.Encoder"
         import="kr.co.kihyun.lang.MString"
         import="kr.co.kihyun.lang.MLong"
         import="kr.co.kihyun.lang.MInteger"
         import="kr.co.kihyun.util.Stopwatch"
         import="kr.co.kihyun.moumi.doc.MDoc"
         import="kr.co.kihyun.moumi.doc.table.MTableAnalysis"
         import="kr.co.kihyun.moumi.Moumi"
         import="kr.co.kihyun.moumi.report.MReport"
         import="kr.co.kihyun.beans.user.Part"
         import="kr.co.kihyun.beans.user.PartList"
         import="kr.co.kihyun.beans.user.HttpSSOLogin"
         import="kr.co.kihyun.beans.totsys.doc.table.Table"
         import="kr.co.kihyun.beans.totsys.doc.table.item.Item"
         import="kr.co.kihyun.beans.totsys.report.tots.ReportsViewer"
         import="kr.co.kihyun.beans.totsys.report.tot.TotReportViewer"
         import="kr.co.kihyun.beans.totsys.report.ReportViewManager"
         import="kr.co.kihyun.beans.totsys.report.ReportFormElement"
         import="kr.co.kihyun.beans.totsys.doc.table.item.ItemList"
         import="kr.co.kihyun.beans.totsys.doc.Doc"
         import="kr.co.kihyun.beans.entity.TotReport"
         import="kr.co.kihyun.beans.entity.TotReportProcess"
         import="kr.co.kihyun.beans.entity.util.TotReportUtil"
         import="kr.co.kihyun.beans.entity.TotDocType"
         import="kr.co.kihyun.beans.entity.Grade"
         import="kr.co.kihyun.beans.entity.util.*"
         import="kr.co.kihyun.beans.entity.TotDoc"
         import="kr.co.kihyun.beans.entity.MDept"
         import="javax.jdo.PersistenceManager"
         import="javax.jdo.Transaction"
         import="kr.co.kihyun.service.PrssReportsService"
         import="kr.co.kihyun.service.vo.PrssReportsVO"
         import="kr.co.kihyun.service.vo.ReportTotalVO"
         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 {

        /**
         * ******** session내의 userId *********
         */
        String dpID = HttpSSOLogin.getDeptID(request);

        Long docID = MLong.parseLong(request.getParameter("docID"), null);
        int gradeCode = MInteger.parseInt(request.getParameter("gradeCode"), -1);
        int tableNum = MInteger.parseInt(request.getParameter("tableNum"), 0);
        int searchType = MInteger.parseInt(request.getParameter("searchType"), -1);
        String findOption = clearXSS(URLDecoder.decode(MString.checkNull(request.getParameter("findOption")), "UTF-8"), "");
        String findWord = clearXSS(URLDecoder.decode(MString.checkNull(request.getParameter("findWord")), "UTF-8"), "");

        String innerFindOption = clearXSS(request.getParameter("innerFindOption"), "");
        String innerFindWord = URLDecoder.decode(MString.checkNull(request.getParameter("innerFindWord")), "UTF-8");

        int listType = searchType;
        String howSort = clearXSS(request.getParameter("howSort"), "");

        String displaySummary = MString.checkNull(request.getParameter("displaySummary"));
        String prssSelect = MString.checkNull(request.getParameter("prssSelect"));
        String sort = clearXSS(request.getParameter("sort"), "");

        String mngcode = clearXSS(request.getParameter("mngcode"), "");//20160509 추가
        if( dpID.contains("A00") && docID > 1700000000){
        	mngcode = "".equals(mngcode) ? "": mngcode ;//20160509 추가
        }

        tx.begin();

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

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

        PrssReportsService prssService = new PrssReportsService(pm);
        //검색조건파라미터
        Map params = new HashMap();
        params.put("docId", docID);
        params.put("listType", listType);
        params.put("findOption", innerFindOption);
        params.put("filterValue", innerFindWord);
        params.put("howSort", howSort);
        params.put("sort", sort);
        params.put("mngcode", mngcode);//20160509 추가 교육청 코드
        params.put("dpId", dpID);//20160509 부서 코드

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

        List<PrssReportsVO> list = null;

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

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

        List<TotReport> compReportList = null;  //전체
        List<TotReport> organReportList = null; //기관
        List<TotReport> gradeReportList1 = null; //학교
        List<TotReport> gradeReportList2 = null; //학교
        List<TotReport> gradeReportList3 = null; //학교
        List<TotReport> gradeReportList4 = null; //학교
        List<TotReport> gradeReportList5 = null; //학교

        int allReportCount = 0;          //전체-전체
        int organReportCount_all = 0;    //기관-전체
        int gradeReport1Count_all = 0;   //유치원-전체
        int gradeReport2Count_all = 0;   //초등학교-전체
        int gradeReport3Count_all = 0;   //중학교-전체
        int gradeReport4Count_all = 0;   //고등학교-전체
        int gradeReport5Count_all = 0;   //기타학교-전체

        int totReportCount = 0;          //전체-제출완료
        int organReportCount = 0;        //기관-제출완료
        int gradeReport1Count = 0;       //유치원-제출완료
        int gradeReport2Count = 0;       //초등학교-제출완료
        int gradeReport3Count = 0;       //중학교-제출완료
        int gradeReport4Count = 0;       //고등학교-제출완료
        int gradeReport5Count = 0;       //기타학교-제출완료

        if ("true".equals(displaySummary)) {
            List totalList = null;
            totalList = prssService.getReportTotalByDocId(docID);

            if (totalList != null && totalList.size() > 0) {
                for (Object obj : totalList) {
                    ReportTotalVO totalVO = (ReportTotalVO) obj;

                    allReportCount = MInteger.parseInt(totalVO.getAllCnt().toString(), 0);
                    organReportCount_all = MInteger.parseInt(totalVO.getAllOrganCnt().toString(), 0);
                    gradeReport1Count_all = MInteger.parseInt(totalVO.getAllGrade1Cnt().toString(), 0);
                    gradeReport2Count_all = MInteger.parseInt(totalVO.getAllGrade2Cnt().toString(), 0);
                    gradeReport3Count_all = MInteger.parseInt(totalVO.getAllGrade3Cnt().toString(), 0);
                    gradeReport4Count_all = MInteger.parseInt(totalVO.getAllGrade4Cnt().toString(), 0);
                    gradeReport5Count_all = MInteger.parseInt(totalVO.getAllGrade5Cnt().toString(), 0);

                    totReportCount = MInteger.parseInt(totalVO.getTotReportCnt().toString(), 0);
                    organReportCount = MInteger.parseInt(totalVO.getOrganReportCnt().toString(), 0);
                    gradeReport1Count = MInteger.parseInt(totalVO.getGrade1ReportCnt().toString(), 0);
                    gradeReport2Count = MInteger.parseInt(totalVO.getGrade2ReportCnt().toString(), 0);
                    gradeReport3Count = MInteger.parseInt(totalVO.getGrade3ReportCnt().toString(), 0);
                    gradeReport4Count = MInteger.parseInt(totalVO.getGrade4ReportCnt().toString(), 0);
                    gradeReport5Count = MInteger.parseInt(totalVO.getGrade5ReportCnt().toString(), 0);
                }
            }

            if (totReportCount > 0) {
                compReportList = prssService.getRecursiveAllReports(docID, TotReportProcess.COMP, "");       //전체-제출완료
            }
            if (organReportCount > 0) {
                organReportList = prssService.getRecursiveAllReports(docID, TotReportProcess.COMP, "organ");  //기관-제출완료
            }
            if (gradeReport1Count > 0) {
                gradeReportList1 = prssService.getRecursiveAllReports(docID, TotReportProcess.COMP, "1");      //1 유치원-제출완료
            }
            if (gradeReport2Count > 0) {
                gradeReportList2 = prssService.getRecursiveAllReports(docID, TotReportProcess.COMP, "2");      //2 초등학교-제출완료
            }
            if (gradeReport3Count > 0) {
                gradeReportList3 = prssService.getRecursiveAllReports(docID, TotReportProcess.COMP, "3");      //3 중학교-제출완료
            }
            if (gradeReport4Count > 0) {
                gradeReportList4 = prssService.getRecursiveAllReports(docID, TotReportProcess.COMP, "4");      //4 고등학교-제출완료
            }
            if (gradeReport5Count > 0) {
                gradeReportList5 = prssService.getRecursiveAllReports(docID, TotReportProcess.COMP, "5");      //5 기타학교-제출완료
            }
        }

        tx.rollback();
%>
<table border="1" width="100%" cellpadding="0" cellspacing="0">
    <tr style="padding-top: 3" valign="middle"
                                            onmouseover="this.style.backgroundColor = 'F9F9F9'"
                                            onmouseout="this.style.backgroundColor = 'white'" bgColor=#ffffff>
        <td align="center" valign="middle" nowrap class="kecttep_graytd"><%=TotReportProcess.SUBMIT%><%=Moumi.getMessageBundle().getString("moumi.message.tot_doc.coporation")%></td>
<%--         <td align="center" valign="middle" nowrap class="kecttep_graytd"><%=Moumi.getMessageBundle().getString("moumi.message.grade")%></td> --%>
<%--         <td align="center" valign="middle" nowrap class="kecttep_graytd"><%=Moumi.getMessageBundle().getString("moumi.message.foundation")%></td> --%>
        <td align="center" valign="middle" nowrap class="kecttep_graytd"><%=TotReportProcess.SUBMIT%><%=Moumi.getMessageBundle().getString("moumi.message.tot_doc.devision")%></td>
        <td align="center" valign="middle" nowrap class="kecttep_graytd"><%=TotReportProcess.SUBMIT%><%=Moumi.getMessageBundle().getString("moumi.message.tot_doc.person")%></td>
        <td align="center" valign="middle" nowrap class="kecttep_graytd"><%=Moumi.getMessageBundle().getString("moumi.message.tot_report.submitDate")%></td>
        <% if (tableNum != 0 && (searchType == TotReportProcess.COMP.ordinal() || searchType == -1)) {%>
        <td><%=TagFilter.removeTableHeight(MDoc.getTitleForm(totDoc, tableNum))%></td>
        <%} else {%>
        <td align="center" valign="middle" nowrap class="kecttep_graytd"><%=Moumi.getMessageBundle().getString("moumi.message.tot_doc.submit")%><%=Moumi.getMessageBundle().getString("moumi.message.tot_doc.content")%></td>
        <%}%>
    </tr>
    <% if ("true".equals(displaySummary) && totReportCount > 0) {%>
    <tr>
        <td class="kecttep_title td_style1" colspan="4">
        	<%=TotReportProcess.ALL%> <%=TotDocType.TOT_DOC%><%=Moumi.getMessageBundle().getString("moumi.message.tot_doc.table")%>&nbsp;&nbsp;
            <%=TotReportProcess.COMP%>:<span style="color: #ff6600;"><%= totReportCount%>&nbsp;&nbsp;</span>
            <%=TotReportProcess.ALL%>:<span style="color: #ff6600;"><%= allReportCount%></span>
        </td>
        <td>
            <%if (totReportCount != 0) { //if (compReportList.size() != 0) {%>
            <%= Excel.removeFmla(TotReportUtil.toHTML(totDoc, compReportList, tableNum, tableNum > 0))%>
            <%} else {%>
            <%=Moumi.getMessageBundle().getString("moumi.message.tot_report.notSubmitDoc")%>
            <%}%>
        </td>
    </tr>


    <tr>
        <td class="kecttep_title td_style1" colspan="4">
        	기관별집계표<%=Moumi.getMessageBundle().getString("moumi.message.tot_doc.table")%>&nbsp;&nbsp;
            <%=TotReportProcess.COMP%>:<span style="color: #ff6600;"><%=organReportCount%></span>
            <%=TotReportProcess.ALL%>:<span style="color: #ff6600;"><%= organReportCount_all%></span>
        </td>
        <td>
            <%if (organReportCount != 0) { //if (organReportList.size() != 0) {%>
            <%= Excel.removeFmla(TotReportUtil.toHTML(totDoc, organReportList, tableNum, tableNum > 0))%>
            <%} else {%>
            <%=Moumi.getMessageBundle().getString("moumi.message.tot_report.notSubmitDoc")%>
            <%}%>
        </td>
    </tr>

    <tr>
        <td class="kecttep_title td_style1" colspan="4">
        	유치원별 집계현황<%=Moumi.getMessageBundle().getString("moumi.message.tot_doc.table")%>&nbsp;&nbsp;
            <%=TotReportProcess.COMP%>:<span style="color: #ff6600;"><%=gradeReport1Count%></span>
            <%=TotReportProcess.ALL%>:<span style="color: #ff6600;"><%= gradeReport1Count_all%></span>
        </td>
        <td>
            <%if (gradeReport1Count != 0) { //if (gradeReportList1.size() != 0) {%>
            <%= Excel.removeFmla(TotReportUtil.toHTML(totDoc, gradeReportList1, tableNum, tableNum > 0))%>
            <%} else {%>
            <%=Moumi.getMessageBundle().getString("moumi.message.tot_report.notSubmitDoc")%>
            <%}%>
        </td>
    </tr>

    <tr>
        <td class="kecttep_title td_style1" colspan="4">
        	초등학교별 집계현황<%=Moumi.getMessageBundle().getString("moumi.message.tot_doc.table")%>&nbsp;&nbsp;
            <%=TotReportProcess.COMP%>:<span style="color: #ff6600;"><%=gradeReport2Count%></span>
            <%=TotReportProcess.ALL%>:<span style="color: #ff6600;"><%= gradeReport2Count_all%></span>
        </td>
        <td>
            <%if (gradeReport2Count != 0) { //if (gradeReportList2.size() != 0) {%>
            <%= Excel.removeFmla(TotReportUtil.toHTML(totDoc, gradeReportList2, tableNum, tableNum > 0))%>
            <%} else {%>
            <%=Moumi.getMessageBundle().getString("moumi.message.tot_report.notSubmitDoc")%>
            <%}%>
        </td>
    </tr>

    <tr>
        <td class="kecttep_title td_style1" colspan="4">
        	중학교별 집계현황<%=Moumi.getMessageBundle().getString("moumi.message.tot_doc.table")%>&nbsp;&nbsp;
            <%=TotReportProcess.COMP%>:<span style="color: #ff6600;"><%=gradeReport3Count%></span>
            <%=TotReportProcess.ALL%>:<span style="color: #ff6600;"><%= gradeReport3Count_all%></span>
        </td>
        <td>
            <%if (gradeReport3Count != 0) { //if (gradeReportList3.size() != 0) {%>
            <%= Excel.removeFmla(TotReportUtil.toHTML(totDoc, gradeReportList3, tableNum, tableNum > 0))%>
            <%} else {%>
            <%=Moumi.getMessageBundle().getString("moumi.message.tot_report.notSubmitDoc")%>
            <%}%>
        </td>
    </tr>

    <tr>
        <td class="kecttep_title td_style1" colspan="4">
        	고등학교별 집계현황<%=Moumi.getMessageBundle().getString("moumi.message.tot_doc.table")%>&nbsp;&nbsp;
            <%=TotReportProcess.COMP%>:<span style="color: #ff6600;"><%=gradeReport4Count%></span>
            <%=TotReportProcess.ALL%>:<span style="color: #ff6600;"><%= gradeReport4Count_all%></span>
        </td>
        <td>
            <%if (gradeReport4Count != 0) { //if (gradeReportList4.size() != 0) {%>
            <%= Excel.removeFmla(TotReportUtil.toHTML(totDoc, gradeReportList4, tableNum, tableNum > 0))%>
            <%} else {%>
            <%=Moumi.getMessageBundle().getString("moumi.message.tot_report.notSubmitDoc")%>
            <%}%>
        </td>
    </tr>
    <tr>
        <td class="kecttep_title td_style1" colspan="4">
        	기타학교별 집계현황<%=Moumi.getMessageBundle().getString("moumi.message.tot_doc.table")%>&nbsp;&nbsp;
            <%=TotReportProcess.COMP%>:<span style="color: #ff6600;"><%=gradeReport5Count%></span>
            <%=TotReportProcess.ALL%>:<span style="color: #ff6600;"><%= gradeReport5Count_all%></span>
        </td>
        <td>
            <%if (gradeReport5Count != 0) { //if (gradeReportList5.size() != 0) {%>
            <%= Excel.removeFmla(TotReportUtil.toHTML(totDoc, gradeReportList5, tableNum, tableNum > 0))%>
            <%} else {%>
            <%=Moumi.getMessageBundle().getString("moumi.message.tot_report.notSubmitDoc")%>
            <%}%>
        </td>
    </tr>

    <%}%>
    <%
        int index = -1;
        //tx.begin();
        int slavePaddingLeft = 0;

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

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

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

                if ("2".equals(prssVO.getDeptNo())) {
                    slavePaddingLeft = 20;
                } else {
                    slavePaddingLeft = 5;
                }

                String html = null;
                if ((process == TotReportProcess.COMP || process ==  TotReportProcess.TEMP || process == TotReportProcess.APPROVE) && prssVO.getDownDocId() == null) {
                    //html = prssVO.getToHTML(pm, tableNum, tableNum > 0, true); //표를 만들어 찍어주는 부분
                    TotReport report = pm.getObjectById(TotReport.class, prssVO.getId());
                    html = TotReportUtil.toHTML(report, tableNum, tableNum > 0, true); //표를 만들어 찍어주는 부분
                }

    %>
    <tr>
        <td class="kecttep_title td_style1" style="padding-left: <%= slavePaddingLeft%>px; text-align: left; width: 10%"><%
            if ("2".equals(prssVO.getDeptNo())) {
                out.write("   [" + Moumi.getMessageBundle().getString("moumi.message.tot_doc.subdoc") + "]");
            }%><%= prssVO.getOrganName() == null ? prssVO.getDeptName() : prssVO.getOrganName()%></td>
<%--         <td class="kecttep_title td_style1" style="width: 10%"><%= MString.checkNull(prssVO.getUpperDeptName())%></td> --%>
<%--         <td class="kecttep_title td_style1" style="width: 10%"><%= MString.checkNull(prssVO.getFoundationType().toString())%></td> --%>
        <td class="kecttep_title td_style1" style="width: 10%"><%= MString.checkNull(prssVO.getDeptName())%></td>
        <td class="kecttep_title td_style1" style="width: 10%"><%= MString.checkNull(prssVO.getOwnerName())%></td>
        <!-- 제출날짜 추가-->
        <td class="kecttep_title td_style1" style="width: 15%"><%= prssVO.getSubmitDate() == null ? "" : new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(prssVO.getSubmitDate())%></td>
        <%if("true".equals(prssSelect)){%>
        <td style="text-align: left;"><%= ((process == TotReportProcess.COMP || process ==  TotReportProcess.TEMP || process == TotReportProcess.APPROVE) && prssVO.getDownDocId() == null) ? Excel.removeFmla(html) : process%></td>
        <%}else{%>
        <td style="text-align: left;"><%= (process == TotReportProcess.COMP && prssVO.getDownDocId() == null) ? Excel.removeFmla(html) : process%></td>
        <%}%>
    </tr>
    <%
            }  //end of for list
        }   //end of if( count > 0)

//        }
        //tx.rollback();
    %>
    <%        if (count <= 0) { //if (reportList.size() <= 0) {
    %>
    <tr>
        <td  style="text-align:center;"><%=Moumi.getMessageBundle().getString("moumi.message.tot_doc.notReportData")%></td>
    </tr>
    <%  } %>
</table>

<%
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
%>
