<style>

    .table tr .bifurcate >td{
        border-top:2px solid #eee;
    }
</style>
<div class="wrapper">
    <div class="container-full">
        <div class="row m-t-20">
            <div class="col-md-12">
                <div class="panel panel-default m-t-20">
                    <div class="panel-body">
                        <div class="row">
                            <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 'SkedWise';
                                    }
                                    ?></h3>
                            </div>
                            <div class="pull-right m-t-20" >
                                <div class="col-md-9 hidden-print m-t-20">
                                    <?php  if($this->session->userdata('show_previous') != 'N' || $this->session->userdata('role') == 'Admin' || $this->session->userdata('role')=='Super User'){?>
                                    <form id="Sheet-Form" method="POST" action='<?php echo base_url(); ?>service-by-staff'>
                                        <input type="hidden" name="csrf_test_name" id="daily_sheet_csrf" value=""/>
                                        <div class="row">
                                            <div class="col-md-12">
                                                <div class="col-md-5">
                                                    <label>For</label>

                                                </div>
                                                
                                                <div class="col-md-2">
                                                    <label>Run</label>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="row">
                                            <div class="col-md-12">
                                                <div class="col-md-5">

                                                    <input type="date" autocomplete="off" class="form-control inline m-r-5" name="fromdate" id="datepicker-autoclose1" value="<?php echo $fromdate; ?>">
                                                </div>
                                                
                                                <div class="col-md-2">
                                                    <button type='submit' onclick="$('#daily_sheet_csrf').val($('#cook').val());" class="btn btn-pink btn-bordred waves-effect w-md waves-light m-b-5">Run</button>
                                                </div>
                                            </div>
                                        </div>
                                    </form>
                                    <?php } ?>
                                </div>
                                <div class="col-md-3 hidden-print m-t-20">
                                    <div class="row">
                                        <div class="col-md-12"><label>&nbsp;</label></div>
                                    </div>
                                    <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', 'StaffDaySheet')" style="float:right">Export to Excel</a>
                                            <a href="javascript:window.print()" class="btn btn-inverse waves-effect waves-light hidden-print pull-right" id="btnprint"><i class="fa fa-print"></i></a>
                                        <?php } ?>
                                        </div>
                                    </div>
                                    <!--<a class="btn btn-warning m-l-10" onclick="tableToExcel('sheet1', 'Marketing Sheet')" style="float:right">Export to Excel</a>-->
<!--                                    <a class="btn btn-warning m-l-10" href="<?php //echo base_url('marketing_controller/marketing_excell'.'/'.$date);                                                      ?>" style="float:right">Export to Excel</a>
                                    <a href="javascript:window.print()" class="btn btn-inverse waves-effect waves-light hidden-print pull-right" id="btnprint"><i class="fa fa-print"></i></a>-->
                                </div>
                            </div>
                            <div class="clearfix"></div>
                        </div>
                        <!-- end row -->

                        <div class="row">
                            <table class="table m-t-10 table-bordered"  id='sheet1'>
                                <tr>
                                    <td colspan="5"><strong>Day's Business By Staff </td>
                                </tr>
                                <tr>
                                    <td colspan="5"><strong><u> For: <span id='namedate'> <?php echo isset($fromdate) && !empty($fromdate) ? date('D d-M-Y', strtotime($fromdate)) : date('D d-M-Y'); ?> </u></strong></span></td>
                                </tr>
                                <tr>
                                    <td colspan="5">
                                        <table class="table table-bordered"  id='datatable-buttons1'>
                                            <thead>
                                                <tr>
                                                    <th>Staff</th>
                                                    <th>Invoice</th>
                                                    <th>Customer</th>
                                                    <th>Service / Product</th>
                                                    <th style="text-align:right;">Price</th>                                                    
                                                    <th style="text-align:right;">Discount</th>
                                                    <th style="text-align:right;">Addition</th>
                                                    <th style="text-align:right;">Discounted Price</th>
                                                    <th style="text-align:right;">Tax</th>
                                                    <th style="text-align:right;">Collection</th>
                                                    <th style="text-align:right;">Commission</th>
                                                    <th>Comments</th>
                                                    <th class="hidden-print"></th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                
                                                <?php
                                                    $grand_total_sold = 0;
                                                    $grand_total_revenue = 0;
                                                    $grand_total_commission = 0;
                                                if (isset($serviceByStaff) && !empty($serviceByStaff)) {
                                                    
                                                    $staff_name = "";
                                                     $invoice= 0;
                                                    foreach ($serviceByStaff as $staffdata){
                                                        foreach ($staffdata['services'] as $row) {?>

                                                            <tr class="<?php if($invoice !==  $row['invoice_id']){echo "bifurcate";}?>">
                                                                <td <?php if($staff_name !==  $row['staff_name']){echo "style='border-top:2px solid #000;'";}?>>
                                                                    <?php echo $row['staff_name']; ?>
                                                                </td>
                                                                <td <?php if($staff_name !==  $row['staff_name']){echo "style='border-top:2px solid #000;'";}?>>
                                                                   <a target="_blank" href="<?php echo base_url();?>existinginvoice/<?php echo ($row['invoice_id']); ?>"><?php echo ($row['invoice_id']); ?></a>
                                                                </td>
                                                                <td <?php if($staff_name !==  $row['staff_name']){echo "style='border-top:2px solid #000;'";}?>>
                                                                    <a target="blank" href="<?php echo base_url().'checkcustomerstaffhistory/'.$row['customer_id'].'/'.$row['staff_id']; ?>"><?php echo $row['customer_name']; ?></a>
                                                                </td>
                                                                <td <?php if($staff_name !==  $row['staff_name']){echo "style='border-top:2px solid #000;'";}?>>
                                                                <?php echo $row['service_name']; ?>
                                                                </td>
                                                                <td style="text-align:right; <?php if($staff_name !==  $row['staff_name']){echo "border-top:2px solid #000;";}?>">
                                                                <?php echo $row['price']; ?>
                                                                </td>
                                                                <td style="text-align:right; <?php if($staff_name !==  $row['staff_name']){echo "border-top:2px solid #000;";}?>">
                                                                <?php echo $row['discount']; ?>
                                                                </td>
                                                                <td style="text-align:right; <?php if($staff_name !==  $row['staff_name']){echo "border-top:2px solid #000;";}?>">
                                                                <?php echo $row['service_addition']; ?>
                                                                </td>
                                                                <td style="text-align:right; <?php if($staff_name !==  $row['staff_name']){echo "border-top:2px solid #000;";}?>">
                                                                <?php echo $row['discounted_price']; $grand_total_sold=$grand_total_sold+$row['discounted_price']; ?>
                                                                </td> 
                                                                <td style="text-align:right; <?php if($staff_name !==  $row['staff_name']){echo "border-top:2px solid #000;";}?>">
                                                                <?php echo $row['taxes']; ?>
                                                                </td>
                                                                <td style="text-align:right; <?php if($staff_name !==  $row['staff_name']){echo "border-top:2px solid #000;";}?>">
                                                                <?php echo $row['staff_paid']; $grand_total_revenue=$grand_total_revenue+ $row['staff_paid'];?>
                                                                </td>  
                                                                <td style="text-align:right; <?php if($staff_name !==  $row['staff_name']){echo "border-top:2px solid #000;";}?>">
                                                                <?php echo $row['commission']; $grand_total_commission=$grand_total_commission+ $row['commission'];?>    
                                                                </td>
                                                                <td <?php if($staff_name !==  $row['staff_name']){echo "style='border-top:2px solid #000;'";}?>></td>
                                                                <td class="hidden-print" <?php if($staff_name !==  $row['staff_name']){echo "style='border-top:2px solid #000;'";}?>>
                                                                    
                                                                </td>
                                                            </tr>
                                                            <?php $staff_name=$row['staff_name'];$invoice= $row['invoice_id'];}?>
                                                            
                                                             <?php $pstaff_name=''; $pinvoice=0; foreach($staffdata['products'] as $products){ 
                                                                if(  $products['staff_name']!==''){?>                                                    
                                                                    <tr class="<?php if($invoice !==  $products['invoice_id']){echo "bifurcate";}?>">
                                                                        <td >
                                                                            <?php echo $products['staff_name']; ?>
                                                                        </td>
                                                                        <td >
                                                                          <?php if($pinvoice !==  $products['invoice_id']){?> <a target="_blank" href="<?php echo base_url();?>existingorderinvoice/<?php echo ($products['invoice_id']); ?>"><?php echo ($products['invoice_id']); ?></a><?php } ?>
                                                                        </td>
                                                                        <td >
                                                                        <?php if($pinvoice !==  $products['invoice_id']){ echo $products['customer_name']; }?>
                                                                        </td>
                                                                        <td >
                                                                        <?php echo $products['product_name'].' x '.$products['invoice_qty']; ?>
                                                                        </td>
                                                                        <td style="text-align:right; ">
                                                                        <?php echo $products['price']; ?>
                                                                        </td>
                                                                        <td style="text-align:right; ">
                                                                        <?php echo $products['discount']; ?>
                                                                        </td>
                                                                        <td style="text-align:right; ">
                                                                        <?php echo $products['service_addition']; ?>
                                                                        </td>
                                                                        <td style="text-align:right; ">
                                                                        <?php echo $products['discounted_price']; $grand_total_sold=$grand_total_sold+$products['discounted_price']; ?>
                                                                        </td>
                                                                        <td style="text-align:right; ">
                                                                        <?php echo $products['taxes']; ?>
                                                                        </td>
                                                                        <td style="text-align:right; ">
                                                                        <?php echo $products['paid']; $grand_total_revenue=$grand_total_revenue+ $products['paid'];?>
                                                                        </td>                                                                          
                                                                        <td style="text-align:right; ">
                                                                        <?php echo $products['commission']; $grand_total_commission=$grand_total_commission+ $products['commission'];?>    
                                                                        </td>
                                                                        <td ></td>
                                                                        <td class="hidden-print"></td>
                                                                    </tr>
                                                             <?php  $pstaff_name=$products['staff_name'];$pinvoice= $products['invoice_id'];}}?>
                                                            
                                                    <?php } ?>   
                                                </tbody>
                                                <tfoot>
                                                    <tr >
                                                        <td style='border-top:2px solid #000;' class="text-right" colspan="6"></td>
                                                        <td style='border-top:2px solid #000;' class="text-right"><strong>Grand Total: </strong></td>
                                                        <td style='border-top:2px solid #000;' class="text-right"><strong><?php echo number_format($grand_total_sold, 2); ?></strong></td>
                                                        <td style='border-top:2px solid #000;' class="text-right"><strong></strong></td>
                                                        <td style='border-top:2px solid #000;' class="text-right"><strong><?php echo number_format($grand_total_revenue, 2); ?></strong></td>
                                                        <td style='border-top:2px solid #000;' class="text-right"><strong><?php echo number_format($grand_total_commission, 2); ?></strong></td>
                                                        <td style='border-top:2px solid #000;' class="text-right" ></td>
                                                        <td style='border-top:2px solid #000;' class="text-right hidden-print"></td>
                                                    </tr>
                                                </tfoot>
                                                <?php } ?>
                                        </table>
                                    </td>
                                </tr>

                            </table>
                        </div>
                        <!-- end row -->
                    </div>
                </div>

                <script>
                    

        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);
    
                var datestring = $("#namedate").html();
                link.download = ctx.worksheet + ' ' + datestring;
                link.href = uri + base64(format(template, ctx));
                link.click();
                //window.location.href = uri + base64(format(template, ctx))
            }
        })()
     
                </script>