<?php 
// Include main file
include_once('../libs/main.php');
// Check admin Login
include_once('checkLogin.php');
// Logical section of page
$referralObj =  new  referral();

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'] != ''){
        $patientname = preg_replace('/\s\s+/', ' ', $_REQUEST['fullname']);
        $fullname = explode(" ", $patientname);
        if($fullname && count($fullname)>0){
            $where ="";
            foreach ($fullname as $key => $f){
                $where .=" patient_fname LIKE '%".w::e($f)."%' OR patient_fname LIKE '%".w::e(ucfirst($f))."%'  OR patient_lname LIKE '%".w::e($f)."%' OR patient_lname LIKE '%".w::e(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['referral_type']) && $_REQUEST['referral_type'] != ''){
        $where .= " AND referral_type = '".$_REQUEST['referral_type']."'";
    }
    if(isset($_REQUEST['referral_on']) && $_REQUEST['referral_on'] != ''){
        $where .= " AND referral_on = '".$_REQUEST['referral_on']."'";
    }
    if(isset($_REQUEST['patient_id_uniqueid']) && $_REQUEST['patient_id_uniqueid'] != ''){
        $where .= " AND (patient_id = '".$_REQUEST['patient_id_uniqueid']."' )";
    }

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

    if (isset($_REQUEST['search']['value']) && $_REQUEST['search']['value']!=""){
        $where = " referral_id LIKE '%".$_REQUEST['search']['value']."%' OR referral_type LIKE '%".$_REQUEST['search']['value']."%' OR referral_on LIKE '%".$_REQUEST['search']['value']."%' ";
    }    
    
    $orderFields = array('referral_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(" referral_id desc ");
    }
    
    $tables = array('tbl_patient p');
    $jfields = array('p.patient_id = r.referral_patient_id');
    $jtype   = array("INNER"); 
    $data    = array('joinTables' =>$tables, 'joinFields' =>$jfields, 'joinTypes' => $jtype,'where' => $where,'limit' => $limit, 'orderBy'=>$orderby);

    $referralDetails = $referralObj->getAllRecords($data);

    $recordsTotal   =   $referralObj->getAllCount($data);

    if ($referralDetails && count($referralDetails)>0){
        foreach ($referralDetails as $key => $referraldata){            
            
            $response[] = array('<input type="checkbox" name="check[]" id="check[]" value="'.$referraldata->referral_id.'" class="check_class" />',
                    $referraldata->referral_id,
                    "<b>Xcare Patient Id: </b>".$referraldata->patient_id."<br/><b>External Id: </b>".$referraldata->patient_unique_id."<br/><b>Name: </b>".w::d($referraldata->patient_fname)." ".w::d($referraldata->patient_lname)."<br/><b>Email: </b>".w::d($referraldata->patient_email_address)."<br><b>Phone: </b>".w::d($referraldata->patient_phone), 
                    $referraldata->referral_type,
                    $referraldata->referral_on,
                    "<b>Total:</b> ".$referraldata->referral_count."<br/><b>Is Used: </b>".$referraldata->referral_isUsed
             );
        }
        $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');
?>