<style>
    .table thead tr th{
        border-top:2px solid #eee;
        border-bottom:2px solid #eee;
        background: #fcfcfc;
    }
    .table tfoot tr th{
        border-top:2px solid #eee;
        border-bottom:2px solid #eee;
        background: #fcfcfc;
    }
</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 >
                                <div class="col-md-6 hidden-print m-t-20">
                                    <div class="row">
                                        <div class="col-md-12" style="margin-bottom: 5px;">
                                            <form method="POST" action='<?php echo base_url(); ?>incentive_report_product'>
                                                <input type="hidden" name="csrf_test_name" id="daily_sheet_csrf" value=""/>
                                                <div class="col-md-6">
                                                    <select class="form-control" multiple="multiple" id="brands" name="brands[]" placeholder="select brand">
                                                        <?php foreach($brands as $brand){?>
                                                        <option value="<?php echo $brand['id_business_brands']; ?>">
                                                            <?php echo $brand['business_brand_name'].' '.'('.$brand['business_name'].')'; ?>
                                                        </option>
                                                        <?php } ?>
                                                    </select>
                                                </div>

                                                <div class="col-md-4">
                                                    <input type="month" autocomplete="off" class="form-control inline m-r-5" name="calendar_date_product" id="calendar_date_product" value="<?php echo $selected_date;?>">
                                                </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 Product Report</button>
                                                </div>
                                            </form>
                                        </div>
                                        <div class="col-md-12">
                                            <form method="POST" action='<?php echo base_url(); ?>incentive_report_service'>
                                                <input type="hidden" name="csrf_test_name" id="daily_sheet_csrf" value=""/>
                                                <div class="col-md-6">
                                                    <select class="form-control" multiple="multiple" id="services" name="services[]" placeholder="select Service">
                                                        <?php foreach($services as $service){?>
                                                        <option value="<?php echo $service['id_service_category']; ?>">
                                                            <?php echo $service['service_category'].' ('. $service['business_name'].')'; ?>
                                                        </option>
                                                        <?php } ?>
                                                    </select>
                                                </div>

                                                <div class="col-md-4">
                                                    <input type="month" autocomplete="off" class="form-control inline m-r-5" name="calendar_date_service" id="calendar_date_service" value="<?php echo $selected_date;?>">
                                                </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 Service Report</button>
                                                </div>
                                            </form>
                                            
                                        </div>
                                    </div>
                                    
                                </div>
                                <div class="col-md-6 hidden-print m-t-20">
                                <?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', 'Incentive Sheet')" 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>
                            <div class="clearfix"></div>
                        </div>
                        <!-- end row -->

                        <div class="row">
                            <table class="table m-t-10"  id='sheet1'>
                                <tr>
                                    <th colspan="7"><strong>Incentive Data Sheet <u> <?php echo isset($selected_date) && !empty($selected_date) ? date('M-Y', strtotime($selected_date)) : date('M-Y'); ?></u></strong></th>
                                </tr>
                                <!--<tr><?php // if(isset($response_products)){ print_r($response_products); }?></tr>--> 
                                <?php if(isset($response_products)){?>
                                    <tr>
                                        <th>Business</th>
                                        <th>Staff ID</th>
                                        <th>Staff Name</th>
                                        <th>Brand</th>
                                        <th>Product Name</th>
                                        <th>Quantity</th>
                                        <th>Month</th>
                                        <th>Year</th>
                                        <th>Price</th>
                                    </tr>                               
                                    <?php foreach($response_products as $res){?>
                                    <tr>
                                        <td><?php echo $res['business_name'];?></td>
                                        <td><?php echo $res['staff_id'];?></td>
                                        <td><?php echo $res['staff_name'];?></td>
                                        <td><?php echo $res['brand_name'];?></td>
                                        <td><?php echo $res['product_name'];?></td>
                                        <td><?php echo $res['qty'];?></td>
                                        <td><?php echo $res['month'];?></td>
                                        <td><?php echo $res['year'];?></td>
                                        <td><?php echo $res['price'];?></td>
                                    </tr>
                                    <?php } ?>
                                <?php } ?>
                                <?php if(isset($response_services)){?>
                                    <tr>
                                        <th>Business</th>
                                        <th>Staff ID</th>
                                        <th>Staff Name</th>
                                        <th>Service Category</th>
                                        <th>Service Name</th>
                                        <th>Month</th>
                                        <th>Year</th>
                                        <th>Price</th>
                                    </tr>
                                    <?php foreach($response_services as $res){?>
                                    <tr>
                                        <td><?php echo $res['business_name'];?></td>
                                        <td><?php echo $res['staff_id'];?></td>
                                        <td><?php echo $res['staff_name'];?></td>
                                        <td><?php echo $res['service_category'];?></td>
                                        <td><?php echo $res['service_name'];?></td>
                                        <td><?php echo $res['month'];?></td>
                                        <td><?php echo $res['year'];?></td>
                                        <td><?php echo $res['price'];?></td>                                        
                                    </tr>
                                    <?php } ?>
                                <?php } ?>
                            </table>                            
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>


<script>
    
    $(document).ready(function(){
        $("#brands").select2();
        
        $("#services").select2();
        
    });
    
    
    
    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))
        }
    })()
    
    </script>