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

// Logical section of page
$messagecenter_eventObj =  new  messagecenter_event();


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['fullname']) && $_REQUEST['fullname'] != '')
    {
        $patientname = preg_replace('/\s\s+/', ' ', $_REQUEST['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))."%')";
                if( ($key+1) != count($fullname) )
                {
                        $where .=" OR ";
                }
            }
        }
    }

    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['search']['value']) && $_REQUEST['search']['value']!=""){
        $where = " messagecenter_event_id LIKE '%".$_REQUEST['search']['value']."%' OR  log_appointment_id LIKE '%".$_REQUEST['search']['value']."%' ";
    }


    $orderFields = array('messagecenter_event_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(" messagecenter_event_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 = $messagecenter_eventObj->getAllRecords($data);
    //print_r($messageCenterDetails);exit;
    $recordsTotal   =   $messagecenter_eventObj->getAllCount($data);
     
    if ($messageCenterDetails && count($messageCenterDetails)>0){
        
        foreach ($messageCenterDetails as $key => $messagecenterdata)
        {
            
            $issentemail = ($messagecenterdata->issentemail == '1') ? 'Yes' : 'No';

            $issentsms = ($messagecenterdata->issentsms == '1') ? 'Yes' : 'No';

            $issentappnotification = ($messagecenterdata->issentappnotification == '1') ? 'Yes' : 'No';

            $isnotified = ($messagecenterdata->isnotified == '1') ? 'Yes' : 'No';

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

            $aptdate = '';
            $apttime = '';
            if(isset($messagecenterdata->metadata) && !is_null($messagecenterdata->metadata) && $messagecenterdata->metadata!=""){
                
                $aptmetadata = json_decode($messagecenterdata->metadata,1);
                
                if(array_key_exists('date', $aptmetadata))
                    $aptdate = $aptmetadata['date'];
                else
                    $aptdate = 'N/A';

                if(array_key_exists('time', $aptmetadata))
                    $apttime = $aptmetadata['time'];
                else
                    $apttime = 'N/A';

            }else{
                
                $aptdate = 'N/A';
                $apttime = 'N/A';
            }
            
            $response[] = array( 
                    "<b>Xcare Id</b>: ".$messagecenterdata->messagecenter_event_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 timestamp: </b><br/>".$messagecenterdata->created_at."<br/><b>Apt Date: </b>".$aptdate."<br/><b>Apt Time: </b>".$apttime,
                    "<b>Email</b>: ".$issentemail."<br/><b>SMS: </b>".$issentsms."<br/><b>Mobile App: </b>".$issentappnotification."<br/><b>Notified: </b>".$isnotified,
            );
        }
        $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');
?>