<%--
    Document   : PACS_REPORT
    Created on : Sep 17, 2015, 12:59:59 PM
    Author     : 454222
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.util.ArrayList" %>
<%@page import="DataEntryBean.EnquiryBean"%>
<%@page import="LoginDb.DbHandler"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Connection"%>
<% try {
%>
<html>
    <head>
        <title>Asset Report</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script src="${pageContext.request.contextPath}/js/Calendar.js?pacs=<%=((new java.util.Date()).getTime())%>"></script>
        <link rel="stylesheet" type="text/css" href="/PacsApplication/css/TableCSSCode.css?pacs=<%=((new java.util.Date()).getTime())%>" />
        <link rel="shortcut icon" href="${pageContext.request.contextPath}/img/favicon.ico?pacs=<%=((new java.util.Date()).getTime())%>" type="image/x-icon" />
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" type="text/css" href="/PacsApplication/css/styles.css?pacs=<%=((new java.util.Date()).getTime())%>"/>
        <link rel="stylesheet" type="text/css" href="/PacsApplication/css/ButtonAndText.css?pacs=<%=((new java.util.Date()).getTime())%>" />
        <link rel="stylesheet" type="text/css" href="/PacsApplication/css/info.css?pacs=<%=((new java.util.Date()).getTime())%>" />
        <script type="text/javascript" src="${pageContext.request.contextPath}/jquery-ui-1.10.4.custom/development-bundle/jquery-1.10.2.js?pacs=<%=((new java.util.Date()).getTime())%>"></script>
        <script type="text/javascript" src="/PacsApplication/chromejs/chrome.js?pacs=<%=((new java.util.Date()).getTime())%>"></script>
        <script type="text/javascript" src="/PacsApplication/js/iFrame_killer.js"></script>

        <script type="text/javascript">
            function noBack()
            {
                window.history.forward();
            }
            function checkdate()
            {
                var s1 = document.getElementById("fromDate").value;
                var s2 = document.getElementById("toDate").value;
                var dateParts1 = s1.split("/");
                var dateParts2 = s2.split("/");

                var dateObject1 = new Date(dateParts1[2], dateParts1[1] - 1, dateParts1[0]);
                var dateObject2 = new Date(dateParts2[2], dateParts2[1] - 1, dateParts2[0]);

                if (dateObject1>dateObject2){

                    return false;
                }

                return true;
            }

            function downloadFiles() {

                var e = document.getElementById("reportSelection");

                if (e.options[e.selectedIndex].value == "")
                {
                    alert("Report Name cannot be empty.");
                    document.forms[0].reportSelection.focus();
                    return false;
                }
                if (document.forms[0].fromDate.value == "")
                {
                    alert("From Date cannot be empty.");
                    document.forms[0].fromDate.focus();
                    return false;
                }
                if (document.forms[0].toDate.value == "")
                {
                    alert("To Date cannot be empty.");
                    document.forms[0].toDate.focus();
                    return false;
                }
                if(!checkdate()){

                    alert("Please Select valid date range");
                    return false;
                }                

                switch (e.options[e.selectedIndex].value) {

                    case "1" :

                        document.forms[0].reportPath.value = "/Reports/Asset_Purchase_Report";
                        document.forms[0].reportName.value = "ASSET PURCHASE REPORT";
                        document.forms[0].DOWNLOAD.value = "P";
                        break;

                    case "2" :

                        document.forms[0].reportPath.value = "/Reports/Asset_Register_Report";
                        document.forms[0].reportName.value = "ASSET REGISTER REPORT";
                        document.forms[0].DOWNLOAD.value = "P";
                        break;
                        
                    case "3" :

                        document.forms[0].reportPath.value = "/Reports/crar_risk_report";
                        document.forms[0].reportName.value = "RISK WEIGHTED ASSETS & CRAR REPORT";
                        document.forms[0].DOWNLOAD.value = "P";
                        break;

                    case "4" :

                        document.forms[0].reportPath.value = "/Reports/asset_depreciation";
                        document.forms[0].reportName.value = "ASSET DEPRECIATION REPORT";
                        document.forms[0].DOWNLOAD.value = "P";
                        break;    
                    default :

                        null;

                }

                document.forms[0].action = '/PacsApplication/ExcelReportServlet';
                document.forms[0].submit();

            }
            function ShowParent()
            {

                document.forms[0].action = "${pageContext.request.contextPath}/Asset_JSP/Asset_Report.jsp";
                document.forms[0].submit();
            }
            <%!
            String blankNull(String s) {
                return (s == null) ? "" : s;
            }

            String blankNA(String s) {
                return (s == null) ? "NA" : s;
            }
            %>
            <%
             String pacsId = (String) session.getAttribute("pacsId");
             String userRole = (String) session.getAttribute("userRole");
             String moduleName = new String();
                moduleName = (String) session.getAttribute("moduleName");
             Object oEnquiryBeanSearchObj = request.getAttribute("oEnquiryBeanSearchObj");
             EnquiryBean oEnquiryBean = new EnquiryBean();
             EnquiryBean oEnquiryBeanSearch = new EnquiryBean();
             if (oEnquiryBeanSearchObj != null) {
                 oEnquiryBean = (EnquiryBean) request.getAttribute("oEnquiryBeanSearchObj");
                 session.setAttribute("oEnquiryBeanSearchObj", oEnquiryBeanSearchObj);
             }
        %>
        <%String subPacsFlag = (String) session.getAttribute("subPacsFlag");
        %>

            // to restrict going back 
            history.pushState(null, null, location.href);
            window.addEventListener('popstate', function(event) {
                history.pushState(null, null, location.href);
            });
            
        </script>

        <title>PACS Report</title>
        <link rel="shortcut icon" href="${pageContext.request.contextPath}/img/favicon.ico" type="image/x-icon" />
    </head>
    <body  onload="noBack();">

        <jsp:include page="MenuHead_ASSET.jsp" flush="true" />
        <br>
        <div style="top:500px">
            <fieldset style="border-color: black; background-image: url('/PacsApplication/img/download.jpg'); border-width: 3px">
                <legend style="padding: 8px; border-radius: 20px; background-color: #006600; color: white">Asset Report</legend>

                <form name="pacsreport" method="post" action="/PacsApplication/DownloadPacsReport">
                    <br>

                    <table>
                        <%if (request.getAttribute("error") != null) {%>
                        <tr><td colspan="2" align="center"><%=request.getAttribute("error").toString()%></td></tr>
                        <%}%>

                        <tr></tr>
                        <tr></tr>
                        <tr></tr>
                        <tr></tr>
                        <tr></tr>
                        <tr></tr>
                        <tr></tr>
                        <tr></tr>
                        <tr>
                            <td style=" font-family: Berlin Sans FB; font-weight:normal; ">Report Name <font color="red" >*</font>: </td>
                            <td><select id="reportSelection" name="reportSelection" >
                                    <option value="" disabled selected style="display:none;" >--Select--</option>
                                    <option value="1">Asset Purchase Report</option>
                                    <option value="2">Asset Register Report</option>
                                    <option value="3">Risk Weighted Assets & CRAR Report</option>
                                    <option value="4">Asset Depreciation Report</option>
                                </select></td>
                        </tr><tr>
                            <td style=" font-family: Berlin Sans FB; font-weight:  normal;">From Date <font color="red" >*</font>: </td>
                                <td><input type="text"  style="text-align:center;" id="fromDate" name="fromDate" maxlength="11" resize="yes" readonly="true">&nbsp;<a href="#" onclick="return getCalendar(document.forms[0].fromDate);"><img src="${pageContext.request.contextPath}/img/calendar.jpg" name="imgr" border="0" width="25" height="20" /></a></td>
                        </tr><tr>
                            <td style=" font-family: Berlin Sans FB; font-weight:  normal;">To Date <font color="red" >*</font>: </td>
                                <td><input type="text" style="text-align:center;" id="toDate" name="toDate" maxlength="11" resize="yes" readonly="true">&nbsp;<a href="#" onclick="return getCalendar(document.forms[0].toDate);"><img src="${pageContext.request.contextPath}/img/calendar.jpg" name="imgr" border="0" width="25" height="20" /></a></td>
                        </tr>
                        <tr id="subPacsName">
                            <td style=" font-family: Berlin Sans FB; font-weight:normal; ">Sub-PACS Name: </td>
                            <td><select id="subpacs_id" name="subpacs_id" >
                                    <%if (oEnquiryBean.getSubpacs_id() == null) {%>
                                    <option value="" disabled selected style="display:none;">--Select--</option>
                                    <%if (subPacsFlag.equalsIgnoreCase("N")) {%>
                                    <option value="ALL" >ALL Pacs</option>
                                    <option value="<%=pacsId%>">Own Pacs</option>
                                    <% }%>
                                    <%
                                        Connection connection = null;
                                        ResultSet rs = null;
                                        Statement statement = null;
                                        connection = DbHandler.getDBConnection();
                                        try {
                                            statement = connection.createStatement();
                                            rs = statement.executeQuery("select m.pacs_id,m.pacs_name from pacs_master m where m.head_pacs_id = '" + pacsId + "' and m.pacs_id <> '" + pacsId + "'");
                                            if (!rs.isBeforeFirst()) {%>
                                    <option value="<%=pacsId%>" selected>No Item to Select</option>
                                    <%} else {
                                                                                    while (rs.next()) {%>
                                    <option value="<%=rs.getString(1)%>"><%=rs.getString(2)%></option>
                                    <%}
                                            }
                                           // statement.close();
                                           // connection.close();
                                        } catch (Exception e) {
                                            System.out.println("Error occurred during processing.");
                                        } finally {
                try {
                    if(statement !=null)
                        statement.close();
                    if (connection != null)
                    connection.close();

            } catch (Exception e) {               
               System.out.println("Error Occurred during connection close.");
            }

            }
                                    } else {%>
                                    <option value="<%=blankNull(oEnquiryBean.getSubpacs_id())%>" selected style="display:none;"><%=blankNull(oEnquiryBean.getSubpacs_name())%></option>
                                    <%
                                        Connection connection = null;
                                        ResultSet rs = null;
                                        Statement statement = null;
                                        connection = DbHandler.getDBConnection();
                                        try {
                                            statement = connection.createStatement();
                                            rs = statement.executeQuery("select m.pacs_id,m.pacs_name from pacs_master m where m.head_pacs_id = '" + pacsId + "' and m.pacs_id <> m.head_pacs_id ");
                                            if (!rs.isBeforeFirst()) {%>
                                    <option value="" selected>No Item to Select</option>
                                    <%} else {
                                                                                    while (rs.next()) {%>
                                    <option value="<%=rs.getString(1)%>"><%=rs.getString(2)%></option>
                                    <%}
                                                 }
                                                // statement.close();
                                                //connection.close();
                                             } catch (Exception e) {
                                                System.out.println("Error occurred during processing.");
                                             } finally {
                try {
                    if(statement !=null)
                        statement.close();
                    if (connection != null)
                    connection.close();

            } catch (Exception e) {               
               System.out.println("Error Occurred during connection close.");
            }

            }
                                         }%>
                                </select></td>
                        </tr>
                    </table>
                        <br><br><br><br>
                    <center>
                        <input type="button" value="Fetch Report" onclick="javascript:downloadFiles();">
                        <input type="button" value="Reset" id="Reset" onclick="ShowParent();"
                    </center>

                    <input type="hidden" name="reportPath" value=""/>
                    <input type="hidden" name="reportName" value=""/>
                    <input type="hidden" name="DOWNLOAD" value=""/>


                </form>
            </fieldset>
        </div>
    </body>
</html>
<% 	} catch (Exception e) {
               
            }
%>