<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true"
    CodeBehind="Loan_payment_installment.aspx.cs" Inherits="HRMS.Loan_payment_installment" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <div class="main-panel">
        <div class="row">
            <div class="col-md-12">
                <section class="panel">
                   <header class="panel-heading" style="padding-bottom:30px">
                           <span class="label label-primary">Loan Management</span>
                        
                        <div class="pull-right">
                            <asp:Button Text="Save" CssClass="btn btn-info" ID="btn_save" Width="120px" ValidationGroup="save"
                                runat="server" OnClick="btn_save_Click" />
                            <asp:Button Text="Back" CssClass="btn btn-info" ID="btn_cancel" runat="server" OnClick="btn_cancel_Click" />
                        </div>
                    </header>

                    <div class="panel-body">
                        <div class="span6">
                            <div id="divmessage" runat="server" visible="false" class="alert alert-info alert-dismissable">
                                <asp:Label Text="" ID="lbl_msg" CssClass="msg" runat="server"></asp:Label>
                            </div>
                        </div>

                        <div class="span6">
                            <div id="diverrmessage" runat="server" visible="false" class="alert alert-info alert-dismissable">
                                <asp:Label Text="" ID="lbl_err_msg" CssClass="msg" runat="server"></asp:Label>
                            </div>
                        </div>


                        <div class="row">
                            <asp:HiddenField ID="hid_loan" runat="server" />
                            <asp:HiddenField ID="hid_per" runat="server" />
                            <asp:HiddenField ID="hid_assignment" runat="server" />
                            <asp:HiddenField ID="hid_pr_loan_amt" runat="server" />
                            <asp:HiddenField ID="hid_sal" runat="server" />
                            <label class="control-label col-md-2">Loan To</label>
                            <div class="col-md-3">
                                <div class="input-group">
                                    <asp:TextBox CssClass="form-control" ID="txt_emp" runat="server"></asp:TextBox>
                                    <asp:RequiredFieldValidator SetFocusOnError="true" ValidationGroup="save" runat="server" ID="req_emp" ControlToValidate="txt_emp" ErrorMessage="Employee is Required!" />
                                    <span class="input-group-btn">
                                        <button id="btn_lov_emp" type="button" runat="server" class="btn btn-white" onserverclick="btn_lov_emp_Click"><i class="fa fa-search"></i></button>
                                    </span>
                                </div>
                            </div>

                            <label class="col-md-2 control-label">Adjust From</label>
                            <div class="col-md-2">
                                <asp:DropDownList ID="ddl_adjst" runat="server" CssClass="form-control select2">
                                </asp:DropDownList>
                                <asp:RequiredFieldValidator SetFocusOnError="true" ValidationGroup="save" runat="server" ID="req" ControlToValidate="ddl_adjst" ErrorMessage="Adjustment is Required!" />
                            </div>
                        </div>

                        <div class="row" style="padding-top: 05px" runat="server" visible="false">
                            <label class="control-label col-md-2">PF Amount</label>
                            <div class="col-md-2">
                                <asp:TextBox ID="txt_val" runat="server" onkeyup="javascript:this.value=Comma(this.value);" Style="text-align: right" CssClass="form-control"></asp:TextBox>
                            </div>


                            <label class="control-label col-md-3">Mark Up</label>
                            <div class="col-md-1">
                                <asp:TextBox ID="txt_mrkup" Text="7" ReadOnly="true" runat="server" CssClass="form-control"></asp:TextBox>

                            </div>
                            <label class="control-label">%</label>
                        </div>

                        <div class="row" style="padding-top: 05px">
                            <label class="col-md-2 control-label">Loan Amount</label>
                            <div class="col-md-2">
                                <asp:TextBox ID="txt_amount" Style="text-align: right" onkeypress="return numbersOnly(event)" runat="server" CssClass="form-control numbers" onchange="CheckPFValue()" onkeyup="javascript:this.value=Comma(this.value);"></asp:TextBox>
                                <asp:RequiredFieldValidator SetFocusOnError="true" ValidationGroup="save" runat="server" ID="req_amt" ControlToValidate="txt_amount" ErrorMessage="Loan Amount is Required!" />
                            </div>

                            <label class="col-md-3 control-label">No. of Installment</label>
                            <div class="col-md-2">
                                <asp:TextBox ID="txt_no_inst" onkeypress="return numbersOnly(event)" runat="server" CssClass="form-control" onchange="GetInstallmentAmt()"></asp:TextBox>
                                <asp:RequiredFieldValidator SetFocusOnError="true" ValidationGroup="save" runat="server" ID="req_no_ins" ControlToValidate="txt_no_inst" ErrorMessage="No. Of Installment is Required!" />
                            </div>


                        </div>

                        <div class="row" style="display: none" id="div_chk">
                            <div class="col-md-3 col-md-offset-2">
                                <asp:CheckBox ID="chk_alow" runat="server" CssClass="checkbox" Text="Allow Once" onclick="ShowHideDiv(this)" />
                            </div>

                        </div>

                        <div class="row" style="padding-top: 05px">
                            <label class="col-md-2 control-label">Installment Amount</label>
                            <div class="col-md-2">
                                <asp:TextBox ID="txt_ints_amt" Style="text-align: right" runat="server" CssClass="form-control"></asp:TextBox>
                                <asp:RequiredFieldValidator SetFocusOnError="true" ValidationGroup="save" runat="server" ID="req_ins_amt" ControlToValidate="txt_ints_amt" ErrorMessage="Installment Amount is Required!" />
                            </div>

                            <label class="col-md-3 control-label">First Installment Date</label>
                            <div class="col-md-2">
                                <asp:TextBox ID="txt_pay_frst_date" runat="server" CssClass="form-control datetime"></asp:TextBox>
                                <asp:RequiredFieldValidator SetFocusOnError="true" ValidationGroup="save" runat="server" ID="req_pay_date" ControlToValidate="txt_pay_frst_date" ErrorMessage="Payment First Date is Required!" />
                            </div>
                        </div>


                        <div class="row" style="padding-top: 05px">
                            <label class="col-md-2 control-label">Start Date</label>
                            <div class="col-md-2">
                                <asp:TextBox ID="txt_strt_date" runat="server" CssClass="form-control datetime"></asp:TextBox>
                                <asp:RequiredFieldValidator SetFocusOnError="true" ValidationGroup="save" runat="server" ID="req_st_date" ControlToValidate="txt_strt_date" ErrorMessage="Date is Required!" />
                            </div>

                            <label class="col-md-3 control-label">End Date</label>
                            <div class="col-md-2">
                                <asp:TextBox ID="txt_end_date" runat="server" CssClass="form-control datetime"></asp:TextBox>
                            </div>
                        </div>

                        <div class="row" style="padding-top: 05px">

                            <label class="col-md-2 control-label">Loan Type</label>
                            <div class="col-md-2">
                                <asp:DropDownList ID="ddl_loan_type" runat="server" CssClass="form-control select2">
                                </asp:DropDownList>
                                <asp:RequiredFieldValidator SetFocusOnError="true" ValidationGroup="save" runat="server" ID="RequiredFieldValidator1" ControlToValidate="ddl_loan_type" ErrorMessage="Loan Type is Required!" />
                            </div>



                            <label class="col-md-3 control-label">Description</label>
                            <div class="col-md-4">
                                <asp:TextBox ID="txt_desc" runat="server" CssClass="form-control"></asp:TextBox>
                            </div>
                        </div>

                        <hr />

                        <div class="row">
                            <div class="col-md-offset-2">
                                <asp:GridView ID="GridView2" Visible="false" runat="server" AutoGenerateColumns="False"
                                    CssClass="table table-striped table-bordered table-hover" CellPadding="0" Width="70%" OnRowCommand="GridView2_RowCommand" OnRowDataBound="GridView2_RowDataBound">
                                    <Columns>
                                        <asp:TemplateField HeaderStyle-Width="1%">
                                            <ItemTemplate>
                                                <asp:HiddenField ID="hid_loan" runat="server" Value='<%#Bind("LOAN_ID")%>' />
                                                <asp:HiddenField ID="hid_pay_mode" runat="server" Value='<%#Bind("PAYMENT_MODE")%>' />
                                                <asp:HiddenField ID="hid_loan_dtl" runat="server" Value='<%#Bind("LOAN_DETAIL_ID")%>' />
                                            </ItemTemplate>
                                        </asp:TemplateField>

                                        <asp:TemplateField HeaderText="Adjusment Date" HeaderStyle-Width="20%">
                                            <ItemTemplate>
                                                <asp:TextBox ID="txt_rcv_date" runat="server" CssClass="form-control datetime" Text='<%#Bind("RECEIVED_DATE") %>'></asp:TextBox>
                                                <asp:RequiredFieldValidator SetFocusOnError="true" ValidationGroup='<%#"add_"+Container.DataItemIndex %>' runat="server" ID="req_rcv_date" ControlToValidate="txt_rcv_date" ErrorMessage="Receive Date is Required!" />
                                            </ItemTemplate>
                                        </asp:TemplateField>

                                        <asp:TemplateField HeaderText="Payment Mode" HeaderStyle-Width="20%">
                                            <ItemTemplate>
                                                <asp:DropDownList ID="ddl_pay_mode" Width="100%" runat="server" CssClass="form-control select2"></asp:DropDownList>
                                                <asp:RequiredFieldValidator SetFocusOnError="true" ValidationGroup='<%#"add_"+Container.DataItemIndex %>' runat="server" ID="req_ddl_pay_mode" ControlToValidate="ddl_pay_mode" ErrorMessage="Payment Mode is Required!" />
                                            </ItemTemplate>
                                        </asp:TemplateField>

                                        <asp:TemplateField HeaderText="Amount" HeaderStyle-Width="20%">
                                            <ItemTemplate>
                                                <asp:TextBox ID="txt_rcv_amount" onkeypress="return numbersOnly(event)" Style="text-align: right" AutoPostBack="true" runat="server" CssClass="form-control" Text='<%#Bind("AMOUNT") %>' OnTextChanged="txt_rcv_amount_TextChanged"></asp:TextBox>
                                                <asp:RequiredFieldValidator SetFocusOnError="true" ValidationGroup='<%#"add_"+Container.DataItemIndex %>' runat="server" ID="req_rcv_amt" ControlToValidate="txt_rcv_amount" ErrorMessage="Receive Amount is Required!" />
                                            </ItemTemplate>
                                        </asp:TemplateField>

                                        <asp:TemplateField HeaderText="Mark Up" HeaderStyle-Width="20%">
                                            <ItemTemplate>
                                                <asp:TextBox ID="txt_mrkup" Enabled="false" runat="server" Style="text-align: right" CssClass="form-control" Text='<%# Bind("MARKUP") %>'></asp:TextBox>
                                            </ItemTemplate>
                                        </asp:TemplateField>

                                        <asp:TemplateField HeaderText="Action" ItemStyle-Width="6%">
                                            <ItemTemplate>
                                                <asp:LinkButton ID="linkedit" runat="server" Visible="false" CommandName="gEdit"><i class="fa fa-pencil" style="font-size:20px !important;"></i></asp:LinkButton>
                                                <asp:LinkButton ID="lnk_add" ValidationGroup='<%#"add_"+Container.DataItemIndex %>' runat="server" CommandName="gadd"><i class="fa fa-plus" style="font-size:20px !important;"></i></asp:LinkButton>
                                                <asp:LinkButton ID="linkdlt" Visible="false" CausesValidation="false" CommandName="gDelete" runat="server" OnClientClick="return confirm('Are you sure to delete?')"><i class="fa fa-trash" style="font-size:20px !important"></i></asp:LinkButton>
                                            </ItemTemplate>
                                            <ItemStyle Width="6%"></ItemStyle>
                                        </asp:TemplateField>
                                    </Columns>
                                    <%--<FooterStyle CssClass="footerStyle" />
                                    <HeaderStyle CssClass="headerStyle" />
                                    <RowStyle CssClass="rowStyle" />--%>
                                </asp:GridView>
                            </div>
                        </div>

                        <div class="row">
                            <asp:Label CssClass="control-label col-md-offset-2" ID="lbl_due" runat="server"></asp:Label>

                        </div>

                        <div class="span6">
                            <label id="lbl1" class="control-label alert alert-danger alert-dismissable" style="display: none; text-align: left">Loan Amount should be Less than your Salary...!</label>
                            <label id="lbl2" class="control-label alert alert-danger alert-dismissable" style="display: none; text-align: left">You are not Eligible for this Loan right now...!</label>
                        </div>
                    </div>
                </section>
            </div>
        </div>
    </div>


    <script type="text/javascript">

        //Calculate Installment amount from Loan Amount to No of Days
        function GetInstallmentAmt() {
            if (document.getElementById('<%= txt_amount.ClientID%>').value != "") {
                var loan_amt = document.getElementById('<%= txt_amount.ClientID%>').value;
                var no_of_inst = document.getElementById('<%= txt_no_inst.ClientID%>').value.replace(',', '');
                let value = parseFloat(loan_amt.replace(',', '')) / parseFloat(no_of_inst);
                document.getElementById('<%=txt_ints_amt.ClientID%>').value = value.toLocaleString();
            }
        }



        function CheckPFValue() {
            let loan_amt = document.getElementById('<%=txt_amount.ClientID%>').value;
           <%--let pf_value = document.getElementById('<%=txt_val.ClientID%>').value;--%>
            let salary = document.getElementById('<%=hid_sal.ClientID%>').value;
            var emp = document.getElementById('<%=txt_emp.ClientID%>').value;



            // Check is loan amount greater than PF amount or less than PF amount

            if (salary != "") {
                if (parseInt(document.getElementById('<%=txt_amount.ClientID%>').value) > parseInt(document.getElementById('<%=hid_sal.ClientID%>').value)) {
                    document.getElementById('<%= btn_save.ClientID %>').disabled = true;
                    document.getElementById('lbl1').style.display = "block";
                    document.getElementById('div_chk').style.display = "block";
                }

                else if (parseInt(document.getElementById('<%=txt_amount.ClientID%>').value) <= parseInt(document.getElementById('<%=hid_sal.ClientID%>').value)) {
                    document.getElementById('<%= btn_save.ClientID %>').disabled = false;
                    document.getElementById('lbl1').style.display = "none";
                    document.getElementById('div_chk').style.display = "none";
                    document.getElementById('<% =chk_alow.ClientID %>').checked = false;

                }
            }


            // Check Previous Balance is remaining or not
            if (emp != "") {
                if (parseInt(document.getElementById('<%=hid_pr_loan_amt.ClientID%>').value) > parseInt(0)) {
                    document.getElementById('<%= btn_save.ClientID %>').disabled = true;
                    document.getElementById('lbl2').style.display = "block";
                }

                else if (parseInt(document.getElementById('<%=hid_pr_loan_amt.ClientID%>').value) == parseInt(0)) {
                    document.getElementById('<%= btn_save.ClientID %>').disabled = false;
                    document.getElementById('lbl2').style.display = "none";
                }
            }

          


          
        }

        function ShowHideDiv(chkAllow) {
            var dvlbl1 = document.getElementById("lbl1");

            if (chkAllow.checked==true) {
                dvlbl1.style.display = "none" ;
                document.getElementById('<%= btn_save.ClientID %>').disabled = false;
            }

            else {
                dvlbl1.style.display = "block";
                document.getElementById('<%= btn_save.ClientID %>').disabled = true ;
            }
            
            

        }

        function CheckLoanValue() {
            let loan_amt = document.getElementById('<%=hid_pr_loan_amt.ClientID%>').value;

            if (parseInt(document.getElementById('<%=hid_pr_loan_amt.ClientID%>').value) > parseInt(0)) {
                document.getElementById('<%= btn_save.ClientID %>').disabled = true;
                document.getElementById('lbl2').style.display = "block";
            }

            else if (parseInt(document.getElementById('<%=hid_pr_loan_amt.ClientID%>').value) == parseInt(0)) {
                document.getElementById('<%= btn_save.ClientID %>').disabled = false;
                document.getElementById('lbl2').style.display = "none";
            }

        }

        function Comma(Num) { //function to add commas to textboxes
            Num += '';
            Num = Num.replace(',', ''); Num = Num.replace(',', ''); Num = Num.replace(',', '');
            Num = Num.replace(',', ''); Num = Num.replace(',', ''); Num = Num.replace(',', '');
            x = Num.split('.');
            x1 = x[0];
            x2 = x.length > 1 ? '.' + x[1] : '';
            var rgx = /(\d+)(\d{3})/;
            while (rgx.test(x1))
                x1 = x1.replace(rgx, '$1' + ',' + '$2');
            return x1 + x2;
        }
    </script>


    <script src="../../ThemeScrips/jquery.min.js"></script>
    <link href="../../ThemeScrips/select2.min.css"
        rel="stylesheet" />
    <script src="../../ThemeScrips/select2.min.js"></script>
    <%--<script src="/ThemeScrips/select2.min.js"></script>--%>
    <script>
        $('.select2').select2();
    </script>

</asp:Content>
