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

        </h1>
        <ol class="breadcrumb">
            <li><a href="#">Receipt</a></li>
            <li class="active">Receipt Details</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">
                        Receipt Details
                    </div>
                    <div class="panel-body">
                        <link rel="stylesheet" href="https://cdn.datatables.net/buttons/1.5.6/css/buttons.bootstrap4.min.css">
                        <script src="https://cdn.datatables.net/buttons/1.5.6/js/dataTables.buttons.min.js"></script>
                        <script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js"></script>
                        <script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/pdfmake.min.js"></script>
                        <script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/vfs_fonts.js"></script>
                        <script src="https://cdn.datatables.net/buttons/1.5.6/js/buttons.html5.min.js"></script>
                        <script src="https://cdn.datatables.net/buttons/1.5.6/js/buttons.print.min.js"></script>
                        <script src="https://cdn.datatables.net/buttons/1.5.6/js/buttons.colVis.min.js"></script>
                        <div class="app-title">
                            <div>

                            </div>
                            <?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/sub_day_book" 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-2">
                                    <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>
                                    <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-2">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-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-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="receipt_detail" 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">Share</th>
                                                    <th class="text-center">Books & Forms</th>
                                                    <th class="text-center">Entry Fees</th>
                                                    <th class="text-center">Thrift</th>
                                                    <th class="text-center">Principal</th>
                                                    <th class="text-center">Interest</th>
                                                    <th class="text-center">P.Int</th>
                                                    <th class="text-center">Others</th>
                                                    <th class="text-center">Due By</th>
                                                    <th class="text-center">Due To</th>
                                                    <th class="text-center">Total</th>
                                                    <th class="text-center">Paid On</th>
                                                    <th class="text-center">Action</th>
                                                    <th class="text-center">View</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/sub_day_book" 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->share_amt ?>

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

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

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

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

                                                                <td>

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

                                                                </td>
                                                                <td>

                                                                    <?php echo $member->p_int + $member->old_p_int; ?>

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

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

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

                                                                </td>

                                                                <td>
                                                                    <?= $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 " id="">
                                                                                    <input type="date" 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)); ?>" />

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

                                                                </td>
                                                                <td class="text-center">
                                                                    <div id="edit_<?php echo $member->payment_id; ?>">
                                                                        <a onclick="editpodate('<?php echo $member->payment_id; ?>')" class='btn btn-primary btn-xs' style="color:white;" target='_blank'><span class="fa fa-edit"></span><span class="sr-only">View</span></a>
                                                                    </div>
                                                                    <div id="save_<?php echo $member->payment_id; ?>" style="display:none;">
                                                                        <a onclick="savepodate('<?php echo $member->payment_id; ?>')" class='btn btn-success btn-xs' target='_blank' style="color:white;"><span class="fa fa-check"></span><span class="sr-only">View</span></a>
                                                                    </div>
                                                                </td>
                                                                <td>
                                                                    <a href="<?php echo base_url(); ?>index.php/Demand/reciept?id=<?php echo $member->payment_id; ?>" style="color:white;" class='btn btn-primary btn-xs' target='_blank'><span class="fa fa-search"></span><span class="sr-only">View</span></a>
                                                                </td>
                                                            </tr>
                                                            <?php
                                                            $tot_ef += $member->ent_fees;
                                                            $tot_sh += $member->share_amt;
                                                            $tot_td += $member->thrift_amt +  $member->old_thrift;
                                                            $tot_books += $member->books;
                                                            $tot_loan_pri += $member->loan_pri_amt + $member->ex_os + $member->os_pay;
                                                            $tot_loan_int += $member->loan_int_amt;
                                                            $tot_p_int += +$member->p_int + $member->old_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_due_by += $member->due_by;
                                                            $tot_due_to += $member->due_to;
                                                            $tot_insurance += $member->insurance;
                                                            $tot_tot += $member->tot_pay;
                                                            ?>
                                                        <?php } ?>
                                                    <?php endforeach; ?>
                                            <tfoot>
                                                <tr>
                                                    <td>

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

                                                    </td>
                                                    <td>

                                                    </td>
                                                    <td>

                                                    </td>
                                                    <td>
                                                        <b> <?php echo $tot_sh; ?></b>
                                                    </td>
                                                    <td>
                                                        <b> <?php echo $tot_books; ?></b>
                                                    </td>
                                                    <td>
                                                        <b> <?php echo $tot_ef; ?></b>
                                                    </td>
                                                    <td>
                                                        <b> <?php echo $tot_td; ?></b>

                                                    </td>
                                                    <td>
                                                        <b> <?php echo $tot_loan_pri + $tot_last_pri; ?></b>

                                                    </td>

                                                    <td>

                                                        <b> <?php echo $tot_loan_int + $tot_last_int; ?></b>

                                                    </td>
                                                    <td>

                                                        <b> <?php echo $tot_p_int; ?></b>

                                                    </td>
                                                    </td>
                                                    <td>
                                                        <b> <?php echo $tot_others; ?></b>

                                                    </td>
                                                    <td>
                                                        <b> <?php echo $tot_due_by; ?></b>

                                                    </td>
                                                    <td>
                                                        <b> <?php echo $tot_due_to; ?></b>

                                                    </td>

                                                    <td>
                                                        <b> <?php echo $tot_tot; ?></b>

                                                    </td>
                                                    <td>

                                                    </td>
                                                    <td>

                                                    </td>
                                                    <td>

                                                    </td>
                                                </tr>
                                            </tfoot>
                                            </tbody>
                                        </table>

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


            </div>
        </div>
        <style>
            .pt-3,
            .py-3 {
                padding-top: 1rem !important;
                padding-left: 468px;
                margin-top: -65px;
            }

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

            .dt-buttons pt-3 pb-0 {
                margin-top: -56px;
            }
        </style>
        <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>
            $(document).ready(function() {
                $('#receipt_detail').DataTable({

                    "lengthMenu": [
                        [100, 250, 500, 1000, -1],
                        [100, 250, 500, 1000, "All"]
                    ],

                });
            });
            $('#import_form').on('submit', function(event) {
                event.preventDefault();
                $.ajax({
                    url: "<?php echo base_url(); ?>index.php/Demand/import",
                    method: "POST",
                    data: new FormData(this),
                    contentType: false,
                    cache: false,
                    processData: false,
                    success: function(data) {
                        $('#file').val('');
                        load_data();
                        alert(data);
                    }
                })
            });

            function disable_month_year() {
                var added_on = $("#added_on_id").val();
                if (added_on != "") {
                    // $("#division_id").val("");
                    // $("#section_id").val("");
                    $("#month_id").val("");
                    $("#year_id").val("");

                    $('#month_id').prop('disabled', 'disabled');
                    $('#year_id').prop('disabled', 'disabled');
                    // $('#division_id').prop('disabled', 'disabled');
                    // $('#section_id').prop('disabled', 'disabled');
                } else {
                    $('#month_id').prop('disabled', false);
                    $('#year_id').prop('disabled', false);
                    // $('#division_id').prop('disabled', false);
                    // $('#section_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() {

                $('#division_id').on('change', function() {
                    var division = this.value;
                    $.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);
                $('.datePicker')
                    .datepicker({
                        format: 'dd-mm-yyyy'
                    })
                    .on('changeDate', function(e) {
                        // Revalidate the date field
                        // $('#eventForm').formValidation('revalidateField', 'date');
                    });


                // $('#eventForm').formValidation({
                //     framework: 'bootstrap',
                //     icon: {
                //         valid: 'glyphicon glyphicon-ok',
                //         invalid: 'glyphicon glyphicon-remove',
                //         validating: 'glyphicon glyphicon-refresh'
                //     },
                //     fields: {
                //         name: {
                //             validators: {
                //                 notEmpty: {
                //                     message: 'The name is required'
                //                 }
                //             }
                //         },
                //         date: {
                //             validators: {
                //                 notEmpty: {
                //                     message: 'The date is required'
                //                 },
                //                 date: {
                //                     format: 'MM/DD/YYYY',
                //                     message: 'The date is not a valid'
                //                 }
                //             }
                //         }
                //     }
                // });
            });
        </script>