
<div class="wrapper">
    <div class="container">
        <div class="row">
            <div class="col-sm-12">
                <div class="btn-group pull-right m-t-15">
                    

                </div>
                <h4 class="page-title">Membership Services</h4>

            </div>
        </div>
        <?php if ($this->session->flashdata('Error') && $this->session->flashdata('Error') != ""){  ?>
        <div class="row">
            <div class="col-sm-12">
                <div class="alert alert-danger">
                    <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
                    <strong>Error!</strong> <?php echo $this->session->flashdata('Error'); ?>
                </div>
            </div>
        </div>
        <?php } ?>
        <div class="row">
            <div class="col-md-4">
                <div class="card-box">
                    <div class="row">
                        <div class="col-md-4">
                            <label class="form-label">Name</label>
                        </div>
                        <div class="col-md-8">
                            <span ><?php echo $enrollmentservices[0]['customer_name'] ;?></span>
                            
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-4">
                            <label class="form-label">Customer Cell</label>
                        </div>
                        <div class="col-md-8">
                            <span ><?php echo $enrollmentservices[0]['customer_cell'] ;?></span>
                           
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-4">
                            <label class="form-label">Date of Birth</label>
                        </div>
                        <div class="col-md-8">
                            <span ><?php echo $enrollmentservices[0]['customer_birthday'].' '.$enrollmentservices[0]['customer_birthmonth'] ;?></span>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-4">
                            <label class="form-label">Program</label>
                        </div>
                        <div class="col-md-8">
                            <span ><?php echo $enrollmentservices[0]['program'];?></span>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-4">
                            <label class="form-label">Session</label>
                        </div>
                        <div class="col-md-8">
                            <span ><?php echo $enrollmentservices[0]['program_session_start'].' To '.$enrollmentservices[0]['program_session_end'];?></span>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-4">
                            <label class="form-label">Member Since</label>
                        </div>
                        <div class="col-md-8">
                            <span ><?php echo $enrollmentservices[0]['start_date'];?></span>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-4">
                            <label class="form-label">Session Validity</label>
                        </div>
                        <div class="col-md-8">
                            <span <?php if($enrollmentservices[0]['validity']=='Valid'){echo 'class="text-success"';}else{echo 'class="text-danger"';}?>> <i <?php if($enrollmentservices[0]['validity']=='Valid'){echo 'class="fa fa-check"';}else {echo 'class="fa fa-close"';}?> ></i> <?php echo $enrollmentservices[0]['validity'];?></span>
                        </div>
                    </div>
                    <hr/>
                    <div class="row">
                        <div class="col-md-4">
                            <label class="form-label">Address</label>
                        </div>
                        <div class="col-md-8">
                            <span ><?php echo $enrollmentservices[0]['customer_address'];?></span>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-4">
                            <label class="form-label">Customer Cell</label>
                        </div>
                        <div class="col-md-8">
                            <span ><?php echo $enrollmentservices[0]['customer_cell'];?></span>
                        </div>
                    </div>
                    <hr/>
                    <div class="row">
                        <div class="col-md-4">
                            <label class="form-label">This Month's Payment</label>
                        </div>
                        <div class="col-md-8">
                            
                            <?php $paymentreceived = 'Pending'; 
                            $thismonth = date('M');
                            $thisyear = date('Y');
                            $lastpaidmonth='';
                            $lastpaidyear='';
                            
                            foreach($months as $month){
                                
                                if($month['month_name'] == substr($programsessionpayments[0]['to_month'],0,3)){
                                    $lastpaidmonth=$month['month'];
                                    $lastpaidyear=substr($programsessionpayments[0]['to_month'],-4);
                                }
                                
                            }
                            $paiddate = date($lastpaidyear.'-'.$lastpaidmonth.'-31');
                            $today = date('Y-m-d');
                            if($paiddate>=$today){
                                $paymentreceived = 'Received'; 
                            }                                
                            
                            ?>
                            <span <?php if($paymentreceived=='Received'){echo "class='text-success'";}else{echo "class='text-danger'";}  ?>><strong><?php echo $paymentreceived;?></strong></span>
                        </div>
                    </div>
                   
                </div>
            </div>
            <div class="col-md-8">
                <div class="card-box">
                    <div class="row">
                        <form id='avail_form' action="<?php echo base_url();?>programs_controller/create_visits" method="post">
                        <div class="col-md-12">
                            <h4>Available Services <?php echo date('Y');?>:</h4>
                            <input type="hidden" name="customer_id" value="<?php echo $enrollmentservices[0]['id_customers'];?>">
                            <input type="hidden" name="enrollment_id" value="<?php echo $enrollment_id;?>">
                            
                            <table class="table">
                                <thead>
                                    <tr>
                                        <th></th>
                                        <?php foreach($months as $month){?>
                                        <th><?php echo $month['month_name'];?></th>
                                        <?php } ?>

                                    </tr>
                                </thead>
                                <tbody>
                                <?php foreach($enrollmentservices as $service){?>
                                <tr>
                                    <td>
                                        <?php  for($x=1; $x <= $service['per_month']; $x++){?>
                                        <input id='serviceid_<?php echo $service['id_business_services'].'_'.$x; ?>' type='hidden' name="service_id[]" serviceid='<?php echo $service['id_business_services'];?>' value=''>
                                        <?php } ?>
                                        <label for="checkbox_<?php echo $service['id_business_services'];?>">
                                                <?php echo $service['service_name'];?>                                                
                                        </label>
                                        
                                    </td>
                                    <?php $html='';//$html='<td><i class="fa fa-close"></i></td>'; 
                                    foreach($months as $month){?>
                                        <?php  for($x=1; $x <= $service['per_month']; $x++){?>
                                            <?php if($month['month_name']==date('M')){?>
                                                <?php $html .= "<div class='checkbox checkbox-pink'><input onclick='update_service(".$service["id_business_services"].",".$x.");' name='services_to_get[]' id='checkbox_".$service['id_business_services']."_".$x."' serviceid='".$service['id_business_services']."' row='".$x."' type='checkbox'><label for='checkbox_".$service['id_business_services']."_".$x."'>Avail</label></div>"; ?>
                                            <?php } else { ?>
                                                <?php $found=false; foreach($services_booked as $booked){ ?>
                                                    <?php if($month['month_name'].' '.date('Y') == $booked['visit_month'] && $booked['service_id']==$service['id_business_services']){?>
                                                        <?php $html .= '<i class="fa fa-check text-success">'; $found=true;?>
                                                    <?php } ?>
                                                <?php } if($found==false){ $html .= '<i class="fa fa-close text-danger">';} ?>
                                            <?php } ?>    
                                        <?php } ?>
                                    <?php echo '<td>'.$html.'</td>'; $html='';?>
                                    <?php } ?>
                                </tr>
                                <?php } ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                    <hr>
                    <div class="row">
                        <div class='col-md-12' >
                            <h4>Book Now:</h4>
                        </div>
                        <div class='col-md-4' >
                            <input  type="text" class="form-control" placeholder="Date" id="ondate" name="ondate"> 
                        </div>
                        <div class='col-md-2' >
                            <input  type="text" class="form-control" placeholder="Time" id="ontime" name="ontime"> 
                        </div>
                        <div class='col-md-4' >
                            <select  class="form-control" placeholder="Staff" id="staffid" name="staffid"> 
                                <?php foreach($staff as $s){?>
                                <option value="<?php echo $s->id_staff;?>"><?php echo $s->staff_fullname;?></option>
                                <?php } ?>
                            </select>
                        </div>
                        <div class='col-md-2'>
                            <input type="hidden" name="csrf_test_name" id="availform_csrf" value=""/>
                            <button style="float:right" class="btn btn-pink" type="button" onclick="$('#availform_csrf').val($('#cook').val()); submit_form();">Create Visit</button>
                            
                        </div>
                        
                    </div>
                    </form>
                </div>
            </div>
            
        </div>

    </div>


    <script>
    $(document).ready(function() {
        $('#ondate').datepicker({
            default: today,
            autoclose: true,
            todayHighlight: true,
            format: 'yyyy-mm-dd'
        }).datepicker('setDate', new Date());
        
        var opentime ='<?php $to = explode(':', $business[0]['business_opening_time']); echo $to[0];?>';
        var closetime ='<?php $tc = explode(':', $business[0]['business_closing_time']); echo $tc[0];?>';
        $('#ontime').timepicker({
            showMeridian: false                
            , maxHours: closetime
            , minuteStep: 15
            ,showInputs: true
        }).on('changeTime.timepicker', function(e) {
            if(e.time.hours < opentime){
                $(this).timepicker('setTime', opentime + ":00");
            }
        });
        
        $("#staffid").select2();
        
    });
        function update_service(serviceid, id){
            var checkbox = "#checkbox_"+ serviceid+"_"+id;
            console.log($(checkbox).prop("checked"));
            
            var temp = "#serviceid_"+ serviceid+"_"+id;
            
            console.log(temp);
            if($(checkbox).prop("checked")===true){
                $(temp).val($(checkbox).attr('serviceid'));
            } else {
                $(temp).val('');
            }                
           
        }
    
        function submit_form(){
            
            var temp ='';
            $('input[type=checkbox]').each(function(){
                //console.log($(this).prop("checked"));
                var temp = "#serviceid_"+ $(this).attr('serviceid')+"_"+$(this).attr('row');
                if($(this).prop("checked")===true){
                    $(temp).val($(this).attr('serviceid'));
                } else {
                    $(temp).val('');
                }                
            });
            
            $('#avail_form').submit();
        }
    
    </script>