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

// Logical section of page
$patientloginchangeObj =  new  patientlogin_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)
        {
            $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['patient_id']) && $_REQUEST['patient_id'] != '')
    {
        $where .=" AND  patient_id LIKE '%".$_REQUEST['patient_id']."%'";           
    }


    if (isset($_REQUEST['search']['value']) && $_REQUEST['search']['value']!=""){
        $where = " p.patientlogin_change_oldemail LIKE '%".$_REQUEST['search']['value']."%' OR p.patientlogin_change_oldphone LIKE '%".$_REQUEST['search']['value']."%' OR p.patientlogin_change_newemail LIKE '%".$_REQUEST['search']['value']."%' OR p.patientlogin_change_newphone LIKE '%".$_REQUEST['search']['value']."%' OR p.patientlogin_change_mode LIKE '%".$_REQUEST['search']['value']."%' OR p.patientlogin_change_by LIKE '%".$_REQUEST['search']['value']."%' OR p.patientlogin_change_created_at LIKE '%".$_REQUEST['search']['value']."%' OR pt.patient_fname LIKE '%".w::e(ucfirst($_REQUEST['search']['value']))."%' OR pt.patient_lname LIKE '%".w::e(ucfirst($_REQUEST['search']['value']))."%' OR pt.patient_fname LIKE '%".w::e(ucwords($_REQUEST['search']['value']))."%' OR pt.patient_lname LIKE '%".w::e(ucwords($_REQUEST['search']['value']))."%' ";
    }    
        
    $orderFields = array('patientlogin_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(" patientlogin_change_id desc ");
    }
    

    $tables = array('tbl_patient pt');
    $jfields = array('pt.patient_id = p.patientlogin_change_patient_id');
    $jtype   = array("INNER");
    $data    = array('where' => $where, 'joinTables' =>$tables, 'joinFields' =>$jfields, 'joinTypes' => $jtype,'orderBy'=>$orderby,'limit' => $limit);
    $patientloginchangeDetails = $patientloginchangeObj->getAllRecords($data);
    $recordsTotal   =   $patientloginchangeObj->getAllCount($data);
        
    if ($patientloginchangeDetails && count($patientloginchangeDetails)>0){
        foreach ($patientloginchangeDetails as $key => $patientloginchange){

            if($patientloginchange->patientlogin_change_by == 'user')
            {
                $userObj = new user();
                $userDetails = $userObj->fromId($patientloginchange->patientlogin_change_action_id);

                $actor_name = $userDetails->user_fname." ".$userDetails->user_lname;

            }else if($patientloginchange->patientlogin_change_by == 'patient')
            {
                $patientObj = new patient();
                $patientDetails = $patientObj->fromId($patientloginchange->patientlogin_change_action_id);
                $actor_name = w::d($patientDetails->patient_fname)." ".w::d($patientDetails->patient_lname);
            }else if($patientloginchange->patientlogin_change_by == 'pms')
            {
                $actor_name = "PMS";
            }
        
            $response[] = array( 
                    $patientloginchange->patientlogin_change_id,

                    "<b>Email</b> : ".w::d($patientloginchange->patientlogin_change_oldemail)."<br/><b>Phone</b> : ".w::d($patientloginchange->patientlogin_change_oldphone),

                    "<b>Email</b> : ".w::d($patientloginchange->patientlogin_change_newemail)."<br/><b>Phone</b> : ".w::d($patientloginchange->patientlogin_change_newphone),

                    "<b>Id : </b>".$patientloginchange->patient_id."<br/><b>External Id</b> : ".$patientloginchange->patient_unique_id."<br/><b>First name</b> : ".w::d($patientloginchange->patient_fname)."<br/><b>Last Name </b> : ".w::d($patientloginchange->patient_lname),

                    "<b>By</b> : ".$patientloginchange->patientlogin_change_by."<br/><b>Actor</b> : ".$actor_name,
                    
                    $patientloginchange->patientlogin_change_mode,
                    
                    $patientloginchange->patientlogin_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');
?>