<div class="wrapper">
    <div class="container">
        <!-- Page-Title -->
        <div class="row">
            <div class="col-sm-12">
                <h4 class="page-title">Yearly Staff Attendance For : <?php echo $year; ?></h4>
            </div>
        </div>
<?php if(sizeof($attendance)==0){?>
        <div class="row" id="divselection">
            <div class="col-sm-12">
                <div class="card-box ">
                    <h4>No Attendance Marked</h4>
                    <?php $y=date('Y'); $z=$y-3; ?>
                    <form class="row" method="POST" action="<?php echo base_url('yearlyattendance');?>">
                        <div class="col-md-4">
                            <div class="form-group">
                                <select id="selectyear" name="selectyear" class="form-control">
                                    <?php for($x=$z; $x<=$y; $x++){?>
                                    <option <?php if($year==$x){echo 'selected="selected"';}?> value="<?php echo $x; ?>"><?php echo $x; ?></option>
                                    <?php } ?>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-2">
                            <button class="btn btn-sm btn-default">Run</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
            
<?php } else { ?>
        <div class="row" id="divselection">
            <div class="col-sm-12">
                <div class="card-box ">
                    <?php $y=date('Y'); $z=$y-3; ?>
                    <form class="row" method="POST" action="<?php echo base_url('yearlyattendance');?>">
                        <div class="col-md-4">
                            <div class="form-group">
                                <select id="selectyear" name="selectyear" class="form-control">
                                    <?php for($x=$z; $x<=$y; $x++){?>
                                    <option <?php if($year==$x){echo 'selected="selected"';}?> value="<?php echo $x; ?>"><?php echo $x; ?></option>
                                    <?php } ?>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-2">
                            <button class="btn btn-sm btn-default">Run</button>
                        </div>
                    </form>
                </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"> 
                        <button onclick="tableToExcel('tblstaff', 'Attendance <?php echo $attendance[0]['staff_fullname']; ?>  <?php echo $monthName.'-'.$year; ?>')" class="btn btn-warning pull-right btn-sm m-l-5">Export to Excel</button>
                        </div>
                    </div>
                    <table id="tblstaff" class="table  table-striped">
                        <thead>
                            <tr>
                                
                                <th>ID</th>
                                <th>Staff</th>
                                <th>Month</th>
                                <th class="text-center">Presents</th>                                
                                <th class="text-center">Leaves</th>
                                <th class="text-center">Holidays</th>
                                <th class="text-center">Absents</th>                                                    
                              
                            </tr>
                        </thead>
                        <tbody>
                            
                            <?php $presents=0; $absents=0; $late=0; $leave=0; $holiday=0; foreach($attendance as $a){ ?>
                            <tr>
                               
                                <td ><?php echo $a['id_staff'];?> </td>
                                <td ><?php echo $a['staff_fullname'];?> </td>
                                 <td><?php echo $a['month'];  ?></td>
                                <td class="text-center"><?php echo $a['present']; ?></td>
                                <td class="text-center"><?php echo $a['leaves']; ?></td>
                                <td class="text-center"><?php echo $a['holidays']; ?></td>
                                <td class="text-center">
                                    <?php echo $a['daysinmonth'] - $a['present'] - $a['holidays'] - $a['leaves']; ?>
                                </td>
                             
                                
                            </tr>
                            <?php } ?>
                        </tbody>
                        <tfoot>
                            <tr>
                               
                                <th>ID</th>
                                <th>Staff</th>
                                 <th>Month</th>
                                <th class="text-center">Presents</th>                                
                                <th class="text-center">Leaves</th>
                                <th class="text-center">Holidays</th>
                                <th class="text-center">Absents</th>                                                    
                              
                            </tr>
                            
                        </tfoot>
                    </table>
                </div>
            </div>
        </div>
<?php } ?>
    </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"}],
            ordering: false,
            responsive: 0

        });
    });
    
    
    
    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>