<%@ 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">
                    Loan Management
                        <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" />
                                  <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">
                            <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="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 PF Amount...!</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;
            var emp = document.getElementById('<%=txt_emp.ClientID%>').value;



            // Check is loan amount greater than PF amount or less than PF amount
            if (pf_value != "") {
                if (parseInt(document.getElementById('<%=txt_amount.ClientID%>').value) > parseInt(document.getElementById('<%=txt_val.ClientID%>').value)) {
                    document.getElementById('<%= btn_save.ClientID %>').disabled = true;
                    document.getElementById('lbl1').style.display = "block";
                }

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


            // 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 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>
