<?php 
// Include main file
include_once('../libs/main.php');
// Check clinic Login
include_once('checkLogin.php');



$clinicObj =  new  clinic();
$addressObj = new address();
// $clinicphotoObj = new clinic_photo();
// $specialityObj = new speciality();

// if(isset($_REQUEST['act']) && $_REQUEST['act']=='deletephoto' && isset($_REQUEST['photoid'])){
//     $c = $clinicphotoObj->fromId($_REQUEST['photoid']);
//     if($c->clinic_photo_image!="" && file_exists(DIR_UPLOAD_CLINIC.$c->clinic_photo_image)){
//         unlink(DIR_UPLOAD_CLINIC.$c->clinic_photo_image);
//         $clinicphotoObj->deleteFromId($_REQUEST['photoid']);
//     }
//     exit;
// }
if(isset($_REQUEST['act']) && $_REQUEST['act']=='feature' && isset($_REQUEST['speciality'])){
    $featureObj = new feature();
    $where =array('feature_speciality_id='=>$_REQUEST['speciality']);
    $dataw =array('where'=>$where); 
    $allfeature =  $featureObj->getAllRecords($dataw);
    if($allfeature && count($allfeature)>0){foreach($allfeature as $key => $val){?>
    <div class="col-md-6 form-group mb-3">
        <label class="checkbox checkbox-outline-primary inline-checkbox">
            <input type="checkbox" name="clinic_feature[]" id="clinic_feature_<?php echo $key ?>"  value="<?php echo $val->feature_id ?>" >
            <span><?php echo $val->feature_name; ?></span>
            <span class="checkmark"></span>
        </label>
    </div>    
    <?php }}
    exit;
}
if(isset($_REQUEST['act']) && $_REQUEST['act']=='edit' && isset($_REQUEST['clinic_id'])){
    // $clinicDetail = $clinicObj->fromId($_REQUEST['clinic_id']);
    // $clinichours = json_decode($clinicDetail->clinic_practice_hours,1);
    
    //Get super admin information
    $addCond = array('where' => "address_clinic_id = '".$_REQUEST['clinic_id']."' ");
    $addressDetail = $addressObj->fromFields($addCond);
    $addressformat = $addressDetail->address_suite." ".$addressDetail->address_route.", ".$addressDetail->address_sublocality.", ".$addressDetail->address_postal.", ".$addressDetail->address_level_1.", ".$addressDetail->address_country;
    
    // $featureObj = new feature();
    // $where =array('feature_speciality_id='=>$clinicDetail->clinic_primary_speciality);
    // $dataw =array('where'=>$where); 
    // $allfeature =  $featureObj->getAllRecords($dataw);
    
    
}
if(isset($_REQUEST['act']) && $_REQUEST['act']=='validate_email'){
	if($_REQUEST['email']==''){
            echo ENTER_EMAIL;
            exit;
	}else if(validateEmail($_REQUEST['email'])){
            $select=array('clinic_email','clinic_id');
            $where =array('clinic_email='=>$_REQUEST['email']);
            if(isset($_REQUEST['clinic_id']) && $_REQUEST['clinic_id']!=""){
                $where['clinic_id!='] =$_REQUEST['clinic_id'];
            }
            $data =array('fields'=>$select,'where'=>$where);        
            if(isset($_REQUEST['clinic_id']) && $clinicObj->fromFields($data)){
                echo EMAIL_EXIST;
                exit;
            }else if(!isset($_REQUEST['clinic_id']) && $clinicObj->fromFields($data)){
                echo EMAIL_EXIST;
                exit;
            }
	}else{
            echo VALID_EMAIL;
            exit;
	}
	return true;
}
if(isset($_REQUEST['act']) && $_REQUEST['act']=='validate_phone'){
	if($_REQUEST['phone']==''){
            echo ENTER_PHONE;
            exit;
	}else{
            $select=array('clinic_phone','clinic_id');
            $where =array('clinic_phone='=>$_REQUEST['phone']);
            if(isset($_REQUEST['clinic_id']) && $_REQUEST['clinic_id']!=""){
                $where['clinic_id!='] =$_REQUEST['clinic_id'];
            }
            $data =array('fields'=>$select,'where'=>$where);        
            if(isset($_REQUEST['clinic_id']) && $clinicObj->fromFields($data)){
                echo PHONE_EXIST;
                exit;
            }else if(!isset($_REQUEST['clinic_id']) && $clinicObj->fromFields($data)){
                echo PHONE_EXIST;
                exit;
            }
        }
	return true;
}

if(isset($_POST['submit'])){

   
       
       // Add Client information
//     	$clinicData=array(
//           'clinic_user_id'=>trim($_POST['clinic_user_id']),
//           'clinic_primary_speciality'=>trim($_POST['clinic_primary_speciality']),
//           'clinic_name'=>trim($_POST['clinic_name']),
//           'clinic_website'=>trim($_POST['clinic_website']),
//           'clinic_email'=>trim($_POST['clinic_email']),
//           'clinic_phone'=>trim($_POST['clinic_phone']),
//           'clinic_practice_hours'=>  json_encode($_POST['clinic_hour']),
// //          'clinic_question'=>  json_encode($_POST['question_answer']),
//           'clinic_desc'=>  $_POST['clinic_desc'],
//           'clinic_feature'=> isset($_POST['clinic_feature'])?implode(",", $_POST['clinic_feature']):"",
//          );
//         if(count($_POST['question_id'])>0){
//             foreach ($_POST['question_id'] as $qkey =>$id){
//                 $ansArray[$id] =$_POST['question_answer'][$qkey]; 
//             }
//         }
//          $clinicData['clinic_question'] = json_encode($ansArray);

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

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

//                 $photoImage = json_encode(array("name"=>$filename,'type'=>$extension));
//                 $clinicData['clinic_logo'] = $photoImage;
//             }
//         }else{
//             $photoImage = json_encode(array("name"=>$_POST['hidden_logo'],'type'=>$_POST['hidden_logo_ext']));
//             $clinicData['clinic_logo'] = $photoImage;
//         }
        
//         $clinicData['clinic_status'] = '1';
  //       if (isset($_POST['clinic_id'])){
		// $id=$_POST['clinic_id'];
		// $rs=$clinicObj->updateRecords($clinicData,$id);
		// if ($rs){
                if (isset($_POST['address_id']) && $_POST['address_id']!=""){
                    $addressData = array(
                        // 'address_clinic_id'=>$id,
                        'address_street'=>trim($_POST['street_number']),
                        'address_route'=>trim($_POST['route']),
                        'address_locality'=>trim($_POST['locality']),
                        'address_sublocality'=>trim($_POST['sublocality_level_1']),
                        'address_neighborhood'=>trim($_POST['neighborhood']),
                        'address_postal'=>trim($_POST['postal_code']),
                        'address_country'=>trim($_POST['country']),
                        'address_level_1'=> trim($_POST['administrative_area_level_1']),
                        'address_level_1_short'=> trim($_POST['address_level_1_short']),
                        'address_suite'=> trim($_POST['subpremise']),
                        'address_latitude'=> trim($_POST['lat']),
                        'address_longitude'=> trim($_POST['lng']),
                    );
                    $rsadd = $addressObj->updateRecords($addressData,$_POST['address_id']);
                }
                // if(isset($_FILES['clinic_photos']) && count($_FILES['clinic_photos']['name'])>0){
                //     foreach ($_FILES['clinic_photos']['name'] as $key =>$file){
                //         $image=$file;
                //         $extension=end(explode(".", $image));
                //         $image=  str_shuffle(time()).'_clinic_photos.'.$extension;
                //         move_uploaded_file($_FILES['clinic_photos']['tmp_name'][$key], DIR_UPLOAD_CLINIC.$image);
                //         $clinicPhotoData = array(
                //             'clinic_photo_clinic_id'=>$rs,
                //             'clinic_photo_image'=>$image,
                //         );
                //         $cpadd = $clinicphotoObj->addRecords($clinicPhotoData);
                //     }   
                // }
                $successMsg = base64_encode(RECORD_UPDATE);			
                header('Location:clinicadd.php?act=edit&clinic_id='.$_REQUEST['clinic_id'].'&successMsg='.$successMsg);
		    exit;
		}
	// }else{
 //            $clinicData['clinic_created_at']= date('Y-m-d H:i:s');
 //            $clinicData['clinic_unique_id']= $clinicObj->getNextUniqueId("clinic_unique_id",'clinic');
 //             $specialityDetail= $specialityObj->fromId($_POST['clinic_primary_speciality']);
 //             $canonicalUrl = $specialityDetail->speciality_slug_plural."/".strtolower($_POST['sublocality_level_1']."-".$_POST['address_level_1_short']."/".w::slugify($_POST['clinic_name'])."/".$clinicData['clinic_unique_id']."c/");
 //             $clinicData['clinic_url'] = $canonicalUrl;
 //            $rs = $clinicObj->addRecords($clinicData);
 //            if($rs){
 //                // Add Client super admin information
 //                $addressData = array(
 //                    'address_clinic_id'=>$rs,
 //                    'address_street'=>trim($_POST['street_number']),
 //                    'address_route'=>trim($_POST['route']),
 //                    'address_locality'=>trim($_POST['locality']),
 //                    'address_sublocality'=>trim($_POST['sublocality_level_1']),
 //                    'address_neighborhood'=>trim($_POST['neighborhood']),
 //                    'address_postal'=>trim($_POST['postal_code']),
 //                    'address_country'=>trim($_POST['country']),
 //                    'address_level_1'=> trim($_POST['administrative_area_level_1']),
 //                    'address_level_1_short'=> trim($_POST['address_level_1_short']),
 //                    'address_suite'=> trim($_POST['subpremise']),
 //                    'address_latitude'=> trim($_POST['lat']),
 //                    'address_longitude'=> trim($_POST['lng']),
 //                );
 //                $rsadd = $addressObj->addRecords($addressData);

 //                // if(isset($_FILES['clinic_photos']) && count($_FILES['clinic_photos']['name'])>0){
 //                //     foreach ($_FILES['clinic_photos']['name'] as $key =>$file){
 //                //         $clinicPhotoData = array();
 //                //         $image=$file;
 //                //         $extension=end(explode(".", $image));
 //                //         $image=  str_shuffle(time()).'_clinic_photos.'.$extension;
 //                //         move_uploaded_file($_FILES['clinic_photos']['tmp_name'][$key], DIR_UPLOAD_CLINIC.$image);
 //                //         $clinicPhotoData = array(
 //                //             'clinic_photo_clinic_id'=>$rs,
 //                //             'clinic_photo_image'=>$image,
 //                //          );
 //                //         $cpadd = $clinicphotoObj->addRecords($clinicPhotoData);
 //                //    }   
 //                // }
 //                $permissionObj = new permission();
 //                $permissionData = array(
 //                    "permission_user_id" => $_POST['clinic_user_id'],
 //                    "permission_model_type" => "Clinic",
 //                    "permission_notification_email" => "1",
 //                    "permission_model_id" =>$rs
 //                );
 //                $prs = $permissionObj->addRecords($permissionData);
 //                $successMsg = base64_encode(RECORD_ADD);			
 //                header('Location:clinicadd.php?act=edit&clinic_id='.$rs.'&successMsg='.$successMsg);
 //                exit;
 //            }
 //    }
// }
// $userObj = new user();
// $specialityObj = new speciality();
// $questionObj = new question();
// $alluser = $userObj->getAllRecords($data=array());
// $allspecialities = $specialityObj->getAllRecords($data=array());
// $where = array('question_type='=>'clinic');
// $allquestions = $questionObj->getAllRecords(array('where'=>$where));
// Include Template 
include_once('../libs/routs.php')

?>