<?php 
// Include main file
include_once('../libs/main.php');
// Check admin Login
include_once('checkLogin.php');
// Logical section of page
$clinicScheduleObj =  new  clinicSchedule();

if(isset($_REQUEST['act']) && $_REQUEST['act']=='change_status' && isset($_REQUEST['clinicSchedule_id'])){
    if(isset($_REQUEST['status'])){
        $clinicScheduleObj->changeStatus($_REQUEST['clinicSchedule_id']);
        echo RECORD_STATUS;
    }
    return true;
}

if(isset($_REQUEST['act']) && $_REQUEST['act']=='delete'){
    if(isset($_REQUEST['detailId']) && $_REQUEST['detailId']!=""){
        $clinicSchedule_detailObj = new clinicSchedule_detail();
        $clinicSchedule_detailObj->deleteFromId($_REQUEST['detailId']);
        $successMsg = base64_encode(RECORD_DELETE);    
    }
    if(isset($_REQUEST['consultation_id']) && $_REQUEST['consultation_id']!=""){
        $clinicScheduleObj = new clinicSchedule();
        $clinicScheduleObj->deleteFromId($_REQUEST['consultation_id']);
        $clinicSchedule_detailObj = new clinicSchedule_detail();
        $deleteWhere =array('clinicSchedule_detail_schedule_id='=>$_REQUEST['consultation_id']);
        $arg = array('where'=>$deleteWhere);
        $clinicSchedule_detailObj->deleteFromFields($arg);
        $successMsg = base64_encode(RECORD_DELETE);    
    }
}
if(isset($_REQUEST['action']) && $_REQUEST['action']!=''){
    if($_REQUEST['action']=="activate_all"){
        $data=array("clinicSchedule_status" => '1');
        $chk_data = $_POST['check'];
        $rs = $clinicScheduleObj->updateManyRecords($data, $_POST['check']);
        $successMsg = base64_encode(RECORD_ALL_ACTIVE);
    }else if($_REQUEST['action']=="inactivate_all"){
        $data=array("clinicSchedule_status" => '0');
        $chk_data = $_POST['check'];
        $rs = $clinicScheduleObj->updateManyRecords($data, $_POST['check']);
        $successMsg = base64_encode(RECORD_ALL_INACTIVE);
    }else if($_REQUEST['action']=="delete_all"){
        $chk_del = implode(",",$_POST['check']);
        $rs = $clinicScheduleObj->deleteManyRecords($_POST['check']);
        $successMsg = base64_encode(RECORD_ALL_DELETE);
    }
}
if (isset($_REQUEST['act']) && $_REQUEST['act']=='load'){
    $records = $_REQUEST['length'];
    $start=0;
    if(isset($_GET['start']) && $_GET['start']!='0'){
        $page = $_GET['start'];
    }
    $start=($page-1)*$limit;
    $limit = array('start'=>$_GET['start'],'record'=>$records);
    
    $where = " 1=1 ";
    if(isset($_REQUEST['clinic_id']) && $_REQUEST['clinic_id']!=""){
        $where .= " and clinicSchedule_clinic_id='".$_REQUEST['clinic_id']."' "; 
    }
    $orderFields = array('clinicSchedule_id');
    
    // Array of order by 
    $orderby=array();
    if (isset($_REQUEST['order']['0']['column']) && $_REQUEST['order']['0']['column']!=""){
        $orderby = array(" ".$orderFields[$_REQUEST['order']['0']['column']]." ".$_REQUEST['order']['0']['dir']);
    }else{
        $orderby = array(" clinicSchedule_id desc ");
    }

    $data    = array('where' => $where,'orderBy'=>$orderby,'limit' => $limit);

    $clinicScheduleDetails = $clinicScheduleObj->getAllRecords($data);
    $recordsTotal   =   $clinicScheduleObj->getAllCount($data);

        
    if ($clinicScheduleDetails && count($clinicScheduleDetails)>0){
        foreach ($clinicScheduleDetails as $key => $clinicSchedule){
            $cdoctorObj = new cdoctor();
            $where =array('cdoctor_clinic_id='=>$clinicSchedule->clinicSchedule_clinic_id,'cdoctor_doctor_id='=>$clinicSchedule->clinicSchedule_doctor_id);
            $cdoctorDetails =$cdoctorObj->fromFields(array('where'=>$where));
            $clinicSchedule_detailObj = new clinicSchedule_detail();
            $where =array('clinicSchedule_detail_schedule_id='=>$clinicSchedule->clinicSchedule_id);
            $arg =array("where"=>$where);
            $alldetails =$clinicSchedule_detailObj->getAllRecords($arg);
            $string="";
            if($alldetails && count($alldetails)>0){
                foreach ($alldetails as $key => $value) {
                    $cconsultationObj =  new  cconsultation();
                    $cconsultation = $cconsultationObj->fromId($value->clinicSchedule_detail_consultation_id);
                    $string .='Consultation: '.$cconsultation->cconsultation_name.'<br/>Open Slot: '.$value->clinicSchedule_detail_openSlot.'<br/>';
                    $blockSlot = explode(",", $value->clinicSchedule_detail_blockSlot);
                    if($blockSlot && count($blockSlot)>0){
                        foreach ($blockSlot as $bkey => $block) {
                            $string .='Block Slot'.($bkey+1).': '.$block.'<br/>';
                        }
                    }
                    $string .='=========================<a href="clinic_schedulelist.php?act=delete&detailId='.$value->clinicSchedule_detail_id.'&clinic_id='.$clinicSchedule->clinicSchedule_clinic_id.'" class="btn btn-danger"><i class="nav-icon i-Close-Window font-weight-bold"></i></a><br/>';
                }
            }
            $action = '<a href="clinic_scheduleadd.php?act=edit&clinicSchedule_id='.$clinicSchedule->clinicSchedule_id.'&clinic_id='.$clinicSchedule->clinicSchedule_clinic_id.'" class="btn btn-success white-btn"><i class="nav-icon i-Pen-2 "></i></a> <a href="clinic_schedulelist.php?act=delete&consultation_id='.$clinicSchedule->clinicSchedule_id.'&clinic_id='.$clinicSchedule->clinicSchedule_clinic_id.'" class="btn btn-danger white-btn"><i class="nav-icon i-Close-Window font-weight-bold"></i></a>';
             $response[] = array('<input type="checkbox" name="check[]" id="check[]" value="'.$clinicSchedule->clinicSchedule_id.'" class="check_class" />',
                 $cdoctorDetails->cdoctor_fullname."<br/>".$clinicSchedule->clinicSchedule_day_id." - ".$clinicSchedule->clinicSchedule_day_label,
                 $clinicSchedule->clinicSchedule_startHour." to ".$clinicSchedule->clinicSchedule_endHour,
                 $string,
                 $action
                );
        }
        $result=array('recordsFiltered'=>$recordsTotal,'recordsTotal'=>$recordsTotal,'data'=>$response);
    }else{
        $result=array('recordsFiltered'=>0,'recordsTotal'=>0,'data'=>array());
    }
    echo json_encode($result);
    exit;
}


$clinicObj = new clinic();
$clinicDetails = $clinicObj->fromId($_REQUEST['clinic_id']);
// Include Template 
include_once('../libs/routs.php')
?>