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

// Logical section of page
$paymentnotification_eventObj =  new  paymentnotification_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['firstname']) && $_REQUEST['firstname'] != '' && $_REQUEST['lastname'] != '')
    {
        $firstname = trim($_REQUEST['firstname']);
        $lastname = trim($_REQUEST['lastname']);

        $where .= " AND ((d.dependant_fname='".w::e($firstname)."' and   d.dependant_lname='".w::e($lastname)."') OR (d.dependant_fname='".w::e(ucfirst($firstname))."' and   d.dependant_lname='".w::e(ucfirst($lastname))."'))";
    }else if(isset($_REQUEST['firstname']) && $_REQUEST['firstname'] != '' && $_REQUEST['lastname'] == ''){

        $firstname = trim($_REQUEST['firstname']);

        $where .= " AND ((d.dependant_fname LIKE '%".w::e($firstname)."%') OR (d.dependant_fname LIKE '%".w::e(ucfirst($firstname))."%' ))";
    }else if(isset($_REQUEST['lastname']) && $_REQUEST['lastname'] != '' && $_REQUEST['firstname'] == ''){

        $lastname = trim($_REQUEST['lastname']);

        $where .= " AND ((d.dependant_lname LIKE '%".w::e($lastname)."%') OR ( d.dependant_lname LIKE '%".w::e(ucfirst($lastname))."%'))";
    }

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

        $patientclinic_final = ' AND ('.$patientclinic_or_rem.')';
         
        $where .= $patientclinic_final;
    }

    if(isset($_REQUEST['event_appointment_id']) && $_REQUEST['event_appointment_id'] != '')
    {
        $where .= " AND (prd.patientrecurring_detail_appointment_id = '".$_REQUEST['event_appointment_id']."' OR p.paymentnotification_event_id = '".$_REQUEST['event_appointment_id']."' OR ap.appointment_unique_id = '".$_REQUEST['event_appointment_id']."' )";
    }

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

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

    if(isset($_REQUEST['patient_messagecenter_event']) && $_REQUEST['patient_messagecenter_event'] != '')
    {
        $where .= " AND eventname = '".$_REQUEST['patient_messagecenter_event']."'";   

    }

    if(isset($_REQUEST['patient_messagecenter_event_date']) && $_REQUEST['patient_messagecenter_event_date'] != '')
    {
        $where .= " AND DATE(created_at) = '".$_REQUEST['patient_messagecenter_event_date']."'";   

    }

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


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

    $tables = array('tbl_patientrecurring_detail prd','tbl_appointment ap','tbl_clinic c','tbl_dependant d');
    $jfields = array('p.patientrecurring_detail_id = prd.patientrecurring_detail_id','ap.appointment_id = prd.patientrecurring_detail_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);

    $patientmessageCenterDetails = $paymentnotification_eventObj->getAllRecords($data);
    //print_r($messageCenterDetails);exit;
    $recordsTotal   =   $paymentnotification_eventObj->getAllCount($data);
     
    if ($patientmessageCenterDetails && count($patientmessageCenterDetails)>0){
        
        foreach ($patientmessageCenterDetails as $key => $patientmessagecenterdata)
        {
            
            $issentemail = ($patientmessagecenterdata->issentemail == '1') ? 'Yes' : 'No';

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

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

            $ispatientactive = ($patientmessagecenterdata->ispatientactive == '1') ? 'Active' : 'Inactive';

            $metadata =json_decode($patientmessagecenterdata->appointment_metadata,1);
            $appointmentStatus = $metadata['pms']['appointmentStatus'];
            $appointmentconfirmation = $metadata['appointment']['confirmation']['status'];
            $appointmentFloating =$metadata['appointment']['floating'];
            if($appointmentFloating == true)
            {
                $allapp_metdata = ' (floating)';
            }else if($appointmentconfirmation != ''){
                $allapp_metdata = "(".$appointmentconfirmation.")";
            }else{
                $allapp_metdata = '';
            }

            $response[] = array( 
                    "<b>Xcare Id</b>: ".$patientmessagecenterdata->paymentnotification_event_id,
                    "<b>Xcare Apt Id: </b>".$patientmessagecenterdata->appointment_id.'<br/><b>Xcare Apt External Id: </b>'.$patientmessagecenterdata->appointment_unique_id.'<br/><b>Date: </b>'.$patientmessagecenterdata->appointment_date.'<br/><b>Time: </b>'.$patientmessagecenterdata->appointment_time.'<br/><b>Xcare Status: </b>'.$patientmessagecenterdata->appointment_status.$allapp_metdata.'<br/><b>PMS Status: </b>'.$appointmentStatus,
                    "<b>Id</b>: ".$patientmessagecenterdata->clinic_id."<br/><b>Name: </b>".$patientmessagecenterdata->clinic_name."<br/><b>Phone</b>: ".$patientmessagecenterdata->clinic_phone."<br/><b>Email</b>: ".$patientmessagecenterdata->clinic_email,
                    "<b>Id</b>: ".$patientmessagecenterdata->dependant_id."<br/><b>External Id</b>: ".$patientmessagecenterdata->dependant_unique_id."<br/><b>First Name</b>: ".w::d($patientmessagecenterdata->dependant_fname)."<br/><b>Last Name</b>: ".w::d($patientmessagecenterdata->dependant_lname)."<br/><b>Email Id</b>: ".w::d($patientmessagecenterdata->dependant_email_address)."<br/><b>Phone</b>: ".w::d($patientmessagecenterdata->dependant_phone)."<br><b>Preference</b>: ".$patientmessagecenterdata->dependant_contact_method."<br/><b>Status</b>:".$ispatientactive,
                    "<b>Event</b>: ".$patientmessagecenterdata->eventname."<br/><b>Event timestamp: </b><br/>".$patientmessagecenterdata->created_at,
                    "<b>Sent Email</b>: ".$issentemail."<br/><b>Sent SMS: </b>".$issentsms."<br/><b>Sent App Notification: </b>".$issentappnotification,
            );
        }
        $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');
?>