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

// Logical section of page
$clinicObj =  new  clinic();
if(isset($_GET['id']) && $_GET['id']!=""){
    $clinic =$clinicObj->fromId($_GET['id']);
}else{
    header("Location:index.php");
    exit;
}

if(isset($_POST) && $_POST['submit']=='Submit'){

    $clinicId = $_REQUEST['clinic_id'];
    $practiceObj =  new  practice();
    $practiceObj->resultAs =  "array";
    $where = array("practice_xcare_id="=>$clinicId);
    $practiceArg = array('where'=>$where);
    $practice_data = $practiceObj->fromFields($practiceArg);

    if(empty($practice_data)){
        $clinicObj = new clinic();
        $clinicDetails = $clinicObj->fromId($clinicId);
        $addpmsdata = array(
            "practice_name" =>$clinicDetails->clinic_name,
            "practice_office_id" =>"DUMMY".$clinicId,
            "practice_secret_key" => "AAA678769KJUTZ1",
            "practice_pms" => 'Dummy',
            "practice_sikka_id" => "1",
            "practice_xcare_id" => $clinicId,
            );
        $rs_practice = $practiceObj->addRecords($addpmsdata);
        $dummy_offic_id  ="DUMMY".$clinicId;
        
    }else{
        $dummy_offic_id  =$practice_data['practice_office_id'];
    }

    $pmsProviderObj = new pmsProvider();
    $pmsProviderObj->resultAs = 'array';
    $where =array('pmsProvider_clinic_id='=>$clinicId);
    $arg =array('where'=>$where);
    $pmsProvider  =$pmsProviderObj->getAllRecords($arg);
    
    if(empty($pmsProvider)){
        $fname  =$_POST['fname'];
        $lname  =$_POST['lname'];
        $code = $_POST['code'];
        if($fname && count($fname)>0){
            foreach($fname as $key =>$f){
                if($f!="" && $code[$key]!="" && $lname[$key]!=""){
                    $pmsProviderData =array(
                        "pmsProvider_sikka_id"=>$code[$key],
                        "pmsProvider_lname"=>$lname[$key],
                        "pmsProvider_fname"=>$f,
                        "pmsProvider_type"=>"Dentist",
                        "pmsProvider_status"=>"Active",
                        "pmsProvider_office_id"=>$dummy_offic_id,
                        "pmsProvider_clinic_id"=>$clinicId
                    );
                    $pmsProviderObj->addRecords($pmsProviderData);
                }
            }
        }
    }
    $pmsAppointmentStatusObj = new pmsAppointmentStatus();
    $pmsAppointmentStatusObj->resultAs = 'array';
    $where =array('pmsAppointmentStatus_clinic_id='=>$clinicId);
    $arg =array('where'=>$where);
    $pmsAppointmentStatus  =$pmsAppointmentStatusObj->getAllRecords($arg);
    if(empty($pmsAppointmentStatus)){
        $statuses =array('PRECON','COMPLETE','LM','BOOKED','CONFIRM','ADVAN','None');
        if($statuses && count($statuses)>0){
            foreach($statuses as $key =>$s){
                $pmsAppointmentStatusData =array(
                    "pmsAppointmentStatus_status"=>$s,
                    "pmsAppointmentStatus_office_id"=>$dummy_offic_id,
                    "pmsAppointmentStatus_clinic_id"=>$clinicId
                );
                $pmsAppointmentStatusObj->addRecords($pmsAppointmentStatusData);
            }
        }
    }
    header("Location:".SITE_URL."x-admin/setup_practice.php?id=".$clinicId."&msg=Successfull created demo.");
    exit;
}



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