<!-- 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">Payment Difference</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">
                       Payment Difference
                    </div>
                    <div class="panel-body">

<div class="app-title">
    <br><br>
    <?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/payment_diff" id="member_add_" enctype='multipart/form-data'>
    <div class="row no-print">
        <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 $sno = 0;
$member_ft = 0;
$tot_os = 0;
$tot_pri = 0;
$tot_int = 0;
$tot_p_int = 0;
$tot_od_int = 0;
$tot_fpri = 0;
$tot_fint = 0;
$tot_fod_int = 0;
$tot_fos = 0;
$tot_td = 0;
$tot_ins = 0;
$tot_total = 0;
$tot_others = 0;
$tot_loan_amount = 0; ?>

<?php if (isset($members) && count($members) > 0) { ?>
    <div class="row mt-3">
        <div class="col-sm-12">
            <div class="tile">
                <div class="tile-body">
                   
                    
            

                    <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">M. No.</th>
                                <th class="text-center">Name</th>
                                <th class="text-center">Desig</th>
                                <th class="text-center">Generated Amount</th>
                                <th class="text-center">Paid Amount</th>
                                <th class="text-center">Extra</th>
                                <th class="text-center">Due</th>
                            </tr>
                        </thead>

                        <tbody>

                      
                                <?php
                                $pre_section = "";
                                $sno=0;
                                foreach ($members as $key => $member)
                                {
                                    // $member = (object) $member;
                                    if ($member->tot_pay != $member->d_total) {
                                    $sno = $sno + 1; ?>

                                    <tr>
                                        <td class="text-center" style="padding:5px; margin:0px;">
                                            <?= $sno; ?>
                                        </td>
                                        <td class="text-center" style="padding:5px; margin:0px;">
                                            <?= $member->me_no ?>
                                       
                                        </td>
                                        <td style="padding:5px; margin:0px;">
                                            <?= $member->mem_name ?>
                                        </td>
                                        <td style="padding:5px; margin:0px;">
                                            <?= $member->mem_desig ?>
                                        </td>
                                        <td style="padding:5px; margin:0px;text-align:right;">
                                            <?php //if ($member->d_total >0) { ?>
                                                <?= $member->d_total ?>
                                            <?php //} ?>

                                        </td>
                                        <td style="padding:5px; margin:0px;text-align:right;">
                                            <?php //if ($member->tot_pay > 0) { ?>
                                                <?= $member->tot_pay ?>
                                            <?php //} ?>
                                           
                                        </td>
                                        <td style="padding:5px; margin:0px;text-align:right;">
                                            <?php if ($member->tot_pay > $member->d_total) { ?>
                                                <?= $member->tot_pay-$member->d_total ?>
                                            <?php }else{ echo "0"; } ?>
                                         
                                        </td>
                                       
                            
                                            <td style="padding:5px; margin:0px;text-align:right;color:red;">
                                                <?php if ($member->tot_pay < $member->d_total) { ?>
                                                    <b> <?php echo $member->d_total-$member->tot_pay; ?></b>
                                                <?php }else{ echo "0"; } ?>
                                            </td>
                                        
                                        </tr>
                              <?php } } ?>
                                   
                        </tbody>
                    </table>
                  
                </div>

            </div>
        </div>
    </div>
    </form>
<?php } ?>
</div>


</div>
</div>


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

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

    });

    $(document).ready(function() {
        $('.alert').fadeOut(3000);
        $(".others_button,.insurance_button,.d_others").keyup(function() {
            var id = $(this).attr("data-id");
            var others = $('#others_' + id).val() == "" ? 0 : $('#others_' + id).val();
            var d_others = $('#d_others_' + id).val() == "" ? 0 : $('#d_others_' + id).val();
            var f_others = $('#f_others_' + id).val() == "" ? 0 : $('#f_others_' + id).val();
            var _total = $('#total_' + id).attr("data-initial-total");
            var _int = $('#f_int_' + id).attr("data-initial-int");
            var int = parseInt(_int);
            var _f_int = $('#ff_int_' + id).attr("data-initial-int");
            var f_int = parseInt(_f_int);
            var total_txt = _total.replace(",", "");
            var init_total = parseInt(total_txt);
            var others_val = parseInt(others);
            var d_others_val = parseInt(d_others);
            var f_others_val = parseInt(f_others);
            var interest = int + others_val;
            var f_interest = f_int + f_others_val;
            if (interest >= 0) {
                $('#f_int_' + id).text(interest);
                $('#int_' + id).val(interest);
            } else if (interest < 0) {
                alert("Interest Cannot be lessthan 0")
                $('#f_int_' + id).text(int);
                $('#others_' + id).val(0);
            }
            if (f_interest >= 0) {
                $('#ff_int_' + id).text(f_interest);
                $('#intf_' + id).val(f_interest);
            } else if (f_interest < 0) {
                alert("Interest Cannot be lessthan 0")
                $('#ff_int_' + id).text(f_int);
                $('#f_others_' + id).val(0);
            }
            var total = init_total + others_val + f_others_val + d_others_val;
            $('#total_' + id).text(total);
            $('#tot_' + id).val(total);
            var tot_m = $('#sno').attr("data-id");
            var tot_others = 0;
            for (j = 1; j <= tot_m; j++) {
                var others_ = $('#others_' + j).val() == "" ? 0 : $('#others_' + j).val();
                tot_others += parseInt(others_);
            }
            $('#tot_others').val(tot_others);
            var final_t = $('#sno').attr("data-id");
            var tot_int = $('#sno').attr("data-tot_int");
            var tot_fint = $('#sno').attr("data-tot_fint");
            var tot = 0;
            for (i = 1; i <= final_t; i++) {
                tot += parseInt($('#tot_' + i).val());
            }
            $('#final_tot').val(tot);
            var int_tot = 0;
            for (i = 1; i <= final_t; i++) {
                int_tot += parseInt($('#f_int_' + i).text());
            }
            var fi_tint = parseInt(tot_int);
            $('#f_int_tot').val(int_tot + fi_tint);
            var fint_tot = 0;

            for (i = 1; i <= final_t; i++) {
                fint_tot += parseInt($('#ff_int_' + i).text());
            }
            var fi_tfint = parseInt(tot_fint);
            $('#ff_int_tot').val(fint_tot + fi_tfint);

            var d_o_tot = 0;
            for (i = 1; i <= final_t; i++) {
                d_o_tot += parseInt($('#d_others_' + i).val() == "" ? 0 : $('#d_others_' + i).val());
                d_o_tot += parseInt($('#old_others_' + i).val() == "" ? 0 : $('#old_others_' + i).val());
            }

            var d_oth_tot = parseInt(d_o_tot);
            $('#d_others_total').val(d_oth_tot);
        });
    });
</script>