<?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 ='faxmail') ";

    
    /*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 m.clinic_id = '".$_REQUEST['clinic_id']."'";
    
    }

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

    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 m.messagecentereventlog_id LIKE '%".$_REQUEST['search']['value']."%' OR m.email LIKE '%".$_REQUEST['search']['value']."%' OR m.phone LIKE '%".$_REQUEST['search']['value']."%' OR fcmtoken LIKE '%".$_REQUEST['search']['value']."%' OR c.clinic_id LIKE '%".$_REQUEST['search']['value']."%' OR c.clinic_name LIKE '%".$_REQUEST['search']['value']."%' OR c.clinic_phone 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_clinic c');
    $jfields = array('c.clinic_id=m.clinic_id');
    $jtype   = array("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;
            }
            
            if($messagecenterdata->notificationtype == 'inactivepatient')
            {
                $notificationtype = "<span style='background-color:#FFDFBF;'>".$messagecenterdata->notificationtype."</span>";
            }else{
                $notificationtype = $messagecenterdata->notificationtype;
            }
            
            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 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>Event</b>: ".$messagecenterdata->eventname,
                    "<b>Notification type: </b>".$notificationtype."<br/>".$issentemaildata." ".$issentsmsdata." ".$issentappnotificationdata."<br/><b>Event timestamp: </b><br/>".date('Y-m-d H:i:s',$utctime),
            );
        }
        $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');
?>