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

// Logical section of page
$reviewObj =  new  review();


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

    if(isset($_REQUEST['firstname']) && $_REQUEST['firstname'] != '' && $_REQUEST['lastname'] != '')
    {
        $firstname = trim($_REQUEST['firstname']);
        $lastname = trim($_REQUEST['lastname']);

        $where .= " AND ((p.patient_fname='".w::e($firstname)."' and   p.patient_lname='".w::e($lastname)."') OR (p.patient_fname='".w::e(ucfirst($firstname))."' and   p.patient_lname='".w::e(ucfirst($lastname))."'))";
    }else if(isset($_REQUEST['firstname']) && $_REQUEST['firstname'] != '' && $_REQUEST['lastname'] == ''){

        $firstname = trim($_REQUEST['firstname']);

        $where .= " AND ((p.patient_fname LIKE '%".w::e($firstname)."%') OR (p.patient_fname LIKE '%".w::e(ucfirst($firstname))."%' ))";
    }else if(isset($_REQUEST['lastname']) && $_REQUEST['lastname'] != '' && $_REQUEST['firstname'] == ''){

        $lastname = trim($_REQUEST['lastname']);

        $where .= " AND ((p.patient_lname LIKE '%".w::e($lastname)."%') OR ( p.patient_lname LIKE '%".w::e(ucfirst($lastname))."%'))";
    }

    if(isset($_REQUEST['review_app_uniqueId']) && $_REQUEST['review_app_uniqueId'] != '')
    {
        $where .= " AND a.appointment_unique_id =".$_REQUEST['review_app_uniqueId'];
    }


    if(isset($_REQUEST['review_text']) && $_REQUEST['review_text'] != '')
    {
        $where .= " AND review_text LIKE '%".$_REQUEST['review_text']."%'";
    }

    if(isset($_REQUEST['clinic_id']) && $_REQUEST['clinic_id'] != '')
    {
        $reviewclinicId = explode(",",$_REQUEST['clinic_id']);

        if(count($reviewclinicId) > 1)
        {
            foreach($reviewclinicId as $rckey => $clinic_value)
            {
                $reviewclinicdata .= " FIND_IN_SET ('".$clinic_value."',review_clinic_id) OR ";
            }
        }else{
            $reviewclinicdata = " FIND_IN_SET('".$_REQUEST['clinic_id']."',review_clinic_id)";
        }

        $reviewclinic_or_rem = rtrim($reviewclinicdata," OR ");
        
        $reviewclinic_final = ' AND ('.$reviewclinic_or_rem.')';

        $where .= $reviewclinic_final;

    }

    if(isset($_REQUEST['review_app_status']) && $_REQUEST['review_app_status'] != '')
    {
        $where .= " AND a.appointment_status = '".$_REQUEST['review_app_status']."'";
    }

    if(isset($_REQUEST['review_patient_uniqueId']) && $_REQUEST['review_patient_uniqueId'] != '')
    {
        $where .= " AND p.patient_unique_id =".$_REQUEST['review_patient_uniqueId'];
    }

    
    if (isset($_REQUEST['search']['value']) && $_REQUEST['search']['value']!=""){
        $where = "review_id LIKE '%".$_REQUEST['search']['value']."%' OR review_visit_score LIKE '%".$_REQUEST['search']['value']."%' OR review_rate LIKE '%".$_REQUEST['search']['value']."%' OR review_text LIKE '%".$_REQUEST['search']['value']."%' OR a.appointment_unique_id LIKE '%".$_REQUEST['search']['value']."%' OR a.appointment_date LIKE '%".$_REQUEST['search']['value']."%' OR  a.appointment_time LIKE '%".$_REQUEST['search']['value']."%' OR  a.appointment_etime LIKE '%".$_REQUEST['search']['value']."%' OR  a.appointment_status LIKE '%".$_REQUEST['search']['value']."%' OR c.clinic_id LIKE '%".$_REQUEST['search']['value']."%' OR c.clinic_unique_id LIKE '%".$_REQUEST['search']['value']."%' OR c.clinic_name LIKE '%".$_REQUEST['search']['value']."%' OR c.clinic_email LIKE '%".$_REQUEST['search']['value']."%' OR c.clinic_phone LIKE '%".$_REQUEST['search']['value']."%' OR c.clinic_website LIKE '%".$_REQUEST['search']['value']."%' OR p.patient_id LIKE '%".$_REQUEST['search']['value']."%' OR p.patient_unique_id LIKE '%".$_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('review_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(" review_id desc ");
    }
    
    $fields  = array('r.*','p.*','a.*','c.*');
    $tables = array('tbl_patient p','tbl_appointment a','tbl_clinic c');
    $jfields = array("p.patient_id=r.review_patient_id",'a.appointment_id=r.review_appointment_id','c.clinic_id=r.review_clinic_id');
    $jtype = array("INNER","LEFT","LEFT");
    $data   = array('fields' => $fields,'where' => $where,'joinTables' =>$tables, 'joinFields' =>$jfields, 'joinTypes' => $jtype,'limit' => $limit);

    //echo $where;exit;
    $reviewDetails = $reviewObj->getAllRecords($data);
    //print_r($reviewDetails);exit;
    $recordsTotal   =   $reviewObj->getAllCount($data);
     
    if ($reviewDetails && count($reviewDetails)>0)
    {
        foreach ($reviewDetails as $key => $reviewdata)
        {

            if(!is_null($reviewdata->review_doctor_id))
            {
                $cdoctorObj = new cdoctor();
                $tables = array("tbl_doctor d");
                $jfields = array("d.doctor_id=c.cdoctor_doctor_id");
                $jtype = array("INNER");
                $where = 'cdoctor_clinic_id = '.$reviewdata->review_clinic_id.' AND cdoctor_doctor_id = '.$reviewdata->review_doctor_id.'';
                $data = array('joinTables'=>$tables,'joinFields'=>$jfields,'joinTypes'=>$jtype,'where' => $where);
                $doctordata = $cdoctorObj->fromFields($data);
                //print_r($doctordata);exit;
                $doctor_detail = '';
                
                if(!empty($doctordata))
                {
                    $doctor_detail = "<b>Provider Id : </b>".$doctordata->cdoctor_doctor_id."<br/><b>Provider External Id : </b>".$doctordata->cdoctor_provider_id."<br/><b>Name : </b>".$doctordata->cdoctor_fullname."<br/><b>Email : </b>".$doctordata->cdoctor_email."<br><b>Phone : </b>".$doctordata->cdoctor_phone."<br><b>Gender : </b>".$doctordata->doctor_gender."<br><b>Licence : </b>".$doctordata->doctor_licence;
                }
            }

            $response[] = array( 
                    "<b>ID: </b>".$reviewdata->review_id,

                    "<b>Patient Id: </b>".$reviewdata->patient_id."<br/><b>External Id: </b>".$reviewdata->patient_unique_id."<br/><b>First Name: </b>".w::d($reviewdata->patient_fname)."<br/><b>Last Name: </b>".w::d($reviewdata->patient_lname)."<br/><b>Email :</b>".w::d($reviewdata->patient_email_address)."<br/><b>Phone: </b>".w::d($reviewdata->patient_phone),

                    "<b>Appointment External Id: </b>".$reviewdata->appointment_unique_id."<br/><b>Date: </b>".$reviewdata->appointment_date."<br/><b>Start Time: </b>".$reviewdata->appointment_time."<br/><b>End Time: </b>".$reviewdata->appointment_etime."<br/><b>Status: </b>".$reviewdata->appointment_status,

                
                    "<b>Xcare Clinic Id: </b>".$reviewdata->clinic_id."<br/><b> External Id: </b>".$reviewdata->clinic_unique_id."<br/><b>Name: </b>".$reviewdata->clinic_name."<br/><b>Email: </b>".$reviewdata->clinic_email.'<br/><b>Phone: </b>'.$reviewdata->clinic_phone."<br/><b>Website: </b>".$reviewdata->clinic_website,

                    $doctor_detail,
                    
                    "<b>Visit score: </b>".$reviewdata->review_visit_score."<br/><b>Rate: </b>".$reviewdata->review_rate."<br/><b>Text: </b>".$reviewdata->review_text."<br><b>created: </b>".$reviewdata->review_created_at,
            );
        }
        $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');
?>