<div class="wrapper">
    <div class="container">
        <!-- Page-Title -->
        <div class="row">
            <div class="col-sm-12">
                
                <h4 class="page-title">Vendor Account (<?php echo $business[0]['business_name'];?>):</h4>
            </div>
        </div>

        <div class="row" id="divselection">
            <div class="col-sm-12">
                <div class="card-box ">
                    <div class="row hidden-print">
                        <div class="col-md-12">
                            
                        </div>
                        
                    </div>
                    <div class="row hidden-print">
                        <div class="col-md-2"></div>
                        <div class="col-lg-8">
                            <div class="row m-t-15">
                                <div class="col-md-12 col-xs-12">
                                    <div class="pull-left">
                                        <h3 class="logo invoice-logo"><?php if(isset($business)){ echo "<img src='".base_url().'assets/images/business/'.$business[0]['business_logo']."' alt='".$business[0]['business_name']."' class='img-responsive' />";} else {echo 'SalonPK';}?></h3>
                                    </div>
                                    <div class="btn-group pull-right hidden-print m-t-20">
                                        <form id="reportform" method="POST" action='<?php echo base_url();?>/accountreports_controller/vendor_account_report'>
                                            
                                            <div class="row">
                                                <div class="form-group">                                        
                                                    <label class="control-label col-md-4">Business Partner Type:</label>
                                                    <div class="col-lg-8">
                                                        <select class="form-control" onchange="partnertypechange();" id="business_partner_type" name="bptype">
                                                            <option value="0">Select</option>
                                                            <?php  foreach($bptypes as $bp){?>
                                                            <option <?php if(isset($bp['id_account_business_partner'])){if($bp['id_account_business_partner']== $business_partner_selected){echo " selected='selected' ";}}?> value="<?php echo $bp['id_account_business_partner'];?>"><?php echo $bp['account_business_partner'];?></option>
                                                            <?php } ?>
                                                        </select>
                                                    </div>
                                                </div>

                                                <div class="form-group">
                                                    <label class="control-label col-md-4">Business Partner Name </label>
                                                    <div class="col-md-8">
                                                        <select <?php if($business_partner_selected!=2){?>disabled="disabled" style="display: none;"<?php } ?> class='form-control'  name='business_partner' id='business_partner_supplier'>
                                                            <?php foreach($suppliers as $supplier){?>
                                                            <option value="<?php echo $supplier['id_business_supplier']; ?>" <?php if($business_partner_id_selected== $supplier['id_business_supplier']){echo "selected='selected'";} ?>><?php echo $supplier['id_business_supplier'].' - '.$supplier['supplier_name'].' - '.$supplier['contact_person']; ?> </option>
                                                            <?php } ?>
                                                        </select>                                            
                                                    </div>
                                                    <div class="col-md-8">
                                                        <select <?php if($business_partner_selected!=3){?>disabled="disabled" style="display: none;"<?php } ?> class='form-control' name='business_partner' id='business_partner_staff'>
                                                            <?php foreach($staffs as $staff){?>
                                                            <option value="<?php echo $staff['id_staff']; ?>" <?php if($business_partner_id_selected== $staff['id_staff']){echo "selected='selected'";} ?>><?php echo $staff['staff_fullname'].' - '.$staff['business_name'].' - '.$staff['staff_eid']; ?> </option>
                                                            <?php } ?>
                                                        </select>                                            
                                                    </div>
                                                    <div class="col-md-8">
                                                        <select <?php if($business_partner_selected!=4){?>disabled="disabled" style="display: none;"<?php } ?> class='form-control' name='business_partner' id='business_partner_franchise'>
                                                            <?php foreach($franchises as $franchise){?>
                                                            <option value="<?php echo $franchise['id_franchises']; ?>" <?php if($business_partner_id_selected== $franchise['id_franchises']){echo "selected='selected'";} ?>><?php echo $franchise['franchise_name']; ?> </option>
                                                            <?php } ?>
                                                        </select>                                            
                                                    </div>
                                                    <div class="col-md-8">
                                                        <select <?php if($business_partner_selected!=5){?>disabled="disabled" style="display: none;"<?php } ?> class='form-control' name='business_partner' id='business_partner_branch'>
                                                            <?php foreach($branches as $branch){?>
                                                            <option value="<?php echo $branch['id_business']; ?>" <?php if($business_partner_id_selected== $branch['id_business']){echo "selected='selected'";} ?>><?php echo $branch['business_name']; ?> </option>
                                                            <?php } ?>
                                                        </select>                                            
                                                    </div>
                                                    <div class="col-md-8">
                                                        <input <?php if($business_partner_selected > 1 ){?>disabled="disabled" style="display: none;"<?php } ?> class='form-control' name='business_partner' id='business_partner'/>
                                                        <input type='hidden' name='business_partner_id' id='business_partner_id'/>
                                                    </div>
                                                </div>
                                            </div>
                                            
                                            <div class="row m-t-15">
                                                <input type="hidden" name="csrf_test_name" id="statement_csrf" value=""/>
                                                
                                                <div class="col-md-3">
                                                    <select class='form-control' name='account_type' id='account_type'>
                                                        <?php foreach($accounttypes as $accounttype){ ?>
                                                        <option value="<?php echo $accounttype->account_head_id."|".$accounttype->account_head; ?>" <?php if($account_type_selected== $accounttype->account_head_id){echo "selected='selected'";} ?>><?php echo $accounttype->account_head; ?> </option>
                                                        <?php } ?>
                                                    </select>                                            
                                                </div>
                                                <div class="col-md-3" >
                                                    <input style="background-color: white;"  class="form-control datepicker" name="startdate" id="startdate" value="<?php if(isset($startdate)){echo $startdate;}?>"/>                                
                                                </div>
                                                <div class="col-md-3 ">
                                                    <input style="background-color: white;"  class="form-control datepicker" name="enddate" id="enddate" value="<?php if(isset($enddate)){echo $enddate;}?>"/>
                                                </div>
                                                <div class="col-md-3">
                                                    <input type="hidden" id="business_partner_name" name="business_partner_name" value="">
                                                    <button type='button' onclick="$('#statement_csrf').val($('#cook').val()); runreport();" class="btn btn-pink btn-bordred waves-effect w-md waves-light m-b-5">Run</button>
                                                </div>
                                                <div class="col-md-3" ></div>
                                            </div>
                                        </form>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                </div><!--Card box-->
            </div>
        </div> <!--End Div Selection-->
        
        
        <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', 'Sales Tax Details')" style="float:right">Export to Excel</a>
                        <?php } ?>   
                            <table id='sheet1' class='table table-bordered'>
                                <thead class="pink">
                                <tr>
                                    <th colspan="10">
                                    <?php foreach($accounttypes as $accounttype){ ?>
                                        <?php if($account_type_selected== $accounttype->account_head_id){echo strtoupper($accounttype->account_head); $type=$accounttype->account_head;} ?>
                                    <?php } ?>
                                    </th>
                                </tr>    
                                <tr>
                                    <th>Of:</th>
                                    <th colspan="9">                                    
                                        <?php if(isset($business_partner_name_selected)){ echo strtoupper($business_partner_name_selected);} ?>
                                    </th>
                                </tr>
                                <tr>
                                    <th>From:</th>
                                    <th><?php echo date_format(date_create($startdate), 'd-m-Y'); ?></th>
                                    <th>To:</th>
                                    <th><?php echo date_format(date_create($enddate), 'd-m-Y');; ?></th>
                                    <td colspan="8"></td>
                                </tr>
                                <tr>
                                    <th>Sr.No.</th>
                                    <th>Branch</th>
                                    <th>ID Voucher</th>
                                    <th>Business Partner Name</th>
                                    <th>Voucher Date</th>
                                    <th>Create Date</th>
                                    <th>Description</th>
                                    <th>Debit</th>
                                    <th>Credit</th>
                                    <th>Balance</th>
                                </tr>
                                </thead>
                                <tbody>
                                    
                                    <tr>
                                        <td colspan="6"></td>
                                        <td >Opening Balance</td>
                                        <td></td><td></td>
                                        <td style='text-align: right;'><?php if(null!==$opening){echo $opening->balance;}else{echo 0;} ?></td>                                        
                                    </tr>
                                <?php $x=1; $totaldebit=0;$totalcredit=0;$totalbalance= isset($opening->balance) ? $opening->balance : 0; foreach($data as $d){?>
                                <tr>
                                    <td><?php echo $x;?>
                                    <td><?php echo $d['Branch'];?></td>
                                    <td><?php echo $d['id_account_vouchers'];?></td>
                                    <td><?php echo $d['business_partner_name'];?></td>
                                    <td><?php echo $d['voucher_date'];?></td>
                                    <td><?php echo $d['create_date'];?></td>
                                    <td><?php echo $d['description'];?></td>
                                    <td style='text-align: right;'><?php echo $d['debit']; $totaldebit = $totaldebit + $d['debit'];?></td>
                                    <td style='text-align: right;'><?php echo $d['credit']; $totalcredit = $totalcredit + $d['credit'];?></td>
                                    <td style='text-align: right;'><?php if(strpos($type, 'Payable')) 
                                    {$totalbalance=($totalbalance+$d['credit'])-$d['debit'];} 
                                    else{ $totalbalance=($totalbalance+$d['debit'])-$d['credit'];}                                     
                                    echo $totalbalance;?></td>
                                    
                                   
                                </tr>
                                <?php $x++; } ?>
                                </tbody>
                                <tfoot>
                                    <tr>
                                        <th>Sr.No.</th>
                                        <th>Branch</th>
                                        <th>ID Voucher</th>
                                        <th>Business Partner Name</th>
                                        <th>Voucher Date</th>
                                        <th>Create Date</th>
                                        <th>Description</th>
                                        <th>Debit</th>
                                        <th>Credit</th>
                                        <th>Balance</th>
                                       
                                    </tr>
                                    <tr>
                                        <td colspan="7"></td><!-- comment -->
                                        <td style='text-align: right; font-weight: bold;'><?php echo number_format($totaldebit,2);?></td>
                                        <td style='text-align: right; font-weight: bold;'><?php echo number_format($totalcredit,2);?></td><!-- comment -->                                        
                                        <td style='text-align: right; font-weight: bold;'><?php echo number_format($totalbalance,2);?></td>
                                    </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'});

        
        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>        