
<style>

    .table tr .bifurcate >td{
        border-top:2px solid #808080;
    }
</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">
                                    <form id="Sheet-Form" method="POST" action='<?php echo base_url(); ?>repeatedcustomers'>
                                        <input type="hidden" name="csrf_test_name" id="daily_sheet_csrf" value=""/>
                                        <div class="row">
                                            <div class="col-md-12">
                                                <div class="col-md-3">
                                                    <label>Year</label>

                                                </div>
                                                <div class="col-md-3">
                                                    <label>Category</label>

                                                </div>
                                                <div class="col-md-2">
                                                    <label>Run</label>
                                                </div>
                                                <div class="col-md-4">
                                                    <div class="col-md-12"><label>&nbsp;</label></div>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="row">
                                            <div class="col-md-12">
                                                <div class="col-md-3">
                                                    <select  autocomplete="off" class="form-control inline m-r-5" name="year">
                                                        <?php $thisyear = date('Y'); 
                                                        for($x=6; $x>=1; $x--){ ?>
                                                        <option <?php if($selectedyear== $thisyear){echo "selected='selected'";}?> value="<?php echo $thisyear?>"><?php echo $thisyear?></option>
                                                        <?php $thisyear = $thisyear -1; } ?>
                                                    </select>
                                                </div>
                                                <div class="col-md-3">
                                                    <select style="width:500px" onchange="categorychange();" id="category_id" autocomplete="off" class="form-control inline m-r-5" name="category_id">
                                                        <?php foreach($categories as $category){?>
                                                        <option flag="<?php echo $category['flag'];?>" <?php if($selectedcategory== $category['id_service_category'] && $selectedflag== $category['flag']){echo "selected='selected'";}?>  value="<?php echo $category['id_service_category']?>"><?php echo  $category['service_type_id']."-".$category['service_category'];?></option>
                                                        <?php } ?>
                                                    </select>
                                                    <input name="flag" id="flag" value="<?php echo $selectedflag;?>" type="hidden">
                                                </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 class="col-md-4">
                                                <?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', 'RepeatedCustomers')" style="float:right">Export to Excel</a>                                            
                                                <?php } ?>
                                                </div>
                                            </div>
                                        </div>
                                    </form>
                                </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>Repeated Customers </td>
                                </tr>
                                <tr>
                                    <td colspan="5"><strong><u> For:  <?php echo $selectedyear; ?> - <span id='category'>   </span></u></strong></td>
                                </tr>
                                <tr>
                                    <td colspan="5">
                                        <table class="table table-bordered"  id='datatable-buttons1'>
                                            <thead>
                                                
                                                <tr>
                                                    <th>Customer ID</th>
                                                    <th>Customer</th>
                                                    <th>Customer Cell</th>
                                                    
                                                    <?php for($x=1; $x<=12; $x++){ ?>
                                                    <th><?php $dateObj = DateTime::createFromFormat('!m', $x); echo $dateObj->format('F');?></th>
                                                    <?php }?>
                                                    
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <!--<tr><td colspan="15"><?php //echo "size:" .sizeof($customers)."<br>"; var_dump($customers); ?></td></tr>-->
                                                <?php if (sizeof($customers)>0){?>
                                                <?php $lastcustomerid=0; $mhtml=""; $myarray=[]; $rowcount=1; foreach($customers as $customer){ ?>
                                                
                                                
                                                <?php if($lastcustomerid!==0){ if($lastcustomerid !== $customer['id_customers']){ 
                                                    echo "<tr>";
                                                    for($x=1; $x<=12; $x++){ 
                                                        if(isset($myarray[$x])){
                                                            $mhtml .= "<td>".$myarray[$x]."</td>";
                                                        } else {
                                                            $mhtml .= "<td></td>";
                                                        }
                                                    }
                                                    echo $mhtml;
                                                    $mhtml="";
                                                    $myarray=[];
                                                    echo "</tr>";
                                                }
                                                }
                                               ?>
                                                <?php 
                                                if($mhtml==""){
                                                $mhtml="<td>".$customer['id_customers']."</td>
                                                <td>".$customer['customer_name']."</td>
                                                <td>". $customer['customer_cell']."</td>";
                                                }
                                                ?>

                                                <?php $myarray[$customer['monthint']] = $customer['service_name'].' '.$customer['service_date'];?>
                                                        
                                                <?php  $lastcustomerid =$customer['id_customers']; $rowcount++;} ?>
                                                
                                                <?php //lastrow
                                                    
                                                        echo "<tr>";
                                                        for($x=1; $x<=12; $x++){ 
                                                            if(isset($myarray[$x])){
                                                                $mhtml .= "<td>".$myarray[$x]."</td>";
                                                            } else {
                                                                $mhtml .= "<td></td>";
                                                            }
                                                        }
                                                        echo $mhtml;
                                                        $mhtml="";
                                                        $myarray=[];
                                                        echo "</tr>";
                                                    
                                               ?>
                                                <?php } else {?>
                                                <tr><td colspan="15">No Data Found</td></tr>
                                                <?php } ?>
                                            </tbody>
                                                
                                        </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);
    
                
                link.download = ctx.worksheet;
                link.href = uri + base64(format(template, ctx));
                link.click();
                //window.location.href = uri + base64(format(template, ctx))
            }
        })()
     
     
    function categorychange(){
        var data = $("#category_id option:selected").select2('data');
       
        $('#flag').val(data.attr('flag'));
    }   
    
    $(document).ready(function() {
        $("#category_id").select2();  
        $("#category").html($("#category_id option:selected").text());
        
        $('#datatable-buttons1').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: "excel",
                        className: "btn-sm btn-warning btn-trans"
                    }, {extend: "pdf", className: "btn-sm btn-purple btn-trans"}],
            <?php } else { ?>
                dom: "frtlip",
            <?php } ?> 
                responsive: !0,
            columnDefs: [{
                "defaultContent": "-",
                "targets": "_all"
              }]    
        });
        
    });     
    </script>