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


$payClinicCardObj = new payClinicCard();
$ClinicObj  = new clinic();

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  = " clinicId LIKE '%".$search."%' 
                    OR cardName LIKE '%".$search."%' 
                    OR brand LIKE '%".$search."%' 
                    OR last4 LIKE '%".$search."%' 
                    OR stripeCardId LIKE '%".$search."%' ";
    }

    $orderFields = array('', 'clinicId', 'cardName', 'brand', 'last4', 'expMonth', 'expYear', 'stripeCardId', '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      = $payClinicCardObj->getAllRecords($data);
    $recordsTotal = $payClinicCardObj->getAllCount($data);

    $response = array();

    if ($details && count($details) > 0) {
        foreach ($details as $row) {
            $clinicName = '';
            if (!empty($row->payClinicId)) {
                $clinic = $ClinicObj->fromId($row->payClinicId);
                if ($clinic) {
                    $clinicName = $clinic->clinic_name;
                }
            }

            // Map numeric status to label
            $statusLabel = '';
            if (isset($row->status)) {
                if ((string)$row->status === '1') {
                    $statusLabel = 'Active';
                } elseif ((string)$row->status === '0') {
                    $statusLabel = 'Inactive';
                } else {
                    $statusLabel = $row->status;
                }
            }

            $response[] = array(
                '<input type="checkbox" name="check[]" class="check_class" value="'.$row->id.'" />',
                $row->id,
                $clinicName,
                isset($row->cardName) ? $row->cardName : '',
                isset($row->brand) ? $row->brand : '',
                isset($row->last4) ? $row->last4 : '',
                isset($row->expMonth) ? $row->expMonth : '',
                isset($row->expYear) ? $row->expYear : '',
                $statusLabel,
                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');
?>

