<?php 
// Include main file
include_once('../libs/main.php');
// Check admin Login
include_once('checkLogin.php');
// Logical section of page
$practiceObj =  new  practice();
$pmsAppointmentObj = new pmsAppointment();
if(isset($_REQUEST['office_id']) && $_REQUEST['office_id']!=""){
    $practiceObj = new practice();
    $where =array('practice_office_id='=>$_REQUEST['office_id']);
    $arg = array('where'=>$where);
    $practiceDetails = $practiceObj->FromFields($arg);
    if($practiceDetails){
            $pmsAppointmentObj = new pmsAppointment();
            $where =array('pmsAppointment_practice_id='=>$practiceDetails->practice_office_id);
            $data =array('where'=>$where);
            $exist = $pmsAppointmentObj->FromFields($data);
            if($exist){
               header("Location:pmsAppointmentlist.php?failedMsg=Appointments already fetched from PMS for this practice.");
               exit;
            }
 
        $pmsAppointmentObj = new pmsAppointment();
        $arg =array(
          "request_key"=>$practiceDetails->practice_request_key,
          "office_id"=>$practiceDetails->practice_office_id,
          "secret_key"=>$practiceDetails->practice_secret_key,
          "clinic_id"=>$practiceDetails->practice_xcare_id,
          "startdate"=>date("Y-m-d"),
        );
        $status  = $pmsAppointmentObj->pullAppointments($arg);
        if($status){
            header("Location:pmsAppointmentlist.php?successMsg=Successfully pulled all patients for ".$practiceDetails->practice_office_id);
            exit;
        }else{
             header("Location:pmsAppointmentlist.php?successMsg=Successfully pulled all patients for ".$practiceDetails->practice_office_id);
            exit;
        }
    }else{
        header("Location:pmsAppointmentlist.php?failedMsg=Error Occur.");
        exit;
    }
}

if (isset($_REQUEST['act']) && $_REQUEST['act']=='load'){
    $records = $_REQUEST['length'];
    $start=0;
    if(isset($_GET['start']) && $_GET['start']!='0'){
        $page = $_GET['start'];
    }
    $start=($page-1)*$limit;
    $limit = array('start'=>$_GET['start'],'record'=>$records);
    
    $where = " 1=1 ";
    if (isset($_REQUEST['search']['value']) && $_REQUEST['search']['value']!=""){
        $where = " pmsPatient_fname LIKE '%".$_REQUEST['search']['value']."%' OR pmsPatient_lname LIKE '%".$_REQUEST['search']['value']."%'  OR pmsPatient_email LIKE '%".$_REQUEST['search']['value']."%' OR pmsAppointment_sr_no LIKE '%".$_REQUEST['search']['value']."%' OR pmsAppointment_id LIKE '%".$_REQUEST['search']['value']."%' OR pmsAppointment_patient_id LIKE '%".$_REQUEST['search']['value']."%' OR pmsAppointment_practice_id LIKE '%".$_REQUEST['search']['value']."%'";
    }    
    $orderFields = array('pmsAppointment_sr_no','pmsPatient_fname','pmsPatient_email');
    
    // Array of order by 
    $orderby=array();
    if (isset($_REQUEST['order']['0']['column']) && $_REQUEST['order']['0']['column']!=""){
        $orderby = array(" ".$orderFields[$_REQUEST['order']['0']['column']]." ".$_REQUEST['order']['0']['dir']);
    }else{
        $orderby = array(" pmsAppointment_date asc ");
    }
     $fields  = array('c.*','p.*','pp.*','pd.*');
    $tables = array('tbl_pmsPatient pp','tbl_clinic c','tbl_pmsProvider pd');
    $jfields = array('p.pmsAppointment_patient_id = pp.pmsPatient_patient_id','c.clinic_id = p.pmsAppointment_clinic_id','p.pmsAppointment_provider_id = pd.pmsProvider_sikka_id');
    $jtype   = array("INNER","INNER","INNER"); 
    $data    = array('fields' => $fields, 'where' => $where,  'joinTables' =>$tables, 'joinFields' =>$jfields, 'joinTypes' => $jtype, 'limit' => $limit);

    $pmsAppointmentDetails = $pmsAppointmentObj->getAllRecords($data);
    $recordsTotal   =   $pmsAppointmentObj->getAllCount($data);

        
    if ($pmsAppointmentDetails && count($pmsAppointmentDetails)>0){
        foreach ($pmsAppointmentDetails as $key => $pmsAppointment){
            $response[] = array(
                    
                    $pmsAppointment->pmsAppointment_sr_no."<br/>".$pmsAppointment->clinic_name.'<br/>'.$pmsAppointment->pmsAppointment_patient_id."<br/>".$pmsAppointment->pmsAppointment_practice_id."<br/>".$pmsAppointment->pmsAppointment_id,
                    $pmsAppointment->pmsPatient_fname." ".$pmsAppointment->pmsPatient_lname,
                    $pmsAppointment->pmsPatient_email.'<br/>',
                    date("Y-m-d",strtotime($pmsAppointment->pmsAppointment_date))." ".$pmsAppointment->pmsAppointment_time,
                    $pmsAppointment->pmsAppointment_provider_id."<br/>".$pmsAppointment->pmsProvider_fname." ".$pmsAppointment->pmsProvider_lname, 
                    $pmsAppointment->pmsAppointment_last_changed_date, 
                    $pmsAppointment->pmsAppointment_schedule."<br/>".$pmsAppointment->pmsAppointment_cancelled.'<br/>'.$pmsAppointment->pmsAppointment_verified.'<br/>'.$pmsAppointment->pmsAppointment_changed.'<br/>'.$pmsAppointment->pmsAppointment_amount, 
                    $pmsAppointment->pmsAppointment_status
             );
        }
        $result=array('recordsFiltered'=>$recordsTotal,'recordsTotal'=>$recordsTotal,'data'=>$response);
    }else{
        $result=array('recordsFiltered'=>0,'recordsTotal'=>0,'data'=>array());
    }
    echo json_encode($result);
    exit;
}

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