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

$payClinicPointObj = new payClinicPoint();

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

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

    $where   = " 1=1 ";

    // Global search (by clinic, card, plan, type, description)
    if (isset($_REQUEST['search']['value']) && $_REQUEST['search']['value'] != "") {
        $search = w::e($_REQUEST['search']['value']);
        $where  = " c.clinic_name LIKE '%".$search."%'
                    OR pc.cardName LIKE '%".$search."%'
                    OR sp.plan_name LIKE '%".$search."%'
                    OR p.type LIKE '%".$search."%'
                    OR p.description LIKE '%".$search."%' ";
    }

    $orderFields = array(
        'p.id'
    );

    $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 ");
    }

    
    $tables  = array(
        'tbl_clinic c',
        'tbl_payClinicCard pc',
        'tbl_subscription_plans sp'
    );
    $jfields = array(
        'p.payClinicId = c.clinic_id',
        'p.cardId = pc.id',
        'p.planId = sp.plan_id'
    );
    $jtypes  = array('LEFT','LEFT','LEFT');

    
    $fields = array(
        'p.id as pointId',
        'c.clinic_name',
        'pc.name',
        'sp.plan_name',
        'p.amount',
        'p.requests',
        'p.type',
        'p.description',
        'p.receiptUrl',
        'p.createdAt'
    );

    $data    = array(
        'fields'     => $fields,
        'where'      => $where,
        'joinTables' => $tables,
        'joinFields' => $jfields,
        'joinTypes'  => $jtypes,
        'orderBy'    => $orderby,
        'limit'      => $limit
    );
    //$data['debug'] = true;
    $details      = $payClinicPointObj->getAllRecords($data);
    $recordsTotal = $payClinicPointObj->getAllCount($data);

    if ($details && count($details) > 0) {
        foreach ($details as $row) {
            $clinicName  = isset($row->clinic_name) ? $row->clinic_name : '';
            $cardName    = isset($row->cardName) ? $row->cardName : '';
            $planName    = isset($row->plan_name) ? $row->plan_name : '';
            $amount      = isset($row->amount) ? $row->amount : '';
            $requests    = isset($row->requests) ? $row->requests : '';
            $type        = isset($row->type) ? $row->type : '';
            $description = isset($row->description) ? $row->description : '';

            // Receipt URL – field name is receiptUrl
            $receiptUrlHtml = '-';
            if (!empty($row->receiptUrl)) {
                $receiptUrlHtml = '<a href="'.htmlspecialchars($row->receiptUrl).'" target="_blank">View Receipt</a>';
            }

            $response[] = array(
                '<input type="checkbox" name="check[]" class="check_class" value="'.$row->pointId.'" />',
                $row->pointId,
                $clinicName,
                $cardName,
                $planName,
                $amount,
                $requests,
                $type,
                $description,
                $receiptUrlHtml,
                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');
?>

