<!-- File: src/Template/Articles/index.ctp -->
<div id="page-wrapper">
    <div class="header">
        <h1 class="page-header">

        </h1>
        <ol class="breadcrumb">
            <li><a href="#">Demand</a></li>
            <li class="active">Demand Generate</li>
        </ol>

    </div>
    <div id="page-inner">



        <div class="row">
            <div class="col-md-12">
                <!--   Kitchen Sink -->
                <div class="panel panel-default">
                    <div class="panel-heading d-flex justify-content-center">
                        Demand Generate
                    </div>
                    <div class="panel-body">
<div class="app-title">
    <div>
        <h1><i class="fa fa-th-list"></i> Demand Recover</h1>
    </div>

    <ol class="breadcrumb">
        <li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>&nbsp;
        <li class="active">Demand</li>
    </ol>
</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 >= 2019; $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" 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_recover = 0; ?>
<?php if (isset($members) && count($members) > 0) { ?>
    <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-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" value="<?php echo date("d-m-Y"); ?>" class="form-control"></div>
                        </div>
                        <table id="table_id" class="table table-striped table-bordered table-responsive" 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">Designation</th>
                                    <th class="text-center">Loan OS</th>
                                    <th class="text-center">Principal</th>
                                    <th class="text-center">%</th>
                                    <th class="text-center">Interest</th>
                                    <th class="text-center">FL OS</th>
                                    <th class="text-center">FL Pri.</th>
                                    <th class="text-center">FL 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 Recoverd</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 . "][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">
                                        </td>
                                        <td>
                                            <?= $member->mem_name ?>
                                        </td>
                                        <td>
                                            <?= $member->mem_desig ?>
                                        </td>
                                        <td>
                                            <?php if ($member->d_loan_os > 0) { ?>
                                                <?= $member->d_loan_os ?>
                                            <?php } ?>
                                            <input type="hidden" name="<?php echo "postvalue[" . $member->me_no . "][d_loan_os]"; ?>" value="<?php echo $member->d_loan_os; ?>" id="d_loan_os<?php echo $sno; ?>" data-id="<?php echo $member->d_loan_os; ?>" class="form-control">
                                        </td>
                                        <td>
                                            <?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_last_int]"; ?>" value="<?php echo $member->d_last_int; ?>" id="d_last_int<?php echo $sno; ?>" data-id="<?php echo $member->d_last_int; ?>" 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>
                                            <?php if ($member->d_int_rate > 0) { ?>
                                                <?= $member->d_int_rate ?>
                                            <?php } ?>
                                            <input type="hidden" name="<?php echo "postvalue[" . $member->me_no . "][d_int_rate]" ?>" value="<?php echo $member->d_int_rate ?>">
                                        </td>
                                        <td>
                                            <?php if ($member->d_int + $member->p_int + $member->d_last_int > 0) { ?>
                                                <?php echo $member->d_int + $member->p_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 . "][d_last_int]" ?>" value="<?php echo $member->d_last_int ?>">
                                        </td>
                                        <td>
                                            <?php if ($member->f_os > 0) { ?>
                                                <?= $member->f_os ?>
                                            <?php } ?>
                                            <input type="hidden" name="<?php echo "postvalue[" . $member->me_no . "][f_os]" ?>" value="<?php echo $member->f_os ?>">
                                        </td>
                                        <td>
                                            <?php if ($member->f_pri + $member->f_old_pri > 0) { ?>
                                                <?php echo $member->f_pri + $member->f_old_pri; ?>
                                            <?php } ?>
                                            <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_int + $member->f_old_int + $member->f_p_int > 0) { ?>
                                                <?php echo $member->f_int + $member->f_old_int + $member->f_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 ?>">
                                        </td>
                                        <td>
                                            <?php if ($member->d_thrift + $member->old_thrift > 0) { ?>
                                                <?php echo $member->d_thrift + $member->old_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 . "][old_thrift]" ?>" value="<?php echo $member->old_thrift ?>">
                                        </td>
                                        <td width="50px" s>
                                            <input type="hidden" name="<?php echo "postvalue[" . $member->mem_no . "][d_insurance]" ?>" id="insurance_<?php echo $sno; ?>" value="<?= $member->d_insurance + $member->old_ins ?>" 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" 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">
                                            <?php if ($member->d_others > 0) { ?>
                                                <?php echo $member->d_others; ?>
                                            <?php } ?>
                                        </td>
                                        <td>
                                            <?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>
                                            <input type="text" 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>
                                    </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->p_int + $member->d_last_int;
                                    $tot_fpri += $member->f_pri + $member->f_old_pri;
                                    $tot_fint += $member->f_int + $member->f_old_int + $member->f_p_int;
                                    $tot_fos += $member->f_os;
                                    $tot_td += $member->d_thrift + $member->old_thrift;
                                    $tot_others += $member->d_others; ?>
                                <?php endforeach; ?>
                                <tr>
                                    <td>

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

                                    </td>
                                    <td>

                                    </td>
                                    <td>

                                    </td>
                                    <td>
                                        <b> <?php echo $tot_os; ?></b>
                                    </td>
                                    <td>
                                        <b> <?php echo $tot_pri; ?></b>
                                    </td>
                                    <td>

                                    </td>
                                    <td>
                                        <b> <?php echo $tot_int; ?></b>
                                    </td>
                                    <td>
                                        <b> <?php echo $tot_fos; ?></b>
                                    </td>
                                    <td>
                                        <b> <?php echo $tot_fpri; ?></b>
                                    </td>
                                    <td>
                                        <b> <?php echo $tot_fint; ?></b>
                                    </td>
                                    <td>
                                        <b> <?php echo $tot_td; ?></b>
                                    </td>
                                    <td>
                                        <b> <?php echo $tot_others; ?></b>
                                    </td>
                                    <td>
                                        <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="submit" class="btn btn-primary col-sm-3">Save</button>
                        </div>
    </form>
    </div>
    </div>
    </div>
    </div>
<?php } ?>
</div>


</div>
</div>
<script>
    function check_validation() {
        var rec_date = $('#rec_date').val();
        if (rec_date == "") {

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

        } else {
            $('#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/members/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/members/getsection",
            data: {
                division: division
            },
            dataType: 'html',
            success: function(data) {

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

    });

    $(document).ready(function() {
        $(".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);

        });
    });
</script>