<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="#">Cash Group</a></li>
                    <li class="breadcrumb-item active">Cash Group Details </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">
                        Cash Group Print 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 class="alert alert-success" id="display">
                            </div>
                            <form method="post" action="<?php echo base_url(); ?>index.php/Demand/cash_group" 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>
                                            Date
                                        </div>
                                        <input type="date" name="added_on" id="added_on_id" value="<?php echo date("Y/m/d"); ?>" onchange="disable_month_year();" class="form-control mt-2">
                                    </div>
                                    <div class="col-sm-2">
                                        <div>Month</div>
                                        <select name="month" id="month_id" class="form-control mt-2">
                                            <option value="">Select Month</option>
                                            <option value="January">January</option>
                                            <option value="February">February</option>
                                            <option value="March">March</option>
                                            <option value="April">April</option>
                                            <option value="May">May</option>
                                            <option value="June">June</option>
                                            <option value="July">July</option>
                                            <option value="August">August</option>
                                            <option value="September">September</option>
                                            <option value="October">October</option>
                                            <option value="November">November</option>
                                            <option value="December">December</option>
                                        </select>
                                    </div>
                                    <div class="col-sm-1">Year
                                        <?php $y = date('Y'); ?>
                                        <select name="year" id="year_id" 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-1 mt-4"><button type="submit" class="btn btn-primary">Submit</button></div>
                            </form>

                        </div>
                        <?php $sno = 0;
                        $member_ft = 0; ?>

                        <?php if (isset($members) && count($members) > 0) { ?>
                            <div class="row">
                                <div class="col-sm-12">
                                    <div class="tile">
                                        <div class="tile-body">
                                            <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">Member Number</th>
                                                        <th class="text-center">Receipt Number</th>
                                                        <th class="text-center">Member Name</th>
                                                        <th class="text-center">Thrift</th>
                                                        <th class="text-center">Principal</th>
                                                        <th class="text-center">Interest</th>
                                                        <!-- <th class="text-center">FL Pri</th>
                                                         <th class="text-center">FL Int</th> -->
                                                        <th class="text-center">Others</th>
                                                        <th class="text-center">Total</th>
                                                        <th class="text-center">Paid On</th>
                                                        <th class="text-center">Action</th>
                                                    </tr>
                                                </thead>
                                                <!-- Here's where we iterate through our $articles query object, printing out article info -->
                                                <tbody>
                                                    <?php $sno = 0;
                                                    $tot_td = 0;
                                                    $tot_loan_pri = 0;
                                                    $tot_loan_int = 0;
                                                    $tot_last_pri = 0;
                                                    $tot_last_int = 0;
                                                    $tot_f_pri = 0;
                                                    $tot_f_int = 0;
                                                    $tot_f_old_pri = 0;
                                                    $tot_f_old_int = 0;
                                                    $tot_others = 0;
                                                    $tot_insurance = 0;
                                                    $tot_tot = 0; ?>
                                                    <?php
                                                    // echo "<pre>";
                                                    // print_r($members);
                                                    // die();
                                                    ?>
                                                    <form method="post" action="<?php echo base_url(); ?>index.php/Demand/cash_group" id="rec_form" enctype='multipart/form-data'>
                                                        <?php foreach ($members as $key => $member) : ?>
                                                            <?php if ($member->tot_pay > 0) { ?>
                                                                <?php
                                                                $sno = $sno + 1;
                                                                ?>
                                                                <tr>
                                                                    <input type="hidden" name="<?php echo "postvalue[" . $member->me_no . "][added_on]"; ?>" value="<?php echo date("Y/m/d"); ?>" id="recover_<?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="recover_<?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="recover_<?php echo $sno; ?>" data-id="<?php echo $sno; ?>" class="form-control">
                                                                    <td>
                                                                        <?= $sno; ?>
                                                                    </td>
                                                                    <td class="text-center">
                                                                        <?= $member->me_no ?>

                                                                    </td>
                                                                    <td class="text-center">
                                                                        <?= $member->rec_no ?>

                                                                    </td>
                                                                    <td>
                                                                        <?= $member->mem_name ?>

                                                                    </td>
                                                                    <td>
                                                                        <?= $member->thrift_amt ?>

                                                                    </td>
                                                                    <td>
                                                                        <?php echo $member->loan_pri_amt + $member->last_principal + $member->ex_os; ?>
                                                                    </td>

                                                                    <td>

                                                                        <?php echo $member->loan_int_amt + $member->last_int + $member->p_int; ?>

                                                                    </td>
                                                                    <!-- <td>
                                            <?php echo $member->f_pri + $member->f_old_pri + $member->f_ex_os; ?>
                                        </td>

                                        <td>

                                            <?php echo $member->f_int + $member->f_old_int + $member->f_p_int; ?>

                                        </td> -->
                                                                    <td>
                                                                        <?= $member->others ?>

                                                                    </td>

                                                                    <td id="total">
                                                                        <?= $member->tot_pay ?>

                                                                    </td>
                                                                    <td>
                                                                        <div id="divpo_<?php echo $member->payment_id; ?>">
                                                                            <?php echo date("d-m-Y", strtotime($member->paid_on)); ?>
                                                                        </div>
                                                                        <div id="input_<?php echo $member->payment_id; ?>" style="display:none;">
                                                                            <div class="form-group">
                                                                                <div class="col-xs-5 date">
                                                                                    <div class="input-group input-append date datePicker" id="datePicker">
                                                                                        <input type="text" class="form-control" name="paid_on_date_<?php echo $member->payment_id; ?>" id="paid_on_date_<?php echo $member->payment_id; ?>" value="<?php echo date("d-m-Y", strtotime($member->paid_on)); ?>" />
                                                                                        <span class="input-group-addon add-on"><span class="glyphicon glyphicon-calendar"></span></span>
                                                                                    </div>
                                                                                </div>
                                                                            </div>
                                                                        </div>
                                                                    </td>

                                                                    <td class="text-center">
                                                                        <input type="checkbox" name="cashgroup" onclick="group();" value="<?php echo $member->payment_id . '-' . $member->tot_pay; ?>">
                                        </div>
                                        </td>
                                        </tr>
                                        <?php
                                                                $tot_td += $member->thrift_amt;
                                                                $tot_loan_pri += $member->loan_pri_amt + $member->ex_os;
                                                                $tot_loan_int += $member->loan_int_amt + $member->p_int;
                                                                $tot_last_pri += $member->last_principal;
                                                                $tot_last_int += $member->last_int;
                                                                $tot_f_pri += $member->f_pri + $member->f_ex_os;
                                                                $tot_f_int += $member->f_int + $member->f_p_int;
                                                                $tot_f_old_pri += $member->f_old_pri;
                                                                $tot_f_old_int += $member->f_old_int;
                                                                $tot_others += $member->others;
                                                                $tot_insurance += $member->insurance;
                                                                $tot_tot += $member->tot_pay;
                                        ?>
                                    <?php } ?>
                                <?php endforeach; ?>
                                </tbody>
                                <tfoot>
                                <th style="text-align:center"> </th>  
                                <th style="text-align:center"></th>
                                <th style="text-align:center"></th>
                                <th style="text-align:center">Total</th>
                                <th style="text-align:center"></th>
                                <th style="text-align:center"></th>
                                <th style="text-align:center"></th>
                                <th style="text-align:center"></th>
                                <th style="text-align:center"></th>
                                <th style="text-align:center"></th>
                                <th style="text-align:center"></th>


                                  </tfoot>
                                </table>

                                <div class="row">
                                    <div class="col-sm-2">

                                    </div>
                                    <div class="col-sm-2">



                                    </div>
                                    <div class="col-sm-2">


                                    </div>
                                    <div class="col-sm-2">
                                        <h4 id="total_amount">

                                        </h4>

                                    </div>
                                    <div class="col-sm-2">

                                    </div>
                                    <div class="col-sm-2 mt-4">
                                        <button type="button" class="btn btn-primary" onclick="creategroup();">Create Group</button>
                                    </div>
                                </div>
                                    </div>
                                </div>
                            </div>
                        <?php } ?>


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

    div.dataTables_wrapper div.dataTables_filter {
        text-align: right;
        margin-top: -37px;
    }

</style>
</section>



            <script>
                function group() {
                    var temp;
                    var temp_array = [];
                    var cash = [];
                    var payment_id = [];
                    var total_amount = 0;
                    $('input[name="cashgroup"]:checked').each(function() {
                        //   console.log(this.value);
                        temp = this.value;
                        // alert(temp);
                        temp_array = temp.split("-");
                        payment_id.push(temp_array[0]);
                        cash.push(temp_array[1]);
                        total_amount += parseInt(temp_array[1]);
                        document.getElementById('total_amount').innerText = "Total:" + total_amount;
                    });

                }

                function creategroup() {
                    confirm("Are You Sure..? Do You Want To Create a Group");
                    var temp;
                    var temp_array = [];
                    var cash = [];
                    var payment_id = [];
                    var total_amount = 0;
                    $('input[name="cashgroup"]:checked').each(function() {
                        temp = this.value;
                        temp_array = temp.split("-");
                        payment_id.push(temp_array[0]);
                        cash.push(temp_array[1]);
                        total_amount += parseInt(temp_array[1]);
                    });
                    $.ajax({
                        type: 'POST',
                        url: "<?php echo base_url() ?>index.php/Demand/cash_grouping",
                        data: {
                            payment_id: payment_id,
                            cash: cash,
                            total_amount: total_amount
                        },
                        dataType: 'html',
                        success: function(data) {
                            if (data != '1') {
                                $('.alert').show();
                                $("#display").html(data);
                                $('.alert').fadeOut(3000);
                            }
                            location.reload();
                        }
                    });
                }

                function disable_month_year() {
                    var added_on = $("#added_on_id").val();
                    if (added_on != "") {
                        $("#month_id").val("");
                        $("#year_id").val("");
                        $('#month_id').prop('disabled', 'disabled');
                        $('#year_id').prop('disabled', 'disabled');
                    } else {
                        $('#month_id').prop('disabled', false);
                        $('#year_id').prop('disabled', false);
                    }
                }

                function editpodate(id) {
                    $("#edit_" + id).hide();
                    $("#save_" + id).show();
                    $("#divpo_" + id).hide();
                    $("#input_" + id).show();
                }

                function savepodate(id) {
                    var paid_on = $("#paid_on_date_" + id).val();
                    if (paid_on != "") {
                        $.ajax({
                            type: 'POST',
                            url: "<?php echo base_url() ?>index.php/Demand/savepodate",
                            data: {
                                id: id,
                                paid_on: paid_on
                            },
                            dataType: 'html',
                            success: function(data) {
                                alert("Paid date updated successfully");
                                $("#edit_" + id).show();
                                $("#save_" + id).hide();
                                $("#divpo_" + id).show();
                                $("#input_" + id).hide();
                                $("#divpo_" + id).html(paid_on);
                            }
                        });
                    } else {
                        alert("Paid date should be filled out");
                    }
                }

                $(document).ready(function() {
                    $('.alert').hide();
                   
                });




                         
</script>
            



<script src="<?php echo base_url() ?>assets/datatables/dataTables.bootstrap.js"></script>
<script src="<?php echo base_url() ?>assets/datatables/jquery.dataTables.js"></script>
<script>
    var table = $('#table_id').DataTable({
        "footerCallback": function ( row, data, start, end, display ) {
            var api = this.api();
 
            // Remove the formatting to get integer data for summation
            var intVal = function ( i ) {
                return typeof i === 'string' ?
                    i.replace(/[\$,]/g, '')*1 :
                    typeof i === 'number' ?
                        i : 0;
            };
            // Total over all pages
            thrift_total = api
                .column( 4 )
                .data()
                .reduce( function (a, b) {
                    return intVal(a) + intVal(b);
                }, 0 );
                pri_total = api
                .column( 5 )
                .data()
                .reduce( function (a, b) {
                    return intVal(a) + intVal(b);
                }, 0 );
                int_total = api
                .column( 6 )
                .data()
                .reduce( function (a, b) {
                    return intVal(a) + intVal(b);
                }, 0 );
                others_total = api
                .column( 7 )
                .data()
                .reduce( function (a, b) {
                    return intVal(a) + intVal(b);
                }, 0 );
                total = api
                .column( 8 )
                .data()
                .reduce( function (a, b) {
                    return intVal(a) + intVal(b);
                }, 0 );
            // Total over this page
            thrift_pageTotal = api
                .column( 4, { page: 'current'} )
                .data()
                .reduce( function (a, b) {
                    return intVal(a) + intVal(b);
                }, 0 );
                pri_pageTotal = api
                .column( 5, { page: 'current'} )
                .data()
                .reduce( function (a, b) {
                    return intVal(a) + intVal(b);
                }, 0 );
                int_pageTotal = api
                .column( 6, { page: 'current'} )
                .data()
                .reduce( function (a, b) {
                    return intVal(a) + intVal(b);
                }, 0 );
                others_pageTotal = api
                .column( 7, { page: 'current'} )
                .data()
                .reduce( function (a, b) {
                    return intVal(a) + intVal(b);
                }, 0 );
                pageTotal = api
                .column( 8, { page: 'current'} )
                .data()
                .reduce( function (a, b) {
                    return intVal(a) + intVal(b);
                }, 0 );
                // Total over this page
           
 
            // Update footer
            $( api.column( 4).footer() ).html(
                +thrift_pageTotal +' ('+ thrift_total +' total)'
            );
            $( api.column( 5).footer() ).html(
                +pri_pageTotal +' ('+ pri_total +' total)'
            );
            $( api.column( 6).footer() ).html(
                +int_pageTotal +' ('+ int_total +' total)'
            );
            $( api.column( 7).footer() ).html(
                +others_pageTotal +' ('+ others_total +' total)'
            );
            $( api.column( 8).footer() ).html(
                +pageTotal +' ('+ total +' total)'
            );
        },
        "processing": true,
        "lengthMenu": [
            [100, 250, 500, 1000, -1],
            [100, 250, 500, 1000, "All"]
        ],
    });
</script>
            </script>