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

// Logical section of page
//$schedulerequestObj =  new schedulerequest();
$schedulerequestObj =  new scheduleRequest();


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['schedulerequest_name']) && $_REQUEST['schedulerequest_name'] != '')
    {
        $name = preg_replace('/\s\s+/', ' ', $_REQUEST['schedulerequest_name']);
        $fullname = explode(" ", $name);
        if($fullname && count($fullname)>0)
        {
            $where ="";
            foreach ($fullname as $key => $f) 
            {
                $where .=" (s.scheduleRequest_fname LIKE '%".$f."%' OR s.scheduleRequest_fname LIKE '%".(ucfirst($f))."%'  OR s.scheduleRequest_lname LIKE '%".$f."%' OR s.scheduleRequest_lname LIKE '%".ucfirst($f)."%')";
                if( ($key+1) != count($fullname) )
                {
                        $where .=" OR ";
                }
            }
        }
    }

    if(isset($_REQUEST['clinic_id']) && $_REQUEST['clinic_id'] != '')
    {
        $where .= " AND s.scheduleRequest_clinic_id = '".$_REQUEST['clinic_id']."'";
    }


    if (isset($_REQUEST['search']['value']) && $_REQUEST['search']['value']!="")
    {
        $where = " scheduleRequest_id LIKE '%".$_REQUEST['search']['value']."%' OR  scheduleRequest_fname LIKE '%".$_REQUEST['search']['value']."%' OR scheduleRequest_lname LIKE '%".$_REQUEST['search']['value']."%' ";
    }

    $orderFields = array('scheduleRequest_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{
        $orderby = array(" scheduleRequest_id desc ");
    }

    /*$tables = array('tbl_appointment ap','tbl_dependant d','tbl_clinic c');
    $jfields = array('ap.appointment_id = l.log_appointment_id','ap.appointment_dependant_id = d.dependant_id','c.clinic_id=l.log_clinic_id');
    $jtype   = array("INNER","INNER","INNER");*/

    $tables = array('tbl_clinic cl');
    $jfields = array('cl.clinic_id = s.scheduleRequest_clinic_id');
    $jtype   = array("INNER");
    $data   = array('where' => $where,  'joinTables' =>$tables, 'joinFields' =>$jfields, 'joinTypes' => $jtype,'orderBy'=>$orderby,'limit' => $limit);

    $schedulerequestDetails = $schedulerequestObj->getAllRecords($data);
    //print_r($schedulerequestDetails);exit;
    $recordsTotal   =  $schedulerequestObj->getAllCount($data);
     
    if ($schedulerequestDetails && count($schedulerequestDetails)>0){
        $alldoctorDetail = '';
        foreach ($schedulerequestDetails as $key => $schedulerequestdata)
        {

            $cdoctorObj =  new cdoctor();
            $fields = array('cdoctor_id','cdoctor_provider_id','cdoctor_fullname','cdoctor_email','cdoctor_phone');
            $where = "c.cdoctor_doctor_id='".$schedulerequestdata->scheduleRequest_doctor_id."' AND cdoctor_clinic_id = '".$schedulerequestdata->scheduleRequest_clinic_id."' ";
            $cdata  = array('fields' => $fields,'where' => $where);
            $cdoctorDetails = $cdoctorObj->fromFields($cdata);

            if($cdoctorDetails)
            {
                $alldoctorDetail = "<b>Provider Id: </b>".$cdoctorDetails->cdoctor_id."<br/><b>Provider External Id: </b>".$cdoctorDetails->cdoctor_provider_id."<br/><b>Name: </b>".$cdoctorDetails->cdoctor_fullname."<br/><b>Email: </b>".$cdoctorDetails->cdoctor_email."<br/><b>Phone: </b>".$cdoctorDetails->cdoctor_phone;
            }
            
            $response[] = array( 
                    "<b>Id</b>: ".$schedulerequestdata->scheduleRequest_id,
                    $alldoctorDetail,
                    "<b>Id</b>: ".$schedulerequestdata->clinic_id."<br/><b>Name</b>: ".$schedulerequestdata->clinic_name."<br/><b>Phone</b>: ".$schedulerequestdata->clinic_phone."<br/><b>Email</b>: ".$schedulerequestdata->clinic_email,
                    $schedulerequestdata->scheduleRequest_fname." ".$schedulerequestdata->scheduleRequest_lname,
                    "<b>Type</b>: ".$schedulerequestdata->scheduleRequest_type."<br/><b>Start Time</b>: ".$schedulerequestdata->scheduleRequest_starttime."<br/><b>End Time</b>: ".$schedulerequestdata->scheduleRequest_endtime."<br/><b>Notes</b>: ".$schedulerequestdata->scheduleRequest_notes
            );
        }
        $result=array('recordsFiltered'=>$recordsTotal,'recordsTotal'=>$recordsTotal,'data'=>$response);
    }else{
        $result=array('recordsFiltered'=>0,'recordsTotal'=>0,'data'=>array());
    }
    echo json_encode($result);
    exit;
}

/*$clinicObj = new clinic();
$where = "clinic_status = '1'";
$data = array('where'=>$where);
$allclinic = $clinicObj->getAllRecords($data);*/

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