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

$appointmentObj =  new  appointment();

if (isset($_REQUEST['act']) && $_REQUEST['act']=='load' && isset($_REQUEST['patient_id']) && $_REQUEST['patient_id']!='' )
{
    
        $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 = " appointment_patient_id = '".$_REQUEST['patient_id']."' ";
    
        if (isset($_REQUEST['search']['value']) && $_REQUEST['search']['value']!=""){

            $where .= " AND (c.clinic_name LIKE '%".$_REQUEST['search']['value']."%' OR c.clinic_id LIKE '%".$_REQUEST['search']['value']."%' OR c.clinic_unique_id LIKE '%".$_REQUEST['search']['value']."%' OR c.clinic_phone LIKE '%".$_REQUEST['search']['value']."%' OR c.clinic_email LIKE '%".$_REQUEST['search']['value']."%' OR c.clinic_website LIKE '%".$_REQUEST['search']['value']."%' OR appointment_id LIKE '%".$_REQUEST['search']['value']."%' OR dep.dependant_fname LIKE '%".w::e($_REQUEST['search']['value'])."%' OR dep.dependant_lname LIKE '%".w::e($_REQUEST['search']['value'])."%' OR a.appointment_unique_id LIKE '%".$_REQUEST['search']['value']."%' OR a.appointment_clinic_id LIKE '%".$_REQUEST['search']['value']."%' OR a.appointment_doctor_id LIKE '%".$_REQUEST['search']['value']."%' OR a.appointment_dependant_id LIKE '%".$_REQUEST['search']['value']."%' OR a.appointment_appointmenttype_id LIKE '%".$_REQUEST['search']['value']."%' OR a.appointment_patient_id LIKE '%".$_REQUEST['search']['value']."%' OR p.patient_fname LIKE '%".w::e($_REQUEST['search']['value'])."%' OR p.patient_lname LIKE '%".w::e($_REQUEST['search']['value'])."%' OR p.patient_email_address LIKE '%".w::e($_REQUEST['search']['value'])."%' OR p.patient_phone LIKE '%".w::e($_REQUEST['search']['value'])."%' OR p.patient_unique_id LIKE '%".$_REQUEST['search']['value']."%' OR p.patient_id LIKE '%".$_REQUEST['search']['value']."%' OR a.appointment_status LIKE '%".$_REQUEST['search']['value']."%' OR a.appointment_date LIKE '%".$_REQUEST['search']['value']."%' OR a.appointment_time LIKE '%".$_REQUEST['search']['value']."%' OR dep.dependant_fname LIKE '%".w::e(ucfirst($_REQUEST['search']['value']))."%' OR dep.dependant_lname LIKE '%".w::e(ucfirst($_REQUEST['search']['value']))."%' OR dep.dependant_fname LIKE '%".w::e(ucwords($_REQUEST['search']['value']))."%' OR dep.dependant_lname LIKE '%".w::e(ucwords($_REQUEST['search']['value']))."%' OR p.patient_fname LIKE '%".w::e(ucfirst($_REQUEST['search']['value']))."%' OR p.patient_lname LIKE '%".w::e(ucfirst($_REQUEST['search']['value']))."%' OR p.patient_fname LIKE '%".w::e(ucwords($_REQUEST['search']['value']))."%' OR p.patient_lname LIKE '%".w::e(ucwords($_REQUEST['search']['value']))."%' OR p.patient_email_address LIKE '%".w::e(ucfirst($_REQUEST['search']['value']))."%' OR p.patient_email_address LIKE '%".w::e(ucwords($_REQUEST['search']['value']))."%' )";

        }    
        $orderFields = array('appointment_id','clinic_id','dependant_fname','patient_id');

        // 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{
            if($app_type == 'requested')
            {
                $orderby = array(" appointment_created_at ASC ");
            }else if($app_type == 'scheduled')
            {
                $orderby = array(" appointment_date ASC ");         
            }else if($app_type == 'completed')
            {
                $orderby = array(" appointment_date DESC ");            
            }else if($app_type == 'cancelled')
            {
                $orderby = array(" appointment_id DESC ");          
            }else if($app_type == 'all_data')
            {
                $orderby = array(" appointment_id DESC ");          
            }
        }

        //'cd.cdoctor_fullname','cd.cdoctor_doctor_id','cd.cdoctor_provider_id','cd.cdoctor_email','cd.cdoctor_phone'
        $fields  = array('a.appointment_id','a.appointment_unique_id','a.appointment_clinic_id','a.appointment_doctor_id','a.appointment_patient_id','appointment_dependant_id','appointment_appointmenttype_id','appointment_date','appointment_time','appointment_etime','appointment_status','appointment_assignTo','c.clinic_name','c.clinic_id','c.clinic_unique_id','c.clinic_phone','c.clinic_email','c.clinic_website','cd.cdoctor_fullname','cd.cdoctor_doctor_id','cd.cdoctor_provider_id','cd.cdoctor_email','cd.cdoctor_phone','p.patient_id','p.patient_unique_id','p.patient_fname','p.patient_lname','p.patient_email_address','p.patient_phone');
        $tables = array('tbl_clinic c','tbl_doctor d','tbl_cdoctor cd','tbl_patient p');
        $jfields = array(
            'a.appointment_clinic_id = c.clinic_id',
            'a.appointment_doctor_id = d.doctor_id',
            'a.appointment_doctor_id = cd.cdoctor_doctor_id',
            'a.appointment_patient_id = p.patient_id',
        );
        $jtype   = array("INNER","INNER","INNER","LEFT");
        $groupBy = array('appointment_id'); 
        $data    = array('fields' => $fields, 'where' => $where,'joinTables' =>$tables, 'joinFields' =>$jfields, 'joinTypes' => $jtype,'limit' => $limit,'orderBy'=>$orderby,"groupBy"=>$groupBy);
        $data_arg    = array('fields' => $fields, 'where' => $where);
        //$data['debug'] = true;
        $appointmentDetails = $appointmentObj->getAllRecords($data);

        //print_r($appointmentDetails);exit;
        $recordsTotal   =   $appointmentObj->getAllCount($data_arg);

        if ($appointmentDetails && count($appointmentDetails)>0){
            foreach ($appointmentDetails as $key => $appRequested){
                
                $assign_detail= '';
                $dependant_detail = '';
                $cdoctor_detail = '';
                $communication_detail = '';
                if(!is_null($appRequested->appointment_assignTo))
                {

                    $patientObj =  new patient();
                    $where = 'patient_id IN ('.$appRequested->appointment_assignTo.')';
                    $patientArg    = array('where' => $where);
                    $patientDetails = $patientObj->getAllRecords($patientArg);
                    foreach ($patientDetails as $key => $patientData) 
                    {
                        $assign_detail .= "<b> ID : </b>".$patientData->patient_id."<br/><b>External Id : </b>".$patientData->patient_unique_id."<br/><b>First Name :</b>".w::d($patientData->patient_fname)."<br/><b>Last Name : </b>".w::d($patientData->patient_lname)."<br/><b>Email : </b>".w::d($patientData->patient_email_address)."<br/><b>Phone : </b>".w::d($patientData->patient_phone)."<br/><br/>";
                    }
                }

                if($appRequested->appointment_dependant_id != '')
                {
                    $dependantObj =  new dependant();
                    $where = 'dependant_id = '.$appRequested->appointment_dependant_id;
                    $dependantArg    = array('where' => $where);
                    $dependantDetails = $dependantObj->getAllRecords($dependantArg);
                    foreach ($dependantDetails as $key => $dependantData) 
                    {
                        /*$dependant_detail .= "<b>First Name: </b>".w::d($dependantData->dependant_fname)."<br/><b>Last Name: </b>".w::d($dependantData->dependant_lname);*/
                        $communication_detail .= "<br/><b>Communication Email: </b>".w::d($dependantData->dependant_email_address)."<br/><b>Communication Phone: </b>".w::d($dependantData->dependant_phone);
                    }
                }

                if($appRequested->appointment_clinic_id != '' && $appRequested->appointment_doctor_id != '')
                {
                    $cdoctorObj =  new cdoctor();
                    $where = 'cdoctor_clinic_id = '.$appRequested->appointment_clinic_id.' and cdoctor_doctor_id = '.$appRequested->appointment_doctor_id;
                    $cdoctorArg    = array('where' => $where);
                    $cdoctorDetails = $cdoctorObj->getAllRecords($cdoctorArg);
                    foreach ($cdoctorDetails as $key => $cdoctorData) 
                    {
                        $cdoctor_detail .= "<br/><b>Provider Id: </b>".$cdoctorData->cdoctor_doctor_id."<br/><b>Provider External Id: </b>".$cdoctorData->cdoctor_provider_id."<br/><b>Provider Fullname: </b>".$cdoctorData->cdoctor_fullname."<br/><b>Provider Email: </b>".$cdoctorData->cdoctor_email."<br/><b>Provider Phone: </b>".$cdoctorData->cdoctor_phone;
                    }
                }

                if($app_type == 'all_data')
                {
                    $more_detail_url = "<b><a href='appointmentdetail.php?type=all_data&appoint_id=".$appRequested->appointment_id."'>View</a></b>";
                }else{
                    $more_detail_url = "<b><a href='appointmentdetail.php?appoint_id=".$appRequested->appointment_id."'>View</a></b>";
                }
                
                

                    $response[] = array(
                        "<b>Appointment Id: </b>".$appRequested->appointment_id."<br/><b>Appointment External Id: </b>".$appRequested->appointment_unique_id."<br/><b>Clinic Id: </b>".$appRequested->appointment_clinic_id."<br/><b>Provider Global Id: </b>".$appRequested->appointment_doctor_id."<br/><b>Sign In/Guarantor Id: </b>".$appRequested->appointment_patient_id."<br/><b>Patient Id: </b>".$appRequested->appointment_dependant_id."<br/><b>AppoitmentType Id: </b>".$appRequested->appointment_appointmenttype_id,

                        "<b>Clinic Id: </b>".$appRequested->clinic_id."<br/><b>Clinic External Id: </b>".$appRequested->clinic_unique_id."<br/><b>Clinic Name: </b>".$appRequested->clinic_name."<br/><b>Clinic Phone: </b>".$appRequested->clinic_phone."<br/><b>Clinic Email: </b>".$appRequested->clinic_email.$cdoctor_detail,
                            
                        /*"<b>First Name: </b>".w::d($appRequested->dependant_fname)."<br/><b>Last Name: </b>".w::d($appRequested->dependant_lname),*/

                        //$dependant_detail,

                        "<b>Id: </b>".$appRequested->patient_id."<br/><b>External Id: </b>".$appRequested->patient_unique_id."<br/><b>First Name: </b>".w::d($appRequested->patient_fname)."<br/><b>Last Name: </b>".w::d($appRequested->patient_lname)."<br/><b>Sign In Email: </b>".w::d($appRequested->patient_email_address)."<br/><b>Sign In Phone: </b>".w::d($appRequested->patient_phone).$communication_detail,
                        
                        $appRequested->appointment_date.' '.$appRequested->appointment_time,
                        $appRequested->appointment_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');
?>