<div class="wrapper">
    <div class="container">
        <!-- Page-Title -->
        <div class="row">
            <div class="col-sm-12">

                <h4 class="page-title">Attendance of Staff For : <?php echo $monthName.'-'.$year; ?></h4>
            </div>
        </div>

        <div class="row" id="divselection">
            <div class="col-sm-12">
                <div class="card-box ">
                    <h4><?php echo $payables[0]['staff_fullname']; ?></h4>
                </div>
            </div>
        </div>

        <div class="row" id="divselection">
            <div class="col-sm-12">
                <div class="card-box ">
                    <div class="row m-b-5">
                        <div class="col-md-12"> 
                        <?php if(DOWNLOAD == true || $this->session->userdata('role') == 'Admin' || $this->session->userdata('role')=='Super User'){ ?>
                        <button onclick="tableToExcel('tblstaff', 'Attendance <?php echo $payables[0]['staff_fullname']; ?>  <?php echo $monthName.'-'.$year; ?>')" class="btn btn-warning pull-right btn-sm m-l-5">Export to Excel</button>
                        <?php } ?>
                        </div>
                    </div>
                    <table id="tblstaff" class="table  table-striped">
                        <thead>
                            <tr>
                                <th>Date</td>
                                <th>Day</td>
                                <th>Time In</td>
                                <th>Time Out</td>
                                <th>Work Hours</th>
                                <th>present</th>
                                <th>Absent</th>
                                <th>Late</th>
                                <th>Leave</th>
                                <th>Holiday</th>
                                <th>Marked From</th>                               
                                <th></th>
                            </tr>
                        </thead>
                        <tbody>
                            
                            <?php $presents=0; $absents=0; $late=0; $leave=0; $holiday=0; foreach($payables as $payable){ ?>
                            <tr>
                                <td><?php echo date_format(date_create($payable['Date']), 'd-M-Y');  ?></td>
                                <td <?php $offday=''; if($payable['week_day_off']== $payable['Day']){echo "class='text-danger'"; $offday='(Day Off)';} elseif($payable['Holiday']!=0){echo "class='text-primary'";}?>
                                    ><?php echo $payable['Day'].' '.$payable['leave_reason'].' '.$payable['holiday_reason'].$offday; ?></td>
                                <td class="text-center"><?php echo $payable['time_in']; ?></td>
                                <td class="text-center"><?php echo $payable['time_out']; ?></td>
                                <td class="text-center"><?php echo $payable['Work_Hours']; ?></td>
                                <td class="text-center <?php if($payable['Present']==1){ ?>text-success<?php } ?>">
                                    <?php if($payable['Present']==1){ ?>
                                        <i class="fa fa-check"></i>
                                        <span class="hidden">1</span>
                                    <?php } else {?>
                                        <i class="fa fa-close"></i>
                                        <span class="hidden">0</span>
                                    <?php } ?>
                                    <?php $presents=$presents + $payable['Present']; ?>
                                </td>
                                <td class="text-center <?php if($payable['Absent']==1){ ?>text-danger<?php } ?>">
                                    <?php if($payable['Absent']==1){ ?>
                                        <i class="fa fa-check"></i>
                                        <span class="hidden">1</span>
                                    <?php } else {?>
                                        <i class="fa fa-close"></i>
                                        <span class="hidden">0</span>
                                    <?php } ?>
                                    <?php $absents = $absents+$payable['Absent']; ?></b>
                                
                                </td>
                                <td class="text-center <?php if($payable['Late']==1){ ?>text-primary<?php } ?>">
                                    <?php if($payable['Late']==1){ ?>
                                        <i class="fa fa-check"></i>
                                        <span class="hidden">1</span>
                                    <?php } else {?>
                                        <i class="fa fa-close"></i>
                                        <span class="hidden">0</span>
                                    <?php } ?>
                                    <?php $late=$late+$payable['Late'];?>
                                </td>
                                <td class="text-center <?php if($payable['Leaves']==1){ ?>text-warning<?php } ?>">
                                    <?php if($payable['Leaves']==1){ ?>
                                        <i class="fa fa-check"></i>
                                        <span class="hidden">1</span>
                                    <?php } else {?>
                                        <i class="fa fa-close"></i>
                                        <span class="hidden">0</span>
                                    <?php } ?>
                                    <?php $leave=$leave+$payable['Leaves'];?>
                                </td>
                                <td class="text-center <?php if($payable['Holiday']==1){ ?>text-warning<?php } ?>">
                                    <?php if($payable['Holiday']==1){ ?>
                                        <i class="fa fa-check"></i>
                                        <span class="hidden">1</span>
                                    <?php } else {?>
                                        <i class="fa fa-close"></i>
                                        <span class="hidden">0</span>
                                    <?php } ?>
                                    <?php $holiday=$holiday+$payable['Holiday'];?>
                                </td>
                                
                                <td><?php echo $payable['address']; ?></td>
                                <td></td>
                            </tr>
                            <?php } ?>
                        </tbody>
                        <tfoot>
                            <tr>
                                <th colspan="5"></th>
                                <th class="text-center text-success"><?php echo $presents;?></th>
                                <th class="text-center text-danger"><?php echo $absents;?></th>
                                <th class="text-center text-primary"><?php echo $late;?></th>
                                <th class="text-center text-warning"><?php echo $leave;?></th>
                                <th class="text-center text-warning"><?php echo $holiday;?></th>
                                <th></th>
                                <th></th>
                            </tr>
                            <tr>
                                <th>Date</td>
                                <th>Day</td>
                                <th>Time In</td>
                                <th>Time Out</td>
                                <th>Work Hours</th>
                                <th class="text-center">present</th>
                                <th class="text-center">Absent</th>
                                <th class="text-center">Late</th>
                                <th class="text-center">Leave</th>
                                <th class="text-center">Holiday</th>
                                <th>Marked From</th>                               
                                <th></th>
                            </tr>
                        </tfoot>
                    </table>
                </div>
            </div>
        </div>
    </div>

    <script>
        
    $(document).ready(function() {

        $('#tblstaff').DataTable({
            
            stateSave: true,
            paging: false,
       
            dom: "Bfrtip",
            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"}],
            responsive: 0,
            "order": [[ 0, "asc" ]]

        });
    });
    
    
    
    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))
            }
        })()
    
    </script>