<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="#">Recovery</a></li>
                    <li class="breadcrumb-item active">Demand Recovery</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">
                            Demand Recovery Details
                        </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'] = "";
                            } ?>

                        </div>
                        <form method="post" action="<?php echo base_url(); ?>index.php/Demand/demand_recover" id="member_add_" enctype='multipart/form-data'>
                            <div class="row">
                                <div class="col-sm-3">
                                    <div>Division</div>
                                    <select name="division" id="division_id" class="form-control">
                                        <option value="">All</option>
                                        <?php foreach ($divisions as $div) { ?>
                                            <option value="<?php echo $div['division']; ?>"><?php echo $div['division']; ?></option>
                                        <?php } ?>
                                    </select>
                                </div>
                                <div class="col-sm-3">
                                    <div>Section</div>
                                    <select name="section" id="section_id" class="form-control">
                                        <option value="">All</option>
                                        <?php foreach ($sections as $sec) { ?>
                                            <option value="<?php echo $sec['section']; ?>"><?php echo $sec['section']; ?></option>
                                        <?php } ?>
                                    </select>
                                </div>
                                <div class="col-sm-2">
                                    <div>Month</div>
                                    <?php $m = date('F'); ?>
                                    <select name="month" class="form-control">
                                        <option value="">Select Month</option>
                                        <option <?php if ($m == "January") {
                                                    echo "";
                                                } ?> value="January">January</option>
                                        <option <?php if ($m == "February") {
                                                    echo "";
                                                } ?> value="February">February</option>
                                        <option <?php if ($m == "March") {
                                                    echo "";
                                                } ?> value="March">March</option>
                                        <option <?php if ($m == "April") {
                                                    echo "";
                                                } ?> value="April">April</option>
                                        <option <?php if ($m == "May") {
                                                    echo "";
                                                } ?> value="May">May</option>
                                        <option <?php if ($m == "June") {
                                                    echo "";
                                                } ?> value="June">June</option>
                                        <option <?php if ($m == "July") {
                                                    echo "";
                                                } ?> value="July">July</option>
                                        <option <?php if ($m == "August") {
                                                    echo "";
                                                } ?> value="August">August</option>
                                        <option <?php if ($m == "September") {
                                                    echo "";
                                                } ?> value="September">September</option>
                                        <option <?php if ($m == "October") {
                                                    echo "";
                                                } ?> value="October">October</option>
                                        <option <?php if ($m == "November") {
                                                    echo "";
                                                } ?> value="November">November</option>
                                        <option <?php if ($m == "December") {
                                                    echo "";
                                                } ?> value="December">December</option>
                                    </select>
                                </div>
                                <div class="col-sm-2">Year
                                    <?php $y = date('Y'); ?>
                                    <select name="year" class="form-control">
                                        <option value="">Select Year</option>
                                        <?php for ($j = $y; $j >= 2023; $j--) { ?>
                                            <option <?php if ($j == $y) {
                                                        echo "";
                                                    } ?> value="<?php echo $j; ?>"><?php echo $j; ?></option>
                                        <?php } ?>
                                    </select>
                                </div>
                                <div class="col-sm-2 mt-4"><button type="submit" onclick="this.disabled=true;this.value='Submitting...'; this.form.submit();" class="btn btn-primary">Submit</button></div>
                        </form>
                    </div>
                    <?php $member_ft = 0;
                    $tot_os = 0;
                    $tot_pri = 0;
                    $tot_int = 0;
                    $tot_fpri = 0;
                    $tot_fint = 0;
                    $tot_fos = 0;
                    $tot_td = 0;
                    $tot_others = 0;
                    $tot_ins = 0;
                    $tot_recover = 0; ?>
                    <?php if (isset($members) && count($members) > 0) { ?><br><br>
                        <form method="post" action="<?php echo base_url(); ?>index.php/Demand/demand_recover" id="rec_form" enctype='multipart/form-data'>
                            <div class="row">
                                <div class="col-sm-12">
                                    <div class="tile">
                                        <div class="tile-body">
                                            <div class="row mb-2">
                                                <div class="col-sm-2">Date : <input type="date" name="added_on" id="added_on_id" value="<?php echo date("d-m-Y"); ?>" class="form-control"></div>
                                                <div class="col-sm-2">Cash Type Bank:<br>
                                                    <select class="form-control" name="cash_type" id="cash_type_id">
                                                        <option value="">--Select--</option>
                                                        <?php foreach ($banks as $bank) { ?>
                                                            <option value="<?php echo  $bank['bank']; ?>"><?php echo  $bank['bank']; ?></option>
                                                        <?php } ?>
                                                    </select>
                                                </div>
                                            </div><br><br><br>
                                            <table id="table_id" class="table table-striped table-bordered " style="width:100%">
                                                <thead>

                                                    <tr>
                                                        <th class="text-center">S.No.</th>
                                                        <th class="text-center">Mem. No.</th>
                                                        <th class="text-center">Name</th>
                                                        <th class="text-center">Principal</th>
                                                        <th class="text-center">Interest</th>
                                                        <th class="text-center">P.Int</th>
                                                        <!-- <th class="text-center">EL Int.</th> -->
                                                        <!-- <th class="text-center">FL Pri.</th>
                                                        <th class="text-center">FL Int.</th>
                                                        <th class="text-center">FP.Int</th> -->
                                                        <th class="text-center">TD</th>
                                                        <th class="text-center">Insurance</th>
                                                        <th class="text-center">Others</th>
                                                        <th class="text-center">Total</th>
                                                        <th class="text-center">Amount Recovered</th>
                                                        <th class="text-center">Due By</th>
                                                    </tr>
                                                    </tr>
                                                </thead>
                                                <!-- Here's where we iterate through our $articles query object, printing out article info -->
                                                <tbody>
                                                    <?php $sno = 0; ?>
                                                    <?php foreach ($members as $key => $member) : ?>

                                                        <?php
                                                        //write query to get thrift from thrfit table by memeber no

                                                        $sno = $sno + 1; ?>
                                                        <input type="hidden" name="<?php echo "postvalue[" . $member->me_no . "][division]"; ?>" value="<?php echo $division; ?>" id="<?php echo $sno; ?>" data-id="<?php echo $sno; ?>" class="form-control">
                                                        <input type="hidden" name="<?php echo "postvalue[" . $member->me_no . "][section]"; ?>" value="<?php echo $section; ?>" id="<?php echo $sno; ?>" data-id="<?php echo $sno; ?>" class="form-control">
                                                        <input type="hidden" name="<?php echo "postvalue[" . $member->me_no . "][month]"; ?>" value="<?php echo $month; ?>" id="<?php echo $sno; ?>" data-id="<?php echo $sno; ?>" class="form-control">
                                                        <input type="hidden" name="<?php echo "postvalue[" . $member->me_no . "][year]"; ?>" value="<?php echo $year; ?>" id="<?php echo $sno; ?>" data-id="<?php echo $sno; ?>" class="form-control">
                                                        <tr>
                                                            <td>
                                                                <?= $sno; ?>
                                                            </td>
                                                            <td class="text-center">
                                                                <?= $member->me_no ?>
                                                                <input type="hidden" name="<?php echo "postvalue[" . $member->me_no . "][me_no]"; ?>" value="<?php echo $member->me_no; ?>" id="me_no<?php echo $sno; ?>" data-id="<?php echo $member->me_no; ?>" class="form-control">
                                                                <input type="hidden" name="<?php echo "postvalue[" . $member->me_no . "][d_loan_os]"; ?>" value="<?php echo $member->d_loan_os; ?>" id="loan_os<?php echo $sno; ?>" class="form-control">
                                                                <input type="hidden" name="<?php echo "postvalue[" . $member->me_no . "][f_os]"; ?>" value="<?php echo $member->f_os; ?>" id="f_os<?php echo $sno; ?>" class="form-control">
                                                            </td>
                                                            <td class="text-center">
                                                                <?= $member->mem_name ?>
                                                            </td>
                                                            <td class="text-center">
                                                                <?php if ($member->d_principal + $member->d_last_principal > 0) { ?>
                                                                    <?php echo $member->d_principal + $member->d_last_principal; ?>
                                                                <?php } ?>

                                                                <input type="hidden" name="<?php echo "postvalue[" . $member->me_no . "][d_principal]"; ?>" value="<?php echo $member->d_principal; ?>" id="d_principal<?php echo $sno; ?>" data-id="<?php echo $member->d_principal; ?>" class="form-control">
                                                                <input type="hidden" name="<?php echo "postvalue[" . $member->me_no . "][d_last_principal]"; ?>" value="<?php echo $member->d_last_principal; ?>" id="d_last_principal<?php echo $sno; ?>" data-id="<?php echo $member->d_last_principal; ?>" class="form-control">
                                                            </td>
                                                            <td class="text-center">
                                                                <?php if ($member->d_int + $member->p_int + $member->d_last_int > 0) { ?>
                                                                    <?php echo $member->d_int + $member->d_last_int; ?>
                                                                <?php } ?>
                                                                <input type="hidden" name="<?php echo "postvalue[" . $member->me_no . "][d_int]" ?>" value="<?php echo $member->d_int ?>">
                                                                <input type="hidden" name="<?php echo "postvalue[" . $member->me_no . "][p_int]" ?>" value="<?php echo $member->p_int ?>">
                                                                <input type="hidden" name="<?php echo "postvalue[" . $member->me_no . "][old_p_int]" ?>" value="<?php echo $member->old_p_int ?>">
                                                                <input type="hidden" name="<?php echo "postvalue[" . $member->me_no . "][d_last_int]" ?>" value="<?php echo $member->d_last_int ?>">
                                                            </td>
                                                            <td class="text-center">
                                                                <?php if ($member->p_int + $member->old_p_int != "") { ?>
                                                                    <?= $member->p_int + $member->old_p_int ?>
                                                                <?php } ?>
                                                                <input type="hidden" name="<?php echo "postvalue[" . $member->me_no . "][f_int]" ?>" value="<?php echo $member->f_int ?>">
                                                                <input type="hidden" name="<?php echo "postvalue[" . $member->me_no . "][f_p_int]" ?>" value="<?php echo $member->f_p_int ?>">
                                                                <input type="hidden" name="<?php echo "postvalue[" . $member->me_no . "][f_old_int]" ?>" value="<?php echo $member->f_old_int ?>">
                                                                <input type="hidden" name="<?php echo "postvalue[" . $member->me_no . "][f_old_p_int]" ?>" value="<?php echo $member->f_old_p_int ?>">
                                                                <input type="hidden" name="<?php echo "postvalue[" . $member->me_no . "][f_pri]" ?>" value="<?php echo $member->f_pri ?>">
                                                                <input type="hidden" name="<?php echo "postvalue[" . $member->me_no . "][f_old_pri]" ?>" value="<?php echo $member->f_old_pri ?>">
                                                            </td>
                                                            <!-- <td>
                                                                <?php if ($member->f_pri + $member->f_old_pri > 0) { ?>
                                                                    <?php echo $member->f_pri + $member->f_old_pri; ?>
                                                                <?php } ?>
                                                            </td>
                                                            <td>
                                                                <?php if ($member->f_int + $member->f_old_int > 0) { ?>
                                                                    <?php echo $member->f_int + $member->f_old_int; ?>
                                                                <?php } ?>
                                                            </td>
                                                            <td class="text-center">
                                                                <?php if ($member->f_p_int + $member->f_old_p_int != "") { ?>
                                                                    <?= $member->f_p_int + $member->f_old_p_int ?>
                                                                <?php } ?>
                                                            </td> -->
                                                            <td class="text-center">
                                                                <?php if ($member->d_thrift + $member->d_last_thrift > 0) { ?>
                                                                    <?= $member->d_thrift + $member->d_last_thrift; ?>
                                                                <?php } ?>
                                                                <input type="hidden" name="<?php echo "postvalue[" . $member->me_no . "][d_thrift]" ?>" value="<?php echo $member->d_thrift ?>">
                                                                <input type="hidden" name="<?php echo "postvalue[" . $member->me_no . "][d_last_thrift]"; ?>" value="<?php echo $member->d_last_thrift; ?>" id="d_last_thrift<?php echo $sno; ?>" data-id="<?php echo $member->d_last_thrift; ?>" class="form-control">
                                                            </td>
                                                            <td width="50px" class="text-center" s>
                                                                <input type="hidden" name="<?php echo "postvalue[" . $member->me_no . "][old_ins]" ?>" id="old_ins_<?php echo $sno; ?>" value="<?= $member->old_ins ?>" data-id="<?php echo $sno; ?>" class=" form-control">
                                                                <input type="hidden" name="<?php echo "postvalue[" . $member->me_no . "][d_insurance]" ?>" id="insurance_<?php echo $sno; ?>" value="<?= $member->d_insurance ?>" data-id="<?php echo $sno; ?>" class="insurance_button form-control">
                                                                <?php if ($member->d_insurance + $member->old_ins > 0) { ?>
                                                                    <?php echo $member->d_insurance + $member->old_ins; ?>
                                                                <?php } ?>
                                                            </td>
                                                            <td width="50px" class="text-center" s>
                                                                <input type="hidden" name="<?php echo "postvalue[" . $member->me_no . "][d_others]" ?>" id="others_<?php echo $sno; ?>" value="<?= $member->d_others ?>" data-id="<?php echo $sno; ?>" class="others_button form-control">
                                                                <input type="hidden" name="<?php echo "postvalue[" . $member->me_no . "][p_pay]" ?>" id="p_pay_<?php echo $sno; ?>" value="<?= $member->p_pay ?>" data-id="<?php echo $sno; ?>" class="p_pay_button form-control">
                                                                <?php if ($member->d_others > 0) { ?>
                                                                    <?php echo $member->d_others; ?>
                                                                <?php } ?>
                                                            </td>
                                                            <td class="text-center">
                                                                <?php if ($member->d_total > 0) { ?>
                                                                    <?php echo $member->d_total; ?>
                                                                <?php } ?>
                                                                <input data-initial-total="<?= $member->d_total ?>" type="hidden" name="<?php echo "postvalue[" . $member->me_no . "][d_total]" ?>" id="tot_<?php echo $sno; ?>" value="<?= $member->d_total ?>" data-id="<?php echo $sno; ?>">
                                                            </td>
                                                            <td class="text-center">
                                                                <input type="text" onchange="checking('<?= $sno; ?>');" name="<?php echo "postvalue[" . $member->me_no . "][amt_recover]"; ?>" id="recover_<?php echo $sno; ?>" data-id="<?php echo $sno; ?>" value="<?= $member->d_total ?>" class="recover_amt form-control">
                                                            </td>
                                                            <td class="text-center">
                                                                <div id="due_by_div<?php echo $sno; ?>" style="display: none;" class="text-center">
                                                                    <input type='checkbox' name="<?php echo "postvalue[" . $member->me_no . "][due_by]"; ?>" id='due_by<?php echo $sno; ?>' value="1" data-id="<?php echo $sno; ?>">
                                                                    <br>
                                                                </div>
                                                            </td>
                                                        </tr>

                                                        <?php $member_ft += $member->d_total;
                                                        $tot_os += $member->d_loan_os;
                                                        $tot_pri += $member->d_principal + $member->d_last_principal;
                                                        $tot_int += $member->d_int + $member->d_last_int;
                                                        $tot_fint += $member->f_int + $member->f_old_int;
                                                        $tot_fpint += $member->f_p_int + $member->f_old_p_int;
                                                        $tot_f_pri += $member->f_pri + $member->f_old_pri;
                                                        $tot_fos += $member->f_os;
                                                        $tot_td += $member->d_thrift + $member->d_last_thrift;
                                                        $tot_others += $member->d_others;
                                                        $tot_p_int += $member->p_int + $member->old_p_int;
                                                        $tot_ins += $member->d_insurance + $member->old_ins;
                                                        ?>
                                                    <?php endforeach; ?>
                                                    <tr>
                                                        <td>

                                                        </td>
                                                        <td class="text-center">

                                                        </td>

                                                        <td>

                                                        </td>
                                                        <td class="text-center">
                                                            <b> <?php echo $tot_pri; ?></b>
                                                        </td>
                                                        <td class="text-center">
                                                            <b> <?php echo $tot_int; ?></b>
                                                        </td>
                                                        <!--   <td>
                                        <b> <!?php echo  $tot_p_int; ?></b>
                                    </td>
                                  <td>
                                        <b> <!?php echo $tot_fint; ?></b>
                                    </td> -->
                                                        <td class="text-center">
                                                            <b> <?php echo  $tot_p_int; ?></b>
                                                        </td>
                                                        <!-- <td class="text-center">
                                                            <b> <?php echo  $tot_f_pri; ?></b>
                                                        </td>
                                                        <td class="text-center">
                                                            <b> <?php echo  $tot_fint; ?></b>
                                                        </td>
                                                        <td class="text-center">
                                                            <b> <?php echo  $tot_fpint; ?></b>
                                                        </td> -->
                                                        <td class="text-center">
                                                            <b> <?php echo $tot_td; ?></b>
                                                        </td>
                                                        <td class="text-center">
                                                            <b> <?php echo $tot_ins; ?></b>
                                                        </td>
                                                        <td class="text-center">
                                                            <b> <?php echo $tot_others; ?></b>
                                                        </td>
                                                        <td class="text-center">
                                                            <b> <?php echo $member_ft; ?></b>
                                                        </td>
                                                        <td style="width:130px;">
                                                            <input type="input" class="form-control" value="<?php echo $member_ft; ?>" id="final_tot" data-id="<?php echo $sno; ?>" width="100%" height="auto" readonly>
                                                        </td>
                                                    </tr>
                                                </tbody>
                                            </table>
                                            <div class="row no-print">
                                                <div class="col-sm-5"></div> <button type="button" onclick="check_validation(); check_validations();" id="recover_save_btn" class="btn btn-primary col-sm-3">Save</button>
                                            </div>
                        </form>
                </div>
            <?php } ?>






        </div>
    </div>
    </div>
    </div>
</section>
</div>
</div>
</section>
<script>
    $(document).ready(function() {
        // var element = $("#textbox").hide();
        // var count = 0;
        // $("#clickme").on("click", function() {
        //     count++; //count button clicked
        //     if (count = 1) {

        //     } else {
        //         alert('you clicking more than one time');
        //         $("form").submit(function(e) {
        //             e.preventDefault();
        //         });
        //     }
        // });
        // //then the element will be cloned same amount as button click, stuck here
        // $('form').preventDoubleSubmission();
        // jQuery.fn.preventDoubleSubmission = function() {
        //     $(this).on('submit', function(e) {
        //         var $form = $(this);

        //         if ($form.data('submitted') === true) {
        //             // Previously submitted - don't submit again
        //             e.preventDefault();
        //         } else {
        //             // Mark it so that the next submit can be ignored
        //             $form.data('submitted', true);
        //         }
        //     });

        //     // Keep chainability
        //     return this;
        // };
    });

    function checkForm(form) {
        //
        // validate form fields
        //

        form.myButton.disabled = true;
        return true;
    }

    function check_validations() {
        $thrift11 = $('#thrift11');
        $due_by11 = $('#due_by11');

        if (document.getElementById("thrift11").checked) {
            $thrift11.val('0');
        } else {
            $thrift11.val('1');
        }

        if (document.getElementById("due_by11").checked) {
            $due_by11.val('1');
        } else {
            $due_by11.val('1');
        }

    }
</script>
<script>
    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 added_on_id = $('#added_on_id').val();
        var cash_type_id = $('#cash_type_id').val();
        if (added_on_id == "") {

            alert("Must Fill Recover Date");
            $('#added_on_id').focus();
            return false;

        }
        if (cash_type_id == "") {
            alert("Must Fill Cash Type");
            $('#tdccb').focus();
            return false;

        }
        $("#recover_save_btn").disabled = true;
        $('#rec_form').submit();

    }
    $('#division_id').on('change', function() {
        var division = this.value;
        var section = $('#section_id').val();
        $.ajax({
            type: 'POST',
            url: "<?php echo base_url() ?>index.php/Member/member_search",
            data: {
                division: division,
                section: section
            },
            dataType: 'html',
            success: function(data) {
                $('#filter_id').html(data);
                var table = $('#table_id1').DataTable({
                    "processing": true,
                    "dom": 'lBfrtip',
                    "lengthMenu": [
                        [10, 25, 50, -1],
                        [10, 25, 50, "All"]
                    ],
                    "buttons": [
                        'copyHtml5',
                        'excelHtml5',
                        'csvHtml5',
                        'pdfHtml5'
                    ]
                });
                $(".buttons-html5").addClass("btn btn-primary");
                $('.dt-buttons').addClass("pt-3 pb-0");
            }
        });

        $.ajax({
            type: 'POST',
            url: "<?php echo base_url() ?>index.php/Member/getsection",
            data: {
                division: division
            },
            dataType: 'html',
            success: function(data) {

                $('#section_id').html(data);
            }
        });

    });




    $('.alert').fadeOut(3000);
    $(".recover_amt").keyup(function() {
        var tot_m = $('#final_tot').attr("data-id");
        var tot_others = 0;
        for (j = 1; j <= tot_m; j++) {
            var others_ = $('#recover_' + j).val() == "" ? 0 : $('#recover_' + j).val();
            tot_others += parseInt(others_);
        }
        $('#final_tot').val(tot_others);

    });

    function checking(sno) {
        // alert();
        var loan_os = $('#loan_os' + sno).val();
        var tot_amount = $('#tot_' + sno).val();
        var recover = $('#recover_' + sno).val();
        // console.log(loan_os + " " + recover);
        if (parseInt(recover) > parseInt(tot_amount)) {
            // $('#action').show();
            $('#due_by_div' + sno).show();
            $('#due_by' + sno).attr("checked", true);
            if (parseInt(loan_os) > 0) {
                $('#due_by' + sno).attr("disabled", false);
            } else {
                $('#due_by' + sno).attr("disabled", true);
            }
        } else {
            $('#due_by_div' + sno).hide();
            $('#due_by' + sno).attr("checked", false);
        }
    }
</script>