<%--
    Document   : Leave Balance
    Created on : May 24, 2022, 1:00:00 PM
    Author     : 1242938
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Connection"%>
<%@page import="DataEntryBean.PayrollBean"%>
<%@page import="java.util.ArrayList"%>
<%@page import="LoginDb.DbHandler"%>
<% try {
%>

<html>

    <head>
        <title>Leave Balance</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>

            function noBack()
            {
                window.history.forward();                
            }
            function submitform(formPayroll)
            {
                if (checkDiv(formPayroll))
                {
                    document.forms[0].handle_id.value = "Create";
                    document.forms[0].action = '/PacsApplication/LeaveBalanceServlet';
                    document.forms[0].submit();
                }
            }
            function amendform(formPayroll)
            { 
                if (checkDivAmend(formPayroll))
                {
                document.forms[0].handle_id.value = "Amend";
                document.forms[0].action = '/PacsApplication/LeaveBalanceServlet';
                document.forms[0].submit();
                }
            }
            function getDetails(formPayroll)
            {
                document.forms[0].handle_id.value = "getDetails";
                document.forms[0].action = '/PacsApplication/LeaveBalanceServlet';
                document.forms[0].submit();
            }

            function checkDiv(formPayroll)
            {
                var counter = document.forms[0].rowCounter.value; 
                
                for (var i = 1; i <= counter; i++) {
                    try{
                        var employee = document.getElementById("employee" + i).value;
                        var leave = document.getElementById("leave" + i).value;
                        var leaveBal = document.getElementById("leaveBal" + i).value;
                        var remarks = document.getElementById("remarks" + i).value;
                        
                        if (employee == "" || leave == "" || leaveBal == "" || remarks == "") 
                        {
                            alert("Please enter all details.");
                            return false;
                        }                                                  
                    }
                    catch(e){}
                }                
                return true;
            } 
            function checkDivAmend(formPayroll)
            {
                var counter2 = document.forms[0].rowCounter2.value; 
                
                for (var i = 2; i < counter2; i++) {
                    try{
                        var employee = document.getElementById("employee" + i).value;
                        var leave = document.getElementById("leave" + i).value;
                        var leaveBal = document.getElementById("leaveBal" + i).value;
                        var remarks = document.getElementById("remarks" + i).value;
                        
                        if (employee == "" || leave == "" || leaveBal == "" || remarks == "") 
                        {
                            alert("Please enter all details.");
                            return false;
                        }
                    }
                    catch(e){}
                }                
                return true;
            }
            
            function SearchEMP(width, height, textIndex)
            {
                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 accNo = document.getElementById("employee"+textIndex).value;
                var screenName = "EmployeeDependentMaster.jsp";
                var lovKey = "PickUPCustomer";

                popWin = open('${pageContext.request.contextPath}/CommonSearchInformationShgLOV.jsp?screenName=' + screenName+'&lovKey='+lovKey + '&accNo=' + accNo + '&textIndex=' + textIndex, 'AccountSearch', attr);
                popWin.focus();
            }
            function SearchLeave(width, height, textIndex)
            {
                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 accNo = document.getElementById("leave"+textIndex).value;
                var screenName = "LeaveBalance.jsp";
                var lovKey = "PickUPLeave";

                popWin = open('${pageContext.request.contextPath}/CommonSearchInformationShgLOV.jsp?screenName=' + screenName+'&lovKey='+lovKey + '&accNo=' + accNo + '&textIndex=' + textIndex, 'AccountSearch', attr);
                popWin.focus();
            }
            function checkop()
            {                
                if (document.getElementById("checkOption").value == "create")
                {

                    document.getElementById('create').style.display = 'block';
                    document.getElementById('forAmend').style.display = 'none';
                    document.getElementById('amend').style.display = 'none';
                }
                if (document.getElementById("checkOption").value == "amend")
                {
                    document.getElementById('create').style.display = 'none';
                    document.getElementById('forAmend').style.display = 'block';
                }
            } 
            function calDecimal(formPayroll) 
            {
                var counter = document.forms[0].rowCounter.value;
                
                for (var i = 1; i <= counter; i++) {    
                    var var1 = String(document.getElementById("leaveBal" + i).value);                    
                    
                    if(var1.includes('.')){
                        var len1 =  var1.split('.')[1].length;                                            
                        if (len1>2) {
                            document.getElementById("leaveBal" + i).value = (parseFloat(var1)).toFixed(2);
                            return false;
                        }
                    }                                        
                }
            }
            function calDecimal2(formPayroll) 
            {
                var counter2 = document.forms[0].rowCounter2.value;
                
                for (var i = 1; i <= counter2; i++) {    
                    var var1 = String(document.getElementById("leaveBal" + i).value);                    
                    
                    if(var1.includes('.')){
                        var len1 =  var1.split('.')[1].length;                                            
                        if (len1>2) {
                            document.getElementById("leaveBal" + i).value = (parseFloat(var1)).toFixed(2);
                            return false;
                        }
                    }                                        
                }
            }
            // 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()">

        <%          int count = 1;
                    String error_msg = "";
                    Object error = request.getAttribute("message");
                    if (error != null) {
                        error_msg = error.toString();
                    }
        %>
        <%!
            String blankNull(String s) {
                return (s == null) ? "" : s;
            }
        %>

        <%
                    Object oPayrollBeanObj = request.getAttribute("oPayrollBean");
                    Object alPayrollBeanObj = request.getAttribute("arrPayrollBean");

                    PayrollBean oPayrollBean = new PayrollBean();
                    if (oPayrollBeanObj != null) {
                        oPayrollBean = (PayrollBean) request.getAttribute("oPayrollBean");
                    }

                    ArrayList<PayrollBean> alPayrollBean = new ArrayList<PayrollBean>();
                    if (alPayrollBeanObj != null) {
                        alPayrollBean = (ArrayList<PayrollBean>) request.getAttribute("arrPayrollBean");
                    }
        %>

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

        <jsp:include page="MenuHead_Payroll.jsp" flush="true" />
        <br>                    
        <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">Leave Balance</legend>

            <form id="formPayroll" name="formPayroll" method="post" action="LeaveBalanceServlet">

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

                <center><label style=" font-family: Berlin Sans FB; font-weight:  normal;">Operation Selection:</label>
                    <select id="checkOption" onchange="checkop()" name="checkOption" >
                        <option value="" style="display:none;" >--Select--</option>                            
                            <option value="create" >Create</option>
                            <option value="amend" >Amend</option>
                    </select>                    
                </center>
                <br>
                <center>
                <div id="create" style="display:none;" >
                    <center>
                        <br><br>
                        <table id="order-list" style="margin-left: 20px;"  border="2" cellspacing="1" cellpadding="1" style="text-align: center ;overflow-y: scroll; font-size: 15px; width: 100%;">
                            <thead  class="CSSTableGenerator" >
                                <tr>
                                    <td></td>
                                    <td colspan="2">Employee ID</td>
                                    <td>Employee Name</td>
                                    <td colspan="2">Leave Type</td>
                                    <td>Leave Balance</td>
                                    <td>Remarks</td>
                                </tr>
                            </thead>
                            <tbody>
                                <tr>
                                    <td><input type="checkbox" name="chckbox<%=count%>" id="chckbox<%=count%>" onclick="return checkboxTrue(<%=count%>)"/></td>
                                    <td><input size="20" type="text" name="employee<%=count%>" id="employee<%=count%>" onkeypress="return event.charCode >= 48 && event.charCode <= 57" ondrop="return false;" onpaste="return false;" /></td>  
                                    <td><input type="button" style="width: 70px" name="SearchEMP<%=count%>" id="SearchEMP<%=count%>" value="Search" onclick="javascript:SearchEMP(550,240,<%=count%>);" /></td>
                                    <td><input readonly size="20" type="text" name="ename<%=count%>" id="ename<%=count%>" ondrop="return false;" onpaste="return false;" /></td>
                                    <td><input size="20" type="text" name="leave<%=count%>" id="leave<%=count%>" onkeypress="return event.charCode >= 48 && event.charCode <= 57" ondrop="return false;" onpaste="return false;" /></td>  
                                    <td><input type="button" style="width: 70px" name="SearchLeave<%=count%>" id="SearchLeave<%=count%>" value="Search" onclick="javascript:SearchLeave(550,240,<%=count%>);" /></td>
                                    <td><input size="20" type="text" name="leaveBal<%=count%>" id="leaveBal<%=count%>" onkeypress='return event.charCode >= 46 && event.charCode <= 57 && event.charCode != 47' onkeyup="calDecimal(formPayroll)" /></td>
                                    <td><input size="20" type="text" name="remarks<%=count%>" id="remarks<%=count%>" /></td>
                                    <td style="display: none"><input type="hidden" name="id<%=count%>" id="id<%=count%>" ></td>
                                    <td style="display: none"><input type="hidden" name="leaveId<%=count%>" id="leaveId<%=count%>" ></td>
                                    <td style="display:none"><input type="hidden" name="employee_id<%=count%>" id="employee_id<%=count%>"></td>
                                </tr>
                            </tbody>
                        </table>                                    
                        <br/><br/>
                        <center>
                            <input type="button" id="addRow" value="Add Row"/>
                            <input type="button" id="deleteRow" value="Delete Row"/>
                            <input type="button" value="Submit" onclick="javascript:submitform(formPayroll)"/>
                            <input type="button" value="Reset" onclick="ShowParent();" />

                        </center>
                        <input type="hidden" name="rowCounter" id="rowCounter" value="<%=count%>">
                        <input type="hidden" name="chkboxCounter" id="chkboxCounter" value="0">
                    </center>
                </div>
               </center> 
                        <br>        
                <center>        
                <div id="forAmend" style="display:none;" >    
                    
                        <input type="button"  value="Fetch Leave Balance Details" onclick="javascript:getDetails()" />
                                             
                </div>        
                <br>
                </center>
                <%if (displayFlag.equalsIgnoreCase("Y")) {%>
                <div id="amend" >
                    <center>                       
                    <table id="order-listAmend" style="overflow-y: scroll">
                        <thead  class="CSSTableGenerator" >
                            <tr>
                                <td>Employee ID</td>
                                <td>Employee Name</td>
                                <td>Leave Type</td>
                                <td>Leave Balance</td>
                                <td>Remarks</td>
                            </tr>
                        </thead>
                        <tbody style="background-color: white; font-size: 15px">
                            <%
                                oPayrollBean = null;
                                oPayrollBean = new PayrollBean();
                                int count2= 1;

                                for (int i = 0; i < alPayrollBean.size(); i++) {
                                    oPayrollBean = alPayrollBean.get(i);
                                    count2++;
                            %>
                            <tr>
                                <td><input size="20" readonly type="text" name="employee<%=count2%>" id="employee<%=count2%>" onkeypress="return event.charCode >= 48 && event.charCode <= 57" ondrop="return false;" onpaste="return false;" value="<%=blankNull(oPayrollBean.getEmployee())%>"/></td>                                      
                                    <td><input readonly size="20" type="text" name="ename<%=count2%>" id="ename<%=count2%>" value="<%=blankNull(oPayrollBean.getEname())%>" onpaste="return false;" /></td>
                                    <td><input size="20" readonly type="text" name="leave<%=count2%>" id="leave<%=count2%>" onkeypress="return event.charCode >= 48 && event.charCode <= 57" ondrop="return false;" onpaste="return false;" value="<%=blankNull(oPayrollBean.getLeave())%>"/></td>                                      
                                    <td><input size="20" type="text" name="leaveBal<%=count2%>" id="leaveBal<%=count2%>" onkeypress='return event.charCode >= 46 && event.charCode <= 57 && event.charCode != 47' onkeyup="calDecimal2(formPayroll)" value="<%=blankNull(oPayrollBean.getLeaveBal())%>"/></td>
                                    <td><input size="20" type="text" name="remarks<%=count2%>" id="remarks<%=count2%>" value="<%=blankNull(oPayrollBean.getRemarks())%>"/></td>                                
                                    <td style="display:none"><input type="hidden" name="id<%=count2%>" id="id<%=count2%>" value="<%=blankNull(oPayrollBean.getId())%>"></td>
                                    <td style="display:none"><input type="hidden" name="leaveId<%=count2%>" id="leaveId<%=count2%>" value="<%=blankNull(oPayrollBean.getLeaveId())%>"></td>
                                    <td style="display:none"><input type="hidden" name="employee_id<%=count2%>" id="employee_id<%=count2%>" value="<%=blankNull(oPayrollBean.getEmployee_id())%>"></td>
                            </tr>

                            <%}%></tbody>
                    </table>                    
                
                    </center>    
                    <br>
                        <center>
                            <input type="button" value="Submit" onclick="javascript:amendform(formPayroll)"/>
                            <input type="button" value="Reset" onclick="ShowParent();" />
                        </center>    
                            <input type="hidden" name="indicator" id="indicator" value="">
                            <input type="hidden" name="rowCounter2" id="rowCounter2" value="<%=count2%>">
                            <input type="hidden" name="chkboxCounter" id="chkboxCounter" value="0">
                </div> 
            <%}%>                
                <input type="hidden" name="handle_id" value="" />
                <input type="hidden" name="actionApl" value="" />
                <input type="hidden" name="vendor_id" id="vendor_id"/>
                <input type="hidden" id="displayFlag" name="displayFlag" value="<%=displayFlag%>"/>
            </form>
            <script>

                $("#addRow").click(function () {

                    //var counter = $("#rowCounter").val();
                    ++counter;
                    $('#order-list tr:last').after('<tr> <td align="center"><input type="checkbox" name="chckbox' + counter + '" id="chckbox' + counter + '" onclick="return checkboxTrue('+counter+')"/></td>\n\
                        <td><input size="20" type="text" id="employee' + counter + '" name="employee' + counter + '" onkeypress="return event.charCode >= 48 && event.charCode <= 57" ondrop="return false;" onpaste="return false;" /></td>\n\
                        <td><input type="button" style="width: 70px" name="SearchEMP' + counter + '" id="SearchEMP' + counter + '" value="Search"  onclick="javascript:SearchEMP(550,240,'+counter+');" /></td>\n\
                        <td><input readonly size="20" type="text" name="ename' + counter + '" id="ename' + counter + '" ondrop="return false;" onpaste="return false;" /></td>\n\\n\
                        <td><input size="20" type="text" id="leave' + counter + '" name="leave' + counter + '" onkeypress="return event.charCode >= 48 && event.charCode <= 57" ondrop="return false;" onpaste="return false;" /></td>\n\
                        <td><input type="button" style="width: 70px" name="SearchLeave' + counter + '" id="SearchLeave' + counter + '" value="Search"  onclick="javascript:SearchLeave(550,240,'+counter+');" /></td>\n\\n\
                        <td><input size="20" type="text" name="leaveBal' + counter + '" id="leaveBal' + counter + '" onkeypress="return event.charCode >= 46 && event.charCode <= 57 && event.charCode != 47" onkeyup="calDecimal(formPayroll)" /></td>\n\
                        <td><input size="20" type="text" id="remarks' + counter + '" name="remarks' + counter + '" /></td>\n\
                        <td style="display: none"><input type="hidden" name="id' + counter + '" id="id' + counter + '"/></td>\n\
                        <td style="display: none"><input type="hidden" name="leaveId' + counter + '" id="leaveId' + counter + '"/></td>\n\\n\
                        <td style="display: none"><input type="hidden" name="employee_id' + counter + '" id="employee_id' + counter + '"/></td>\n\\n\
                        </tr>');
                    $("#rowCounter").val(counter);

                });

                $( "#deleteRow" ).click(function() {

                    var len = ($("#order-list tr").length)-1;
                    var counter = $("#rowCounter").val();
                    var chkboxCounter=$("#chkboxCounter").val();
                    //alert(chkboxCounter);
                    counter=counter-chkboxCounter;
                    // alert(counter);
                    if (counter==0){

                        alert("All rows cannot be deleted");
                    }
                    else{

                        if (len ==chkboxCounter) {
                            alert("All rows can not be deleted");
                            return false;
                        }

                        $('input:checkbox:checked').closest('tr').remove();
                        chkboxCounter=0;
                        $("#chkboxCounter").val(chkboxCounter);

                    }
                });

                function checkboxTrue(i){

                    if(document.getElementById("chckbox"+i).checked==true){

                        var counter = $("#chkboxCounter").val();
                        counter++;
                        $("#chkboxCounter").val(counter);
                    }

                    else{

                        var counter = $("#chkboxCounter").val();
                        counter--;
                        $("#chkboxCounter").val(counter);
                    }
                }

                function ShowParent()
                {
                    document.forms[0].action = "${pageContext.request.contextPath}/Payroll/LeaveBalance.jsp";
                    document.forms[0].submit();
                }

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