<?php 
// Include main file
include_once('../libs/main.php');
// Check doctor Login
include_once('checkLogin.php');
$doctorObj =  new  doctor();

if(isset($_REQUEST['act']) && $_REQUEST['act']=='service' && isset($_REQUEST['speciality'])){
    $serviceObj = new service();
    $where =array('service_speciality_id='=>$_REQUEST['speciality']);
    $dataw =array('where'=>$where); 
    $allservice =  $serviceObj->getAllRecords($dataw);
    if($allservice && count($allservice)>0){foreach($allservice as $key => $val){?>
    <div class="col-md-6 form-group mb-3">
        <label class="checkbox checkbox-outline-primary inline-checkbox">
            <input type="checkbox" name="doctor_services[]" id="doctor_services_<?php echo $key ?>"  value="<?php echo $val->service_id ?>" >
            <span><?php echo $val->service_name; ?></span>
            <span class="checkmark"></span>
        </label>
    </div>    
    <?php }}
    exit;
}
if(isset($_REQUEST['act']) && $_REQUEST['act']=='edit' && isset($_REQUEST['doctor_id'])){
    $doctorDetail = $doctorObj->fromId($_REQUEST['doctor_id']);
//    $doctorhours = json_decode($doctorDetail->doctor_hours,1);
    $quesData = json_decode($doctorDetail->doctor_question,1);
    $serviceObj = new service();
    $where =array('service_speciality_id='=>$doctorDetail->doctor_speciality);
    $dataw =array('where'=>$where); 
    $allservice =  $serviceObj->getAllRecords($dataw);
    
}
if(isset($_REQUEST['act']) && $_REQUEST['act']=='validate_email'){
    if($_REQUEST['email']==''){
        echo ENTER_EMAIL;
        exit;
    }else if(validateEmail($_REQUEST['email'])){
        $select=array('doctor_email','doctor_id');
        $where =array('doctor_email='=>$_REQUEST['email']);
        if(isset($_REQUEST['doctor_id']) && $_REQUEST['doctor_id']!=""){
            $where['doctor_id!='] =$_REQUEST['doctor_id'];
        }
        $data =array('fields'=>$select,'where'=>$where);        
        if(isset($_REQUEST['doctor_id']) && $doctorObj->fromFields($data)){
            echo EMAIL_EXIST;
            exit;
        }else if(!isset($_REQUEST['doctor_id']) && $doctorObj->fromFields($data)){
            echo EMAIL_EXIST;
            exit;
        }
    }else{
        echo VALID_EMAIL;
        exit;
    }
    return true;
}
if(isset($_POST['submit'])){
    $doctorData=array(
          'doctor_speciality'=> isset($_POST['doctor_speciality'])?implode(",", $_POST['doctor_speciality']):"",
          'doctor_fullname'=>trim($_POST['doctor_title'])." ".trim($_POST['doctor_fullname']),
          'doctor_gender'=>trim($_POST['doctor_gender']),
          'doctor_email'=>trim($_POST['doctor_email']),
          'doctor_desc'=>  $_POST['doctor_desc'],
          'doctor_languages'=> isset($_POST['doctor_languages'])?implode(",", $_POST['doctor_languages']):"",
         );
        if(count($_POST['question_id'])>0){
            foreach ($_POST['question_id'] as $qkey =>$id){
                $ansArray[$id] =$_POST['question_answer'][$qkey]; 
            }
        }
         $doctorData['doctor_question'] = json_encode($ansArray);

        $ext = array('image/png','image/jpg','image/jpeg');
        if(isset($_FILES['doctor_photo']) && $_FILES['doctor_photo']['name']!=''){
            $temp = $_FILES['doctor_photo']['tmp_name'];
            $mime = getimagesize($temp);
            if(in_array($mime['mime'], $ext)){

                $extension=explode("/", $_FILES['doctor_photo']['type'])[1];
                $filename = w::getImageName('cliniclogo');
                $uploadPath  = DIR_UPLOAD_DOCTOR.$filename;
                $differentSize = w::imageSizeArray('cliniclogo');
                w::imageCropAndUpload($temp,$uploadPath,$differentSize,$mime);

                $photoImage = json_encode(array("name"=>$filename,'type'=>$extension));
                $doctorData['doctor_photo'] = $photoImage;
            }
        }else{
            $photoImage = json_encode(array("name"=>$_POST['hidden_logo'],'type'=>$_POST['hidden_logo_ext']));
            $doctorData['doctor_photo'] = $photoImage;
        }


        if (isset($_POST['doctor_id'])){
            $id=$_POST['doctor_id'];
            $doctorData['doctor_updated_at']= date('Y-m-d H:i:s');
            $rs=$doctorObj->updateRecords($doctorData,$id);
            if ($rs){
                $successMsg = base64_encode(RECORD_UPDATE);			
                header('Location:doctoradd.php?act=edit&doctor_id='.$_POST['doctor_id'].'&successMsg='.$successMsg);
                exit;
            }
	}else{
        $doctorData['doctor_created_at']= date('Y-m-d H:i:s');
        $doctorData['doctor_unique_id']= $doctorObj->getNextUniqueId("doctor_unique_id",'doctor');
        $rs = $doctorObj->addRecords($doctorData);
        if($rs){
            $successMsg = base64_encode(RECORD_ADD);			
            header('Location:doctoradd.php?act=edit&doctor_id='.$rs.'&successMsg='.$successMsg);
            exit;
        }
    }
}

$specialityObj = new speciality();
$languageObj = new language();
$questionObj = new question();
$allspecialities = $specialityObj->getAllRecords($data=array());
$alllanguage = $languageObj->getAllRecords($data=array());
$where = array('question_type='=>'doctor');
$allquestions = $questionObj->getAllRecords(array('where'=>$where));
$userObj = new user();
$data = array("where"=>array("user_owner="=>1));
$alluser = $userObj->getAllRecords($data);
// Include Template 
include_once('../libs/routs.php')

?>