<style>

    .ui-datepicker-calendar {
        display: none;
        }
    .ui-widget {
        font-size:.7em;
    }

</style>
<div class="wrapper">
    <div class="container">
        <div class="row m-t-20">
            <div class="col-sm-12 ">
                <div class="btn-group pull-right m-t-15">
                </div>
                <h4 class="page-title">Staff Service Count</h4>
            </div>
        </div>

        <div class="row ">
            <div class="col-md-12">
                <div class="panel panel-default m-t-20">
                    <div class="panel-body">
                        <form method="POST" action='<?php echo base_url(); ?>marketing_controller/staff_day_wise_service_count'>
                            <div class="row">
                                <div class="col-md-2 hidden-print m-t-20">
                                    <label for='monthpicker'>Month</label>
                                    <input id="monthpicker" name="month" class="form-control datepicker-dropdown" value="<?php echo $postedmonth;?>">
                                </div>
                                <div class="col-md-2 hidden-print m-t-20">
                                    <label for='yearpicker'>Year</label>
                                    <input id="yearpicker" name="year" class="form-control datepicker-dropdown" value="<?php echo $postedyear;?>">
                                </div>
                                <div class="col-md-2 hidden-print m-t-20">
                                    <button  type="submit" class="btn btn-sm btn-success m-t-20">Run</button>
                                </div>
                                <div class="col-md-2 hidden-print m-t-20">
                                <?php if(DOWNLOAD == true || $this->session->userdata('role') == 'Admin' || $this->session->userdata('role')=='Super User'){ ?>
                                    <button type="button"  onclick="tableToExcel('sheet1', 'StaffDaySheet')" class="btn btn-sm btn-success m-t-20">Export to Excel</button>
                                <?php } ?>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
        
        
        <div class="row " >
            <div class="col-md-12">
                <div class="panel panel-default m-t-20" style="min-height:200px;">
                    <div class="panel-body">
                        <div class="row">
                            <?php if(sizeof($services)>0){?>
                            <table class="table m-t-10"  id='sheet1'>
                                <thead>
                                <tr>
                                    <th>Staff ID</th>
                                    <th>Staff</th>
                                    <th>Service</th>
                                    <th>Price</th>
                                    <?php foreach($services[0] as $key => $value){
                                        if(strlen($key) <= 2){?>
                                        <th><?php echo $key; ?></th>
                                    <?php }} ?>
                                    <th>Count</th>
                                    <th>Total</th>
                                </tr>
                                </thead>
                                <tbody>
                                <?php $Totalamount=0; $laststaffid=1; $laststaff='';  foreach($services as $service){?>
                                    <?php if($service['staff_id'] != $laststaffid && $laststaffid!=''){  ?>
                                        <tr>
                                            <td colspan="<?php echo count($service)-6;?>"></td>
                                            <td colspan="6"><?php echo $laststaff;?></td>
                                            <td style='text-align: right;'><strong><?php  echo $Totalamount > 0 ? $Totalamount :  ''; ?></strong></td>
                                        </tr>
                                        <tr>
                                            <td style="border-top:1px solid #808080;"> 
                                                <?php echo $service['staff_id'];?>
                                            </td>
                                            <td style="border-top:1px solid #808080;">
                                                <?php echo $service['staff_name'];?>
                                            </td>
                                            <td style="border-top:1px solid #808080;">
                                                <?php echo $service['service_name'];?>
                                            </td>
                                            <td style="text-align:right; border-top:1px solid #808080;">
                                                <?php echo $service['price'];?>
                                            </td>                                        
                                            <?php $service_count =0; foreach($service as $key => $value){
                                                if(strlen($key) <= 2){?>
                                                <td style="text-align:right; border-top:1px solid #808080;">
                                                    <?php echo $value; ?>
                                                    <?php $service_count = $service_count + $value; ?>
                                                </td>
                                            <?php }} ?>
                                            <td style="text-align:right; border-top:1px solid #808080;"><strong><?php echo $service_count;?></strong></td>
                                            <td style="text-align:right; border-top:1px solid #808080;">
                                                <?php echo $service_count*$service['price'];?>
                                                <?php $Totalamount += $service_count*$service['price'];?>
                                            </td>
                                        </tr>                   
                                    <?php }else{ ?>    
                                        <tr>
                                            <td >
                                            <?php echo $service['staff_id'];?>
                                            </td>
                                            <td >
                                                <?php echo $service['staff_name'];?>
                                            </td>
                                            <td >
                                                <?php echo $service['service_name'];?>
                                            </td>
                                            <td style="text-align:right;">
                                                <?php echo $service['price'];?>
                                            </td>                                        
                                            <?php $service_count =0; foreach($service as $key => $value){
                                                if(strlen($key) <= 2){?>
                                                <td style="text-align:right;">
                                                    <?php echo $value; ?>
                                                    <?php $service_count = $service_count + $value; ?>
                                                </td>
                                            <?php }} ?>
                                            <td style="text-align:right;"><strong><?php echo $service_count;?></strong></td>
                                            <td style="text-align:right;">
                                                <?php echo $service_count*$service['price'];?>
                                                <?php $Totalamount += $service_count*$service['price'];?>
                                            </td>
                                        </tr>                     
                                    <?php } ?>
                                <?php $laststaffid=$service['staff_id']; $laststaff=$service['staff_name']; } ?>
                                <tr >
                                    <td colspan="<?php echo count($service)-6;?>"></td>
                                    <td colspan="6"><?php echo $laststaff;?></td>
                                    
                                    <td style='text-align: right;'><strong><?php  echo $Totalamount > 0 ? $Totalamount :  ''; ?></strong></td>
                                </tr>
                                </tbody>
                            </table>
                            <?php } else { ?>
                                <div class="row">
                                    <div class='col-md-12'>
                                        <h5>No Data Available</h5>
                                    </div>
                                </div>
                            <?php } ?>
                        </div>
                    </div>
                </div>
            </div>
        </div>

    <script>
        $(document).ready(function(){
            
            $('#monthpicker').datepicker({
                format: 'mm',
                viewMode: 'months',
                minViewMode: 'months'
            });

            $('#yearpicker').datepicker({
                format: 'yyyy',
                viewMode: 'years',
                minViewMode: 'years'
            });

            $('#sheet2').DataTable({
                lengthMenu: [[10, 25, 50, -1], [10, 25, 50, "All"]],
                stateSave: true, 
                fixedHeader: {header: true},
                <?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: "pdf", className: "btn-sm btn-purple btn-trans"},                    
                    {extend: "print", className: "btn-sm btn-warning btn-trans"}
                ],
                <?php } else { ?>
                    dom: "frtlip",
                <?php } ?> 
                responsive: !1,
                ordering: 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);
    
                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>