<%--
    Document   : LoanInquiry
    Created on : Oct 19, 2016, 3:11:52 PM
    Author     : 986137
--%>

<%@page import="LoginDb.DbHandler"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.util.ArrayList"%>
<%@page import="DataEntryBean.EnquiryBean"%>
<%@page import="java.util.Date"%>


<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script src="${pageContext.request.contextPath}/jquery-ui-1.10.4.custom/development-bundle/jquery-1.10.2.js"></script>
        <script src="${pageContext.request.contextPath}/js/Calendar.js"></script>
        <link rel="stylesheet" type="text/css" href="/PacsApplication/css/TableCSSCode.css" />
        <title>Loan Account Enquiry</title>
        <link rel="stylesheet" type="text/css" href="/PacsApplication/css/TableCSSCode.css" />
        <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())%>" />
        <link rel="shortcut icon" href="${pageContext.request.contextPath}/img/favicon.ico?pacs=<%=((new java.util.Date()).getTime())%>" type="image/x-icon" />
        <script type="text/javascript">
            function noBack()
            {
                window.history.forward();
            }

            function accountsubmitform(enquiryForm)
            {
                if(!checkdate()){

                    alert("Please Select valid date range.");
                    return false;
                }
                <%--if( document.forms[0].accountNo.value == ""  ){
                    alert("Please provide Loan Account Number.");
                    document.forms[0].accountNo.focus();
                    return false;
                }
                if( document.forms[0].cifNumber.value == "" ){
                    alert("Please provide value in CIF Number.");
                    document.forms[0].cifNumber.focus();
                    return false;
                }--%>
                if( document.forms[0].productName.value == ""){
                    alert("Please enter Product Name.");
                    document.forms[0].productName.focus();
                    return false; 
                }

                document.forms[0].totalRecordCount.value=0;
                document.forms[0].action = '/PacsApplication/EnquiryServlet';
                document.forms[0].submit();
            }
            function popup_CIF(width, height, cifNumber) {
                if (window.innerWidth) {
                    LeftPosition = (window.innerWidth - width) / 2;
                    TopPosition = ((window.innerHeight - height) / 2) - 20;
                }
                else {
                    LeftPosition = (parseInt(window.screen.width) - width) / 2;
                    TopPosition = ((parseInt(window.screen.height) - height) / 2) - 20;
                }
                attr = 'resizable=yes,scrollbars=yes,width=' + width + ',height=' +
                    height + ',screenX=300,screenY=200,left=' + LeftPosition + ',top=' +
                    TopPosition + '';
                var screenName = "LoanInquiry.jsp";
                var  lovKey="CifDetails"
                popWin = open('/PacsApplication/CommonSearchInformationLoanLOV.jsp?cifNumber=' + cifNumber + '&screenName=' + screenName+'&lovKey='+lovKey, 'CifDetails', attr);
                popWin.focus();

            }

            ///added Later
            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 ShowParent(    )
            {
                document.forms[0].action = "${pageContext.request.contextPath}/Loan/LoanInquiry.jsp";
                document.forms[0].submit();
            }

            function excelDownload() {

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

            function callNextPage()
            {
                var totalRecordCount=document.forms[0].totalRecordCount.value;
                var recordCount=document.forms[0].recordCount.value;

                if (totalRecordCount==recordCount){

                    alert("No more record to display");
                    return false;

                }

                document.forms[0].but_type.value = 'N';
                document.forms[0].action = '/PacsApplication/EnquiryServlet';
                document.forms[0].submit();
            }

            function callPrevPage()
            {
                var totalRecordCount= document.forms[0].totalRecordCount.value;
                var noOfRecords= document.forms[0].noOfRecords.value;
                document.forms[0].totalRecordCount.value= Number(totalRecordCount) - Number(noOfRecords)-10;
                document.forms[0].but_type.value = 'P';
                document.forms[0].action = '/PacsApplication/EnquiryServlet';
                document.forms[0].submit();
            }
             function dnldReport(){
                document.forms[0].reportPath.value = "/Reports/loanAccStmt";
                document.forms[0].reportName.value = "ACCOUNT STATEMENT REPORT";
                document.forms[0].DOWNLOAD.value = "P";
                document.forms[0].action = '/PacsApplication/ExcelReportServlet';
                document.forms[0].submit();
            }
            
        // to restrict back button
            history.pushState(null, null, location.href);
            window.addEventListener('popstate', function(event) {
                history.pushState(null, null, location.href);
            });    

        </script>
    </head>
    <body  onload="noBack()">


        <%
                    String displayFlag = "";
                    Object display = request.getAttribute("displayFlag");
                    if (display != null) {
                        displayFlag = display.toString();
                    }
        %>

        <jsp:include page="MenuHead_Loan.jsp" flush="true" />
        <br>
        <%!
            String blankNull(String s) {
                return (s == null) ? "" : s;
            }
        %>

        <%
                    String error_msg = "";
                    Object error = request.getAttribute("message");
                    if (error != null) {
                        error_msg = error.toString();
                    }
        %>

        <%
                    Object alEnquiryBeanObj = request.getAttribute("alEnquiryBean");
                    ArrayList<EnquiryBean> alEnquiryBean = new ArrayList<EnquiryBean>();
                    if (alEnquiryBeanObj != null) {
                        alEnquiryBean = (ArrayList<EnquiryBean>) request.getAttribute("alEnquiryBean");
                    }
        %>
        <%
                    String pacsId = (String) session.getAttribute("pacsId");
                    Object oEnquiryBeanSearchObj = request.getAttribute("oEnquiryBeanSearchObj");
                    EnquiryBean oEnquiryBean = new EnquiryBean();

                    if (oEnquiryBeanSearchObj != null) {
                        oEnquiryBean = (EnquiryBean) request.getAttribute("oEnquiryBeanSearchObj");
                        session.setAttribute("oEnquiryBeanSearchObj", oEnquiryBeanSearchObj);
                    }
        %>


        <%
                    int flag2 = 1;
                    int curPage = 0;
                    try {
                        curPage = Integer.parseInt(request.getAttribute("currentPage").toString());
                    } catch (Exception e) {
                        curPage = 1;
                    }
                    if (alEnquiryBean.size() > 0) {
                        flag2 = 2;
                    }
                    int totalRecordCount = 0;
                    int recordCount = 0;

                    try {
                        totalRecordCount = Integer.parseInt(request.getAttribute("totalRecordCount").toString());
                        recordCount = Integer.parseInt(request.getAttribute("recordCount").toString());
                    } catch (Exception e) {
                        totalRecordCount = 0;
                        recordCount = 0;
                    }
                    totalRecordCount = totalRecordCount + alEnquiryBean.size();

        %>
        <%String subPacsFlag = (String) session.getAttribute("subPacsFlag");
        %>


        <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">Loan Account Enquiry</legend>
            <form id="enquiryForm" name="enquiryForm" method="post" action="">


                <%if (!error_msg.equalsIgnoreCase("")) {%>
                <center><div class="info" id="errorMessage"><center><%= error_msg%></center></div></center>
                <% }%>
                <br>
                <br>

                <div id="filterDiv" >
                                          
                            <table>
                                <tr>
                                    <td style="text-align: right">Loan Account Number: </td>
                                    <td><input type="text"  maxlength="17" name="accountNo" id="accountNo" value="<%=blankNull(oEnquiryBean.getAccountNo())%>" style="text-align:right;" onkeypress='return event.charCode >= 48 && event.charCode <= 57' /></td>
                                    <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
                                    <td style="text-align:right;"><lable>CIF Number:</lable></td>
                                <td><input type="text"  id="cifNumber" name="cifNumber" value="<%=blankNull(oEnquiryBean.getCifNumber())%>" style="text-align:right;" onkeypress='return event.charCode >= 48 && event.charCode <= 57' ></td>
                                <td><input type="button" value="Search" onclick="javascript:popup_CIF(220, 180, document.enquiryForm.cifNumber.value);"/></td>
                                </tr>
                                <tr>
                                    <td><br></td>
                                </tr>
                                <tr>
                                    <td style="text-align: right">From Date: </td>
                                    <td><input value="<%=blankNull(oEnquiryBean.getFromDate())%>" type="text" id="fromDate" style="text-align:center;" name="fromDate" readonly /> <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>
                                    <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
                                    <td style="text-align: right">To Date: </td>
                                    <td><input value="<%=blankNull(oEnquiryBean.getToDate())%>" type="text" id="toDate" style="text-align:center;" name="toDate" readonly /> <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></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr>
                                <tr>
                                <td style="text-align:right;">Old A/C Number:</td>
                                <td><input value="<%=blankNull(oEnquiryBean.getOldAccNo())%>" type="text" name="oldAccNo" style="text-align:right;" /></td>
                                <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
                                <td style="text-align:right;">Old CIF Number:</td>
                                <td><input value="<%=blankNull(oEnquiryBean.getOldCifNo())%>" type="text" name="oldCifNo" style="text-align:right;" onkeypress='return event.charCode >= 48 && event.charCode <= 57' /></td>
                            </tr>
                            <tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr>
                            <tr>
                                    <td style="text-align:right;">Sub-PACS Name: </td>
                                    <td colspan="3"><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="<%=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="" 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>
                                            <%if (subPacsFlag.equalsIgnoreCase("N")) {%>
                                            <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 <> 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> 
                                        <tr>
                                    <td><br></td>
                                    </tr>
                                        <tr>
                                        <td style="text-align:right;">Product Name<font color="red" >*</font>: </td>
                                        <td  colspan="3"><select id="productName" name="productName" >
                                        <%if (oEnquiryBean.getProductName() == null) {%>
                                        <option value="" disabled selected style="display:none;">--Select--</option>
                                        <%
                                            Connection connection = null;
                                            ResultSet rs = null;
                                            Statement statement = null;
                                            connection = DbHandler.getDBConnection();
                                            try {
                                                statement = connection.createStatement();
                                                rs = statement.executeQuery("select lp.id, lp.prod_name from loan_product lp where lp.pacs_id like '%" + pacsId + "#%'");
                                                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.getProductId())%>" selected style="display:none;"><%=blankNull(oEnquiryBean.getProductName())%></option>
                                        <%
                                            Connection connection = null;
                                            ResultSet rs = null;
                                            Statement statement = null;
                                            connection = DbHandler.getDBConnection();
                                            try {
                                                statement = connection.createStatement();
                                                rs = statement.executeQuery("select lp.id, lp.prod_name from loan_product lp where lp.id!='" + oEnquiryBean.getProductId() + "' and lp.pacs_id like '%" + pacsId + "#%'");
                                                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>

                                <tr>
                                    <td><br></td>
                                </tr>
                                <tr>
                                </tr>
                            </table>
                            <br><br>
                            
                            <center>
                                <input type="button" value="Submit" onclick="javascript:accountsubmitform(enquiryForm)">
                                <input type="button" value="Reset" name="reset" onclick="ShowParent();" />
                            </center>
                            </div><br><br>
                            <input type="hidden" value="LoanInquiry" name="screenName" />
                            <input name="handle_id" id="handle_id" type="hidden" value="" />
                            <input name="reportPath" id="reportPath" type="hidden" value="" />
                            <input name="reportName" id="reportName" type="hidden" value="" />
                            <input name="reportname" id="reportname" type="hidden" value="LOAN_ACCOUNT_ENQUIRY_REPORT" />
                            <input name="DOWNLOAD" id="DOWNLOAD" type="hidden" value="" />
                            <input type="hidden" name="page" value="<%=request.getAttribute("currentPage")%>">
                            <input type="hidden" name="but_type" >
                            <input type="hidden" name="totalRecordCount" id="totalRecordCount" value="<%=totalRecordCount%>">
                            <input type="hidden" name="recordCount" id="recordCount" value="<%=recordCount%>">
                            <input type="hidden" name="noOfRecords" id="noOfRecords" value="<%=request.getAttribute("noOfRecords")%>">
                            <input type="hidden" name="accno" id="accno" value=""/>

                            </form>

                            <%if (displayFlag.equalsIgnoreCase("Y")) {%>
                            <center>
                                <div id="displayDiv" >
                                    <div style="overflow:scroll; width: 900px;">

                                        <table border="2" cellspacing="1" cellpadding="1" style="text-align: center ;overflow-y: scroll; font-size: 15px; width: 90%;">
                                            <thead class="CSSTableGenerator">
                                                <tr>
                                                    <td>Pacs ID</td>
                                                    <td>Account Number</td>
                                                    <td>Customer Number</td>
                                                    <td>Customer Name</td>
                                                    <td>Product Name</td>
                                                    <td>Interest Description</td>
                                                    <td>Account Status</td>
                                                    <td>Interest Rate</td>
                                                    <td>Penal Interest Rate</td>
                                                    <td>Sanction Amount</td>
                                                    <td>Sanction Date</td>
                                                    <td>Amount Disbursed</td>
                                                    <td>EMI Amount</td>
                                                    <td>Principal Outstanding</td>
                                                    <td>Interest Outstanding</td>
                                                    <td>Principal Paid</td>
                                                    <td>Interest Paid</td>
                                                    <td>Total Interest Outstanding</td>
                                                    <td>Total Interest Accrued</td>
                                                    <td>Total Interest Paid</td>
                                                    <td>Next Due Date</td>
                                                    <td>Last Repayment Date</td>
                                                    <td>Old Account Number</td>
                                                    <td>Account Statement of The Day</td>

                                                </tr>
                                            </thead>
                                            <tbody style="background-color: white; font-size: 15px">
                                                <%
                                                    oEnquiryBean = null;

                                                    for (int i = 0; i < alEnquiryBean.size(); i++) {
                                                        oEnquiryBean = alEnquiryBean.get(i);
                                                %>

                                                <tr>
                                                    <td><%=blankNull(oEnquiryBean.getPacs_id())%></td>
                                                    <td><input id="accno<%=i%>" style="border: none; text-align: center" size="20" type="text" readonly value="<%=blankNull(oEnquiryBean.getAccountNo())%>" /></td>
                                                    <%--<td style="padding:0px 5px 0px 5px;"><%=blankNull(oEnquiryBean.getAccountNo())%></td>--%>
                                                    <td style="padding:0px 5px 0px 5px;"><%=blankNull(oEnquiryBean.getCustomerNo())%></td>
                                                    <td style="padding:0px 5px 0px 5px;"><%=blankNull(oEnquiryBean.getCustomerName())%></td>
                                                    <td><%=blankNull(oEnquiryBean.getProduct_name())%></td>
                                                    <td><%=blankNull(oEnquiryBean.getIntt_cat_desc())%></td>
                                                    <td><%=blankNull(oEnquiryBean.getCurr_Status())%></td>
                                                    <td><%=blankNull(oEnquiryBean.getInttrate())%></td>
                                                    <td><%=blankNull(oEnquiryBean.getPenalIntt())%></td>
                                                    <td><%=blankNull(oEnquiryBean.getSanctionAmount())%></td>
                                                    <td><%=blankNull(oEnquiryBean.getSancDt())%></td>
                                                    <td><%=blankNull(oEnquiryBean.getAmntDsbrd())%></td>
                                                    <td><%=blankNull(oEnquiryBean.getEmi())%></td>
                                                    <td><%=blankNull(oEnquiryBean.getPrinOutstanding())%></td>
                                                    <td><%=blankNull(oEnquiryBean.getInttOutstanding())%></td>
                                                    <td><%=blankNull(oEnquiryBean.getPrinPaid())%></td>
                                                    <td><%=blankNull(oEnquiryBean.getInttPaid())%></td>
                                                    <td><%=blankNull(oEnquiryBean.getTotal_intt_outst())%></td>
                                                    <td><%=blankNull(oEnquiryBean.getTotal_intt_accr())%></td>
                                                    <td><%=blankNull(oEnquiryBean.getTotal_intt_paid())%></td>
                                                    <td><%=blankNull(oEnquiryBean.getNext_due_date())%></td>
                                                    <td><%=blankNull(oEnquiryBean.getLast_repay_date())%></td>
                                                    <td><%=blankNull(oEnquiryBean.getOldAccNo())%></td>
                                                    <td><input type="button" value="Download" onclick="document.getElementById('accno').value=document.getElementById('accno<%=i%>').value;javascript:dnldReport();"/></td>


                                                </tr>
                                                <%}%>
                                            </tbody>

                                        </table>
                                    </div><br><br>
                                    <%--For displaying Previous link except for the 1st page --%>

                                    <table align="right" colspan="2">
                                        <% if (curPage > 1 && flag2 == 2) {%>
                                        <td><input type="button"  name="nextPage" value="Previous" onclick="callPrevPage()"><input type="button"  name="nextPage" value="Next" onclick="callNextPage()">
                                            <% }%>
                                            <% if (flag2 == 2) {%>

                                            <% if (curPage == 1) {%>
                                        <td><input type="button"  name="nextPage" value="Next" onclick="callNextPage()">
                                            <% }
                                }%>

                                    </table><br><br>

                                    <input type="button" value="Export Result" onclick="javascript:excelDownload()">

                                </div>
                            </center>
                            <%}%>
                            </fieldset>
                            </body>
                            </html>
