<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">Member Group 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 class="header-title m-t-0 m-b-30">Filters:</h4>
                    <form action="<?php echo base_url(); ?>coaching_group_attendance_report/5" method="post">
                        <input type="hidden" name="csrf_test_name" id="members_csrf" value=""/>
                    <div class="row">
                        <div class="col-lg-12">  
                            
                          <div class="row"> 
                          <div class="row">
                                <div class="col-lg-2">
                                    <div class="row">
                                        <div class="col-lg-12"> <label class="control-label">Start Date</label></div>
                                    </div>
                                    <div class="row">
                                        <div class="col-lg-12"> 
                                        <input class="form-control mdate" id="mDate" name="mDate" value="<?php echo $today;?>" onchange="getting_services_by_date_date()">
                                    </div>
                                    </div>
                                 </div> 
                                <div class="col-lg-2">
                                    <div class="row">
                                        <div class="col-lg-12"> <label class="control-label">End Date</label></div>
                                    </div>
                                    <div class="row">
                                        <div class="col-lg-12"> 
                                        <input class="form-control mdate" id="eDate" name="eDate" value="<?php echo $enddate;?>" onchange="getting_services_by_date_date()">
                                    </div>
                                    </div>
                                 </div> 
                               
                                 <div class="row">
                                   <div class="col-lg-2">
                                    <div class="row">
                                        <div class="col-lg-12"> <label class="control-label">Select Group :</label></div>
                                    </div>
                                    <div class="row">
                                        <div class="col-lg-12"> 
                                            <select  class="form-control id_program_group" id="coaching_service" name="id_program_group" require onchange="getting_members_by_group()">
                                            </select>
                                        </div>
                                    </div>
                                </div>  
                                 <div class="row">
                                   <div class="col-lg-2">
                                    <div class="row">
                                        <div class="col-lg-12"> <label class="control-label">Select Member :</label></div>
                                    </div>
                                    <div class="row">
                                        <div class="col-lg-12"> 
                                            <select  class="form-control " id="member_id" name="member_id" >
                                            </select>
                                        </div>
                                    </div>
                                </div>  

                               

                                
                                
                                <!-- <div class="col-lg-2">
                                    <div class="row">
                                        <div class="col-lg-12"> <label class="control-label">View:</label></div>
                                    </div>
                                    <div class="row">
                                        <div class="col-lg-12"> 
                                            <select class="form-control" id="filter_veiw" name="filter_veiw">                                               
                                                <option <?php if(isset($status)){if($status=='Active'){echo 'selected="selected"';}} ?> value="Active">Active</option>
                                                <option <?php if(isset($status)){if($status=='Completed'){echo 'selected="selected"';}} ?> value="Completed">Completed</option>
                                                <option <?php if(isset($status)){if($status=='All'){echo 'selected="selected"';}} ?> value="All">All</option>                                                
                                            </select>
                                        </div>
                                    </div>
                                </div> -->
                               
                                <div class="col-lg-2">
                                    <div class="row">
                                        <div class="col-lg-12"> <label class="control-label"></label></div>
                                    </div>
                                    <div class="row">
                                        <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6" > 
                                            <button onclick="$('#members_csrf').val($('#cook').val());"  type="submit" class="btn btn-pink btn-bordred waves-effect w-md waves-light m-b-5">Run</button>
                                        </div>
                                        <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6"><i id="cog" class="fa fa-spin fa-cog" style="display:none; font-size:26px;width: auto;margin-right: 10px;"></i></div>
                                    </div>
                                </div>

                            </div>
                        </div>
                    </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"></h4>
                    <div class="row">
                        <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 width="2%">No #</th>
                                        <th width="28%">Name</th>
                                        <th width="28%">Group Visit Date</th>
                                        <th width="28%">Start Time</th>
                                        <th width="28%">End Time</th>
                                        <th width="28%">Attendance Time</th>
                                        <th width="28%">Present</th>
                                        <th width="28%">Absent</th>
            
                                                 
                                    </tr>
                                </thead>
                                <tbody>  
         
                                     <?php  
                                          if(isset($program_group_attendance_report)){ 
                                            $presents=0;
                                            $present_counter =0;
                                            $absents=0;
                                            $absents_counter =0; 
                                            ?>
                                            <tr>
                                                <th>Group Name </th>
                                                <th><?php if(isset($program_group_attendance_report[0]->program_group)){echo $program_group_attendance_report[0]->program_group;} ?></th>
                                                <td></td>
                                                <td></td>
                                                <td></td>
                                                <td></td>
                                                <td></td>
                                                <td></td>
                                            </tr>
                                            <?php
                                             foreach($program_group_attendance_report  as $row){    
                                                ?> 
                                                <tr>
                                                    <td><?php echo  $row->id_customer_visits; ?></td>
                                                    <td><?php echo $row->customer_name  ; ?></td>
                                                    <td> <?php  echo date_format(date_create($row->group_visit_date), 'd-M-Y'); ?></td>
                                                    <td><?php echo $row->visit_service_start  ; ?></td>
                                                    <td><?php echo $row->visit_service_end  ; ?></td>
                                                    <td><?php echo $row->inservice_time  ; ?></td>

                                                     <td class="text-center <?php if($row->inservice =="Yes"){ ?>text-success<?php } ?>">
                                                            <?php if($row->inservice =="Yes"){ ?>
                                                                <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->inservice =="No"){ ?>text-danger<?php } ?>">
                                                        <?php if($row->inservice =="No"){ ?>
                                                            <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    
                                             }
                                            }
                                     ?>
                                   
                                </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'
            });
        
        $("#staff_id,#coaching_service").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,
		     "ordering": false,
	    	"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_services_by_date_date(){ 
      var selecteddate = $("#mDate").val(); 
       var enddate = $("#eDate").val(); 
                $.ajax({
                        type: 'POST',
                        //url: 'Scheduler_controller/staffreporting',
                        url: "<?php echo base_url() . 'Coaching_controller/getting_groups_by_date_range'; ?>",
                        //data: 'staff_id=' +staff_id+ '&calendar_date=' +calendar_date,
                        data: {
                            selected_date:selecteddate,
                            enddate:enddate,
                        },
                        success: function(response) {  
                            
                            var parsedResponse = JSON.parse(response); 
                            var options = ''; 
                            if (parsedResponse.length > 0) { 
                                $.each(parsedResponse, function(index, group) { 
                                    if ("<?php echo $program_group_id; ?>" == group.id_program_groups) {
                                        options += '<option value="' + group.id_program_groups + '" selected>' + group.program_group + '</option>';
                                    } else {
                                        options += '<option value="' + group.id_program_groups + '">' + group.program_group + '</option>';
                                    }
                                });  
                                $("#coaching_service").html(options); 
                                // Initialize or refresh Select2 
                                $("#coaching_service").select2(); 
                            } else { 
                                $("#coaching_service").html(options); 
                                $("#coaching_service").select2(); 
                                swal({
                                    title: 'Groups Not Available',
                                    text: "",
                                    icon: 'info',
                                    buttons: {
                                        confirm: {
                                            text: 'Ok',
                                            value: true,
                                            visible: true,
                                            className: 'btn btn-primary',
                                            closeModal: true
                                        }
                                    }
                                }); 
                            } 

                            getting_members_by_group();
                        }
                    });     

    } 

    getting_services_by_date_date() 

    function getting_members_by_group(){  
     var group_id = $("#coaching_service").val();


                     $.ajax({
                        type: 'POST',
                        //url: 'Scheduler_controller/staffreporting',
                        url: "<?php echo base_url() . 'Coaching_controller/getting_members_by_group'; ?>",
                        //data: 'staff_id=' +staff_id+ '&calendar_date=' +calendar_date,
                        data: {
                            group_id:group_id
                        },
                        success: function(response) { 
                            var parsedResponse = JSON.parse(response);  
                            console.log(parsedResponse);
                            var options = '<option value="All">All</option>'; 
                            if (parsedResponse.length > 0) { 
                                $.each(parsedResponse, function(index, member) { 
                                    if ("<?php echo $member_id; ?>" == member.id_customers) {
                                        options += '<option value="' + member.id_customers + '" selected>' + member.customer_name + '</option>';
                                    } else {
                                        options += '<option value="' + member.id_customers + '">' + member.customer_name + '</option>';
                                    }
                                });  
                                $("#member_id").html(options); 
                                // Initialize or refresh Select2 
                                $("#member_id").select2(); 
                            } else { 
                                $("#member_id").html(options); 
                                $("#member_id").select2(); 

                            }
                        }
                    });     

    }

</script>