<div class="wrapper">
    <div class="container">
        <!-- Page-Title -->
        <div class="row">
            <div class="col-sm-12">
                
                <h4 class="page-title"><?php echo $title;?> Aging Report (<?php echo $business[0]['business_name'];?>):</h4>
            </div>
        </div>
        
        <div class="row">
            <div class="col-md-12">
                <div class="card-box">
                    <form  method="post" action="<?php if($title == 'Payable'){ echo base_url('payable_aging_report'); } else { echo base_url('receivable_aging_report');} ?>">
                        <div class="row m-b-10" >
                            <div class="col-md-4">
                                <label class="form-label">As on</label>
                            </div>
                            <div class="col-md-2">
                            </div>
                        </div>
                        <div class='row'>
                            <div class="col-md-4">
                                <input 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-md-2">
                                <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">Run</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
        
        
        <div class="row">
             <div class="col-sm-12">
                <div class="card-box print-area">
                    <div class='row'>
                        <div class="col-md-12">
                        <?php if(DOWNLOAD == true || $this->session->userdata('role') == 'Admin' || $this->session->userdata('role')=='Super User'){ ?>
                            <a class="btn btn-warning m-l-10" onclick="tableToExcel('sheet1', 'Againg Report Payables')" style="float:right">Export to Excel</a>
                        <?php } ?>    
                            <table id='sheet1' class='table table-bordered'>
                                <thead class="pink">
                                    <tr>
                                        <th>ID</th>
                                        <th>Business Partner</th>
                                        <th style="text-align: right;">< 30</th>
                                        <th style="text-align: right;">30 - 59</th>
                                        <th style="text-align: right;">60 - 89</th>
                                        <th style="text-align: right;">> 90</th>
                                        <th style="text-align: right;">Total</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php $grandtotal=0; foreach($aging as $a){ $total=0; ?>
                                    <tr>
                                    
                                    <form method="post" target="_blank" action="<?php echo base_url();?>general_ledger">
                                        <input type="hidden" name="csrf_test_name" value="">
                                        <input type="hidden" name="startdate" value="<?php echo date('Y-m-d');?>">
                                        <input type="hidden" name="enddate" value="<?php echo date('Y-m-t');?>">
                                        <input type="hidden" name="selected_account_sub_control" value="<?php echo date('Y-m-t');?>">
                                        <input type="hidden" name="from_accounts" value="<?php echo  $a->account_head_id;?>">
                                        <input type="hidden" name="to_accounts" value="<?php echo  $a->account_head_id;?>">
                                        <input type="hidden" name="bptype" value="<?php echo $a->business_partner;?>">
                                        <input type="hidden" name="business_partner" value="<?php echo $a->business_partner_name;?>">
                                        <input type="hidden" name="business_partner_id" value="<?php echo $a->business_partner_id;?>">
                                        <input type="hidden" name="cost_center_type" value="0">
                                        <input type="hidden" name="cost_center_name" value="">
                                        <input type="hidden" name="business_id" value="">
                                        
                                        <td><?php echo $a->business_partner_id;?></td>
                                        <td><button class="btn btn-link" type="submit"> <?php echo  $a->business_partner_name;?></button></td>
                                        <td style="text-align: right;"><?php echo  number_format($a->lessthen30,2);?></td>
                                        <td style="text-align: right;"><?php echo  number_format($a->thirtyfiftynine,2);?></td>
                                        <td style="text-align: right;"><?php echo  number_format($a->sixtyninty,2);?></td>
                                        <td style="text-align: right;"><?php echo  number_format($a->over90,2);?></td>
                                        <?php $total = $a->lessthen30 + $a->thirtyfiftynine +$a->sixtyninty+ $a->over90;
                                            $grandtotal=$grandtotal+$total;
                                        ?>
                                        <td style="text-align: right;"><strong><?php echo  number_format($total,2);?></strong></td>
                                    </form>
                                    </tr>
                                    <?php } ?>
                                </tbody>
                                <tfoot>
                                    <tr>
                                        <th style="text-align: right;" colspan="6">Total Outstanding Payables:</th>
                                        <th style="text-align: right;"><?php echo  number_format($grandtotal,2);?></th>
                                    </tr>
                                    
                                </tfoot>
                            </table>
                            
                            
                        </div>
                    </div>
                    
                    
                </div>
            </div>
        </div>
        
    </div>
        

<script>
    $(document).ready(function() {
       
       $("#startdate").datepicker({autoclose:true, format:'yyyy-mm-dd'});
        $("#enddate").datepicker({autoclose:true, format:'yyyy-mm-dd'});

        $('#sheet1').DataTable({
            <?php if(DOWNLOAD == true || $this->session->userdata('role') == 'Admin' || $this->session->userdata('role')=='Super User'){ ?>
                dom: "Bfrtlip",
                buttons: [{extend: "copy", className: "btn-sm"}, {extend: "csv", className: "btn-sm"}, {
                    extend: "excel",
                    className: "btn-sm"
                }, {extend: "pdf", className: "btn-sm"}, {extend: "print", className: "btn-sm"}],
            <?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;
            }
        });
    });
    
    
    var tableToExcel = (function () {
          //  application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
            var uri = 'data:application/vnd.ms-excel;base64,'
              , template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--><meta http-equiv="content-type" content="text/plain; charset=UTF-8"/></head><body><table>{table}</table></body></html>'
              , base64 = function (s) { return window.btoa(unescape(encodeURIComponent(s))) }
              , format = function (s, c) { return s.replace(/{(\w+)}/g, function (m, p) { return c[p]; }) }
            return function (table, name) {
                if (!table.nodeType) table = document.getElementById(table)
                var ctx = { worksheet: name || 'Worksheet', table: table.innerHTML }
                var link = document.createElement("a");
                
                var d = new Date();
                var datestring = ("0" + d.getDate()).slice(-2) + "-" + ("0"+(d.getMonth()+1)).slice(-2) + "-" +
                d.getFullYear() + " " + ("0" + d.getHours()).slice(-2) + ":" + ("0" + d.getMinutes()).slice(-2);
    
                link.download = ctx.worksheet + ' ' + datestring;
                link.href = uri + base64(format(template, ctx));
                link.click();
                //window.location.href = uri + base64(format(template, ctx))
            }
        })()

    function partnertypechange(){
        
        $("#business_partner").select2('destroy'); 
        $('#business_partner_franchise').select2('destroy');
        $('#business_partner_supplier').select2('destroy');
        $('#business_partner_staff').select2('destroy');
        
        
        var partnertypeid = $("#business_partner_type option:selected").val();
        //console.log(partnertypeid);
        if(partnertypeid=='1'){
            $("#business_partner_supplier").hide();
            $("#business_partner_staff").hide();
            $('#business_partner_franchise').hide();
            $("#business_partner_branch").hide();
            $("#business_partner").show();
            enable_customers();
        } else if (partnertypeid=='2'){
            $("#business_partner_supplier").attr('disabled',false);
            $("#business_partner_supplier").show();
            $('#business_partner_supplier').select2();
            $("#business_partner_staff").hide();
            $('#business_partner_franchise').hide();
            $("#business_partner").hide();
            $("#business_partner_branch").hide();
            //enable_suppliers();
        } else if (partnertypeid=='3'){
            $("#business_partner_staff").attr('disabled',false);
            $("#business_partner_staff").show();
            $('#business_partner_staff').select2();
            $("#business_partner_supplier").hide();
            $("#business_partner").hide();
            $('#business_partner_franchise').hide();
            $("#business_partner_branch").hide();
          //  enable_staff();
        } else if (partnertypeid=='4'){
            $("#business_partner_franchise").attr('disabled',false);
            $('#business_partner_franchise').show();
            $('#business_partner_franchise').select2();
            $("#business_partner_supplier").hide();
            $("#business_partner_staff").hide();
            $("#business_partner").hide();
            $("#business_partner_branch").hide();
          //  enable_franchise();
        } else if (partnertypeid=='5'){
            $("#business_partner_branch").attr('disabled',false);
            $("#business_partner_branch").show();
            $('#business_partner_franchise').hide();
            $("#business_partner_supplier").hide();
            $("#business_partner_staff").hide();
            $("#business_partner").hide();
          //  enable_franchise();
        }else {
            $("#business_partner_staff").attr('disabled',true);
            $("#business_partner_supplier").attr('disabled',true);
            $("#business_partner").attr('disabled',false);             
            $("#business_partner_staff").hide();
            $("#business_partner_supplier").hide();
            $('#business_partner_franchise').hide();
            $("#business_partner_branch").hide();
            $("#business_partner").show();
             $("#business_partner").attr('disabled',false);
            
        }
        
    }
    
    
    function enable_customers(){
       $("#business_partner").select2({
            ajax: {
              url: '<?php echo base_url();?>customer_controller/searchnameforco',
              dataType: 'json',
              quietMillis: 1000,
              data: function (term, page) {
                  
                return {
                    customername: term, // search term
                    page_limit: 30, // page size
                    page: page // page number
                };
              },
              results: function (data, page) {
                 
                    var more = (page * 30) < data.length;
                    return {results: data, more: more};
                }
              },
            escapeMarkup: function (m) { return m; }, // let our custom formatter work
            minimumInputLength: 3,
            formatResult: function (option) {
               return option.customer_name+" | "+option.customer_cell ;
            },
            formatSelection: function (option) {
                
                return option.customer_name+" | "+option.customer_cell;
            }
          });
         
    }
    
    
    function runreport() {
        
        if($("#startdate").val()=='' || $("#enddate").val()==''){
            
            swal({
                title: 'Select a Valid Date Range!',
                type: 'info'
            });
            
            return false;
            
        }
        
        
         var business_partner;
            
            if($("#business_partner_type").val()==1){
                business_partner = $("#business_partner").select2('data');
                $("#business_partner_id").val(business_partner.id_customers);
                
            } else if ($("#business_partner_type").val()==2){
               $("#business_partner_id").val($("#business_partner_supplier option:selected").val());
               business_partner = $("#business_partner_supplier option:selected").text();
            } else if ($("#business_partner_type").val()==3){
                $("#business_partner_id").val($("#business_partner_staff option:selected").val());
                business_partner = $("#business_partner_staff option:selected").text();
            }  else if ($("#business_partner_type").val()==4){
                $("#business_partner_id").val($("#business_partner_franchise option:selected").val());
                business_partner = $("#business_partner_franchise option:selected").text();
            }  else if ($("#business_partner_type").val()==5){
                $("#business_partner_id").val($("#business_partner_branch option:selected").val());
                business_partner = $("#business_partner_branch option:selected").text();
            }else{
                return false;
            }
            $("#business_partner_name").val(business_partner);
       
        voucher_list();

    }
    
    function voucher_list(){
       
        $("#reportform").submit();
        
            
    }
    
</script>        