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

// Logical section of page
$referral_eventObj =  new  referral_event();


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['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['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 .=" (p.patient_fname LIKE '%".w::e($f)."%' OR p.patient_fname LIKE '%".w::e(ucfirst($f))."%'  OR p.patient_lname LIKE '%".w::e($f)."%' OR p.patient_lname LIKE '%".w::e(ucfirst($f))."%')";
                if( ($key+1) != count($fullname) )
                {
                        $where .=" OR ";
                }
            }
        }
    }*/


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

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

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


    if (isset($_REQUEST['search']['value']) && $_REQUEST['search']['value']!=""){
        $where = " referral_event_id LIKE '%".$_REQUEST['search']['value']."%' OR ref.referral_type LIKE '%".$_REQUEST['search']['value']."%' ";
    }


    $orderFields = array('referral_event_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_event_id desc ");
    }

    
    $tables = array('tbl_referral ref','tbl_patient p');
    $jfields = array('ref.referral_id=r.referral_id','p.patient_id = ref.referral_patient_id');
    $jtype   = array("INNER","INNER");
    $data   = array('where' => $where,  'joinTables' =>$tables, 'joinFields' =>$jfields, 'joinTypes' => $jtype,'orderBy'=>$orderby,'limit' => $limit);

    $referralDetails = $referral_eventObj->getAllRecords($data);
    //print_r($referralDetails);exit;
    $recordsTotal   =   $referral_eventObj->getAllCount($data);
     
    if ($referralDetails && count($referralDetails)>0){
        
        foreach ($referralDetails as $key => $referraldata)
        {
            
            $issentemail = ($referraldata->issentemail == '1') ? 'Yes' : 'No';

            $issentsms = ($referraldata->issentsms == '1') ? 'Yes' : 'No';

            /*$issentappnotification = ($referraldata->issentappnotification == '1') ? 'Yes' : 'No';*/

            $isnotified = ($referraldata->isnotified == '1') ? 'Yes' : 'No';

            if($referraldata->referral_type == 'email')
            {
                $eventdata = "<b>Receiver Email: </b>".$referraldata->referral_on;
            }else if($referraldata->referral_type == 'sms')
            {
                $eventdata = "<b>Receiver SMS: </b>".$referraldata->referral_on;
            }

            if($referraldata->event_name == 'email')
            {
                $send_data = "<b>Email</b>: ".$issentemail;
            }else if ($referraldata->event_name == 'sms')
            {
                $send_data = "<b>SMS: </b>".$issentsms;
            }

            
            $response[] = array( 
                    "<b>Xcare Id</b>: ".$referraldata->referral_event_id,
                    "<b>Xcare Patient Id</b>: ".$referraldata->patient_id."<br/><b>Xcare Patient External Id</b>: ".$referraldata->patient_unique_id."<br/><b>First Name</b>: ".w::d($referraldata->patient_fname)."<br/><b>Last Name</b>: ".w::d($referraldata->patient_lname)."<br/><b>Email</b>: ".w::d($referraldata->patient_email_address)."<br/><b>Phone</b>: ".w::d($referraldata->patient_phone)."<br/><b>Referral Code: </b>".$referraldata->referral_code."<br/><b>Sender Point: </b>".$referraldata->referral_senderpoint,
                    "<b>Event</b>: ".$referraldata->event_name."<br/>".$eventdata."<br/><b>Receiver Point: </b>".$referraldata->referral_receiverpoint,
                    $send_data."<br/><b>Notified: </b>".$isnotified."<br/><b>Event timestamp: </b>".$referraldata->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');
?>