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

// Logical section of page
$messagecentereventlogObj =  new  messagecentereventlog();


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 = " issent = '1' and (eventname = 'recall') ";

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

    if(isset($_REQUEST['clinic_id']) && $_REQUEST['clinic_id'] != '')
    {
        //$where .= " AND ap.appointment_clinic_id = '".$_REQUEST['clinic_id']."'";
        $appclinicId = explode(",", $_REQUEST['clinic_id']);
        
        if(count($appclinicId) > 1)
        {
            foreach ($appclinicId as $ackey => $appclinicvalue)
            {
                $appclinic_data .= " FIND_IN_SET('".$appclinicvalue."', appointment_clinic_id) OR ";   
            }
        }else{
            $appclinic_data = " FIND_IN_SET('".$_REQUEST['clinic_id']."', appointment_clinic_id)";
        }
        $appclinic_or_rem = rtrim($appclinic_data," OR ");

        $appclinic_final = ' AND ('.$appclinic_or_rem.')';
         
        $where .= $appclinic_final;
    }

    if(isset($_REQUEST['event_appointment_id']) && $_REQUEST['event_appointment_id'] != '')
    {
        $where .= " AND m.appointment_id = '".$_REQUEST['event_appointment_id']."'";
    }

    if(isset($_REQUEST['event_name']) && $_REQUEST['event_name'] != '')
    {
        $where .= " AND m.eventname LIKE '%".$_REQUEST['event_name']."%'";
    }
 
    if(isset($_REQUEST['event_name_app_srno']) && $_REQUEST['event_name_app_srno'] != '')
    {
        $data = '%"srNo"'.":".'"'.$_REQUEST['event_name_app_srno'].'"'."%".'';
        
        $where .="AND appointment_metadata LIKE '".$data."'";
    }

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

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


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

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

    if(isset($_REQUEST['event_date']) && $_REQUEST['event_date'] != '')
    {
        $where .= " AND DATE(m.eventfireddate) = '".$_REQUEST['event_date']."'";
    }
    

    if (isset($_REQUEST['search']['value']) && $_REQUEST['search']['value']!=""){
        $where .= " AND  messagecenter_event_id LIKE '%".$_REQUEST['search']['value']."%' OR  m.appointment_id LIKE '%".$_REQUEST['search']['value']."%' OR  m.clinic_id LIKE '%".$_REQUEST['search']['value']."%' OR m.email LIKE '%".$_REQUEST['search']['value']."%' OR m.phone LIKE '%".$_REQUEST['search']['value']."%' m.fcmtoken LIKE '%".$_REQUEST['search']['value']."%' OR c.clinic_id LIKE '%".$_REQUEST['search']['value']."%' OR c.clinic_name LIKE '%".$_REQUEST['search']['value']."%' OR c.clinic_email LIKE '".$_REQUEST['search']['value']."' OR m.eventname LIKE '%".$_REQUEST['search']['value']."%' OR m.notificationtype LIKE '%".$_REQUEST['search']['value']."%' ";
    }


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

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

    $messageCenterDetails = $messagecentereventlogObj->getAllRecords($data);
    //print_r($messageCenterDetails);exit;
    $recordsTotal   =   $messagecentereventlogObj->getAllCount($data);
     
    if ($messageCenterDetails && count($messageCenterDetails)>0){
        
        foreach ($messageCenterDetails as $key => $messagecenterdata)
        {
            $issentemaildata = '';
            $issentsmsdata ='';
            $issentappnotificationdata = '';
            if($messagecenterdata->notificationtype == 'email')
            {
                $issentemail = ($messagecenterdata->issent == '1') ? 'Yes' : 'No';
                $issentemaildata = "<b>Email</b>: ".$messagecenterdata->email."<br/><b>Email Sent</b>: ".$issentemail;
            }

            if($messagecenterdata->notificationtype == 'sms')
            {
                $issentsms = ($messagecenterdata->issent == '1') ? 'Yes' : 'No';
                $issentsmsdata = "<b>Phone</b>: ".$messagecenterdata->phone."<br/><b>SMS Sent</b>: ".$issentsms;
            }

            if($messagecenterdata->notificationtype == 'appnotification')
            {
                $issentappnotification = ($messagecenterdata->issent == '1') ? 'Yes' : 'No';
                $issentappnotificationdata = "<b>FCM Token</b>: ".$messagecenterdata->fcmtoken."<br/><b>Mobile App Sent</b>: ".$issentappnotification;
            }

            $metadata =json_decode($messagecenterdata->appointment_metadata,1);
            $srNo =$metadata['pms']['srNo'];

            date_default_timezone_set('UTC');
            $utctime = strtotime($messagecenterdata->eventfireddate);
            date_default_timezone_set('Canada/Eastern');
            
            $response[] = array( 
                    "<b>Xcare Id</b>: ".$messagecenterdata->messagecentereventlog_id,
                    "<b>Xcare Apt Id: </b>".$messagecenterdata->appointment_id.'<br/><b>Xcare Apt External Id: </b>'.$messagecenterdata->appointment_unique_id.'<br/><b>Date: </b>'.$messagecenterdata->appointment_date.'<br/><b>Time: </b>'.$messagecenterdata->appointment_time.'<br/><b>Xcare Status: </b>'.$messagecenterdata->appointment_status.'<br/><b>PMS Appointment Sr No: </b>'.$srNo,
                    "<b>Xcare Id</b>: ".$messagecenterdata->clinic_id."<br/><b>Name: </b>".$messagecenterdata->clinic_name."<br/><b>Phone</b>: ".$messagecenterdata->clinic_phone."<br/><b>Email</b>: ".$messagecenterdata->clinic_email,
                    "<b>Xcare Id</b>: ".$messagecenterdata->dependant_id."<br/><b>Xcare External Id</b>: ".$messagecenterdata->dependant_unique_id."<br/><b>First Name</b>: ".w::d($messagecenterdata->dependant_fname)."<br/><b>Last Name</b>: ".w::d($messagecenterdata->dependant_lname)."<br/><b>Email Id</b>: ".w::d($messagecenterdata->dependant_email_address)."<br/><b>Phone</b>: ".w::d($messagecenterdata->dependant_phone)."<br><b>Preference</b>: ".$messagecenterdata->dependant_contact_method,
                    "<b>Event</b>: ".$messagecenterdata->eventname."<br/><b>Event type: </b><br/>".$messagecenterdata->notificationtype."<br/><b>Event timestamp: </b><br/>".date('Y-m-d H:i:s',$utctime),
                    $issentemaildata." ".$issentsmsdata." ".$issentappnotificationdata,
            );
        }
        $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');
?>