
<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 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-6 hidden-print m-t-20">
                                    <form method="POST" action='<?php echo base_url(); ?>attendancesheet'>
                                        <input type="hidden" name="csrf_test_name" id="daily_sheet_csrf" value=""/>
                                        <div class="col-md-10">
                                            <input type="month" autocomplete="off" class="form-control inline m-r-5" name="calendar_date" id="datepicker-autoclose1">
                                        </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>
                                    </form>
                                </div>
                                <div class="col-md-6 hidden-print m-t-20">
                                    <!--<a class="btn btn-warning m-l-10" onclick="tableToExcel('sheet1', 'Marketing Sheet')" style="float:right">Export to Excel</a>-->
<!--                                    <a class="btn btn-warning m-l-10" href="<?php //echo base_url('marketing_controller/marketing_excell'.'/'.$date); ?>" 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>-->
                                </div>
                            </div>
                            <div class="clearfix"></div>
                        </div>
                        <!-- end row -->

                        <div class="row">
                            
                                <table class="table table-striped table-bordered"  id='datatable-buttons'>
                                    <thead>

                                        <tr>
                                            <th>Staff ID</th>
                                            <th>Staff Name</th>
                                            
                                            <?php $gazzatted=0; $days=1; for($x=1; $x<=$daysinmonth; $x++){?>
                                            
                                            
                                            <?php 
                                                //Our YYYY-MM-DD date string.
                                                $date = $year."-".$monthdigit."-".str_pad($x, 2, '0', STR_PAD_LEFT);
                                               
                                                //Convert the date string into a unix timestamp.
                                                $unixTimestamp = strtotime($date);

                                                //Get the day of the week using PHP's date function.
                                                $dayOfWeek = date("D", $unixTimestamp);

                                                //Print out the day that our date fell on.
                                                //echo $date . ' fell on a ' . $dayOfWeek;
                                            ?>
                                            
                                            <th <?php if($dayOfWeek == 'Sun'){echo 'class="text-primary"';}?> <?php 
                                            $reason='';
                                            foreach($holidays as $holiday){  
                                                if($date === $holiday['holiday_date']){ 
                                                    echo 'class="text-warning"';
                                                    $reason="(".$holiday['holiday_reason'].")";
                                                    $gazzatted++;
                                                }                                            
                                            }
                                            ?>>
                                            <?php echo $x.' '.substr($month,0,3).' '.$dayOfWeek.' '.$reason;?>                                                
                                            </th>
                                            <?php $days++;} ?>
                                            
                                            <th>Presents</th>
                                            <th>Absents</th>
                                            <th>Late</th>
                                            <th>Off Days</th>
                                            <th>Leaves</th>
                                            <th>Staff Name</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <tr><td></td><td><?php echo $month.'-'.$year;?></td>
                                            <?php $days=1; for($x=1; $x<=$daysinmonth; $x++){?>
                                            <td></td>
                                            <?php $days++;} ?>
                                            <td></td><td></td><td></td><td></td><td></td><td></td>
                                        </tr>
                                        <?php
                                        if(isset($staff)){

                                            foreach ($staff as $s) {
                                                $presents=0; $offs=0; $leaves=0; $absents=0; $found=0;$lates=0;$address='';
                                                ?>
                                                 <tr >
                                                     
                                                     <td><?php echo $s['id_staff'] ;?></td>
                                                     <td><a target="_blank" href="<?php echo base_url()."view_staff_attendance_single/".$s["id_staff"]."/".$monthdigit."/".$year; ?>"><?php if($this->session->userdata("ho")=='No'){ echo $s['staff_fullname']; }else{ echo $s['staff_fullname'].' '.$s['business_name'];}?></a></td>
                                                     
                                                    <?php for($day=1; $day<=$daysinmonth;$day++){ ?>
                                                        <?php $monTime='ontime'; $mTime='A'; 
                                                        
                                                        //Our YYYY-MM-DD date string.
                                                        $date = $year."-".$monthdigit."-".str_pad($day, 2, '0', STR_PAD_LEFT);
                                                        //Convert the date string into a unix timestamp.
                                                        $unixTimestamp = strtotime($date);
                                                        //Get the day of the week using PHP's date function.
                                                        $dayOfWeek = date("D", $unixTimestamp);
                                                        
                                                        foreach($holidays as $holiday){  
                                                            $d = $year.'-'.$monthdigit.'-'.str_pad($day,2,'0', STR_PAD_LEFT);
                                                            //$d = $year.'-'.$monthdigit.'-'.$day;
                                                            if($d == $holiday['holiday_date']){ 
                                                                $mTime = 'H';   
                                                                if($dayOfWeek === $s['week_day_off']){ $offs--; }
                                                            }
                                                            
                                                        }    
                                                        
                                                        //if($s['week_day_off']===$dayOfWeek){ 
                                                        //if(str_contains($s['week_day_off'], $dayOfWeek))  {
                                                        if(stripos($s['week_day_off'], $dayOfWeek)>-1) {    
                                                            $mTime="Off"; 
                                                            $offs++;
                                                        }
                                                        
                                                        foreach($s['leaves'] as $leave){
                                                            if($leave['leave_date']===$date){ 
                                                                if($mTime !='H' && $mTime!='Off'){
                                                                    $mTime="L"; 
                                                                    $leaves++;
                                                                    //if($s['week_day_off']==$dayOfWeek){ $offs= $offs-1;}
                                                                }
                                                            }
                                                        }
                                                        
                                                        if(isset($attendance) && null!==$attendance){  
                                                            foreach($attendance as $att){  
                                                                if($day==$att['day'] && $s['id_staff']==$att['id_staff']){
                                                                    $mTime=$att['TimeIn'];
                                                                    $presents++;
                                                                    if($s['week_day_off']==$dayOfWeek){ $offs--;}
                                                                    if($att['onTime']=='late'){$monTime='late';}else{$monTime='ontime';}  
                                                                    
                                                                    $address=$att['address'];                                                                
                                                                } 
                                                            } 
                                                        } else {echo "<td colspan=".$daysinmonth."></td>";}?>
                                                    <td <?php if($monTime=='late'){echo 'class="text-danger"'; $lates++;}?> <?php if($mTime=='A'){echo 'class="text-danger"';} ?> <?php if($mTime=='H' || $mTime=='L'){echo 'class="text-warning"';} ?> <?php if($mTime=='Off'){echo 'class="text-info"';} ?>><?php echo $mTime . ' '. $address; $address='';?></td>
                                                    <?php } ?>
                                                    
                                                    <?php echo "<td>".$presents."</td>";?>
                                                    <?php $absents = $daysinmonth-$presents-$gazzatted-$leaves-$offs;
                                                            $offdays=$offs+$gazzatted;
                                                    ?>
                                                    <?php echo "<td>".$absents."</td>";?>
                                                    <?php echo "<td>".$lates."</td>";?>
                                                    <?php echo "<td>".$offdays."</td>";?>
                                                    <?php echo "<td>".$leaves."</td>";?>
                                                    <td><?php echo $s['staff_fullname'];?></td>
                                                 </tr>           
                                        <?php } }?>

                                    </tbody>                                    
                                </table>
                        </div>
                        <!-- end row -->
                    </div>
                </div>
            </div>
        </div>
    </div>
<script>
    $(document).ready(function(){
//        $('#datepicker-autoclose').datepicker({
//            autoclose: true,
//                todayHighlight: true,
//                format: 'yyyy-mm-dd'
//        });

        $('#datatable-buttons').DataTable({
                    lengthMenu: [[10, 25, 50, -1], [10, 25, 50, "All"]],
                    stateSave: 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 } ?>
                    scrollX:true,
                    ordering: false
                });
    
    });


</script>