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

$payClinicLeadObj = new payClinicLead();
$dependantObj     = new dependant();

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

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

    $where   = " 1=1 ";

    // Global search on clinic/patient/domain names
    if (isset($_REQUEST['search']['value']) && $_REQUEST['search']['value'] != "") {
        $search = w::e($_REQUEST['search']['value']);
        $where  = " c.clinic_name LIKE '%".$search."%' 
                    OR dep.dependant_fname LIKE '%".$search."%' 
                    OR dep.dependant_lname LIKE '%".$search."%' 
                    OR d.domain_title LIKE '%".$search."%' 
                    OR p.status LIKE '%".$search."%' ";
    }

    // Order fields based on joined columns
    $orderFields = array('', 'c.clinic_name', 'dep.dependant_fname', 'd.domain_title', 'p.status', 'p.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(" p.id desc ");
    }

    // Join tbl_conciergeLead, tbl_clinic, tbl_dependant, tbl_domain
    $tables  = array(
        'tbl_conciergeLead cl',
        'tbl_clinic c',
        'tbl_dependant dep',
        'tbl_domain d'
    );
    $jfields = array(
        'p.conciergeLead = cl.id',
        'p.clinicId = c.clinic_id',
        'cl.dependantId = dep.dependant_id',
        'cl.domain = d.domain_id'
    );
    $jtypes  = array('LEFT','LEFT','LEFT','LEFT');

    $data    = array(
        'where'      => $where,
        'joinTables' => $tables,
        'joinFields' => $jfields,
        'joinTypes'  => $jtypes,
        'orderBy'    => $orderby,
        'limit'      => $limit
    );
    
    $details      = $payClinicLeadObj->getAllRecords($data);
    $recordsTotal = $payClinicLeadObj->getAllCount($data);

    if ($details && count($details) > 0) {
        foreach ($details as $row) {
            // Clinic name directly from tbl_clinic
            $clinicName = isset($row->clinic_name) ? $row->clinic_name : '';

            // Patient name (decrypt)
            $patientName = '';
            if (isset($row->dependant_fname) || isset($row->dependant_lname)) {
                $firstName = isset($row->dependant_fname) ? w::d($row->dependant_fname) : '';
                $lastName  = isset($row->dependant_lname) ? w::d($row->dependant_lname) : '';
                $patientName = trim($firstName.' '.$lastName);
            }

            // Domain title from tbl_domain
            $domainName = isset($row->domain_title) ? $row->domain_title : '';

            $response[] = array(
                '<input type="checkbox" name="check[]" class="check_class" value="'.$row->id.'" />',
                $row->id,
                $clinicName,
                $patientName,
                $domainName,
                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');
?>

