
<?php
/* ============================================================
   INTERNATIONAL ADVENTURES OVERSEAS SERVICES
   COMPLETE PREMIUM ADMIN ENQUIRY MANAGEMENT

   Database: bhawani_db
   Main Table: overseas_applications
   Child Tables:
      application_experience
      application_qualifications

   File: admin/enquiry.php
============================================================ */

// SESSION MUST START BEFORE OUTPUT
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

// ADMIN LOGIN SECURITY
if (
    empty($_SESSION['ADMIN_ID']) &&
    empty($_SESSION['admin_id'])
) {
    header("Location: login.php");
    exit;
}

// DATABASE CONNECTION
require_once __DIR__ . '/includes/db.php';

// SUPPORT BOTH CONNECTION VARIABLES
if (
    (!isset($con) || !($con instanceof mysqli)) &&
    isset($conn) && $conn instanceof mysqli
) {
    $con = $conn;
}

if (!isset($con) || !($con instanceof mysqli)) {
    die("Database connection failed. Please check admin/includes/db.php");
}

$con->set_charset("utf8mb4");

// ------------------------------------------------------------
// HELPER FUNCTIONS
// ------------------------------------------------------------

function iae($value) {
    return htmlspecialchars(
        (string)($value ?? ''),
        ENT_QUOTES,
        'UTF-8'
    );
}

function iae_label($key) {
    $key = preg_replace('/([a-z])([A-Z])/', '$1 $2', (string)$key);
    $key = str_replace(['_', '-'], ' ', $key);
    return ucwords($key);
}

function iae_value($value) {
    if ($value === null || trim((string)$value) === '') {
        return '<span class="iae-no-value">Not Provided</span>';
    }

    return nl2br(iae($value));
}

function iae_find_value($row, $keys) {
    foreach ($keys as $key) {
        if (
            isset($row[$key]) &&
            trim((string)$row[$key]) !== ''
        ) {
            return $row[$key];
        }
    }

    return '';
}

function iae_columns($db, $table) {
    $allowed = [
        'overseas_applications',
        'application_experience',
        'application_qualifications'
    ];

    if (!in_array($table, $allowed, true)) {
        return [];
    }

    $columns = [];
    $result = $db->query("SHOW COLUMNS FROM `$table`");

    if ($result) {
        while ($row = $result->fetch_assoc()) {
            $columns[] = $row['Field'];
        }
        $result->free();
    }

    return $columns;
}

function iae_get_application($db, $id) {
    $stmt = $db->prepare(
        "SELECT * FROM overseas_applications WHERE id = ? LIMIT 1"
    );

    if (!$stmt) {
        return null;
    }

    $stmt->bind_param("i", $id);
    $stmt->execute();

    $result = $stmt->get_result();
    $data = $result ? $result->fetch_assoc() : null;

    $stmt->close();

    return $data;
}

function iae_get_child_records($db, $table, $applicationId) {
    $allowed = [
        'application_experience',
        'application_qualifications'
    ];

    if (!in_array($table, $allowed, true)) {
        return [];
    }

    $columns = iae_columns($db, $table);

    if (!in_array('application_id', $columns, true)) {
        return [];
    }

    $records = [];

    $stmt = $db->prepare(
        "SELECT * FROM `$table` WHERE application_id = ? ORDER BY id ASC"
    );

    if (!$stmt) {
        return [];
    }

    $stmt->bind_param("i", $applicationId);
    $stmt->execute();

    $result = $stmt->get_result();

    if ($result) {
        while ($row = $result->fetch_assoc()) {
            $records[] = $row;
        }
    }

    $stmt->close();

    return $records;
}

function iae_is_image_field($field, $value) {
    $field = strtolower((string)$field);
    $value = strtolower((string)$value);

    $imageField =
        strpos($field, 'image') !== false ||
        strpos($field, 'photo') !== false ||
        strpos($field, 'signature') !== false ||
        strpos($field, 'thumb') !== false ||
        strpos($field, 'picture') !== false ||
        strpos($field, 'document') !== false ||
        strpos($field, 'passport') !== false ||
        strpos($field, 'license') !== false ||
        strpos($field, 'licence') !== false;

    $extension = preg_match(
        '/\.(jpg|jpeg|png|gif|webp|bmp)$/i',
        $value
    );

    return $imageField && $extension;
}

function iae_image_url($value) {
    $value = trim((string)$value);

    if ($value === '') {
        return '';
    }

    if (preg_match('~^https?://~i', $value)) {
        return $value;
    }

    $value = str_replace('\\', '/', $value);

    if (in_array('..', explode('/', $value), true)) {
        return '';
    }

    if (strpos($value, '/') === 0) {
        return $value;
    }

    if (strpos($value, 'uploads/') === 0) {
        return '../' . $value;
    }

    return '../uploads/applications/' . basename($value);
}

function iae_customer_name($row) {
    $first = iae_find_value(
        $row,
        ['given_name', 'first_name', 'name']
    );

    $last = iae_find_value(
        $row,
        ['surname', 'last_name']
    );

    $name = trim($first . ' ' . $last);

    return $name !== '' ? $name : 'Customer Application';
}

// ------------------------------------------------------------
// PAGE VARIABLES
// ------------------------------------------------------------

$applicationId = isset($_GET['id']) ? (int)$_GET['id'] : 0;
$search = trim($_GET['search'] ?? '');

$application = null;
$experiences = [];
$qualifications = [];
$applications = [];
$errorMessage = '';

// ------------------------------------------------------------
// SINGLE APPLICATION
// ------------------------------------------------------------

if ($applicationId > 0) {
    $application = iae_get_application($con, $applicationId);

    if ($application) {
        $experiences = iae_get_child_records(
            $con,
            'application_experience',
            $applicationId
        );

        $qualifications = iae_get_child_records(
            $con,
            'application_qualifications',
            $applicationId
        );
    } else {
        $errorMessage = "Customer application was not found.";
    }
}

// ------------------------------------------------------------
// ALL APPLICATIONS
// ------------------------------------------------------------

if ($applicationId === 0) {
    $result = $con->query(
        "SELECT * FROM overseas_applications ORDER BY id DESC LIMIT 1000"
    );

    if ($result) {
        while ($row = $result->fetch_assoc()) {
            if ($search !== '') {
                $rowText = strtolower(
                    implode(' ', array_map(
                        function($value) {
                            return is_scalar($value) ? (string)$value : '';
                        },
                        $row
                    ))
                );

                if (strpos($rowText, strtolower($search)) === false) {
                    continue;
                }
            }

            $applications[] = $row;
        }

        $result->free();
    }
}

// ------------------------------------------------------------
// STATISTICS
// ------------------------------------------------------------

$totalApplications = 0;

$countResult = $con->query(
    "SELECT COUNT(*) AS total FROM overseas_applications"
);

if ($countResult) {
    $countRow = $countResult->fetch_assoc();
    $totalApplications = (int)($countRow['total'] ?? 0);
    $countResult->free();
}

$customerName = $application
    ? iae_customer_name($application)
    : '';

?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Enquiries | International Adventures Overseas Services</title>

<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>

<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800;900&display=swap" rel="stylesheet">

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">

<style>
:root {
    --iae-navy: #081d3b;
    --iae-blue: #1255a5;
    --iae-blue2: #2476dc;
    --iae-gold: #e9aa35;
    --iae-bg: #f3f6fb;
    --iae-text: #192c47;
    --iae-muted: #7d8ba0;
    --iae-border: #e6ebf2;
    --iae-shadow: 0 12px 35px rgba(12,38,75,.07);
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    background: var(--iae-bg);
    color: var(--iae-text);
    font-family: Inter, Arial, sans-serif;
}

/* =====================================================
   ADMIN CONTENT POSITION
   SIDEBAR WIDTH = 260PX
===================================================== */

.iae-page {
    width: calc(100% - 280px);
    max-width: 1600px;
    margin-left: 260px;
    margin-right: 20px;
    padding: 25px 0;
    min-height: 100vh;
}

.iae-page,
.iae-page * {
    box-sizing: border-box;
}

/* =====================================================
   COMPANY HEADER
===================================================== */

.iae-brandbar {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 18px;
    padding: 23px 27px;
    margin-bottom: 25px;
    overflow: hidden;
    border-radius: 17px;
    color: #fff;
    background:
        radial-gradient(circle at 95% 10%,rgba(233,170,53,.25),transparent 25%),
        linear-gradient(115deg,#071a35,#103b73 65%,#1559a8);
    box-shadow: 0 15px 35px rgba(8,29,59,.17);
}

.iae-brandbar:after {
    content: "";
    position: absolute;
    width: 230px;
    height: 230px;
    right: -80px;
    bottom: -150px;
    border: 1px solid rgba(255,255,255,.16);
    border-radius: 50%;
    box-shadow: 0 0 0 25px rgba(255,255,255,.035),
                0 0 0 50px rgba(255,255,255,.025);
    pointer-events: none;
}

.iae-brand {
    display: flex;
    align-items: center;
    gap: 15px;
    position: relative;
    z-index: 1;
}

.iae-brand-icon {
    width: 57px;
    height: 57px;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 2px solid var(--iae-gold);
    border-radius: 15px;
    color: #ffd078;
    background: rgba(255,255,255,.09);
    font-size: 25px;
}

.iae-brand h1 {
    margin: 0 0 6px;
    color: #fff;
    font-size: 19px;
    font-weight: 900;
    line-height: 1.4;
}

.iae-brand p {
    margin: 0;
    color: #d5e4f9;
    font-size: 11px;
    font-weight: 700;
    letter-spacing: 2px;
    text-transform: uppercase;
}

.iae-brand-right {
    position: relative;
    z-index: 1;
    color: #fff;
    text-align: right;
    font-size: 12px;
    line-height: 1.9;
}

.iae-brand-right i {
    margin-right: 6px;
    color: #ffd078;
}

/* =====================================================
   PAGE TITLE
===================================================== */

.iae-page-heading {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 15px;
    flex-wrap: wrap;
    margin: 0 0 22px;
}

.iae-page-heading h2 {
    margin: 0 0 7px;
    color: var(--iae-navy);
    font-size: 26px;
    font-weight: 900;
}

.iae-page-heading p {
    margin: 0;
    color: var(--iae-muted);
    font-size: 13px;
}

/* BUTTONS */

.iae-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 9px;
    min-height: 43px;
    padding: 0 17px;
    border: none;
    border-radius: 9px;
    text-decoration: none;
    font-size: 12px;
    font-weight: 800;
    cursor: pointer;
    transition: .25s ease;
}

.iae-btn-blue {
    color: #fff;
    background: linear-gradient(110deg,#10417f,#2476dc);
    box-shadow: 0 6px 16px rgba(24,91,170,.17);
}

.iae-btn-blue:hover {
    color: #fff;
    transform: translateY(-2px);
}

.iae-btn-gold {
    color: #142d4c;
    background: linear-gradient(110deg,#e6a12a,#ffd078);
}

.iae-btn-gold:hover {
    transform: translateY(-2px);
}

.iae-btn-light {
    color: var(--iae-navy);
    background: #fff;
    border: 1px solid var(--iae-border);
}

/* =====================================================
   STATISTICS
===================================================== */

.iae-stats {
    display: grid;
    grid-template-columns: repeat(3,minmax(0,1fr));
    gap: 17px;
    margin-bottom: 23px;
}

.iae-stat {
    display: flex;
    align-items: center;
    gap: 15px;
    padding: 20px;
    border: 1px solid var(--iae-border);
    border-radius: 13px;
    background: #fff;
    box-shadow: var(--iae-shadow);
}

.iae-stat-icon {
    width: 49px;
    height: 49px;
    flex: 0 0 49px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 13px;
    color: #fff;
    background: linear-gradient(135deg,#123c76,#2779dc);
    font-size: 19px;
}

.iae-stat:nth-child(2) .iae-stat-icon {
    color: #68430b;
    background: linear-gradient(135deg,#e7a32c,#ffda87);
}

.iae-stat:nth-child(3) .iae-stat-icon {
    background: linear-gradient(135deg,#087b70,#35c0a5);
}

.iae-stat span {
    display: block;
    margin-bottom: 5px;
    color: var(--iae-muted);
    font-size: 11px;
    font-weight: 700;
}

.iae-stat strong {
    display: block;
    color: var(--iae-navy);
    font-size: 23px;
    font-weight: 900;
}

/* =====================================================
   PANELS
===================================================== */

.iae-card {
    padding: 23px;
    margin-bottom: 22px;
    border: 1px solid var(--iae-border);
    border-radius: 14px;
    background: #fff;
    box-shadow: var(--iae-shadow);
}

.iae-card-heading {
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 12px;
    margin-bottom: 18px;
}

.iae-card-heading h3 {
    margin: 0;
    color: var(--iae-navy);
    font-size: 16px;
    font-weight: 900;
}

/* SEARCH */

.iae-search {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 10px;
}

.iae-search-input {
    position: relative;
    flex: 1;
    min-width: 220px;
}

.iae-search-input i {
    position: absolute;
    top: 50%;
    left: 15px;
    color: #8494aa;
    transform: translateY(-50%);
}

.iae-search input {
    width: 100%;
    height: 47px;
    padding: 0 15px 0 43px;
    border: 1px solid #dce4ef;
    border-radius: 9px;
    outline: none;
    background: #fbfcfe;
    color: var(--iae-text);
    font-size: 13px;
}

.iae-search input:focus {
    border-color: #2476dc;
    box-shadow: 0 0 0 3px rgba(36,118,220,.09);
}

/* =====================================================
   APPLICATION TABLE
===================================================== */

.iae-table-wrap {
    width: 100%;
    overflow-x: auto;
}

.iae-table {
    width: 100%;
    min-width: 1050px;
    border-collapse: collapse;
}

.iae-table thead th {
    padding: 15px 13px;
    color: #fff;
    background: var(--iae-navy);
    text-align: left;
    font-size: 11px;
    font-weight: 800;
    white-space: nowrap;
}

.iae-table thead th:first-child {
    border-radius: 8px 0 0 0;
}

.iae-table thead th:last-child {
    border-radius: 0 8px 0 0;
}

.iae-table tbody td {
    padding: 15px 13px;
    border-bottom: 1px solid #edf1f6;
    color: #42536a;
    font-size: 12px;
    vertical-align: middle;
}

.iae-table tbody tr:hover {
    background: #f5f9ff;
}

.iae-id {
    color: #1765c8 !important;
    font-weight: 900;
}

.iae-customer {
    color: #142e51 !important;
    font-weight: 800;
}

.iae-email {
    max-width: 190px;
    overflow-wrap: anywhere;
}

.iae-status {
    display: inline-flex;
    padding: 6px 10px;
    border: 1px solid #d9e9ff;
    border-radius: 30px;
    color: #1458a8;
    background: #edf5ff;
    font-size: 10px;
    font-weight: 800;
}

.iae-view {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 7px;
    padding: 9px 13px;
    border-radius: 7px;
    color: #fff;
    background: #1765c8;
    text-decoration: none;
    font-size: 11px;
    font-weight: 800;
}

.iae-view:hover {
    color: #fff;
    background: #0b3f83;
}

/* EMPTY */

.iae-empty {
    padding: 40px 15px;
    color: #8997aa;
    text-align: center;
    font-size: 13px;
}

.iae-empty i {
    display: block;
    margin-bottom: 12px;
    color: #2476dc;
    font-size: 38px;
}

.iae-no-value {
    color: #9ba8b8;
    font-size: 11px;
    font-weight: 500;
}

/* =====================================================
   CUSTOMER DETAIL BANNER
===================================================== */

.iae-detail-banner {
    padding: 24px;
    margin-bottom: 22px;
    overflow: hidden;
    border-radius: 14px;
    color: #fff;
    background:
        radial-gradient(circle at 90% 0%,rgba(255,201,105,.24),transparent 27%),
        linear-gradient(115deg,#071a35,#10417f 65%,#2476dc);
}

.iae-detail-banner h2 {
    margin: 0 0 9px;
    color: #fff;
    font-size: 23px;
    font-weight: 900;
}

.iae-detail-banner p {
    margin: 0;
    color: #dceaff;
    font-size: 12px;
    line-height: 1.9;
}

.iae-gold-line {
    width: 65px;
    height: 4px;
    margin-top: 15px;
    border-radius: 10px;
    background: var(--iae-gold);
}

/* =====================================================
   DETAIL SECTIONS
===================================================== */

.iae-section {
    margin-bottom: 20px;
    overflow: hidden;
    border: 1px solid var(--iae-border);
    border-radius: 13px;
    background: #fff;
    box-shadow: 0 6px 22px rgba(12,38,75,.045);
    break-inside: avoid;
}

.iae-section-title {
    display: flex;
    align-items: center;
    gap: 11px;
    padding: 15px 19px;
    color: #fff;
    background: linear-gradient(105deg,#0a2346,#174f92);
}

.iae-section-title i {
    width: 31px;
    height: 31px;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 1px solid rgba(255,255,255,.35);
    border-radius: 9px;
    color: #ffd078;
    background: rgba(255,255,255,.09);
}

.iae-section-title h3 {
    margin: 0;
    color: #fff;
    font-size: 13px;
    font-weight: 900;
    letter-spacing: .4px;
}

.iae-detail-grid {
    display: grid;
    grid-template-columns: repeat(3,minmax(0,1fr));
}

.iae-detail-item {
    min-width: 0;
    padding: 15px 17px;
    border-right: 1px solid #edf1f6;
    border-bottom: 1px solid #edf1f6;
    overflow-wrap: anywhere;
}

.iae-detail-item span {
    display: block;
    margin-bottom: 7px;
    color: #8492a6;
    font-size: 10px;
    font-weight: 800;
    letter-spacing: .5px;
    text-transform: uppercase;
}

.iae-detail-item strong {
    color: #203a5c;
    font-size: 12px;
    font-weight: 700;
    line-height: 1.7;
}

/* CHILD TABLES */

.iae-subtable {
    width: 100%;
    border-collapse: collapse;
}

.iae-subtable th {
    padding: 13px;
    color: #fff;
    background: #174b86;
    text-align: left;
    font-size: 11px;
    white-space: nowrap;
}

.iae-subtable td {
    padding: 13px;
    border-bottom: 1px solid #edf1f6;
    color: #45576f;
    font-size: 12px;
    overflow-wrap: anywhere;
}

.iae-subtable tr:nth-child(even) {
    background: #f8faff;
}

/* DOCUMENTS */

.iae-doc-grid {
    display: grid;
    grid-template-columns: repeat(3,minmax(0,1fr));
    gap: 16px;
    padding: 20px;
}

.iae-doc {
    padding: 18px;
    border: 1px solid #e5ebf3;
    border-radius: 12px;
    background: #fbfcff;
    text-align: center;
    break-inside: avoid;
}

.iae-doc h4 {
    margin: 0 0 14px;
    color: #16365f;
    font-size: 12px;
    font-weight: 900;
}

.iae-doc img {
    max-width: 100%;
    max-height: 150px;
    object-fit: contain;
}

.iae-doc a {
    display: inline-block;
    margin-top: 12px;
    color: #1765c8;
    font-size: 11px;
    font-weight: 800;
    text-decoration: none;
}

/* FOOTER */

.iae-footer {
    padding: 20px;
    color: #8190a5;
    text-align: center;
    font-size: 11px;
}

.iae-footer strong {
    color: #174b86;
}

/* =====================================================
   TABLET AND MOBILE
===================================================== */

@media(max-width:991px) {
    .iae-page {
        width: calc(100% - 30px);
        margin-left: 15px;
        margin-right: 15px;
        margin-top: 75px;
        padding: 15px 0;
    }

    .iae-stats {
        grid-template-columns: repeat(2,minmax(0,1fr));
    }

    .iae-detail-grid {
        grid-template-columns: repeat(2,minmax(0,1fr));
    }

    .iae-doc-grid {
        grid-template-columns: repeat(2,minmax(0,1fr));
    }
}

@media(max-width:576px) {
    .iae-page {
        width: calc(100% - 20px);
        margin-left: 10px;
        margin-right: 10px;
        margin-top: 70px;
        padding: 10px 0;
    }

    .iae-brandbar {
        padding: 17px;
    }

    .iae-brand-icon {
        width: 46px;
        height: 46px;
        font-size: 20px;
    }

    .iae-brand h1 {
        font-size: 14px;
    }

    .iae-brand p {
        font-size: 9px;
        letter-spacing: 1px;
    }

    .iae-brand-right {
        width: 100%;
        text-align: left;
    }

    .iae-page-heading h2 {
        font-size: 21px;
    }

    .iae-stats {
        grid-template-columns: 1fr;
        gap: 11px;
    }

    .iae-card {
        padding: 14px;
    }

    .iae-detail-grid,
    .iae-doc-grid {
        grid-template-columns: 1fr;
    }

    .iae-detail-banner h2 {
        font-size: 19px;
    }

    .iae-search-input {
        min-width: 100%;
    }

    .iae-search .iae-btn {
        flex: 1;
    }
}

/* =====================================================
   PDF / PRINT
===================================================== */

@media print {
    @page {
        size: A4;
        margin: 10mm;
    }

    body {
        background: #fff !important;
        color: #111 !important;
        font-family: Arial,sans-serif;
    }

    .iae-page {
        width: 100% !important;
        max-width: 100% !important;
        margin: 0 !important;
        padding: 0 !important;
    }

    .no-print,
    .iae-footer {
        display: none !important;
    }

    .iae-brandbar {
        padding: 15px;
        margin-bottom: 15px;
        box-shadow: none;
        print-color-adjust: exact;
        -webkit-print-color-adjust: exact;
    }

    .iae-detail-banner,
    .iae-section-title,
    .iae-subtable th {
        print-color-adjust: exact;
        -webkit-print-color-adjust: exact;
    }

    .iae-detail-banner {
        padding: 15px;
    }

    .iae-detail-grid {
        grid-template-columns: repeat(2,minmax(0,1fr));
    }

    .iae-detail-item {
        padding: 9px 10px;
    }

    .iae-section {
        break-inside: avoid;
        page-break-inside: avoid;
        box-shadow: none;
    }

    .iae-doc-grid {
        grid-template-columns: repeat(3,minmax(0,1fr));
    }

    .iae-doc img {
        max-height: 100px;
    }

    .iae-doc a {
        display: none;
    }

    .iae-table-wrap {
        overflow: visible;
    }
}
</style>
</head>

<body>

<?php
/*
  IMPORTANT:
  top.php and sidebar.php must be HTML layout partials.
  They should not contain another DOCTYPE, html, head or body tag.
*/
require_once __DIR__ . '/top.php';
require_once __DIR__ . '/sidebar.php';
?>

<main class="iae-page">

    <!-- COMPANY BRANDING -->

    <header class="iae-brandbar">

        <div class="iae-brand">
            <div class="iae-brand-icon">
                <i class="fas fa-globe-asia"></i>
            </div>

            <div>
                <h1>INTERNATIONAL ADVENTURES</h1>
                <p>Overseas Services</p>
            </div>
        </div>

        <div class="iae-brand-right">
            <div><i class="fas fa-shield-alt"></i> Secure Admin Management</div>
            <div><i class="fas fa-file-invoice"></i> Customer Application Portal</div>
        </div>

    </header>

<?php if ($applicationId > 0): ?>

    <!-- =================================================
         CUSTOMER DETAIL PAGE
    ================================================== -->

    <div class="iae-page-heading no-print">

        <div>
            <h2><i class="fas fa-address-card"></i> Application Details</h2>
            <p>Complete customer application record</p>
        </div>

        <div style="display:flex;gap:10px;flex-wrap:wrap;">

            <a href="enquiry.php" class="iae-btn iae-btn-light">
                <i class="fas fa-arrow-left"></i> Back to Enquiries
            </a>

            <button
                type="button"
                class="iae-btn iae-btn-gold"
                onclick="window.print()">
                <i class="fas fa-file-pdf"></i> Download / Save PDF
            </button>

        </div>
    </div>

    <?php if ($application): ?>

        <div class="iae-detail-banner">

            <h2><?php echo iae($customerName); ?></h2>

            <p>
                Application ID:
                <strong><?php echo (int)$applicationId; ?></strong>

                &nbsp; | &nbsp;

                Application No:
                <strong>
                    <?php
                    echo iae(
                        iae_find_value(
                            $application,
                            ['application_no','application_number']
                        ) ?: 'Not Available'
                    );
                    ?>
                </strong>

                <br>
                Company: International Adventures Overseas Services
            </p>

            <div class="iae-gold-line"></div>

        </div>

        <!-- PERSONAL AND MAIN DETAILS -->

        <section class="iae-section">

            <div class="iae-section-title">
                <i class="fas fa-user"></i>
                <h3>COMPLETE CUSTOMER APPLICATION DETAILS</h3>
            </div>

            <div class="iae-detail-grid">

                <?php foreach ($application as $key => $value): ?>

                    <?php
                    if (
                        strtolower($key) === 'id' ||
                        strtolower($key) === 'application_id'
                    ) {
                        continue;
                    }

                    $isImage = iae_is_image_field($key, $value);
                    ?>

                    <div class="iae-detail-item">

                        <span><?php echo iae(iae_label($key)); ?></span>

                        <strong>

                            <?php if ($isImage): ?>

                                <?php $imageUrl = iae_image_url($value); ?>

                                <?php if ($imageUrl !== ''): ?>
                                    <a href="<?php echo iae($imageUrl); ?>" target="_blank">
                                        View Uploaded Document
                                    </a>
                                <?php else: ?>
                                    <span class="iae-no-value">Not Available</span>
                                <?php endif; ?>

                            <?php else: ?>

                                <?php echo iae_value($value); ?>

                            <?php endif; ?>

                        </strong>

                    </div>

                <?php endforeach; ?>

            </div>

        </section>

        <!-- EXPERIENCE DETAILS -->

        <section class="iae-section">

            <div class="iae-section-title">
                <i class="fas fa-briefcase"></i>
                <h3>EXPERIENCE DETAILS</h3>
            </div>

            <div class="iae-table-wrap">

                <?php if (!empty($experiences)): ?>

                    <table class="iae-subtable">

                        <thead>
                            <tr>
                                <?php foreach (array_keys($experiences[0]) as $column): ?>
                                    <?php if ($column !== 'id' && $column !== 'application_id'): ?>
                                        <th><?php echo iae(iae_label($column)); ?></th>
                                    <?php endif; ?>
                                <?php endforeach; ?>
                            </tr>
                        </thead>

                        <tbody>

                            <?php foreach ($experiences as $experience): ?>
                                <tr>
                                    <?php foreach ($experience as $key => $value): ?>
                                        <?php if ($key === 'id' || $key === 'application_id') continue; ?>
                                        <td><?php echo iae_value($value); ?></td>
                                    <?php endforeach; ?>
                                </tr>
                            <?php endforeach; ?>

                        </tbody>

                    </table>

                <?php else: ?>

                    <div class="iae-empty">
                        <i class="fas fa-briefcase"></i>
                        No experience records found.
                    </div>

                <?php endif; ?>

            </div>

        </section>

        <!-- QUALIFICATION DETAILS -->

        <section class="iae-section">

            <div class="iae-section-title">
                <i class="fas fa-graduation-cap"></i>
                <h3>QUALIFICATION DETAILS</h3>
            </div>

            <div class="iae-table-wrap">

                <?php if (!empty($qualifications)): ?>

                    <table class="iae-subtable">

                        <thead>
                            <tr>
                                <?php foreach (array_keys($qualifications[0]) as $column): ?>
                                    <?php if ($column !== 'id' && $column !== 'application_id'): ?>
                                        <th><?php echo iae(iae_label($column)); ?></th>
                                    <?php endif; ?>
                                <?php endforeach; ?>
                            </tr>
                        </thead>

                        <tbody>

                            <?php foreach ($qualifications as $qualification): ?>
                                <tr>
                                    <?php foreach ($qualification as $key => $value): ?>
                                        <?php if ($key === 'id' || $key === 'application_id') continue; ?>
                                        <td><?php echo iae_value($value); ?></td>
                                    <?php endforeach; ?>
                                </tr>
                            <?php endforeach; ?>

                        </tbody>

                    </table>

                <?php else: ?>

                    <div class="iae-empty">
                        <i class="fas fa-graduation-cap"></i>
                        No qualification records found.
                    </div>

                <?php endif; ?>

            </div>

        </section>

        <!-- UPLOADED DOCUMENTS -->

        <section class="iae-section">

            <div class="iae-section-title">
                <i class="fas fa-file-image"></i>
                <h3>UPLOADED DOCUMENTS / PHOTO / SIGNATURE / THUMB</h3>
            </div>

            <div class="iae-doc-grid">

                <?php
                $documentFound = false;

                foreach ($application as $key => $value):

                    if (!iae_is_image_field($key, $value)) {
                        continue;
                    }

                    $imageUrl = iae_image_url($value);

                    if ($imageUrl === '') {
                        continue;
                    }

                    $documentFound = true;
                ?>

                    <div class="iae-doc">

                        <h4><?php echo iae(iae_label($key)); ?></h4>

                        <a href="<?php echo iae($imageUrl); ?>" target="_blank">
                            <img
                                src="<?php echo iae($imageUrl); ?>"
                                alt="<?php echo iae_label($key); ?>">
                        </a>

                        <br>

                        <a href="<?php echo iae($imageUrl); ?>" target="_blank">
                            View Original
                        </a>

                    </div>

                <?php endforeach; ?>

                <?php if (!$documentFound): ?>

                    <div class="iae-empty">
                        <i class="fas fa-folder-open"></i>
                        No uploaded image documents found.
                    </div>

                <?php endif; ?>

            </div>

        </section>

        <div class="iae-footer">
            <strong>International Adventures Overseas Services</strong>
            <br>
            Customer Application Record | Confidential
        </div>

    <?php else: ?>

        <div class="iae-card">
            <div class="iae-empty">
                <i class="fas fa-exclamation-circle"></i>
                <?php echo iae($errorMessage); ?>
            </div>
        </div>

    <?php endif; ?>

<?php else: ?>

    <!-- =================================================
         ENQUIRY LIST PAGE
    ================================================== -->

    <div class="iae-page-heading">

        <div>
            <h2><i class="fas fa-users"></i> Customer Enquiries</h2>
            <p>Manage all overseas applications and customer records.</p>
        </div>

        <a href="index.php" class="iae-btn iae-btn-blue">
            <i class="fas fa-gauge-high"></i> Admin Dashboard
        </a>

    </div>

    <!-- STATISTICS -->

    <div class="iae-stats">

        <div class="iae-stat">
            <div class="iae-stat-icon">
                <i class="fas fa-users"></i>
            </div>
            <div>
                <span>Total Applications</span>
                <strong><?php echo $totalApplications; ?></strong>
            </div>
        </div>

        <div class="iae-stat">
            <div class="iae-stat-icon">
                <i class="fas fa-search"></i>
            </div>
            <div>
                <span>Matching Records</span>
                <strong><?php echo count($applications); ?></strong>
            </div>
        </div>

        <div class="iae-stat">
            <div class="iae-stat-icon">
                <i class="fas fa-globe"></i>
            </div>
            <div>
                <span>Company</span>
                <strong style="font-size:14px;">IA Overseas</strong>
            </div>
        </div>

    </div>

    <!-- SEARCH -->

    <div class="iae-card no-print">

        <div class="iae-card-heading">
            <h3>
                <i class="fas fa-filter" style="color:#d99a25;"></i>
                Search Customer Applications
            </h3>
        </div>

        <form method="GET" class="iae-search">

            <div class="iae-search-input">
                <i class="fas fa-search"></i>

                <input
                    type="text"
                    name="search"
                    value="<?php echo iae($search); ?>"
                    placeholder="Search name, mobile, passport, email or application number...">
            </div>

            <button type="submit" class="iae-btn iae-btn-blue">
                <i class="fas fa-search"></i> Search
            </button>

            <a href="enquiry.php" class="iae-btn iae-btn-light">
                <i class="fas fa-rotate-left"></i> Reset
            </a>

        </form>

    </div>

    <!-- APPLICATION LIST -->

    <div class="iae-card">

        <div class="iae-card-heading">

            <h3>
                <i class="fas fa-file-lines" style="color:#d99a25;"></i>
                Application Records
            </h3>

            <span class="iae-status">
                <?php echo count($applications); ?> Records
            </span>

        </div>

        <div class="iae-table-wrap">

            <table class="iae-table">

                <thead>
                    <tr>
                        <th>S.NO.</th>
                        <th>APPLICATION ID</th>
                        <th>CUSTOMER NAME</th>
                        <th>POST APPLIED</th>
                        <th>MOBILE NUMBER</th>
                        <th>EMAIL</th>
                        <th>PASSPORT NUMBER</th>
                        <th>DATE</th>
                        <th>DETAILS</th>
                    </tr>
                </thead>

                <tbody>

                <?php if (!empty($applications)): ?>

                    <?php foreach ($applications as $index => $row): ?>

                        <?php
                        $name = iae_customer_name($row);

                        $applicationNumber = iae_find_value(
                            $row,
                            ['application_no','application_number']
                        );

                        $mobile = iae_find_value(
                            $row,
                            ['mobile_number','mobile','phone','contact_number']
                        );

                        $email = iae_find_value(
                            $row,
                            ['email','email_address']
                        );

                        $passport = iae_find_value(
                            $row,
                            ['passport_number','passport_no']
                        );

                        $post = iae_find_value(
                            $row,
                            ['post_applied_for','post','position']
                        );

                        $date = iae_find_value(
                            $row,
                            ['created_at','application_date','date']
                        );
                        ?>

                        <tr>

                            <td><?php echo $index + 1; ?></td>

                            <td class="iae-id">
                                <?php
                                echo iae(
                                    $applicationNumber !== ''
                                        ? $applicationNumber
                                        : ('#' . ($row['id'] ?? ''))
                                );
                                ?>
                            </td>

                            <td class="iae-customer">
                                <?php echo iae($name); ?>
                            </td>

                            <td><?php echo iae($post ?: '—'); ?></td>

                            <td><?php echo iae($mobile ?: '—'); ?></td>

                            <td class="iae-email">
                                <?php echo iae($email ?: '—'); ?>
                            </td>

                            <td><?php echo iae($passport ?: '—'); ?></td>

                            <td><?php echo iae($date ?: '—'); ?></td>

                            <td>
                                <a
                                    href="enquiry.php?id=<?php echo (int)$row['id']; ?>"
                                    class="iae-view">
                                    <i class="fas fa-eye"></i> View
                                </a>
                            </td>

                        </tr>

                    <?php endforeach; ?>

                <?php else: ?>

                    <tr>
                        <td colspan="9">
                            <div class="iae-empty">
                                <i class="fas fa-folder-open"></i>
                                <strong>No Customer Enquiries Found</strong>
                                <br><br>
                                Customer applications submitted through the website will appear here.
                            </div>
                        </td>
                    </tr>

                <?php endif; ?>

                </tbody>

            </table>

        </div>

    </div>

<?php endif; ?>

</main>

</body>
</html>
