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

// Logical section of page (same pattern as payClinicList)
$conciergeLeadObj = new conciergeLead();
$dependantObj     = new dependant();
$domainObj        = new domain();

if (isset($_REQUEST['act']) && $_REQUEST['act'] == 'load') {

    $records = $_REQUEST['length'];
    $limit   = array('start' => $_REQUEST['start'], 'record' => $records);

    $where   = " 1=1 ";

    // Global search
    if (isset($_REQUEST['search']['value']) && $_REQUEST['search']['value'] != "") {
        $search = w::e($_REQUEST['search']['value']);
        $where  = " dependantId LIKE '%".$search."%' 
                    OR appointmentId LIKE '%".$search."%' 
                    OR domain LIKE '%".$search."%' 
                    OR notes LIKE '%".$search."%' ";
    }

    $orderFields = array('', 'dependantId', 'appointmentId', 'domain', 'status', 'createdAt');

    $orderby = array();
    if (isset($_REQUEST['order'][0]['column']) && $_REQUEST['order'][0]['column'] != "") {
        $colIndex = (int)$_REQUEST['order'][0]['column'];
        $dir      = $_REQUEST['order'][0]['dir'] === 'asc' ? 'asc' : 'desc';
        if (isset($orderFields[$colIndex])) {
            $orderby = array(" ".$orderFields[$colIndex]." ".$dir);
        }
    } else {
        $orderby = array(" id desc ");
    }

    $data    = array('where' => $where, 'orderBy' => $orderby, 'limit' => $limit);

    $details      = $conciergeLeadObj->getAllRecords($data);
    $recordsTotal = $conciergeLeadObj->getAllCount($data);

    if ($details && count($details) > 0) {
        foreach ($details as $row) {
            $patientName = '';
            $domainName  = '';

            if (!empty($row->dependantId)) {
                $dep = $dependantObj->fromId($row->dependantId);
                if ($dep) {
                    $firstName = isset($dep->dependant_fname) ? w::d($dep->dependant_fname) : '';
                    $lastName  = isset($dep->dependant_lname) ? w::d($dep->dependant_lname) : '';
                    $patientName = trim($firstName.' '.$lastName);
                }
            }

            if (!empty($row->domain)) {
                $dom = $domainObj->fromId($row->domain);
                if ($dom && isset($dom->domain_title)) {
                    $domainName = $dom->domain_title;
                }
            }

            $response[] = array(
                '<input type="checkbox" name="check[]" class="check_class" value="'.$row->id.'" />',
                $row->id,
                $patientName,
                isset($row->appointmentId) ? $row->appointmentId : '',
                $domainName,
                isset($row->isEmergency) ? $row->isEmergency : '',
                isset($row->preferredDays) ? $row->preferredDays : '',
                isset($row->preferredTime) ? $row->preferredTime : '',
                isset($row->status) ? $row->status : '',
                isset($row->createdAt) ? $row->createdAt : ''
            );
        }
        $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');
?>

