<style>
    /* enable absolute positioning */
    .inner-addon { 
        position: relative; 
    }

    /* style icon */
    .inner-addon .glyphicon {
        position: absolute;
        padding: 10px;
        pointer-events: none;
    }

    /* align icon */
    .left-addon .glyphicon  { left:  0px;}
    .right-addon .glyphicon { right: 0px;}

    /* add padding  */
    .left-addon input  { padding-left:  30px; }
    .right-addon input { padding-right: 30px; }

    .loader {
        position: fixed;
        left: 0px;
        top: 0px;
        width: 100%;
        height: 100%;
        z-index: 9999;
        background: url('<?php echo base_url(); ?>assets/images/page-loader-1.gif') 50% 50% no-repeat ;
        display: none;
    }


    table.dataTable tr.group td{font-weight:bold;background-color:#e0e0e0}

    table.dataTable tr.group,
    tr.group:hover {
        background-color: #ddd !important;
    }

    tr.strikethrough > td {text-decoration:line-through !important; color:red;}   
    tr.unposted > td {color:red;}
</style>

<div class="wrapper">
    <div class="container">
        <!-- Page-Title -->
        <div class="row">
            <div class="col-sm-12">
                <div class="btn-group pull-right m-t-15">
                    <form method="get" action="<?php echo base_url(); ?>accounting_controller/accountvoucheradd">
                        <input type="hidden" name="voucher_add_csrf" id="voucher_add_csrf" value=""/>
                        <button type="submit" onclick="$('#voucher_add_csrf').val($('#cook').val());" class="btn btn-custom waves-effect waves-light" >Add Account Voucher <span class="m-l-5"><i class="fa fa-plus"></i></span></button>
                    </form>
                </div>
                <h4 class="page-title">Account Vouchers (<?php echo $business[0]['business_name']; ?>):</h4>
            </div>
        </div>

        <div class="row" id="divselection">
            <div class="col-sm-12">
                <div class="card-box ">
                    <h4 class="header-title m-t-0 m-b-10">Filters:</h4>
                    <div class="row m-b-10" id="div-filters">
                        
                        <div class="row" id="normalfilters">
                            <form method="post" action="<?php echo base_url(); ?>account_vouchers">
                                <div class="col-lg-12">
                                    <div class="row">
                                        <div class="col-lg-4">
                                            <label class="control-label">Date Range</label>
                                        </div>
                                        <div class="col-lg-2">
                                            <label class="control-label">Voucher Type</label>
                                        </div>
                                        <div class="col-lg-4">
                                            <label class="control-label">Search By:</label>
                                        </div>    
                                    </div>
                                </div>
                                
                                <input type="hidden" name="csrf_test_name" id="accounting_voucher_csrf" value=""/>
                                <div class="col-lg-2">
                                    <input onchange="checkdaterange();" style="background-color: white;" readonly class="form-control" type="text" name="startdate" id="startdate" value="<?php
                                    if (isset($startdate)) {
                                        echo $startdate;
                                    }
                                    ?>"/>                                
                                </div>
                                <div class="col-lg-2">
                                    <input onchange="checkdaterange();" style="background-color: white;" readonly class="form-control" type="text" name="enddate" id="enddate" value="<?php
                                    if (isset($enddate)) {
                                        echo $enddate;
                                    }
                                    ?>"/>
                                </div>
                                <div class="col-lg-2">
                                    <select class="form-control" id="voucher_type" name="voucher_type">
                                        <option value="">All</option>
                                        <?php foreach ($voucher_types as $vt) { ?>
                                            <option <?php
                                            if (isset($voucher_type)) {
                                                if ($voucher_type == $vt['id_account_voucher_type']) {
                                                    echo " selected='selected' ";
                                                }
                                            }
                                            ?> value="<?php echo $vt['id_account_voucher_type']; ?>"><?php echo $vt['account_voucher_type']; ?></option>
                                            <?php } ?>
                                    </select>
                                    <input type="hidden" name="dateselected" id="dateselected" value="">
                                </div>
                                <div class="col-lg-1">
                                    <select class="form-control" id="auto_voucher" name="auto_voucher">
                                        <option  <?php
                                        if (isset($auto_voucher)) {
                                            if ($auto_voucher == 'all') {
                                                echo " selected='selected' ";
                                            }
                                        }
                                        ?> value="all">All</option>
                                        <option <?php
                                        if (isset($auto_voucher)) {
                                            if ($auto_voucher == 'no') {
                                                echo " selected='selected' ";
                                            }
                                        }
                                        ?> value="no">User Created</option>
                                        <option <?php
                                        if (isset($auto_voucher)) {
                                            if ($auto_voucher == 'yes') {
                                                echo " selected='selected' ";
                                            }
                                        }
                                        ?> value="yes">Auto Vouchers</option>
                                    </select>
                                    <input type="hidden" name="dateselected" id="dateselected" value="">
                                </div>
                                <?php if ($business[0]['ho'] == 'Yes') { ?>
                                    <div class="col-lg-2">
                                        <select class="form-control" id="business_id" name="business_id">
                                            <option  <?php
                                            if (isset($b)) {
                                                if ($b['id_business'] == $businessselected) {
                                                    echo " selected='selected' ";
                                                }
                                            }
                                            ?> value="">All</option>
                                                <?php foreach ($businesses as $b) { ?>
                                                <option  <?php
                                                if (isset($b)) {
                                                    if ($b['id_business'] == $businessselected) {
                                                        echo " selected='selected' ";
                                                    }
                                                }
                                                ?> value="<?php echo $b['id_business']; ?>"><?php echo $b['business_name']; ?></option>
                                                <?php } ?>

                                        </select>
                                        <input type="hidden" name="dateselected" id="businessselected" value="">
                                    </div>
                                <?php } ?>

                                <div class="col-lg-1"><button type="submit" onclick="$('#accounting_voucher_csrf').val($('#cook').val()); $('#dateselected').val('Voucher');" class="btn btn-pink btn-bordred waves-effect w-md waves-light m-b-5">Voucher Date</button></div>
                                <div class="col-lg-1"><button type="submit" onclick="$('#accounting_voucher_csrf').val($('#cook').val()); $('#dateselected').val('Create');" class="btn btn-pink btn-bordred waves-effect w-md waves-light m-b-5">Create Date</button></div>
                                <!--<div class="col-lg-2"><i id="cog" class="fa fa-spin fa-cog" style="display:none; font-size:26px;width: auto;margin-right: 10px;"></i></div>-->
                            </form>
                            


                        </div>

                        <div class="row hidden" id="advance_filters">
                            <form method="post" action="<?php echo base_url(); ?>account_vouchers_ref">
                                <div class="col-md-4">
                                    <input type="hidden" name="csrf_test_name" id="ref_voucher_csrf" value=""/>
                                    <label class="form-label">Select Reference Type:</label>
                                    <select id="reftype" name="reftype" class="form-control">
                                        <option value="id_account_vouchers">Account Voucher ID</option>
                                        <option value="account_voucher_number">Account Voucher Number</option>
                                        <option value="invoice_id">Invoice ID</option>
                                        <option value="visit_id">Customer Visit ID</option>
                                        <option value="visit_advance_id">Visit Advance ID</option>
                                        <option value="business_partner_id">Customer ID</option>
                                        <option value="cashregister_id">Cash Register ID</option>
                                        <option value="staffpayment_id">Staff Payment Note ID</option>
                                        <option value="purchase_order_id">Purchase Order ID</option>
                                        <option value="grn_id">GRN ID</option>
                                        <option value="dispatch_id">Dispatch Note ID</option>
                                        <option value="return_note_id">Return Note ID</option>
                                        <option value="adjustment_id">Adjustment Note ID</option>
                                        <option value="gtn_id">Goods Transfer Note ID</option>
                                        <option value="order_voucher_id">Gift Voucher ID</option>
                                        <option value="franchise_order_id">Franchise Order ID</option>
                                        <option value="program_invoice_id">Program Invoice ID</option>
                                    </select>
                                </div>
                                <div class="col-md-4">
                                    <label class="form-label">Enter Reference ID</label>
                                    <input class="form-control numeric" id="refid" name="refid" required="required">
                                </div>
                                <div class="col-md-4">
                                    <div class="col-lg-1"><button type="submit" onclick="$('#ref_voucher_csrf').val($('#cook').val()); $('#dateselected').val('Voucher');" class="btn btn-pink btn-bordred waves-effect w-md waves-light m-t-20">Run</button></div>
                                </div>    
                            </form>
                        </div>
                        <div class="row">
                            <div class="col-md-12 m-t-20">
                                <button onclick="showadvancefilter();" class="btn btn-sm btn-warning" ><i class="fa fa-black-tie" ></i> Advance Filters</button>
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-12">
                            <h4 class="header-title m-t-30 m-b-10">Vouchers:</h4>
                            <table id="datatable-buttons" class="table table-bordered dt-responsive nowrap" cellspacing="0" width="100%">
                                <thead>
                                    <tr>
                                        <th>Branch</th>
                                        <th>Voucher ID</th>
                                        <th>Desc.</th>
                                        <th>Type</th>
                                        <th>Business Partner</th>
                                        <th>Voucher Date</th>

                                        <th>Created By</th>
                                        <th>Created On</th>
                                        <th>Cost Center</th>
                                        <th>Account#</th>
                                        <th>Account</th>
                                        <th>Debit</th>
                                        <th>Credit</th>
                                        <th>Status</th>
                                        <th>Delete</th>
                                        <th class="">Edit</th>
                                        <th>Print</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    $last = "";
                                    $debit = 0;
                                    $credit = 0;
                                    foreach ($account_vouchers as $account_head) {
                                        ?>
                                        <tr <?php if ($account_head['voucher_status'] !== 'Active') { if($account_head['voucher_status'] !== 'UnPosted'){?> class="strikethrough"<?php }else{ ?> class="unposted" <?php }} ?>>
                                            <?php if ($last !== $account_head['id_account_vouchers']) { ?>
                                                <td style="border-top:1px solid #000"><?php echo $account_head['business_name']; ?></td>
                                                <td style="border-top:1px solid #000"><?php echo $account_head['id_account_vouchers']; ?></td>
                                                <td style="border-top:1px solid #000"><?php echo $account_head['description']; ?></td>
                                                <td style="border-top:1px solid #000"><?php echo $account_head['account_voucher_type']; ?></td>
                                                <td style="border-top:1px solid #000"><?php echo $account_head['business_partner_name']; ?></td>
                                                <td style="border-top:1px solid #000"><?php echo $account_head['voucher_date']; ?></td>

                                                <td style="border-top:1px solid #000"><?php echo $account_head['created_by']; ?></td>
                                                <td style="border-top:1px solid #000"><?php echo $account_head['created_on']; ?></td>
                                                <td style="border-top:1px solid #000"><?php echo $account_head['cost_center_name']; ?></td>
                                                <td style="border-top:1px solid #000" class="text-info"><?php echo $account_head['account_head_number']; ?></td>
                                                <td style="border-top:1px solid #000" class="text-info"><?php echo $account_head['account_head']; ?></td>
                                                <td style="border-top:1px solid #000; text-align: right; font-weight: bold;" class="text-success"><?php
                                                    echo $account_head['debit'];
                                                    if ($account_head['voucher_status'] == 'Active') {
                                                        $debit = $debit + $account_head['debit'];
                                                    }
                                                    ?></td>
                                                <td style="border-top:1px solid #000; text-align: right; font-weight: bold;" class="text-success"><?php
                                                    echo $account_head['credit'];
                                                    if ($account_head['voucher_status'] == 'Active') {
                                                        $credit = $credit + $account_head['credit'];
                                                    }
                                                    ?></td>
                                                
                                                <td style="border-top:1px solid #000" class='noprint'> 
                                                    <?php if($account_head['voucher_status']=="UnPosted"){ echo $account_head['voucher_status']; ?>
                                                        <a  href="<?php echo base_url() . 'accounting_controller/post_voucher/' . $account_head['id_account_vouchers'].'/'.$startdate.'/'.$enddate; ?>" class="btn btn-success waves-effect waves-light m-r-4"><i class="fa fa-calendar-check-o"></i></a>
                                                    <?php } else { echo $account_head['voucher_status']; } ?>    
                                                </td>
                                                <td style="border-top:1px solid #000" class='noprint'> 
                                                    
                                                    
                                                    <?php if ($account_head['auto_voucher'] == 'No' || $this->session->userdata('role') == "Super User") { ?>
                                                        <form id="removeform_<?php echo $account_head['id_account_vouchers']; ?>" method="post" action="<?php echo base_url(); ?>accounting_controller/cancel_account_voucher">
                                                            <input type="hidden" name="id_account_vouchers" id="account_voucher_<?php echo $account_head['id_account_vouchers']; ?>" value="<?php echo $account_head['id_account_vouchers']; ?>">
                                                            <input type="hidden" name="csrf_test_name" id="account_voucher_delete_csrf_<?php echo $account_head['id_account_vouchers']; ?>" value=""/>
                                                            <button id="voucher_acount_id" type="button" onclick="account_voucher_remove(<?php echo $account_head['id_account_vouchers']; ?>);" class="btn btn-icon waves-effect waves-light btn-danger m-b-5"><i class="fa fa-trash"></i></button></td>

                                                    </form>
                                                    <?php
                                                } else {
                                                    echo "Auto";
                                                }
                                                ?>
                                                </td>
                                                <td style="border-top:1px solid #000" class='noprint'>
                                                    <?php if ($account_head['auto_voucher'] == 'No' || $this->session->userdata('role') == "Accountant" || $this->session->userdata('role') == "Super User") { ?>
                                                        <form method="post" action="<?php echo base_url(); ?>accounting_controller/accountvoucheredit">
                                                            <input type="hidden" name="id_account_vouchers" id="edit_account_voucher_<?php echo $account_head['id_account_vouchers']; ?>" value="<?php echo $account_head['id_account_vouchers']; ?>">
                                                            <input type="hidden" name="csrf_test_name" id="account_voucher_edit_csrf_<?php echo $account_head['id_account_vouchers']; ?>" value=""/>

                                                            <button type="submit" onclick="$('#account_voucher_edit_csrf_<?php echo $account_head['id_account_vouchers']; ?>').val($('#cook').val());" class="btn btn-icon waves-effect waves-light btn-info m-b-5"> <i class="fa fa-keyboard-o"></i> </button>  
                                                        </form>
                                                        <?php
                                                    } else {
                                                        echo "Auto";
                                                    }
                                                    ?>
                                                </td>
                                                <td style="border-top:1px solid #000" class='noprint'>
                                                    <a target="_blank" href="<?php echo base_url() . 'print_voucher/' . $account_head['id_account_vouchers']; ?>" class="btn btn-default waves-effect waves-light"><i class="fa fa-print"></i></a>
                                                </td>
                                            <?php } else { ?>
                                                <td></td>
                                                <td></td>
                                                <td></td>
                                                <td></td>
                                                <td></td>
                                                <td></td>
                                                <td></td>
                                                <td></td>
                                                <td ><?php echo $account_head['cost_center_name']; ?></td>
                                                <td class="text-info"><?php echo $account_head['account_head_number']; ?></td>
                                                <td class="text-info"><?php echo $account_head['account_head']; ?></td>
                                                <td class="text-success" style="text-align: right; font-weight: bold;"><?php
                                                    echo $account_head['debit'];
                                                    if ($account_head['voucher_status'] == 'Active') {
                                                        $debit = $debit + $account_head['debit'];
                                                    }
                                                    ?></td>
                                                <td class="text-success" style="text-align: right; font-weight: bold;"><?php
                                                    echo $account_head['credit'];
                                                    if ($account_head['voucher_status'] == 'Active') {
                                                        $credit = $credit + $account_head['credit'];
                                                    }
                                                    ?></td>
                                                <td></td>
                                                <td class=""></td>
                                                <td></td>
                                                <td></td>
                                            <?php } $last = $account_head['id_account_vouchers']; ?>
                                        </tr>
                                    <?php } ?>
                                </tbody>
                                <tfoot>
                                <td colspan="11" style="text-align: right; font-weight: bold;">Total</td>
                                <td style="text-align: right; font-weight: bold;"><?php echo $debit; ?></td>
                                <td style="text-align: right; font-weight: bold;"><?php echo $credit; ?></td>
                                <td></td>
                                <td></td>
                                <td></td>
                                </tfoot>
                            </table>

                        </div>
                    </div>

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

    <script>
        var startdate = '';
        var enddate = '';

        $(document).ready(function () {

            $("#reftype").select2();
            var mindate=new Date(2022,01,01);
            //console.log(mindate);
            $("#startdate").datepicker({autoclose: true, format: 'yyyy-mm-dd'});
            $("#enddate").datepicker({autoclose: true, format: 'yyyy-mm-dd'});


            $('#datatable-buttons').DataTable({
                lengthMenu: [[25, 50, -1], [25, 50, "All"]],
                stateSave: true,
                ordering: false,
                <?php if(DOWNLOAD == true || $this->session->userdata('role') == 'Admin' || $this->session->userdata('role')=='Super User'){ ?>
                    dom: "Bfrtlip",                
                    buttons: [{extend: "copy", className: "btn-sm btn-custom btn-trans"}, {extend: "csv", className: "btn-sm btn-pink btn-trans"}, {
                        extend: "excel",
                        className: "btn-sm btn-warning btn-trans"
                    }, {extend: "pdf", className: "btn-sm btn-purple btn-trans"}],
                <?php } else { ?>
                        dom: "frtlip",
                <?php } ?>  
                responsive: !0
            });

            toastr.options = {
                "closeButton": true,
                "debug": true,
                "newestOnTop": true,
                "progressBar": true,
                "positionClass": "toast-bottom-center",
                "preventDuplicates": true,
                "onclick": null,
                "showDuration": "300",
                "hideDuration": "1000",
                "timeOut": "5000",
                "extendedTimeOut": "1000",
                "showEasing": "swing",
                "hideEasing": "linear",
                "showMethod": "fadeIn",
                "hideMethod": "fadeOut"
            }

            $(".numeric").keypress(function (e) {
                //if the letter is not digit then display error and don't type anything
                if (e.which != 13 && e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
                    return false;
                }
            });
        });


        function account_voucher_remove(id) {

            swal({
                title: 'Are You Sure',
                text: "This will remove the voucher permenantly",
                type: 'warning',
                showCancelButton: true,
                confirmButtonColor: '#ff5b5b !important',
                cancelButtonColor: '#d33',
                cancelButtonText: 'No',
                confirmButtonText: 'Yes'

            }, function (isConfirm) {
                if (isConfirm) {
                    var rf = "#removeform_" + id;
                    $(rf).submit();
                }
            })



        }

        function showadvancefilter() {

            if ($("#normalfilters").hasClass("hidden") == false) {
                $("#normalfilters").addClass('hidden');
                $("#advance_filters").removeClass('hidden');
            } else {
                $("#normalfilters").removeClass('hidden');
                $("#advance_filters").addClass('hidden');
            }

        }


        function checkdaterange(){

            const startdate = new Date($("#startdate").val());
            const enddate = new Date($("#enddate").val());
            
            if($("#enddate").val()==''){
                var d = new Date();
                d.setDate(startdate.getDate() + 1);           
                var mMonth = ((d.getMonth()+1) <10 ? '0' : '') + (d.getMonth()+1);
                var mDay = (d.getDate() < 10 ? '0' : '') + d.getDate();
                $("#enddate").val(d.getFullYear() +'-'+ mMonth + '-' + mDay); 
            }else if($("#startdate").val()==''){
                var d = new Date();
                d.setDate(enddate.getDate() - 1);           
                var mMonth = ((d.getMonth()+1) <10 ? '0' : '') + (d.getMonth()+1);
                var mDay = (d.getDate() < 10 ? '0' : '') + d.getDate();
                $("#startdate").val(d.getFullYear() +'-'+ mMonth + '-' + mDay);                 
            }
            
            if($("#startdate").val()!='' && $("#enddate").val()!=''){  
                var selecteddays = date_diff_indays(startdate, enddate);
                console.log(selecteddays);
                if(selecteddays < 0){
                    swal({
                        title: 'Invalid Date Selected',
                        text: "End Date cannot be less then you start date",
                        type: 'warning',
                        showCancelButton: false,
                        confirmButtonColor: '#ff5b5b !important'
                    });
                } else if(selecteddays > 31){
                    swal({
                        title: 'Date Range Is Too Big',
                        text: "It is advisable to use date range of maximum 31 days at a time",
                        type: 'warning',
                        showCancelButton: false,
                        confirmButtonColor: '#ff5b5b !important'
                    });
                    
                    var e = new Date(startdate);
                    e.setDate(startdate.getDate() + 31);
                    var eMonth = ((e.getMonth()+1) <10 ? '0' : '') + (e.getMonth()+1);
                    var eDay = (e.getDate() < 10 ? '0' : '') + e.getDate();
                    $("#enddate").val(e.getFullYear() +'-'+ eMonth + '-' + eDay); 
                    
                }
            }
            
        }
        
        
        var date_diff_indays = function(date1, date2) {
            dt1 = new Date(date1);
            dt2 = new Date(date2);
            return Math.floor((Date.UTC(dt2.getFullYear(), dt2.getMonth(), dt2.getDate()) - Date.UTC(dt1.getFullYear(), dt1.getMonth(), dt1.getDate()) ) /(1000 * 60 * 60 * 24));
        }
        

    </script>