<script type="text/javascript">

function validate_clinicSchedule(){
	
    if($("#clinicSchedule_consultation_id").val()==''){
        $("#clinicSchedule_consultation_id").addClass('error');
        $("#clinicSchedule_consultation_id").focus();
        $("#msg_clinicSchedule_consultation_id").html('Please select Consultantion type').show();
        return false;
    }else{
        $("#clinicSchedule_consultation_id").removeClass('error');
        $("#msg_clinicSchedule_consultation_id").hide();
    }
    if($("#clinicSchedule_day").val()==''){
        $("#clinicSchedule_day").addClass('error');
        $("#clinicSchedule_day").focus();
        $("#msg_clinicSchedule_day").html('Please select day.').show();
        return false;
    }else{
        $("#clinicSchedule_day").removeClass('error');
        $("#msg_clinicSchedule_day").hide();
    }
    if($("#clinicSchedule_doctor_id").val()==''){
        $("#clinicSchedule_doctor_id").addClass('error');
        $("#clinicSchedule_doctor_id").focus();
        $("#msg_clinicSchedule_doctor_id").html('Please select provider.').show();
        return false;
    }else{
        $("#clinicSchedule_doctor_id").removeClass('error');
        $("#msg_clinicSchedule_doctor_id").hide();
    }
    if($("#clinicSchedule_startHour").val()==''){
        $("#clinicSchedule_startHour").addClass('error');
        $("#clinicSchedule_startHour").focus();
        $("#msg_clinicSchedule_startHour").html('Please select start Hour').show();
        return false;
    }else{
        $("#clinicSchedule_startHour").removeClass('error');
        $("#msg_clinicSchedule_startHour").hide();
    }
    if($("#clinicSchedule_endHour").val()==''){
        $("#clinicSchedule_endHour").addClass('error');
        $("#clinicSchedule_endHour").focus();
        $("#msg_clinicSchedule_endHour").html('Please select end Hour').show();
        return false;
    }else{
        $("#clinicSchedule_endHour").removeClass('error');
        $("#msg_clinicSchedule_endHour").hide();
    }
    if($("#clinicSchedule_openSlot").val()==''){
        $("#clinicSchedule_openSlot").addClass('error');
        $("#clinicSchedule_openSlot").focus();
        $("#msg_clinicSchedule_openSlot").html('Please select end Hour').show();
        return false;
    }else{
        $("#clinicSchedule_openSlot").removeClass('error');
        $("#msg_clinicSchedule_openSlot").hide();
    }
}

</script>


<div class="breadcrumb">
    <h1><?php echo $clinicDetails->clinic_name ?> | Clinic Schedule</h1>
    <ul>
        <li><a href="<?php echo SITE_URL.ADMIN_DIR ?>">Dashboard</a></li>
        <li>Add New Clinic Schedule</li>

    </ul>
</div>


<div class="separator-breadcrumb border-top"></div>

<div class="row">
    <div class="col-md-12">
        <div class="card mb-4">
            <div class="card-body">
                <div class="card-title mb-3">Add New Clinic Schedule</div>
                <form action="" method="post" onsubmit="return validate_clinicSchedule();" enctype="multipart/form-data">
                    <?php if(isset($clinicScheduleDetail->clinicSchedule_id) && $clinicScheduleDetail->clinicSchedule_id!=0) { ?>
                            <input type="hidden" id="clinicSchedule_id" name="clinicSchedule_id" value="<?php echo $clinicScheduleDetail->clinicSchedule_id ?>" />
                    <?php } ?>
                    <input type="hidden" name="clinic_id" id="clinic_id" value="<?php echo $_GET['clinic_id'] ?>" />
                    <div class="row">
                         <?php if(!isset($clinicScheduleDetail) ){ ?> 
                            <div class="col-md-2 form-group mb-3" style="visibility: hidden;">
                                <label for="clinicSchedule_doctor_id">Provider <span class="asterisk">*</span></label>
                                <select class="form-control provider" id="clinicSchedule_doctor_id" name="clinicSchedule_doctor_id[]" multiple="multiple">
                                    
                                    <?php if($allProviders && count($allProviders) > 0){ 
                                        foreach($allProviders as $provider){ ?>
                                        <option value="<?php echo $provider->cdoctor_doctor_id; ?>" selected='selected' >
                                            <?php echo $provider->cdoctor_fullname ?>
                                        </option>
                                    <?php } } ?>
                                </select>    
                                <div class="invalid-tooltip" id="msg_clinicSchedule_doctor_id"></div>
                            </div>
                        <?php }else{?>
                            <div class="col-md-2 form-group mb-3" style="visibility: hidden;">
                                <label for="clinicSchedule_doctor_id">Provider <span class="asterisk">*</span></label>
                                <select class="form-control" id="clinicSchedule_doctor_id" name="clinicSchedule_doctor_id">
                                    
                                    <?php if($allProviders && count($allProviders) > 0){ 
                                        foreach($allProviders as $provider){ ?>
                                        <option value="<?php echo $provider->cdoctor_doctor_id; ?>" 
                                            <?php echo ($clinicScheduleDetail->clinicSchedule_doctor_id == $provider->cdoctor_doctor_id)?"selected='selected'":"" ?> ><?php echo $provider->cdoctor_fullname ?></option>
                                    <?php } } ?>
                                </select>    
                                <div class="invalid-tooltip" id="msg_clinicSchedule_doctor_id"></div>
                            </div>
                        <?php } ?>
                        <?php if(!isset($clinicScheduleDetail) ){ ?> 
                            <div class="col-md-2 form-group mb-3">
                                <label for="clinicSchedule_day">Day <span class="asterisk">*</span></label>
                                <select class="form-control day" id="clinicSchedule_day" name="clinicSchedule_day[]" multiple="multiple">
                                    
                                    <?php if($allDay && count($allDay) > 0){ 
                                        foreach($allDay as $day){ ?>
                                        <option value="<?php echo $day; ?>" 
                                            <?php echo (in_array($clinicScheduleDetail->clinicSchedule_day_id, explode("-",$day)))?"selected='selected'":"" ?> ><?php echo explode("-",$day)[1]; ?></option>
                                    <?php } } ?>
                                </select>    
                                <div class="invalid-tooltip" id="msg_clinicSchedule_day"></div>
                            </div>
                        <?php }else{?>
                            <div class="col-md-2 form-group mb-3">
                                <label for="clinicSchedule_day">Day <span class="asterisk">*</span></label>
                                <select class="form-control" id="clinicSchedule_day" name="clinicSchedule_day">
                                    
                                    <?php if($allDay && count($allDay) > 0){ 
                                        foreach($allDay as $day){ ?>
                                        <option value="<?php echo $day; ?>" 
                                            <?php echo (in_array($clinicScheduleDetail->clinicSchedule_day_id, explode("-",$day)))?"selected='selected'":"" ?> ><?php echo explode("-",$day)[1]; ?></option>
                                    <?php } } ?>
                                </select>    
                                <div class="invalid-tooltip" id="msg_clinicSchedule_day"></div>
                            </div>
                        <?php } ?>
                        <div class="col-md-2 form-group mb-3">
                            <label for="clinicSchedule_startHour">Start Time <span class="asterisk">*</span></label>
                            <select class="form-control" id="clinicSchedule_startHour" name="clinicSchedule_startHour" >
                                <?php if($allHours && count($allHours) > 0){ 
                                    foreach($allHours as $hours){ ?>
                                    <option value="<?php echo $hours; ?>" <?php echo ($clinicScheduleDetail->clinicSchedule_startHour==$hours)?"selected='selected'":"" ?> ><?php echo $hours; ?></option>
                                <?php } } ?>
                            </select>    
                            <div class="invalid-tooltip" id="msg_clinicSchedule_startHour"></div>
                        </div>
                        <div class="col-md-2 form-group mb-3">
                            <label for="clinicSchedule_endHour">End Time <span class="asterisk">*</span></label>
                            <select class="form-control" id="clinicSchedule_endHour" name="clinicSchedule_endHour" >
                                
                                <?php if($allHours && count($allHours) > 0){ 
                                    foreach($allHours as $hours){ ?>
                                    <option value="<?php echo $hours; ?>" <?php echo ($clinicScheduleDetail->clinicSchedule_endHour==$hours)?"selected='selected'":"" ?> ><?php echo $hours; ?></option>
                                <?php } } ?>
                            </select>    
                            <div class="invalid-tooltip" id="msg_clinicSchedule_endHours"></div>
                        </div>
                    </div>
                    <div id="cloneAppend">
                        <?php 
                            $detailCount =0;
                            $conCount =count($allconsultation);
                        ?>

                        <?php if(isset($allDetails) && count($allDetails)>0){
                            $detailCount =count($allDetails);
                            foreach ($allDetails as $key => $detail) {
                                $allStart =array();
                                $allEnd = array();
                                $slots =explode(",", $detail->clinicSchedule_detail_blockSlot);
                                if($slots && count($slots)>0){
                                    foreach ($slots as $skey => $slot) {
                                        array_push($allStart, explode("-", $slot)[0]);
                                        array_push($allEnd, explode("-", $slot)[1]);
                                    }
                                }
                                ?>
                                <div class="row" id="cloneId">
                                    <div class="col-md-3 form-group mb-3">
                                        <label for="clinicSchedule_name">Consultantion Type<span class="asterisk">*</span></label>
                                        <select class="form-control" id="clinicSchedule_detail_consultation_id" name="clinicSchedule_detail_consultation_id[]" placeholder="Select Consultantion">
                                            
                                            <?php 
                                               if($allconsultation && count($allconsultation)>0){
                                                foreach ($allconsultation as $ckey => $value) {?>
                                                    <option value="<?php echo $value->cconsultation_id ?>" <?php echo ($value->cconsultation_id==$detail->clinicSchedule_detail_consultation_id)?"selected='selected'":""?> ><?php echo $value->cconsultation_name ?></option>
                                                <?php }
                                               } 
                                            ?>
                                        </select>
                                    </div>
                                    <div class="col-md-3 form-group mb-3">
                                        <label for="clinicSchedule_desc">How Many Open Slot <span class="asterisk">*</span></label>
                                        <input type="text" class="form-control" id="clinicSchedule_detail_openSlot" name="clinicSchedule_detail_openSlot[]" value="<?php echo $detail->clinicSchedule_detail_openSlot ?>" /> 
                                    </div>
                                    <div class="col-md-2 form-group mb-3">
                                        <label for="clinicSchedule_detail_blockSlot<?php echo $key+1 ?>">Block Time Start <span class="asterisk">*</span></label>
                                        <select class="form-control blockSlot" id="clinicSchedule_detail_blockSlotstart<?php echo $key+1 ?>" name="clinicSchedule_detail_blockSlotstart<?php echo $key+1 ?>[]" multiple="multiple">
                                            
                                            <?php if($allHours && count($allHours) > 0){ 
                                                foreach($allHours as $hours){ ?>
                                                <option value="<?php echo $hours; ?>" 
                                                    <?php echo (in_array($hours, $allStart))?"selected='selected'":""  ?>
                                                    ><?php echo $hours; ?></option>
                                            <?php } } ?>
                                        </select>    
                                    </div>
                                    <div class="col-md-2 form-group mb-3">
                                        <label for="clinicSchedule_detail_blockSlot<?php echo $key+1 ?>">Block Time End <span class="asterisk">*</span></label>
                                        <select class="form-control blockSlot" id="clinicSchedule_detail_blockSlotend<?php echo $key+1 ?>" name="clinicSchedule_detail_blockSlotend<?php echo $key+1 ?>[]" multiple="multiple" >
                                           
                                            <?php if($allHours && count($allHours) > 0){ 
                                                foreach($allHours as $hours){ ?>
                                                <option value="<?php echo $hours; ?>"
                                                    <?php echo (in_array($hours, $allEnd))?"selected='selected'":""  ?>
                                                    ><?php echo $hours; ?></option>
                                            <?php } } ?>
                                        </select>    
                                    </div>
                                    <input type="hidden" name="clinicSchedule_detail_id[]" id="clinicSchedule_detail_id" value="<?php echo $detail->clinicSchedule_detail_id ?>">
                                </div>
                           <?php }
                           $remainCount  =$conCount-$detailCount;
                           if($remainCount>0){
                                for ($i=1; $i<=$remainCount ; $i++) { ?>
                                     <div class="row" id="cloneId">
                                        <div class="col-md-3 form-group mb-3">
                                            <label for="clinicSchedule_name">Consultantion Type<span class="asterisk">*</span></label>
                                            <select class="form-control" id="clinicSchedule_detail_consultation_id_edit_add" name="clinicSchedule_detail_consultation_id_edit_add[]" placeholder="Select Consultantion">
                                                <?php 
                                                   if($allconsultation && count($allconsultation)>0){
                                                    foreach ($allconsultation as $ckey => $value) {?>
                                                        <option value="<?php echo $value->cconsultation_id ?>"><?php echo $value->cconsultation_name ?></option>
                                                    <?php }
                                                   } 
                                                ?>
                                            </select>
                                        </div>
                                        <div class="col-md-3 form-group mb-3">
                                            <label for="clinicSchedule_desc">How Many Open Slot <span class="asterisk">*</span></label>
                                            <input type="text" class="form-control" id="clinicSchedule_detail_openSlot_edit_add" name="clinicSchedule_detail_openSlot_edit_add[]" /> 
                                        </div>
                                        <div class="col-md-2 form-group mb-3">
                                            <label for="clinicSchedule_detail_blockSlot1">Block Time Start <span class="asterisk">*</span></label>
                                            <select class="form-control blockSlot" id="clinicSchedule_detail_blockSlotstart_edit_add" name="clinicSchedule_detail_blockSlotstart_edit_add<?php echo $i ?>[]" multiple="multiple">
                                                <?php if($allHours && count($allHours) > 0){ 
                                                    foreach($allHours as $hours){ ?>
                                                    <option value="<?php echo $hours; ?>"><?php echo $hours; ?></option>
                                                <?php } } ?>
                                            </select>    
                                        </div>
                                        <div class="col-md-2 form-group mb-3">
                                            <label for="clinicSchedule_detail_blockSlot2">Block Time End <span class="asterisk">*</span></label>
                                            <select class="form-control blockSlot" id="clinicSchedule_detail_blockSlotend_edit_add" name="clinicSchedule_detail_blockSlotend_edit_add<?php echo $i ?>[]" multiple="multiple" >
                                                <?php if($allHours && count($allHours) > 0){ 
                                                    foreach($allHours as $hours){ ?>
                                                    <option value="<?php echo $hours; ?>"><?php echo $hours; ?></option>
                                                <?php } } ?>
                                            </select>    
                                        </div>
                                    </div>
                                <?php }
                           }
                        }else{ 
                            $remainCount  =$conCount-$detailCount;
                            if($remainCount>0){
                                for ($i=1; $i<=$remainCount ; $i++) { ?>
                        
                        <div class="row" id="cloneId">
                            <?php if(!isset($clinicScheduleDetail) ){ ?>  
                                <div class="col-md-3 form-group mb-3">
                                    <label for="clinicSchedule_name">Consultantion Type<span class="asterisk">*</span></label>
                                    <select class="form-control consultantion" id="clinicSchedule_detail_consultation_id<?php echo $i ?>" name="clinicSchedule_detail_consultation_id<?php echo $i ?>[]" placeholder="Select Consultantion" multiple="multiple">
                                        <?php 
                                           if($allconsultation && count($allconsultation)>0){
                                            foreach ($allconsultation as $ckey => $value) {?>
                                                <option value="<?php echo $value->cconsultation_id ?>"><?php echo $value->cconsultation_name ?></option>
                                            <?php }
                                           } 
                                        ?>
                                    </select>
                                </div>
                                <div class="col-md-3 form-group mb-3">
                                    <label for="clinicSchedule_desc">How Many Open Slot <span class="asterisk">*</span></label>
                                    <input type="text" class="form-control" id="clinicSchedule_detail_openSlot<?php echo $i ?>" name="clinicSchedule_detail_openSlot<?php echo $i ?>[]" /> 
                                </div>
                            <?php }else{?>
                                <div class="col-md-3 form-group mb-3">
                                    <label for="clinicSchedule_name">Consultantion Type<span class="asterisk">*</span></label>
                                    <select class="form-control consultantion" id="clinicSchedule_detail_consultation_id" name="clinicSchedule_detail_consultation_id[]" placeholder="Select Consultantion">
                                        <?php 
                                           if($allconsultation && count($allconsultation)>0){
                                            foreach ($allconsultation as $ckey => $value) {?>
                                                <option value="<?php echo $value->cconsultation_id ?>"><?php echo $value->cconsultation_name ?></option>
                                            <?php }
                                           } 
                                        ?>
                                    </select>
                                </div>
                                <div class="col-md-3 form-group mb-3">
                                    <label for="clinicSchedule_desc">How Many Open Slot <span class="asterisk">*</span></label>
                                    <input type="text" class="form-control" id="clinicSchedule_detail_openSlot" name="clinicSchedule_detail_openSlot[]" /> 
                                </div>
                            <?php } ?>
                            
                            <div class="col-md-2 form-group mb-3">
                                <label for="clinicSchedule_detail_blockSlot1">Block Time Start <span class="asterisk">*</span></label>
                                <select class="form-control blockSlot" id="clinicSchedule_detail_blockSlotstart<?php echo $i ?>" name="clinicSchedule_detail_blockSlotstart<?php echo $i ?>[]" multiple="multiple">
                                    <?php if($allHours && count($allHours) > 0){ 
                                        foreach($allHours as $hours){ ?>
                                        <option value="<?php echo $hours; ?>"><?php echo $hours; ?></option>
                                    <?php } } ?>
                                </select>    
                            </div>
                            <div class="col-md-2 form-group mb-3">
                                <label for="clinicSchedule_detail_blockSlot2">Block Time End <span class="asterisk">*</span></label>
                                <select class="form-control blockSlot" id="clinicSchedule_detail_blockSlotend<?php echo $i ?>" name="clinicSchedule_detail_blockSlotend<?php echo $i ?>[]" multiple="multiple" >
                                    <?php if($allHours && count($allHours) > 0){ 
                                        foreach($allHours as $hours){ ?>
                                        <option value="<?php echo $hours; ?>"><?php echo $hours; ?></option>
                                    <?php } } ?>
                                </select>    
                            </div>
                        </div>
                        <?php }}} ?>
                    </div>
                    <div>
                        <div class="col-md-12">
                            <button class="btn btn-primary" id="submit" name="submit">Submit</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<script src="./assets/js/jquery.multiselect.js"></script>
<link rel="stylesheet" href="./assets/styles/css/jquery.multiselect.css">
<script type="text/javascript">
$(document).ready(function() { 
    $('.blockSlot').multiselect({
        columns: 1,
        placeholder: 'Select block time',
        search: true,
        selectAll: true
    });
    <?php if(!isset($clinicScheduleDetail) ){ ?>
    $('.day').multiselect({
        columns: 1,
        placeholder: 'Select day',
        search: true,
        selectAll: true
    });

    $('.consultantion').multiselect({
        columns: 1,
        placeholder: 'Select consultantion type',
        search: true,
        selectAll: true
    });
    $('.provider').multiselect({
        columns: 1,
        placeholder: 'Select all provider',
        search: true,
        selectAll: true
    });
    <?php } ?>
    $(".add_button").click(function(){
        $("#cloneId").clone().appendTo("#cloneAppend");
    });
});
</script>