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



$patientObj =  new  patient();
if(isset($_REQUEST['act']) && $_REQUEST['act']=='edit' && isset($_REQUEST['patient_id'])){
    $patientDetail = $patientObj->fromId($_REQUEST['patient_id']);
}
if(isset($_REQUEST['act']) && $_REQUEST['act']=='validate_email'){
    if($_REQUEST['email']==''){
        echo ENTER_EMAIL;
        exit;
    }else if(validateEmail($_REQUEST['email'])){

        $select=array('patient_email_address','patient_id');
        $where =array('patient_email_address='=>$_REQUEST['email']);

        if(isset($_REQUEST['patient_id']) && $_REQUEST['patient_id']!=""){
            $where['patient_id!='] =$_REQUEST['patient_id'];
        }
        $data =array('fields'=>$select,'where'=>$where);        

        if(isset($_REQUEST['patient_id']) && $patientObj->fromFields($data)){
            echo EMAIL_EXIST;
            exit;
        }else if(!isset($_REQUEST['patient_id']) && $patientObj->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_EMAIL;
        exit;
    }else if(validateEmail($_REQUEST['phone'])){

        $select=array('patient_phone','patient_id');
        $where =array('patient_phone='=>$_REQUEST['phone']);

        if(isset($_REQUEST['patient_id']) && $_REQUEST['patient_id']!=""){
            $where['patient_id!='] =$_REQUEST['patient_id'];
        }
        $data =array('fields'=>$select,'where'=>$where);        

        if(isset($_REQUEST['patient_id']) && $patientObj->fromFields($data)){
            echo EMAIL_EXIST;
            exit;
        }else if(!isset($_REQUEST['patient_id']) && $patientObj->fromFields($data)){
            echo EMAIL_EXIST;
            exit;
        }
    }else{
        echo VALID_EMAIL;
        exit;
    }
    return true;
}
if(isset($_POST['submit'])){
       // Add Client information
    	$patientData=array(
          'patient_fname'=>trim($_POST['patient_fname']),
          'patient_lname'=>trim($_POST['patient_lname']),
          'patient_gender'=>trim($_POST['patient_gender']),
          'patient_email_address'=>trim($_POST['patient_email_address']),
          'patient_phone'=>trim($_POST['patient_phone']),
          'patient_dob'=>date("Y-m-d", strtotime($_POST['patient_dob'])),
          'patient_phone'=>trim($_POST['patient_phone']),
          'patient_addressline1'=>trim($_POST['patient_addressline1']),
          'patient_addressline2'=>trim($_POST['patient_addressline2']),
          'patient_country'=>trim($_POST['patient_country']),
          'patient_state'=>trim($_POST['patient_state']),
          'patient_city'=>trim($_POST['patient_city']),
          'patient_postal_code'=>trim($_POST['patient_postal_code']),
         );
        $patientData['patient_status'] = '1';

    if (isset($_POST['patient_id'])){
		$id=$_POST['patient_id'];
		$rs=$patientObj->updateRecords($patientData,$id);
		if ($rs){
            $successMsg = base64_encode(RECORD_UPDATE);			
            header('Location:patientadd.php?act=edit&patient_id='.$_POST['patient_id'].'&successMsg='.$successMsg);
		    exit;
		}
	}else{
        $patientData['patient_password']= w::password($_POST['patient_password']);
        $patientData['patient_created_at']= date('Y-m-d H:i:s');
        $patientData['patient_unique_id']= $patientObj->getNextUniqueId("patient_unique_id",'patient');
        $patientData['patient_referralCode'] = w::getReferralCode($_POST['patient_fname'],$_POST['patient_lname'],$patientData['patient_unique_id']);

        $rs = $patientObj->addRecords($patientData);
        if($rs){
            $successMsg = base64_encode(RECORD_ADD);			
            header('Location:patientadd.php?act=edit&patient_id='.$rs.'&successMsg='.$successMsg);
            exit;
        }
    }
}

// Include Template 
include_once('../libs/routs.php')

?>