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

// Logical section of page
$logObj =  new  log();


if (isset($_REQUEST['act']) && $_REQUEST['act']=='load' && $_REQUEST['clinic_id'] != ''){
    
    $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 AND c.clinic_id='".$_REQUEST['clinic_id']."'";

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

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

    if(isset($_REQUEST['searchloglistdate']) && $_REQUEST['searchloglistdate'] != '' && $_REQUEST['searchloglisttime'] == '')
    {
        $where .= " AND DATE(log_created_at) = '".$_REQUEST['searchloglistdate']."'";   
    }

    if(isset($_REQUEST['searchloglisttime']) && $_REQUEST['searchloglisttime'] != '')
    {
        $searchdttime = $_REQUEST['searchloglistdate']." ".$_REQUEST['searchloglisttime'];
        $where .= " AND DATE_FORMAT(log_created_at, '%Y-%m-%d %H:%i') = '".$searchdttime."'";
    }

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


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

    
    $tables = array('tbl_appointment ap','tbl_dependant d','tbl_clinic c');
    $jfields = array('ap.appointment_id = l.log_appointment_id','ap.appointment_dependant_id = d.dependant_id','c.clinic_id=l.log_clinic_id');
    $jtype   = array("INNER","INNER","INNER");
    $data   = array('where' => $where,  'joinTables' =>$tables, 'joinFields' =>$jfields, 'joinTypes' => $jtype,'orderBy'=>$orderby,'limit' => $limit);

    $logDetails = $logObj->getAllRecords($data);
    //print_r($logDetails);exit;
    $recordsTotal   =   $logObj->getAllCount($data);
     
    if ($logDetails && count($logDetails)>0){
        foreach ($logDetails as $key => $logdata)
        {
            $metadata =json_decode($logdata->appointment_metadata,1);
            $srNo =$metadata['pms']['srNo'];
            $patientObj = new patient();
            $signInEmail ="";
            $signInPhone ="";
            if(!is_null($logdata->dependant_login_id)){
                $patient=$patientObj->fromId($logdata->dependant_login_id);
                $signInEmail=w::d($patient->patient_email_address);
                $signInPhone =w::d($patient->patient_phone);
            }

            if(is_null($logdata->log_appointment_date) && is_null($logdata->log_appointment_time)){
                
                $appointment_date = $logdata->appointment_date." ".$logdata->appointment_time;
            }
            else{
                $appointment_date = $logdata->log_appointment_date." ".$logdata->log_appointment_time;
            }
            $guarantor = $patientObj->fromId($logdata->dependant_guarantor_id);
            $ginfo ="Xcare Id: ".$guarantor->patient_unique_id."<br/>".w::d($guarantor->patient_fname)." ".w::d($guarantor->patient_lname);
            $response[] = array( 
                    "<b>Origin</b>: ".$logdata->log_origin."<br/><b>Xcare Appoinmnet Id</b>: ".$logdata->appointment_unique_id."<br/> <b>PMS Appoinmnet Sr No</b>: ".$srNo,
                    "<b>Patient Id</b>: ".$logdata->dependant_unique_id."<br/><b>First Name</b>: ".w::d($logdata->dependant_fname)."<br/><b>Last Name</b>: ".w::d($logdata->dependant_lname)."<br/><b>Email Id</b>: ".w::d($logdata->dependant_email_address)."<br/><b>Phone</b>: ".w::d($logdata->dependant_phone)."<br/><br/><b>Details:</b><br/><b>Sign In Email</b>: ".$signInEmail."<br/><b>Sign In Mobile</b>: ".$signInPhone."<br/><b>Guarantor Info</b>: ".$ginfo,
                    /*"Sign In Email: ".$signInEmail."<br/>Sign In Mobile: ".$signInPhone."<br/>Guarantor Info: ".$ginfo,*/
                    $logdata->log_type,
                    '<b>Xcare</b>: '.$logdata->log_xcareStatus.'</br><b>PMS</b>: '.$logdata->log_pmsStatus,
                    //$logdata->log_loaded_date,
                    //$logdata->log_created_at
                    "<b>xcare loaded date: </b>".$logdata->log_datetime."<br/><b>Sikka loaded date: </b>".$logdata->log_loaded_date."<br/><b>Appoinmnet date Time: </b>".$appointment_date,
                    $logdata->log_text
            );
        }
        $result=array('recordsFiltered'=>$recordsTotal,'recordsTotal'=>$recordsTotal,'data'=>$response);
    }else{
        $result=array('recordsFiltered'=>0,'recordsTotal'=>0,'data'=>array());
    }
    echo json_encode($result);
    exit;
}

/*$clinicObj = new clinic();
$where = "clinic_status = '1'";
$data = array('where'=>$where);
$allclinic = $clinicObj->getAllRecords($data);*/

// Include Template 
include_once('../libs/routs.php');
?>