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

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

if(isset($_REQUEST['act']) && $_REQUEST['act']=='OnBoard' && $_REQUEST['id']!=""){
    $tables = array('tbl_address a','tbl_user u','tbl_speciality s');
    $jfields = array('a.address_clinic_id = c.clinic_id','u.user_id = c.clinic_user_id','s.speciality_id = c.clinic_primary_speciality');
    $jtype   = array("INNER","INNER","INNER"); 
    $where = array("clinic_id="=>$_REQUEST['id']);
    $clinicArg    = array('where' => $where,  'joinTables' =>$tables, 'joinFields' =>$jfields, 'joinTypes' => $jtype);
    $clinicDetails = $clinicObj->fromFields($clinicArg);
    if($clinicDetails && $clinicDetails->address_sublocality!="" && !is_null($clinicDetails->clinic_email_verified) && !is_null($clinicDetails->clinic_phone_verified)){

        $specialityDetail= $specialityObj->fromId($clinicDetails->clinic_primary_speciality);
        $canonicalUrl = $specialityDetail->speciality_slug_plural."/".strtolower($clinicDetails->address_sublocality."-".$clinicDetails->address_level_1_short."/".w::slugify($clinicDetails->clinic_name)."/".$clinicDetails->clinic_unique_id."/c/");
         $clinicData['clinic_url'] = $canonicalUrl;
         $clinicData['clinic_onBoard'] = "1";
         $rs = $clinicObj->updateRecords($clinicData,$clinicDetails->clinic_id);
         if($rs){
            $array = ['error'=>false,"msg"=>"Success"];
            echo json_encode($array);exit;   
         }
    }else{
        $array = ['error'=>true,"msg"=>"Not fullfill requirement to onboard clinic"];
        echo json_encode($array);exit;
    }
}

if(isset($_POST['submit'])){
    if (isset($_POST['address_id']) && $_POST['address_id']!=""){
        $addressData = array(
            'address_street'=>trim($_POST['address_street']),
            'address_route'=>trim($_POST['address_route']),
            'address_locality'=>trim($_POST['address_locality']),
            'address_sublocality'=>trim($_POST['address_sublocality']),
            'address_neighborhood'=>trim($_POST['address_neighborhood']),
            'address_postal'=>trim($_POST['address_postal']),
            'address_country'=>trim($_POST['address_country']),
            'address_level_1'=> trim($_POST['address_level_1']),
            'address_level_1_short'=> trim($_POST['address_level_1_short']),
            'address_suite'=> trim($_POST['address_suite'])
        );
        $rsadd = $addressObj->updateRecords($addressData,$_POST['address_id']);
    }
}



$tables = array('tbl_address a','tbl_user u','tbl_speciality s');
$jfields = array('a.address_clinic_id = c.clinic_id','u.user_id = c.clinic_user_id','s.speciality_id = c.clinic_primary_speciality');
$jtype   = array("LEFT","LEFT","LEFT"); 
$where = array("clinic_id="=>$_REQUEST['clinic_id']);
$clinicArg    = array('where' => $where,  'joinTables' =>$tables, 'joinFields' =>$jfields, 'joinTypes' => $jtype);
$clinicDetails = $clinicObj->fromFields($clinicArg);

$cdoctorObj = new cdoctor();
$fields = array('s.*','d.doctor_unique_id');
$tables = array('tbl_stripecard s','tbl_doctor d');
$jfields = array('s.stripecard_id = c.cdoctor_stripecard','c.cdoctor_doctor_id = d.doctor_id');
$jtype   = array("INNER"); 
$where = array("cdoctor_clinic_id="=>$clinicDetails->clinic_id,'stripecard_status='=>"Active");
$clinicArg    = array('fields'=>$fields,'where' => $where,  'joinTables' =>$tables, 'joinFields' =>$jfields, 'joinTypes' => $jtype);
$allCards = $cdoctorObj->getAllRecords($clinicArg);

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

?>