<div class="wrapper">
    <div class="container">
        <!-- Page-Title -->
        <div class="row">
            <div class="col-sm-12">
                <div class="btn-group pull-right m-t-15">
                    <!-- <a type="button" href="<?php echo base_url(); ?>coachingsearch" class="btn btn-custom waves-effect waves-light" >Add New Enrollment <span class="m-l-5"><i class="fa fa-plus"></i></span></a>                     -->
                </div>
                <h4 class="page-title">Monthly  Member Attendance (<?php echo $business[0]['business_name'];?>):</h4>
            </div>
        </div>
        <div class="row" id="divselection">
            <div class="col-sm-12">
                <div class="card-box ">
                    <h4>Filters:</h4>
                    <div class="row">
                        <form  action="<?php echo base_url()?>monthly_student_attendance_report" method="post">
                            <div class="col-md-2">
                                <label class="form-label">Year</label>
                                <select class="form-control" name="year" id="year" onchange="getting_groups_for_monthly_attendance_report()">
                                    <?php $tyear = date('Y');?>
                                    <option <?php if($year==$selected_year){echo "selected='selected'";}?> value="<?php echo $tyear; ?>"><?php echo $tyear;?></option> 
                                    <option <?php if($tyear-1==$selected_year){echo "selected='selected'";}?> value="<?php echo $tyear-1; ?>"><?php echo $tyear-1;?></option> 

                                </select>
                            </div>
                            <div class="col-md-2">
                                <label class="form-label">Month</label>
                                <select class="form-control" name="month" id="month" onchange="getting_groups_for_monthly_attendance_report()">
                                    <?php for($x=1; $x<=12; $x++){
                                        if($x==1){$m = "Jan";}
                                        if($x==2){$m = "Feb";}
                                        if($x==3){$m = "Mar";}
                                        if($x==4){$m = "Apr";}
                                        if($x==5){$m = "May";}
                                        if($x==6){$m = "Jun";}
                                        if($x==7){$m = "Jul";}
                                        if($x==8){$m = "Aug";}
                                        if($x==9){$m = "Sep";}
                                        if($x==10){$m = "Oct";}
                                        if($x==11){$m = "Nov";}
                                        if($x==12){$m = "Dec";}
                                        ?>
                                    <option <?php if($x==$selected_month){echo "selected='selected'";}?>  value="<?php echo $x;?>"><?php echo $m;?></option>
                                    <?php } ?>
                                    </select>
                            </div>
                          
                            <div class="col-md-2"> 
                                <label class="form-label"> Select Member </label>
                                <select class="form-control" name="enroll_member" id="enroll_member" onchange="getting_groups_for_monthly_attendance_report()">
                                   <?php 
                                        foreach($enroll_customers as $row){  
                                            ?>
                                            <option value="<?php echo  $row->id_customers  ?>"  <?php   if($row->id_customers==$select_customer_id){echo "selected='selected'";}  ?>><?php echo  $row->customer_name; ?> </option>
                                            <?php 
                                        } 
                                    ?>
                                </select>
                            </div>
                            <div class="col-md-2" style="display: none;"> 
                                <label class="form-label"> Select Group </label>
                                <select class="form-control" name="program_group_id" id="program_group_id" >
                                </select>
                            </div>
                            <div class="col-md-2">
                                <button class="btn btn-pink m-t-20">Run</button>                            
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
        
        <div class="row" >
            <div class="col-sm-12">
                <div class="card-box ">
                    <h4 class="header-title m-t-0 m-b-30"><?php if(isset($customer_data) ){ echo "Member Name : ". $customer_data->customer_name; }else{ echo ''; } ?> </h4> 
                    <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('datatable-buttons', 'Attendance <?php if(isset($customer_data) ){ echo $customer_data->customer_name; }else{ echo 'Not Available'; } ?>  <?php echo $selected_month.'-'.$selected_year; ?>')" class="btn btn-warning pull-right btn-sm m-l-5">Export to Excel</button>
                        <?php } ?>
                        </div>
                    </div>
                    <div class="row">  
                    <div class="row m-b-5">
                    </div>
                        <div class="col-lg-12">
                            <table id="datatable-buttons" class="table table-striped table-bordered dt-responsive nowrap" cellspacing="0" width="100%">
                                <thead>
                                    <tr>
                                        <th>Date</td>
                                        <th>Day</td>
                                        <th>Start time</td>
                                        <th>End Time</td>
                                        <th>present</th>
                                        <th>Absent</th>
                                    </tr>
                                </thead>
                                <tbody>   
                                    <?php  
                                        if(isset($month_attendance_data)){ 
                                            $presents=0;
                                            $present_counter =0;
                                            $absents=0;
                                            $absents_counter =0;
                                           
                                            foreach($month_attendance_data as $row){ 
                                                ?> 
                                                <tr>
                                                <td><?php echo date_format(date_create($row->date), 'd-M-Y');  ?></td> 
                                                <td><?php echo $row->day ?></td>
                                                <td><?php echo $row->visit_service_start;  ?></td>
                                                <td><?php echo $row->visit_service_end;  ?></td> 

                                                <td class="text-center <?php if($row->present_count=="present"){ ?>text-success<?php } ?>">
                                                            <?php if($row->present_count=="present"){ ?>
                                                                <i class="fa fa-check"></i>
                                                                <span class="hidden">1</span>
                                                            <?php $presents += 1 ;} else {?>
                                                                <i class="fa fa-close"></i>
                                                                <span class="hidden">0</span>
                                                            <?php } ?>
                                                        <?php ?>
                                                    </td>
                                                    <td class="text-center <?php if($row->absent_count=="absent"){ ?>text-danger<?php } ?>">
                                                        <?php if($row->absent_count=="absent"){ ?>
                                                            <i class="fa fa-check"></i>
                                                            <span class="hidden">1</span>
                                                        <?php  $absents += 1; } else {?>
                                                            <i class="fa fa-close"></i>
                                                            <span class="hidden">0</span>
                                                        <?php } ?>
                                                        </b>
                                                    
                                                    </td>
                                                    </tr>   
                                                <?php 
                                            } 

                                            ?> 
                                              <tfoot> 
                                                <tr>
                                                <td></td>
                                                <td></td>
                                                <td></td>
                                                <td></td>
                                                <th class="text-center"><?php echo $presents ?></th>
                                                <th class="text-center"><?php echo  $absents;  ?></th>
                                                </tr>
                                              </tfoot> 
                                            <?php  
                                        } 
                                        ?> 
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div> 

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

        $('.mdate').datepicker({
                default: today,
                autoclose: true,
                todayHighlight: true,
                format: 'yyyy-mm-dd'
            });
        
        $("#enroll_member,#year , #month").select2(); 
        jQuery('#txteditcustomeranniversary').datepicker({
                    autoclose: true,
                    todayHighlight: true,
                    format: 'yyyy/mm/dd'
                });

                jQuery('#txtcustomeranniversary').datepicker({
                    autoclose: true,
                    todayHighlight: true,
                    format: 'yyyy/mm/dd'
                });
       
       $('#datatable-buttons').DataTable({
                "lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
                "stateSave": true,
                "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"}],
                "responsive": !0,
		"order": [[ 1, 'asc' ]],
		"displayLength": 25
		
            });
            //changeview();
    });
    
    function changeview(){
        $('#datatable-buttons tr').each(function(row, tr) {
            //$(this).css('display','');
            console.log( $(tr).find('td:eq(7)').text().trim());
            console.log( $(tr).find('td:eq(8)').text().trim());
            if(row>0){
                var total = $(tr).find('td:eq(7)').text().trim();
                var booked = $(tr).find('td:eq(8)').text().trim();
                var status='';

                if(parseInt(total) <= parseInt(booked)){status = 'Completed';}
                else if(total > booked) {status = 'Active';}

                if($("#filter_veiw").val() != 'All'){ 
                    if(status != $("#filter_veiw").val()){
                        $(this).css('display','none');            
                    }else{
                        $(this).css('display','');
                    }
                }else{
                    $(this).css('display','');
                }
            }
        });
    }   



    function getting_groups_for_monthly_attendance_report(){ 
                var year =   $("#year").val();
                var month =  $("#month").val();  
                var customer_id  =  $("#enroll_member").val();  
                if( year !="" && month !=""  && customer_id !=""){
                $.ajax({
                        type: 'POST',
                        //url: 'Scheduler_controller/staffreporting',
                        url: "<?php echo base_url() . 'Coaching_controller/getting_groups_for_monthly_attendance_report'; ?>",
                        //data: 'staff_id=' +staff_id+ '&calendar_date=' +calendar_date,
                        data: {
                            "year": year,
                            "month":month,
                            "customer_id":customer_id
                        },
                        success: function(response) {  
                            var parsedResponse = JSON.parse(response); 
                            var options = '<option value="individual">None</option>'; 
                            if (parsedResponse.length > 0) { 
                                options+='<option value="All">ALL</option>';
                                $.each(parsedResponse, function(index, group) { 
                                        options += '<option value="' + group.id_program_groups + '" selected>' + group.program_group + '</option>';
                                });  
                                $("#program_group_id").html(options); 
           
                                $("#program_group_id").select2(); 
                            } else { 
                                $("#program_group_id").html(options); 
                                $("#program_group_id").select2(); 
                                // swal({
                                //     title: 'Groups Not Available',
                                //     text: "",
                                //     icon: 'info',
                                //     buttons: {
                                //         confirm: {
                                //             text: 'Ok',
                                //             value: true,
                                //             visible: true,
                                //             className: 'btn btn-primary',
                                //             closeModal: true
                                //         }
                                //     }
                                // }); 
                            }
                        }
                    });
                }     

     
    } 
    getting_groups_for_monthly_attendance_report();

    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>