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

// Logical section of page
$patientdetailchangeObj =  new  patient_detail_change();

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['patient_fullname']) && $_REQUEST['patient_fullname'] != '')
    {

        $patientname = preg_replace('/\s\s+/', ' ', $_REQUEST['patient_fullname']);
        $fullname = explode(" ", $patientname);
        if($fullname && count($fullname)>0)
        {
            if(count($fullname)==2){
                $where .=" AND (dependant_fname='".w::e($fullname['0'])."' AND  dependant_lname='".w::e($fullname['1'])."' ) OR (dependant_lname='".w::e($fullname['0'])."' AND  dependant_fname='".w::e($fullname['1'])."' ) OR (dependant_fname='".w::e(ucfirst($fullname['0']))."' AND  dependant_lname='".w::e(ucfirst($fullname['1']))."' ) OR (dependant_lname='".w::e(ucfirst($fullname['0']))."' AND  dependant_fname='".w::e(ucfirst($fullname['1']))."' ) ";
            }else{
                foreach ($fullname as $key => $f) {
                    if($key==0){
                        $where .=" AND ( ";
                    }
                    $where .=" dependant_fname LIKE '%".w::e($f)."%' OR dependant_fname LIKE '%".w::e(ucfirst($f))."%'  OR dependant_lname LIKE '%".w::e($f)."%' OR dependant_lname LIKE '%".w::e(ucfirst($f))."%' OR dependant_email_address ='".w::e($f)."' OR dependant_phone = '".w::e($f)."' ";
                    if( ($key+1) != count($fullname) ){
                            $where .=" OR ";
                    }
                    if( ($key+1) == count($fullname) ){
                        $where .=" )";
                    }
                }
            }
        }    
    }

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

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

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

    if (isset($_REQUEST['search']['value']) && $_REQUEST['search']['value']!=""){
        $where = " p.patient_detail_change_id LIKE '%".$_REQUEST['search']['value']."%' OR p.patient_detail_change_pid LIKE '%".$_REQUEST['search']['value']."%' OR p.patient_detail_change_cid LIKE '%".$_REQUEST['search']['value']."%' OR p.patient_detail_change_type LIKE '%".$_REQUEST['search']['value']."%' OR p.patient_detail_change_value LIKE '%".$_REQUEST['search']['value']."%' OR p.patient_detail_change_loadeddate LIKE '%".$_REQUEST['search']['value']."%' OR p.patient_detail_change_created_at LIKE '%".$_REQUEST['search']['value']."%' ";
    }    
        
    $orderFields = array('patient_detail_change_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(" patient_detail_change_id desc ");
    }
    
    $tables = array('tbl_dependant dep','tbl_clinic cl','tbl_pmsPatient pms','tbl_user u');
    $jfields = array('dep.dependant_id = p.patient_detail_change_pid','cl.clinic_id = p.patient_detail_change_cid','pms.pmsPatient_xcare_id=dep.dependant_id','u.user_id=p.patient_detail_origin');
    $jtype   = array("INNER","LEFT","LEFT","LEFT");
    $data    = array('where' => $where, 'joinTables' =>$tables, 'joinFields' =>$jfields, 'joinTypes' => $jtype,'orderBy'=>$orderby,'limit' => $limit);
    $patientlogDetails = $patientdetailchangeObj->getAllRecords($data);
    //print_r($patientlogDetails);exit;
    $recordsTotal   =   $patientdetailchangeObj->getAllCount($data);
        
    if ($patientlogDetails && count($patientlogDetails)>0){
        foreach ($patientlogDetails as $key => $patientlog){

            $clinicDetails = '';
            if(isset($patientlog->clinic_id) && $patientlog->clinic_id != '')
            {
                $clinicDetails = "<b>Xcare Id</b>: ".$patientlog->clinic_id."<br/><b>Xcare External Id</b>: ".$patientlog->clinic_unique_id."<br/><b>Name</b>: ".$patientlog->clinic_name."<br/><b>Email</b>:".$patientlog->clinic_email;
            }
            
            $userdata = '';
            if(!is_null($patientlog->patient_detail_origin))
            {
                $userdata = "<br/><b>Clinic User:</b> ".$patientlog->user_fname." ".$patientlog->user_lname;
            }

            $response[] = array( 
                    $patientlog->patient_detail_change_id,

                    "<b>Xcare Id</b>: ".$patientlog->dependant_id."<br/><b>Xcare External Id</b>: ".$patientlog->dependant_unique_id."<br/><b>First Name</b>: ".w::d($patientlog->dependant_fname).
                        "<br/><b>Last Name</b>: ".w::d($patientlog->dependant_lname)."<br><b>PMS Id</b>:".$patientlog->pmsPatient_patient_id,

                    $clinicDetails,

                    $patientlog->patient_detail_change_type,

                    "<b>Old Value</b>: ".$patientlog->patient_detail_change_oldvalue."<br/><b>New Value</b>: ".$patientlog->patient_detail_change_value.$userdata."<br/><b>Timestamp:</b> ".$patientlog->patient_detail_change_created_at,
                    
                    "<b>Sikka Loaded</b>: ".$patientlog->patient_detail_change_loadeddate."<br/><b>Xcre Updated</b> :".$patientlog->patient_detail_change_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');
?>