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

$payClinicObj =  new  payClinic();
$domainObj = new domain();

$adminlogObj = new admin_log();
$modulepagesObj = new module_pages();

$filename=basename($_SERVER['SCRIPT_FILENAME']);
if($filename == 'payClinicAdd.php')
{
    $module_id = $adminlogObj->getModuleId($filename);
    $page_id = $adminlogObj->getPageId($filename);
    $allpages = $modulepagesObj->fromId($page_id);
    $pagename = $allpages->page;

}

if(isset($_REQUEST['act']) && $_REQUEST['act']=='edit' && isset($_REQUEST['id'])){
    $clinicDetail = $payClinicObj->fromId($_REQUEST['id']);
}
if(isset($_REQUEST['act']) && $_REQUEST['act']=='validate_email'){
    
        $select=array('email');
        $where =array('email='=>$_REQUEST['email']);

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

        if(isset($_REQUEST['id']) && $payClinicObj->fromFields($data)){
            echo "Email already exist";
            exit;
        }else if(!isset($_REQUEST['id']) && $payClinicObj->fromFields($data)){
            echo "Email already exist";
            exit;
        }
    return true;
}

if(isset($_POST['submit'])){
       // Add information
    	$clinicData=array(
          'name'=>trim($_POST['name']),
          'phone'=>trim($_POST['phone']),
          'domain'=>trim($_POST['domain']),
          'email'=>trim($_POST['email']),
          'street'=>trim($_POST['street']),
          'route'=>trim($_POST['route']),
          'sublocality'=>trim($_POST['sublocality']),
          'neighborhood'=>trim($_POST['neighborhood']),
          'locality'=>trim($_POST['locality']),
          'postal'=>trim($_POST['postal']),
          'country'=>trim($_POST['country']),
          'level_1'=>trim($_POST['level_1']),
          'level_1_short'=>trim($_POST['level_1_short']),
          'suite'=>trim($_POST['suite']),
          'latitude'=>trim($_POST['latitude']),
          'longitude'=>trim($_POST['longitude']),
          'formattedAddress'=>trim($_POST['formattedAddress']),
          'stripeData'=>trim($_POST['stripeData']),
         );

        if(isset($_POST['password']) && $_POST['password']!=''){
             $clinicData['password'] = w::password($_POST['password']);
        }

        if(isset($_POST['status'])){
            $clinicData['status']           = '1';
        }else{
            $clinicData['status']           = '0';
        } 

        
    if (isset($_POST['id'])){
		$id=$_POST['id'];
        $clinicData['updatedAt'] = date("Y-m-d H:i:s");
		$rs=$payClinicObj->updateRecords($clinicData,$id);
		if ($rs){
            $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,"Update Pay Clinic id: ".$id);
            $successMsg = base64_encode(RECORD_UPDATE);			
            header('Location:payClinicList.php');
		    exit;
		}
	}else{
        $clinicData['createdAt'] = date("Y-m-d H:i:s");

        $rs = $payClinicObj->addRecords($clinicData);
        if($rs){
            $successMsg = base64_encode(RECORD_ADD);
            $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,"Add New Pay Clinic id: ".$rs);

            header('Location:payClinicList.php');
            exit;
        }
    }
}

// Fetch domains for dropdown
$domainList = $domainObj->getAllRecords(array());


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

?>

