<div class="content-header">
    <div class="container-fluid">
        <div class="row mb-2">
            <div class="col-sm-6">
                <!-- <h1 class="m-0">Deactive Member </h1> -->
            </div>
            <div class="col-sm-6">
                <ol class="breadcrumb float-sm-right">
                    <li class="breadcrumb-item"><a href="#">Partial Payment</a></li>
                    <li class="breadcrumb-item active">Loan Os</li>
                </ol>
            </div>
        </div>
    </div>
</div>




<section class="content">
    <div class="container-fluid">




        <div class="row">

            <section class="col-lg-12 connectedSortable">

                <div class="card">
                    <div class="card-header " style="background-color:#31708e;color:white">
                        <h3 class="card-title">
                            Loan Os
                        </h3>
                        <div class="card-tools">
                            <ul class="nav nav-pills ml-auto">
                            </ul>
                        </div>
                    </div>
                    <div class="card-body">
                        <div class="tab-content p-0">




                            <?php if (isset($_SESSION['mem_add']) && $_SESSION['mem_add'] != "") { ?>
                                <div class="alert alert-success">
                                    <strong><?php echo $_SESSION['mem_add']; ?></strong>
                                </div>
                            <?php $_SESSION['mem_add'] = "";
                            } ?>
                            <h3 class="text-center btn btn-lg btn-light  float-left"><a href="<?php echo base_url(); ?>index.php/Member/loan_details">Back</a></h3>

                            <div class="row">
                                <div class="col-sm-3"></div>
                                <div class="col-sm-8">
                                    <form method="post" action="<?php echo base_url(); ?>index.php/Member/loan_close_amt" id="partial_payment" enctype='multipart/form-data'>
                                        <input type="hidden" name="updated_on" value="<?php echo date("Y-m-d H:i:s"); ?>">
                                        <div class="row mt-3">
                                            <div class="col-sm-4"><b>Member Number</b></div>
                                            <div class="col-sm-8">
                                                <select name="mem_no" id="mem_no" required class="form-control">
                                                    <option value="">-- Select --</option>
                                                    <?php foreach ($members as $mem) { ?>
                                                        <option value="<?php echo $mem['me_no']; ?>"><?php echo $mem['me_no'] . "-" . $mem['mem_name']; ?></option>
                                                    <?php } ?>
                                                </select>
                                            </div>
                                        </div><br><br>
                                        <div id="partial_payment_details">

                                        </div>
                                    </form>
                                </div>
                            </div>


                        </div>
                    </div>
                </div>
        </div>
</section>
</div>
</div>
</section>


<script>
    function get_others_amt() {
        var old_others_val = $('#old_others_val').val();
        var others_amt = $('#others_amt').val();
        var total_amount = $('#total_amount').val();
        var dtotal_amount = $('#dtotal_amount').val();
        var interest = $('#interest').val();
        var fl_interest = $('#fl_interest').val();
        if (old_others_val > others_amt) {
            alert("Others should be greater than or equal to " + old_others_val);
            $('#others_amt').val(old_others_val);
            $('#others_amt').focus();
            return false;
        }
        tot_amt = parseInt(dtotal_amount) + parseInt(interest) + parseInt(fl_interest) + parseInt(others_amt) - parseInt(old_others_val);
        $('#total_amount').val(tot_amt);
    }

    function get_interest() {
        var interest = "";
        var temp_array = [];
        var me_no = [];
        var percentage = "";
        var days = $('#no_of_days').val();
        if (parseInt(days) <= 0) {
            alert("No of days should not be empty");
            $("#no_of_days").focus();
            return false;
        }
        var temp = $("#mem_no option:selected").text();
        var temp_array = temp.split("-");
        me_no.push(temp_array[0]);
        var loan_os = $('#loan_os_amt').val();
        $.ajax({
            type: 'POST',
            url: "<?php echo base_url() ?>index.php/Member/get_loan_percentage",
            data: {
                me_no: me_no
            },
            dataType: 'json',
            success: function(data) {
                var percentage = data.loan_int_rate;
                var loan = (loan_os * percentage) / 100;
                interest = Math.round(loan * days / 365);
                $('#interest').val(interest);
                var total_amount = $('#total_amount').val();
                var dtotal_amount = $('#dtotal_amount').val();
                var interest = $('#interest').val();
                var fl_interest = $('#fl_interest').val();
                var old_others_val = $('#old_others_val').val();
                var others_amt = $('#others_amt').val();
                tot_amt = parseInt(dtotal_amount) + parseInt(interest) + parseInt(fl_interest) + parseInt(others_amt) - parseInt(old_others_val);
                $('#total_amount').val(tot_amt);
            }
        });
    }

    function get_fl_interest() {
        var interest = "";
        var temp_array = [];
        var me_no = [];
        var percentage = "";
        var days = $('#fl_no_of_days').val();
        if (parseInt(days) <= 0) {
            alert("No of days should not be empty");
            $("#fl_no_of_days").focus();
            return false;
        }
        var temp = $("#mem_no option:selected").text();
        var temp_array = temp.split("-");
        me_no.push(temp_array[0]);
        var loan_os = $('#f_os').val();
        var fl_int_rate = $('#fl_int_rate').val();
        var percentage = fl_int_rate;
        var loan = (loan_os * percentage) / 100;
        interest = Math.round(loan * days / 365);
        $('#fl_interest').val(interest);
        var total_amount = $('#total_amount').val();
        var dtotal_amount = $('#dtotal_amount').val();
        var interest = $('#interest').val();
        var fl_interest = $('#fl_interest').val();
        var old_others_val = $('#old_others_val').val();
        var others_amt = $('#others_amt').val();
        tot_amt = parseInt(dtotal_amount) + parseInt(interest) + parseInt(fl_interest) + parseInt(others_amt) - parseInt(old_others_val);
        $('#total_amount').val(tot_amt);
    }

    $(document).ready(function() {
        $('.alert').fadeOut(3000);

        $("#interest").bind('change', function() {
            var total_amount = $('#total_amount').val();
            var dtotal_amount = $('#dtotal_amount').val();
            var interest = $('#interest').val();
            tot_amt = parseInt(dtotal_amount) + parseInt(interest);
            $('#total_amount').val(tot_amt);
        });

        $("#totals").bind('change', function() {
            var others = $('#p_others').val();
            var insurance = $('#pinsurance').val();
            var p_int = $('#pp_int').val();
            var thrift = $('#p_thrift').val();
            var interest = $('#interest').val();
            var os = $('#loan_os').val();
            var share = $('#p_share').val();


            var total = parseInt(os) - parseInt(others) - parseInt(insurance) - parseInt(p_int) - parseInt(thrift) - parseInt(interest) - parseInt(share);


            $('#_total1').val(total);


        });



        $("#p_share").keyup(function(event) {
            var others = $('#p_others').val();
            var insurance = $('#pinsurance').val();
            var p_int = $('#pp_int').val();
            var thrift = $('#p_thrift').val();
            var interest = $('#interest').val();
            var os = $('#loan_os').val();
            var share = $('#p_share').val();

            var totals = parseInt(others) + parseInt(insurance) + parseInt(p_int) + parseInt(thrift) + parseInt(interest) + parseInt(share);


            $('#totals').val(totals).trigger("change");



        });

        $("#loan_os").keyup(function(event) {
            var others = $('#p_others').val();
            var insurance = $('#pinsurance').val();
            var p_int = $('#pp_int').val();
            var thrift = $('#p_thrift').val();
            var interest = $('#interest').val();
            var os = $('#loan_os').val();
            var share = $('#p_share').val();

            var totals = parseInt(others) + parseInt(insurance) + parseInt(p_int) + parseInt(thrift) + parseInt(interest) + parseInt(share);


            $('#totals').val(totals).trigger("change");



        });

        $("#p_others").keyup(function(event) {
            var others = $('#p_others').val();
            var insurance = $('#pinsurance').val();
            var p_int = $('#pp_int').val();
            var thrift = $('#p_thrift').val();
            var interest = $('#interest').val();
            var os = $('#loan_os').val();
            var share = $('#p_share').val();


            var totals = parseInt(others) + parseInt(insurance) + parseInt(p_int) + parseInt(thrift) + parseInt(interest) + parseInt(share);

            $('#totals').val(totals).trigger("change");



        });

        $("#pp_int").keyup(function(event) {
            var others = $('#p_others').val();
            var insurance = $('#pinsurance').val();
            var p_int = $('#pp_int').val();
            var thrift = $('#p_thrift').val();
            var interest = $('#interest').val();
            var os = $('#loan_os').val();
            var share = $('#p_share').val();

            var totals = parseInt(others) + parseInt(insurance) + parseInt(p_int) + parseInt(thrift) + parseInt(interest) + parseInt(share);

            $('#totals').val(totals).trigger("change");



        });

        $("#pinsurance").keyup(function(event) {
            var others = $('#p_others').val();
            var insurance = $('#pinsurance').val();
            var p_int = $('#pp_int').val();
            var thrift = $('#p_thrift').val();
            var interest = $('#interest').val();
            var os = $('#loan_os').val();
            var share = $('#p_share').val();

            var totals = parseInt(others) + parseInt(insurance) + parseInt(p_int) + parseInt(thrift) + parseInt(interest) + parseInt(share);


            $('#totals').val(totals).trigger("change");



        });

        $("#p_thrift").keyup(function(event) {
            var others = $('#p_others').val();
            var insurance = $('#pinsurance').val();
            var p_int = $('#pp_int').val();
            var thrift = $('#p_thrift').val();
            var interest = $('#interest').val();
            var os = $('#loan_os').val();
            var share = $('#p_share').val();

            var totals = parseInt(others) + parseInt(insurance) + parseInt(p_int) + parseInt(thrift) + parseInt(interest) + parseInt(share);


            $('#totals').val(totals).trigger("change");


        });

        $("#pd_thrift").keyup(function(event) {
            var others = $('#p_others').val();
            var insurance = $('#pinsurance').val();
            var p_int = $('#pp_int').val();
            var thrift = $('#p_thrift').val();
            var interest = $('#interest').val();
            var os = $('#loan_os').val();
            var share = $('#p_share').val();

            var totals = parseInt(others) + parseInt(insurance) + parseInt(p_int) + parseInt(thrift) + parseInt(interest) + parseInt(share);


            $('#totals').val(totals).trigger("change");


        });
        $("#pold_p_int").keyup(function(event) {
            var others = $('#p_others').val();
            var insurance = $('#pinsurance').val();
            var p_int = $('#pp_int').val();
            var thrift = $('#p_thrift').val();
            var interest = $('#interest').val();
            var os = $('#loan_os').val();
            var share = $('#p_share').val();

            var totals = parseInt(others) + parseInt(insurance) + parseInt(p_int) + parseInt(thrift) + parseInt(interest) + parseInt(share);


            $('#totals').val(totals).trigger("change");


        });

        $("#pay_int").keyup(function(event) {
            var others = $('#p_others').val();
            var insurance = $('#pinsurance').val();
            var p_int = $('#pp_int').val();
            var thrift = $('#p_thrift').val();
            var interest = $('#interest').val();
            var os = $('#loan_os').val();
            var share = $('#p_share').val();

            var totals = parseInt(others) + parseInt(insurance) + parseInt(p_int) + parseInt(thrift) + parseInt(interest) + parseInt(share);


            $('#totals').val(totals).trigger("change");


        });

        $('input[name="pay_total"]:checked').each(function() {
            alert('check');
            //   console.log(this.value);
            temp = this.value;
            // alert(temp);

        });
    });




    function cheque_trans_id_div_fun() {
        var cash_type_id = $('#cash_type_id').val();
        if (cash_type_id != "" && cash_type_id != "Cash") {
            $("#cheque_trans_id_div").show();
        } else {
            $("#cheque_trans_id_div").hide();
        }
    }

    function check_validation() {
        var mem_no = $('#mem_no').val();
        var tot_thrift_amount = $('#tot_thrift_amount').val();
        var tot_share_amount = $('#tot_share_amount').val();

        if (mem_no == "") {
            alert("Must Fill Member Number");
            $('#mem_no').focus();
            return false;
        }
        if (tot_thrift_amount <= 0) {
            alert("Must Fill Thrift Amount");
            $('#no_of_days').focus();
            return false;
        }
        if (tot_share_amount == "") {
            alert("Must Fill Share Amount");
            $('#loan_os_amt').focus();
            return false;
        }
        if (!$("input[name='cash_type']:checked").val()) {
            alert("Must Fill Cash Type");
            return false;
        }
        if (added_on_id == "") {
            alert("Must Fill Payment Date");
            $('#added_on_id').focus();
            return false;
        }
        $('#partial_payment').submit();

    }


    $('#mem_no').on('change', function() {
        var me_no = this.value;
        $.ajax({
            type: 'POST',
            url: "<?php echo base_url() ?>index.php/Member/get_loan_close_amt_details",
            data: {
                me_no: me_no
            },
            dataType: 'html',
            success: function(data) {
                $('#partial_payment_details').html(data);
            }
        });
    });
</script>