<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">Mark 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 class="header-title m-t-0 m-b-30">Filters:</h4>
                    <form action="<?php echo base_url(); ?>coaching_group_mark_student_attendance/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">Select 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_staff_id()">
                                    </div>
                                    </div>
                                 </div> 
                                <div class="col-lg-2">
                                    <div class="row">
                                        <div class="col-lg-12"> <label class="control-label">Select Staff:</label></div>
                                    </div>
                                    <div class="row">
                                        <div class="col-lg-12"> 
                                            <select  class="form-control" id="staff_id" name="staff_id" onchange="getting_services_by_staff_id()">
                                                <option >Select Staff</option> 
                                                <?php foreach ($staff as $row) { ?>
                                                    <option value="<?php echo $row['id_staff']; ?>" <?php echo ($program_staff_id == $row['id_staff']) ? "selected" : ""; ?>><?php echo $row['staff_fullname']; ?></option>
                                                <?php } ?>
                                            </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 Group :</label></div>
                                    </div>
                                    <div class="row">
                                        <div class="col-lg-12"> 
                                            <select  class="form-control" id="coaching_service" name="id_program_group">
                                            </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 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%">Visit Date</th>
                                        <th width="28%">Start Time</th>
                                        <th width="28%">End Time</th>
                                        <th width="28%">Mark In</th>
                                        <th width="28%">Mark In Time</th>
            
                                                 
                                    </tr>
                                </thead>
                                <tbody>  
         
                                     <?php  
                                          if(isset($program_group_attendance_report)){
                                             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('Y-m-d', strtotime($row->group_visit_date)); ?></td>

                                                    <td><?php echo $row->visit_service_start  ; ?></td>
                                                    <td><?php echo $row->visit_service_end  ; ?></td>
                                                    <td class="mark_in_attendance_colum"><?php if($row->inservice =="Yes"){
                                                                 ?>
                                                                 <span style="cursor:pointer;" class="text-primary mark_in_service_cancel_btn"   data-id="<?php echo  $row->id_customer_visits ?>"><button class="btn btn-sm btn-danger">Remove</button></span>
                                                                 <?php
                                                                 }else{ 
                                                                    ?>
                                                                    <span style="cursor:pointer;" class="text-primary mark_in_service_btn"  data-id="<?php  echo  $row->id_customer_visits ?>" ><button class="btn btn-sm btn-primary">Mark In</button></span>
                                                                <?php 
                                                               }
                                                     ?></td> 
                                                    <td class="mark_in_time_attendance_colum" > <?php echo $row->inservice_time  ; ?></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();
        // mark In Customer 
        $(document).on("click",".mark_in_service_btn", function(){  
           var customer_visitid = $(this).data('id');   
           var mDate = $("#mDate").val();
           var thisbtn = $(this); 
           swal({
              title: "Are you sure?",
            text: "",
            type: "info",
            showCancelButton: true,
            confirmButtonClass: 'btn-info',
            confirmButtonText: "Yes, Mark In!",
            closeOnConfirm: true
            }, function() {
                    $.ajax({
                        type: 'POST',
                        //url: 'Scheduler_controller/staffreporting',
                        url: "<?php echo base_url() . 'Coaching_controller/coaching_group_mark_student_attendance_mark_in'; ?>",
                        //data: 'staff_id=' +staff_id+ '&calendar_date=' +calendar_date,
                        data: {
                            customer_visit_id: customer_visitid,
                            attendance_date:mDate
                        },
                        success: function(response) { 
                             var response = JSON.parse(response);
                            if (response.status == "success") {
                                toastr.success("Operation was successful!");  
                                var html =' <span style="cursor:pointer;" class="text-primary mark_in_service_cancel_btn"  data-id="'+customer_visitid+'" ><button class="btn btn-sm btn-danger">Remove</button></span>';
                                thisbtn.closest('tr').find('.mark_in_time_attendance_colum').html(response.inservice_time);
                                thisbtn.closest('tr').find('.mark_in_attendance_colum').html(html);
                            } else {
                                toastr.error("Something Went Wrong");
                            }

                            
                        }
                    });     
            });
        });  




          // mark In Customer 
          $(document).on("click",".mark_in_service_cancel_btn", function(){  
           var customer_visitid = $(this).data('id');   
           var thisbtn = $(this); 
          
           swal({
              title: "Are you sure?",
            text: "",
            type: "warning",
            showCancelButton: true,
            confirmButtonClass: 'btn-warning',
            confirmButtonText: "Yes, Remove it!",
            closeOnConfirm: true
            }, function() {
                    $.ajax({
                        type: 'POST',
                        //url: 'Scheduler_controller/staffreporting',
                        url: "<?php echo base_url() . 'Coaching_controller/coaching_group_mark_student_attendance_cancel'; ?>",
                        //data: 'staff_id=' +staff_id+ '&calendar_date=' +calendar_date,
                        data: {
                            customer_visit_id: customer_visitid
                        },
                        success: function(response) { 
                             var response = JSON.parse(response);
                            if (response.status == "success") {
                                toastr.success("Operation was successful!");  
                                var html =' <span style="cursor:pointer;" class="text-primary mark_in_service_btn"  data-id="'+customer_visitid+'" ><button class="btn btn-sm btn-primary">Mark In</button></span>';
                                thisbtn.closest('tr').find('.mark_in_time_attendance_colum').html('');
                                thisbtn.closest('tr').find('.mark_in_attendance_colum').html(html);
                            } else {
                                toastr.error("Something Went Wrong");
                            }

                            
                        }
                    });     
            });
        }); 



        


        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_services_by_staff_id(){ 
      var id_staff = $("#staff_id option:selected").val();
      var selecteddate = $("#mDate").val(); 
                $.ajax({
                        type: 'POST',
                        //url: 'Scheduler_controller/staffreporting',
                        url: "<?php echo base_url() . 'Coaching_controller/getting_groups_by_staff_id'; ?>",
                        //data: 'staff_id=' +staff_id+ '&calendar_date=' +calendar_date,
                        data: {
                            staff_id: id_staff,
                            selected_date:selecteddate
                        },
                        success: function(response) { 
                            var parsedResponse = JSON.parse(response); 
                            var options = '<option value="individual">None</option>'; 
                            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_services_by_staff_id()
</script>