<?php
session_start();

require_once __DIR__ . '/../includes/helpers.php';
require_once __DIR__ . '/../includes/layout.php';

if (!isset($_SESSION['authenticated'])) {
    redirect(page_url('signin'));
}

function travel_value(array $row, string $key, string $default = ''): string
{
    $value = trim((string) ($row[$key] ?? ''));
    return $value !== '' ? $value : $default;
}

function travel_date_display(?string $value): string
{
    $value = trim((string) $value);
    if ($value === '') {
        return '';
    }

    $date = DateTime::createFromFormat('Y-m-d', $value);
    return $date instanceof DateTime ? $date->format('d/m/Y') : $value;
}

function travel_select_option(string $current, string $option): string
{
    return strtolower(trim($current)) === strtolower(trim($option)) ? ' selected' : '';
}

function travel_photo_url(string $path): string
{
    $path = trim($path);
    if ($path === '') {
        return '../images/img1.jpg';
    }

    if (str_starts_with($path, '../') || str_starts_with($path, 'http://') || str_starts_with($path, 'https://')) {
        return $path;
    }

    return '../' . ltrim($path, '/');
}

function travel_store_photo(array $file, string $currentPath = ''): string
{
    if (($file['error'] ?? UPLOAD_ERR_NO_FILE) !== UPLOAD_ERR_OK || empty($file['tmp_name'])) {
        return $currentPath;
    }

    $allowedExt = ['jpg', 'jpeg', 'png', 'gif', 'webp'];
    $originalName = (string) ($file['name'] ?? '');
    $extension = strtolower(pathinfo($originalName, PATHINFO_EXTENSION));
    if (!in_array($extension, $allowedExt, true)) {
        return $currentPath;
    }

    $uploadDir = __DIR__ . '/../images/travel_profiles';
    if (!is_dir($uploadDir)) {
        mkdir($uploadDir, 0777, true);
    }

    $fileName = 'travel_profile_' . $GLOBALS['userId'] . '_' . time() . '.' . $extension;
    $targetPath = $uploadDir . '/' . $fileName;

    if (!move_uploaded_file((string) $file['tmp_name'], $targetPath)) {
        return $currentPath;
    }

    return 'images/travel_profiles/' . $fileName;
}

function travel_ensure_profile_photo_column(): void
{
    $exists = fetch_one(
        'SELECT 1
         FROM information_schema.columns
         WHERE table_schema = DATABASE()
           AND table_name = ?
           AND column_name = ?
         LIMIT 1',
        'ss',
        ['user_travel_profiles', 'profile_photo_path']
    );

    if (!$exists) {
        db()->query('ALTER TABLE user_travel_profiles ADD COLUMN profile_photo_path VARCHAR(255) NOT NULL DEFAULT "" AFTER user_id');
    }
}

$userId = (int) ($_SESSION['user_id'] ?? 0);
$displayName = trim((string) ($_SESSION['user_company'] ?? 'Expense user'));
$settingsSuccess = flash('settings_success');
$settingsError = flash('settings_error');

$organization = null;
if ($userId > 0) {
    $organization = fetch_one(
        'SELECT id, organization_name
         FROM organizations
         WHERE user_id = ?
         ORDER BY id DESC
         LIMIT 1',
        'i',
        [$userId]
    );
}

$organizationId = (int) ($organization['id'] ?? 0);
$editMode = isset($_GET['edit']) || $organizationId === 0;
if ($userId > 0) {
    travel_ensure_profile_photo_column();
}

$userProfile = [
    'display_name' => $displayName,
    'employee_id' => '',
    'mobile_number' => '',
    'department' => '',
    'designation' => '',
    'email' => trim((string) ($_SESSION['user_email'] ?? '')),
];

$dbUserProfile = $userId > 0 ? fetch_one('SELECT * FROM user_profiles WHERE user_id = ? LIMIT 1', 'i', [$userId]) : null;
if ($dbUserProfile) {
    $userProfile['display_name'] = travel_value($dbUserProfile, 'display_name', $userProfile['display_name']);
    $userProfile['employee_id'] = travel_value($dbUserProfile, 'employee_id');
    $userProfile['mobile_number'] = travel_value($dbUserProfile, 'mobile_number');
    $userProfile['department'] = travel_value($dbUserProfile, 'department');
    $userProfile['designation'] = travel_value($dbUserProfile, 'designation');
    $userProfile['email'] = travel_value($dbUserProfile, 'email', $userProfile['email']);
}

$travelProfile = [
    'profile_photo_path' => '',
    'salutation' => '',
    'first_name' => '',
    'middle_name' => '',
    'last_name' => '',
    'passport_name' => $userProfile['display_name'],
    'nationality' => '',
    'date_of_birth' => '',
    'gender' => 'Male',
    'personal_email' => $userProfile['email'],
    'personal_mobile' => '',
    'redress_number' => '',
    'known_traveler_number' => '',
    'fax_number' => '',
    'preferred_departure_airport' => '',
    'home_address_line1' => '',
    'home_address_line2' => '',
    'home_country' => '',
    'home_state' => '',
    'home_city' => '',
    'home_pin_code' => '',
    'home_phone' => '',
    'work_address_line1' => '',
    'work_address_line2' => '',
    'work_country' => '',
    'work_state' => '',
    'work_city' => '',
    'work_pin_code' => '',
    'work_phone' => '',
];

$dbTravelProfile = $userId > 0 ? fetch_one('SELECT * FROM user_travel_profiles WHERE user_id = ? ORDER BY id DESC LIMIT 1', 'i', [$userId]) : null;
if ($dbTravelProfile) {
    foreach (array_keys($travelProfile) as $key) {
        $travelProfile[$key] = travel_value($dbTravelProfile, $key, $travelProfile[$key]);
    }
    $travelProfile['profile_photo_path'] = travel_value($dbTravelProfile, 'profile_photo_path', $travelProfile['profile_photo_path']);
    if ($travelProfile['date_of_birth'] !== '') {
        $travelProfile['date_of_birth'] = travel_date_display($travelProfile['date_of_birth']);
    }
}

$travelProfileExists = $dbTravelProfile !== null;
$pageTitle = 'Travel Profile';
if ($editMode) {
    $pageTitle = $travelProfileExists ? 'Edit Travel Profile' : 'Add Travel Profile';
}

if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['settings_action'] ?? '') === 'save_travel_profile') {
    if ($userId <= 0 || $organizationId <= 0) {
        flash('settings_error', 'Your account is not ready yet.');
        redirect('travel_profile.php?edit=1');
    }

    $fields = [];
    foreach ($travelProfile as $key => $default) {
        $fields[$key] = trim((string) ($_POST[$key] ?? ''));
    }
    $fields['profile_photo_path'] = $travelProfile['profile_photo_path'];
    $fields['profile_photo_path'] = travel_store_photo($_FILES['profile_photo'] ?? [], $fields['profile_photo_path']);

    if ($fields['passport_name'] === '') {
        flash('settings_error', 'Please enter the name as in passport/ID proof.');
        redirect('travel_profile.php?edit=1');
    }

    if ($fields['personal_email'] === '') {
        flash('settings_error', 'Please enter the personal email address.');
        redirect('travel_profile.php?edit=1');
    }

    $existing = fetch_one(
        'SELECT id
         FROM user_travel_profiles
         WHERE user_id = ?
         ORDER BY id DESC
         LIMIT 1',
        'i',
        [$userId]
    );

    $updateTypes = str_repeat('s', 29) . 'i';
    $insertTypes = 'ii' . str_repeat('s', 29);

    $saved = $existing
        ? execute_query(
            'UPDATE user_travel_profiles
             SET profile_photo_path = ?, salutation = ?, first_name = ?, middle_name = ?, last_name = ?, passport_name = ?, nationality = ?, date_of_birth = ?, gender = ?, personal_email = ?, personal_mobile = ?, redress_number = ?, known_traveler_number = ?, fax_number = ?, preferred_departure_airport = ?, home_address_line1 = ?, home_address_line2 = ?, home_country = ?, home_state = ?, home_city = ?, home_pin_code = ?, home_phone = ?, work_address_line1 = ?, work_address_line2 = ?, work_country = ?, work_state = ?, work_city = ?, work_pin_code = ?, work_phone = ?, status = "active"
             WHERE id = ?',
            $updateTypes,
            [
                $fields['profile_photo_path'],
                $fields['salutation'],
                $fields['first_name'],
                $fields['middle_name'],
                $fields['last_name'],
                $fields['passport_name'],
                $fields['nationality'],
                $fields['date_of_birth'] !== '' ? $fields['date_of_birth'] : null,
                $fields['gender'],
                $fields['personal_email'],
                $fields['personal_mobile'],
                $fields['redress_number'],
                $fields['known_traveler_number'],
                $fields['fax_number'],
                $fields['preferred_departure_airport'],
                $fields['home_address_line1'],
                $fields['home_address_line2'],
                $fields['home_country'],
                $fields['home_state'],
                $fields['home_city'],
                $fields['home_pin_code'],
                $fields['home_phone'],
                $fields['work_address_line1'],
                $fields['work_address_line2'],
                $fields['work_country'],
                $fields['work_state'],
                $fields['work_city'],
                $fields['work_pin_code'],
                $fields['work_phone'],
                (int) $existing['id'],
            ]
        )
        : execute_query(
            'INSERT INTO user_travel_profiles
                (organization_id, user_id, profile_photo_path, salutation, first_name, middle_name, last_name, passport_name, nationality, date_of_birth, gender, personal_email, personal_mobile, redress_number, known_traveler_number, fax_number, preferred_departure_airport, home_address_line1, home_address_line2, home_country, home_state, home_city, home_pin_code, home_phone, work_address_line1, work_address_line2, work_country, work_state, work_city, work_pin_code, work_phone, status)
             VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, "active")',
            $insertTypes,
            [
                $organizationId,
                $userId,
                $fields['profile_photo_path'],
                $fields['salutation'],
                $fields['first_name'],
                $fields['middle_name'],
                $fields['last_name'],
                $fields['passport_name'],
                $fields['nationality'],
                $fields['date_of_birth'] !== '' ? $fields['date_of_birth'] : null,
                $fields['gender'],
                $fields['personal_email'],
                $fields['personal_mobile'],
                $fields['redress_number'],
                $fields['known_traveler_number'],
                $fields['fax_number'],
                $fields['preferred_departure_airport'],
                $fields['home_address_line1'],
                $fields['home_address_line2'],
                $fields['home_country'],
                $fields['home_state'],
                $fields['home_city'],
                $fields['home_pin_code'],
                $fields['home_phone'],
                $fields['work_address_line1'],
                $fields['work_address_line2'],
                $fields['work_country'],
                $fields['work_state'],
                $fields['work_city'],
                $fields['work_pin_code'],
                $fields['work_phone'],
            ]
        );

    if ($saved >= 0) {
        flash('settings_success', 'Saved successfully.');
        redirect('travel_profile.php');
    }

    flash('settings_error', 'Unable to save the travel profile.');
    redirect('travel_profile.php?edit=1');
}

$countries = ['India', 'United States', 'United Kingdom', 'Canada', 'Australia'];
$states = ['Arunachal Pradesh', 'Tamil Nadu', 'Karnataka', 'Maharashtra', 'Delhi'];
$genders = ['Male', 'Female', 'Other'];
$salutations = ['Mr.', 'Mrs.', 'Ms.', 'Dr.'];

$sidebarItems = [
    ['icon' => 'fa-solid fa-house', 'label' => 'Home', 'href' => page_url('dashboard')],
    ['icon' => 'fa-solid fa-compass', 'label' => 'Trips', 'href' => route_url('trips/new/')],
    ['icon' => 'fa-regular fa-credit-card', 'label' => 'Expenses', 'href' => route_url('expenses/new/')],
    ['icon' => 'fa-regular fa-file-lines', 'label' => 'Reports', 'href' => '#'],
    ['icon' => 'fa-solid fa-folder-open', 'label' => 'Advances', 'href' => '#'],
    ['icon' => 'fa-regular fa-credit-card', 'label' => 'Cards', 'href' => page_url('cards')],
    ['icon' => 'fa-solid fa-circle-check', 'label' => 'Approvals', 'href' => '#'],
    ['icon' => 'fa-solid fa-chart-column', 'label' => 'Analytics', 'href' => page_url('analytics')],
    ['icon' => 'fa-solid fa-gear', 'label' => 'My Settings', 'href' => page_url('my_settings'), 'active' => true],
];

render_header($pageTitle . ' | Expense', 'dashboard-layout travel-profile-page travel-profile-shell');
?>
<div class="dashboard-shell">
    <div class="dashboard-content">
        <aside class="dashboard-sidebar">
            <div class="sidebar-brand">
                <span class="brand-mark">
                    <span class="brand-ring"></span>
                </span>
                <span class="brand-text">Expense</span>
            </div>

            <nav class="sidebar-nav" aria-label="Primary">
                <?php foreach ($sidebarItems as $item): ?>
                    <a class="sidebar-link<?= !empty($item['active']) ? ' is-active' : '' ?>" href="<?= e($item['href'] ?? '#') ?>">
                        <i class="<?= e($item['icon']) ?>" aria-hidden="true"></i>
                        <span><?= e($item['label']) ?></span>
                    </a>
                <?php endforeach; ?>
            </nav>

            <button type="button" class="sidebar-admin-switch">
                <i class="fa-solid fa-shield-halved"></i>
                <span>Switch to Admin View</span>
            </button>

            <button type="button" class="sidebar-collapse" aria-label="Collapse sidebar">
                <i class="fa-solid fa-arrow-left-long"></i>
            </button>
        </aside>
        <main class="dashboard-main travel-profile-content">
            <header class="dashboard-topbar">
                <label class="dashboard-search">
                    <i class="fa-solid fa-magnifying-glass" aria-hidden="true"></i>
                    <input type="search" placeholder="Search in Expenses ( / )" aria-label="Search in Expenses">
                </label>

                <div class="topbar-right">
                    <div class="trial-pill">
                        <span>Trial expires in 11 days.</span>
                        <a href="#">Upgrade Now</a>
                    </div>

                    <button type="button" class="view-pill">
                        <span class="view-stack">
                            <span class="view-mini">demo</span>
                            <strong>My View</strong>
                        </span>
                        <i class="fa-solid fa-chevron-down" aria-hidden="true"></i>
                    </button>

                    <div class="topbar-quick-add" data-quick-add>
                        <button type="button" class="topbar-square primary" data-quick-add-toggle aria-haspopup="true" aria-expanded="false" aria-label="Quick add">
                            <i class="fa-solid fa-plus" aria-hidden="true"></i>
                        </button>
                        <div class="quick-add-menu" data-quick-add-menu aria-hidden="true">
                            <div class="quick-add-menu-header">QUICK ADD</div>
                            <a class="quick-add-menu-item" href="<?= e(route_url('trips/new/')) ?>">
                                <span class="quick-add-menu-icon"><i class="fa-solid fa-suitcase" aria-hidden="true"></i></span>
                                <span>Trip</span>
                            </a>
                            <a class="quick-add-menu-item" href="<?= e(route_url('expenses/new/')) ?>">
                                <span class="quick-add-menu-icon"><i class="fa-regular fa-credit-card" aria-hidden="true"></i></span>
                                <span>Expense</span>
                            </a>
                            <a class="quick-add-menu-item" href="<?= e(route_url('reports/new/')) ?>">
                                <span class="quick-add-menu-icon"><i class="fa-regular fa-file-lines" aria-hidden="true"></i></span>
                                <span>Report</span>
                            </a>
                            <a class="quick-add-menu-item" href="<?= e(route_url('advances/new/')) ?>">
                                <span class="quick-add-menu-icon"><i class="fa-solid fa-folder-open" aria-hidden="true"></i></span>
                                <span>Advance</span>
                            </a>
                        </div>
                    </div>

                    <button type="button" class="topbar-icon" data-topbar-drawer-open="users" aria-expanded="false" aria-label="Delegates">
                        <i class="fa-solid fa-users" aria-hidden="true"></i>
                    </button>

                    <button type="button" class="topbar-icon" data-topbar-drawer-open="notifications" aria-expanded="false" aria-label="Notifications">
                        <i class="fa-solid fa-bell" aria-hidden="true"></i>
                    </button>

                    <a class="topbar-icon topbar-icon-link" href="<?= e(page_url('my_settings')) ?>" aria-label="Settings">
                        <i class="fa-solid fa-gear" aria-hidden="true"></i>
                    </a>

                    <button type="button" class="avatar-button" data-topbar-drawer-open="profile" aria-expanded="false" aria-label="Open profile menu">
                        <img src="../images/img1.jpg" alt="" class="avatar-image">
                    </button>

                    <button type="button" class="topbar-icon topbar-icon--launcher" data-topbar-drawer-open="apps" aria-expanded="false" aria-label="All Zoho apps">
                        <span class="topbar-launcher" aria-hidden="true">
                            <span></span><span></span><span></span>
                            <span></span><span></span><span></span>
                            <span></span><span></span><span></span>
                        </span>
                    </button>
                </div>
            </header>

            <div class="topbar-drawer topbar-drawer--users" data-topbar-drawer="users" aria-hidden="true">
                <button type="button" class="topbar-drawer-backdrop" data-topbar-drawer-close aria-label="Close delegates panel"></button>
                <aside class="topbar-drawer-panel" role="dialog" aria-modal="true" aria-label="Delegates">
                    <button type="button" class="topbar-drawer-close" data-topbar-drawer-close aria-label="Close">
                        <i class="fa-solid fa-xmark" aria-hidden="true"></i>
                    </button>
                    <div class="topbar-drawer-header">
                        <h2>Delegated To Me</h2>
                    </div>
                    <div class="topbar-drawer-empty" style="min-height: 180px;">
                        <div class="topbar-drawer-empty-icon">
                            <i class="fa-regular fa-circle-user" aria-hidden="true"></i>
                        </div>
                        <p>0 users</p>
                    </div>
                </aside>
            </div>

            <div class="topbar-drawer topbar-drawer--notifications" data-topbar-drawer="notifications" aria-hidden="true">
                <button type="button" class="topbar-drawer-backdrop" data-topbar-drawer-close aria-label="Close notifications panel"></button>
                <aside class="topbar-drawer-panel" role="dialog" aria-modal="true" aria-label="Notifications">
                    <button type="button" class="topbar-drawer-close" data-topbar-drawer-close aria-label="Close">
                        <i class="fa-solid fa-xmark" aria-hidden="true"></i>
                    </button>
                    <div class="topbar-drawer-header">
                        <h2>Notifications</h2>
                        <button type="button" class="topbar-drawer-filter">
                            Unread
                            <i class="fa-solid fa-caret-down" aria-hidden="true"></i>
                        </button>
                    </div>
                    <div class="topbar-drawer-note-row">
                        <div>
                            <strong>Desktop Notifications</strong>
                            <span>Stay up to date while you work.</span>
                        </div>
                        <a href="#">Enable</a>
                    </div>
                    <div class="topbar-drawer-empty">
                        <div class="topbar-drawer-empty-icon">
                            <i class="fa-regular fa-bell" aria-hidden="true"></i>
                        </div>
                        <p>Uhh... You don't have any notifications at the moment.</p>
                    </div>
                </aside>
            </div>

            <div class="topbar-drawer topbar-drawer--profile" data-topbar-drawer="profile" aria-hidden="true">
                <button type="button" class="topbar-drawer-backdrop" data-topbar-drawer-close aria-label="Close profile panel"></button>
                <aside class="topbar-drawer-panel" role="dialog" aria-modal="true" aria-label="Profile menu">
                    <button type="button" class="topbar-drawer-close" data-topbar-drawer-close aria-label="Close">
                        <i class="fa-solid fa-xmark" aria-hidden="true"></i>
                    </button>

                    <div class="topbar-drawer-card">
                        <img src="../images/img1.jpg" alt="" class="topbar-drawer-avatar">
                        <div>
                            <strong><?= e(trim((string) ($_SESSION['user_company'] ?? $_SESSION['user'] ?? 'Expense user'))) ?></strong>
                            <span><?= e(trim((string) ($_SESSION['user_email'] ?? ''))) ?></span>
                            <div class="topbar-drawer-account-links">
                                <a href="<?= e(page_url('my_settings')) ?>">My Account</a>
                                <span aria-hidden="true">&bull;</span>
                                <a href="<?= e(page_url('logout')) ?>">Sign Out</a>
                            </div>
                        </div>
                    </div>

                    <div class="topbar-drawer-section">
                        <div class="topbar-drawer-note-row">
                            <div>
                                <strong>Delegated To Me</strong>
                                <span>0 users</span>
                            </div>
                            <a href="#">Open</a>
                        </div>
                    </div>

                    <div class="topbar-drawer-section">
                        <div class="topbar-drawer-links">
                            <a href="#"><i class="fa-regular fa-circle-question" aria-hidden="true"></i><span>Help &amp; Support</span></a>
                            <a href="#"><i class="fa-regular fa-compass" aria-hidden="true"></i><span>Take a Tour</span></a>
                            <a href="#"><i class="fa-regular fa-comment-dots" aria-hidden="true"></i><span>Share Feedback</span></a>
                            <a href="#"><i class="fa-regular fa-keyboard" aria-hidden="true"></i><span>View Keyboard Shortcuts</span> <span class="topbar-drawer-badge">New</span></a>
                            <a href="#"><i class="fa-regular fa-calendar" aria-hidden="true"></i><span>Webinar</span></a>
                        </div>
                    </div>

                    <div class="topbar-drawer-section">
                        <div class="topbar-drawer-links">
                            <a href="#"><i class="fa-regular fa-lightbulb" aria-hidden="true"></i><span>Request a Feature</span></a>
                            <a href="#"><i class="fa-regular fa-file-lines" aria-hidden="true"></i><span>Request a Demo</span></a>
                            <a href="#"><i class="fa-regular fa-phone" aria-hidden="true"></i><span>Request a Call</span></a>
                            <a href="#"><i class="fa-solid fa-headphones-simple" aria-hidden="true"></i><span>Contact Support</span></a>
                        </div>
                    </div>

                    <div class="topbar-drawer-divider"></div>

                    <div class="topbar-drawer-section">
                        <div class="topbar-drawer-section-title">
                            <i class="fa-solid fa-sparkles" aria-hidden="true"></i>
                            <h3>What's New?</h3>
                        </div>
                        <div class="topbar-drawer-callout">
                            You can directly talk to us every Monday to Friday
                            <strong>18005692747 - 9.00 AM to 7:00 PM</strong>
                        </div>
                        <div class="topbar-drawer-qr">
                            <div class="topbar-drawer-qr-code" aria-hidden="true"></div>
                            <div class="topbar-drawer-qr-copy">
                                Scan the QR code to download the Zoho Expense mobile app.
                                <strong>Available in</strong>
                                <div class="topbar-drawer-qr-apps" aria-label="Available platforms">
                                    <i class="fa-brands fa-apple" aria-hidden="true"></i>
                                    <i class="fa-brands fa-google-play" aria-hidden="true"></i>
                                </div>
                            </div>
                        </div>
                    </div>
                </aside>
            </div>

            <div class="topbar-drawer topbar-drawer--apps" data-topbar-drawer="apps" aria-hidden="true">
                <button type="button" class="topbar-drawer-backdrop" data-topbar-drawer-close aria-label="Close apps panel"></button>
                <aside class="topbar-drawer-panel topbar-drawer-panel--apps" role="dialog" aria-modal="true" aria-label="All Zoho apps">
                    <button type="button" class="topbar-drawer-close" data-topbar-drawer-close aria-label="Close">
                        <i class="fa-solid fa-xmark" aria-hidden="true"></i>
                    </button>
                    <div class="topbar-drawer-header topbar-drawer-header--apps">
                        <h2>All Zoho Apps</h2>
                        <button type="button" class="topbar-drawer-search-toggle" aria-label="Search apps">
                            <i class="fa-solid fa-magnifying-glass" aria-hidden="true"></i>
                        </button>
                    </div>
                    <label class="topbar-drawer-search">
                        <i class="fa-solid fa-magnifying-glass" aria-hidden="true"></i>
                        <input type="search" data-app-search placeholder="Search apps" aria-label="Search apps">
                    </label>

                    <section class="topbar-drawer-section" data-app-section>
                        <div class="topbar-drawer-section-label">Finance Apps</div>
                        <div class="topbar-drawer-app-grid">
                            <a href="#" class="topbar-app-item" data-app-item><i class="fa-solid fa-book-bookmark" aria-hidden="true"></i><span class="topbar-app-item-text"><strong>Zoho Books</strong><small>Online Accounting Software</small></span></a>
                            <a href="#" class="topbar-app-item" data-app-item><i class="fa-solid fa-receipt" aria-hidden="true"></i><span class="topbar-app-item-text"><strong>Zoho Billing</strong><small>End-to-end Billing Solution</small></span></a>
                            <a href="#" class="topbar-app-item" data-app-item><i class="fa-solid fa-boxes-stacked" aria-hidden="true"></i><span class="topbar-app-item-text"><strong>Zoho Inventory</strong><small>Order &amp; Inventory Management Software</small></span></a>
                            <a href="#" class="topbar-app-item" data-app-item><i class="fa-solid fa-wallet" aria-hidden="true"></i><span class="topbar-app-item-text"><strong>Zoho Checkout</strong><small>One-time and recurring payments software</small></span></a>
                            <a href="#" class="topbar-app-item" data-app-item><i class="fa-solid fa-cart-shopping" aria-hidden="true"></i><span class="topbar-app-item-text"><strong>Zoho Commerce</strong><small>Ecommerce software</small></span></a>
                            <a href="#" class="topbar-app-item" data-app-item><i class="fa-solid fa-file-invoice-dollar" aria-hidden="true"></i><span class="topbar-app-item-text"><strong>Zoho Payroll</strong><small>Simplified Payroll Management Software</small></span></a>
                        </div>
                    </section>

                    <section class="topbar-drawer-section" data-app-section>
                        <div class="topbar-drawer-section-label">Email and Collaboration</div>
                        <div class="topbar-drawer-app-grid topbar-drawer-app-grid--compact">
                            <a href="#" class="topbar-app-item topbar-app-item--compact" data-app-item><i class="fa-solid fa-book"></i><span class="topbar-app-item-text"><strong>Notebook</strong></span></a>
                            <a href="#" class="topbar-app-item topbar-app-item--compact" data-app-item><i class="fa-solid fa-circle-play"></i><span class="topbar-app-item-text"><strong>Meeting</strong></span></a>
                            <a href="#" class="topbar-app-item topbar-app-item--compact" data-app-item><i class="fa-solid fa-diagram-project"></i><span class="topbar-app-item-text"><strong>Projects</strong></span></a>
                            <a href="#" class="topbar-app-item topbar-app-item--compact" data-app-item><i class="fa-solid fa-pen-nib"></i><span class="topbar-app-item-text"><strong>Sign</strong></span></a>
                            <a href="#" class="topbar-app-item topbar-app-item--compact" data-app-item><i class="fa-solid fa-jet-fighter-up"></i><span class="topbar-app-item-text"><strong>Sprints</strong></span></a>
                            <a href="#" class="topbar-app-item topbar-app-item--compact" data-app-item><i class="fa-solid fa-inbox"></i><span class="topbar-app-item-text"><strong>TeamInbox</strong></span></a>
                            <a href="#" class="topbar-app-item topbar-app-item--compact" data-app-item><i class="fa-solid fa-envelope"></i><span class="topbar-app-item-text"><strong>Mail</strong></span></a>
                            <a href="#" class="topbar-app-item topbar-app-item--compact" data-app-item><i class="fa-solid fa-comment-dots"></i><span class="topbar-app-item-text"><strong>Cliq</strong></span></a>
                            <a href="#" class="topbar-app-item topbar-app-item--compact" data-app-item><i class="fa-solid fa-table"></i><span class="topbar-app-item-text"><strong>Sheet</strong></span></a>
                            <a href="#" class="topbar-app-item topbar-app-item--compact" data-app-item><i class="fa-solid fa-clapperboard"></i><span class="topbar-app-item-text"><strong>Show</strong></span></a>
                            <a href="#" class="topbar-app-item topbar-app-item--compact" data-app-item><i class="fa-solid fa-file-lines"></i><span class="topbar-app-item-text"><strong>Writer</strong></span></a>
                            <a href="#" class="topbar-app-item topbar-app-item--compact" data-app-item><i class="fa-solid fa-circle-nodes"></i><span class="topbar-app-item-text"><strong>Connect</strong></span></a>
                            <a href="#" class="topbar-app-item topbar-app-item--compact" data-app-item><i class="fa-solid fa-vault"></i><span class="topbar-app-item-text"><strong>Vault</strong></span></a>
                            <a href="#" class="topbar-app-item topbar-app-item--compact" data-app-item><i class="fa-solid fa-folder-open"></i><span class="topbar-app-item-text"><strong>WorkDrive</strong></span></a>
                            <a href="#" class="topbar-app-item topbar-app-item--compact" data-app-item><i class="fa-solid fa-graduation-cap"></i><span class="topbar-app-item-text"><strong>Learn</strong></span></a>
                        </div>
                    </section>
                </aside>
            </div>
            <div class="travel-page-head">
                <h1><?= e($pageTitle) ?></h1>
                <a class="travel-page-close" href="<?= e('my_settings.php?navType=travel&travelTab=profile') ?>" aria-label="Back to settings">
                    <i class="fa-solid fa-xmark"></i>
                </a>
            </div>

            <?php if ($settingsSuccess): ?>
                <div class="settings-message settings-success"><?= e($settingsSuccess) ?></div>
            <?php endif; ?>
            <?php if ($settingsError): ?>
                <div class="settings-message settings-error"><?= e($settingsError) ?></div>
            <?php endif; ?>

            <?php if ($editMode): ?>
                <form method="post" class="travel-form-shell">
                    <div class="travel-section">
                        <h2>Personal Details</h2>
                        <div class="travel-grid">
                            <div class="travel-two-col compact">
                                <label class="travel-field">
                                    <div class="travel-label-row">
                                        <span>Name as in Passport/ID Proof *</span>
                                        <a href="#" class="travel-helper-link">Don't have a passport?</a>
                                    </div>
                                    <div class="travel-inline travel-inline--name">
                                        <select name="salutation">
                                            <option value="">Salutation</option>
                                            <?php foreach ($salutations as $salutation): ?>
                                                <option value="<?= e($salutation) ?>"<?= travel_select_option($travelProfile['salutation'], $salutation) ?>><?= e($salutation) ?></option>
                                            <?php endforeach; ?>
                                        </select>
                                        <input type="text" name="first_name" value="<?= e($travelProfile['first_name']) ?>" placeholder="balachandar">
                                        <input type="text" name="middle_name" value="<?= e($travelProfile['middle_name']) ?>" placeholder="Middle Name">
                                        <input type="text" name="last_name" value="<?= e($travelProfile['last_name']) ?>" placeholder="kathiresan">
                                    </div>
                                </label>
                                <label class="travel-field">
                                    <span>D.O.B as in Passport/ID Proof *</span>
                                    <input type="text" name="date_of_birth" value="<?= e($travelProfile['date_of_birth']) ?>" placeholder="dd/mm/yyyy">
                                </label>
                            </div>

                            <div class="travel-two-col compact">
                                <label class="travel-field">
                                    <span>Nationality</span>
                                    <select name="nationality">
                                        <option value="">Select</option>
                                        <?php foreach ($countries as $country): ?>
                                            <option value="<?= e($country) ?>"<?= travel_select_option($travelProfile['nationality'], $country) ?>><?= e($country) ?></option>
                                        <?php endforeach; ?>
                                    </select>
                                </label>
                                <label class="travel-field">
                                    <span>Gender</span>
                                    <select name="gender">
                                        <?php foreach ($genders as $gender): ?>
                                            <option value="<?= e($gender) ?>"<?= travel_select_option($travelProfile['gender'], $gender) ?>><?= e($gender) ?></option>
                                        <?php endforeach; ?>
                                    </select>
                                </label>
                            </div>

                            <div class="travel-two-col compact">
                                <label class="travel-field">
                                    <span>Personal Email Address *</span>
                                    <input type="email" name="personal_email" value="<?= e($travelProfile['personal_email']) ?>" placeholder="<?= e($userProfile['email']) ?>">
                                </label>
                                <label class="travel-field">
                                    <span>Personal Mobile Number</span>
                                    <div class="travel-phone">
                                        <span>+91</span>
                                        <input type="text" name="personal_mobile" value="<?= e($travelProfile['personal_mobile']) ?>" placeholder="Personal Mobile Number">
                                    </div>
                                </label>
                            </div>

                            <div class="travel-two-col compact">
                                <label class="travel-field">
                                    <span>Redress Number</span>
                                    <input type="text" name="redress_number" value="<?= e($travelProfile['redress_number']) ?>" placeholder="Redress Number">
                                </label>
                                <label class="travel-field">
                                    <span>Known Traveler Number</span>
                                    <input type="text" name="known_traveler_number" value="<?= e($travelProfile['known_traveler_number']) ?>" placeholder="Known Traveler Number">
                                </label>
                            </div>

                            <div class="travel-two-col compact">
                                <label class="travel-field">
                                    <span>Fax</span>
                                    <input type="text" name="fax_number" value="<?= e($travelProfile['fax_number']) ?>" placeholder="Fax">
                                </label>
                                <label class="travel-field">
                                    <span>Preferred Departure Airport</span>
                                    <select name="preferred_departure_airport">
                                        <option value="">Select</option>
                                        <option value="Chennai International Airport"<?= travel_select_option($travelProfile['preferred_departure_airport'], 'Chennai International Airport') ?>>Chennai International Airport</option>
                                        <option value="Indira Gandhi International Airport"<?= travel_select_option($travelProfile['preferred_departure_airport'], 'Indira Gandhi International Airport') ?>>Indira Gandhi International Airport</option>
                                        <option value="Kempegowda International Airport"<?= travel_select_option($travelProfile['preferred_departure_airport'], 'Kempegowda International Airport') ?>>Kempegowda International Airport</option>
                                    </select>
                                </label>
                            </div>
                        </div>
                    </div>

                    <div class="travel-section">
                        <h2>Address</h2>
                        <div class="travel-address-layout">
                            <div class="travel-address-column">
                                <div class="travel-grid">
                                    <label class="travel-field">
                                        <span>Address</span>
                                        <textarea name="home_address_line1" rows="2" placeholder="Address Line 1"><?= e($travelProfile['home_address_line1']) ?></textarea>
                                    </label>
                                    <label class="travel-field">
                                        <span class="travel-field-spacer">&nbsp;</span>
                                        <textarea name="home_address_line2" rows="2" placeholder="Address Line 2"><?= e($travelProfile['home_address_line2']) ?></textarea>
                                    </label>
                                    <div class="travel-two-col compact">
                                        <label class="travel-field">
                                            <span>Country</span>
                                            <select name="home_country">
                                                <option value="">Select</option>
                                                <?php foreach ($countries as $country): ?>
                                                    <option value="<?= e($country) ?>"<?= travel_select_option($travelProfile['home_country'], $country) ?>><?= e($country) ?></option>
                                                <?php endforeach; ?>
                                            </select>
                                        </label>
                                        <label class="travel-field">
                                            <span>State</span>
                                            <select name="home_state">
                                                <option value="">State</option>
                                                <?php foreach ($states as $state): ?>
                                                    <option value="<?= e($state) ?>"<?= travel_select_option($travelProfile['home_state'], $state) ?>><?= e($state) ?></option>
                                                <?php endforeach; ?>
                                            </select>
                                        </label>
                                    </div>
                                    <div class="travel-two-col compact">
                                        <label class="travel-field">
                                            <span>City</span>
                                            <input type="text" name="home_city" value="<?= e($travelProfile['home_city']) ?>" placeholder="City">
                                        </label>
                                        <label class="travel-field">
                                            <span>Pin Code</span>
                                            <input type="text" name="home_pin_code" value="<?= e($travelProfile['home_pin_code']) ?>" placeholder="Pin Code">
                                        </label>
                                    </div>
                                    <label class="travel-field travel-field--phone">
                                        <span>Home Phone</span>
                                        <input type="text" name="home_phone" value="<?= e($travelProfile['home_phone']) ?>" placeholder="Home Phone">
                                    </label>
                                </div>
                            </div>

                            <div class="travel-address-column">
                                <div class="travel-grid">
                                    <label class="travel-field">
                                        <span>Address</span>
                                        <textarea name="work_address_line1" rows="2" placeholder="Address Line 1"><?= e($travelProfile['work_address_line1']) ?></textarea>
                                    </label>
                                    <label class="travel-field">
                                        <span class="travel-field-spacer">&nbsp;</span>
                                        <textarea name="work_address_line2" rows="2" placeholder="Address Line 2"><?= e($travelProfile['work_address_line2']) ?></textarea>
                                    </label>
                                    <div class="travel-two-col compact">
                                        <label class="travel-field">
                                            <span>Country</span>
                                            <select name="work_country">
                                                <option value="">Select</option>
                                                <?php foreach ($countries as $country): ?>
                                                    <option value="<?= e($country) ?>"<?= travel_select_option($travelProfile['work_country'], $country) ?>><?= e($country) ?></option>
                                                <?php endforeach; ?>
                                            </select>
                                        </label>
                                        <label class="travel-field">
                                            <span>State</span>
                                            <input type="text" name="work_state" value="<?= e($travelProfile['work_state']) ?>" placeholder="State">
                                        </label>
                                    </div>
                                    <div class="travel-two-col compact">
                                        <label class="travel-field">
                                            <span>City</span>
                                            <input type="text" name="work_city" value="<?= e($travelProfile['work_city']) ?>" placeholder="City">
                                        </label>
                                        <label class="travel-field">
                                            <span>Pin Code</span>
                                            <input type="text" name="work_pin_code" value="<?= e($travelProfile['work_pin_code']) ?>" placeholder="Pin Code">
                                        </label>
                                    </div>
                                    <label class="travel-field travel-field--phone">
                                        <span>Work Phone</span>
                                        <input type="text" name="work_phone" value="<?= e($travelProfile['work_phone']) ?>" placeholder="Work Phone">
                                    </label>
                                </div>
                            </div>

                            <div class="travel-note">
                                <strong>Disclaimer:</strong> Kindly note that your business trip bookings will be made based on the above information. So, it's your responsibility to provide accurate information for your travel profile.
                            </div>
                            <div class="travel-form-divider"></div>
                            <div class="travel-form-actions">
                                <button type="submit" class="settings-dialog-primary" name="settings_action" value="save_travel_profile">Save</button>
                                <a class="settings-dialog-secondary settings-anchor-action" href="<?= e('travel_profile.php') ?>">Cancel</a>
                            </div>
                        </div>
                    </div>
                </form>
            <?php else: ?>
                <section class="travel-summary-layout">
                    <article class="travel-summary-card">
                        <header>Personal Details</header>
                        <div class="travel-summary-grid">
                            <div class="travel-summary-row"><span>Name</span><strong><?= e($travelProfile['passport_name']) ?></strong></div>
                            <div class="travel-summary-row"><span>Nationality</span><strong><?= e($travelProfile['nationality'] ?: '-') ?></strong></div>
                            <div class="travel-summary-row"><span>Date of Birth</span><strong><?= e($travelProfile['date_of_birth'] ?: '-') ?></strong></div>
                            <div class="travel-summary-row"><span>Gender</span><strong><?= e($travelProfile['gender'] ?: '-') ?></strong></div>
                            <div class="travel-summary-row"><span>Email</span><strong><?= e($travelProfile['personal_email'] ?: '-') ?></strong></div>
                            <div class="travel-summary-row"><span>Mobile</span><strong><?= e($travelProfile['personal_mobile'] ?: '-') ?></strong></div>
                        </div>
                        <div class="travel-summary-actions">
                            <a class="settings-primary-action settings-anchor-action" href="<?= e('travel_profile.php?edit=1') ?>">Edit Travel Profile</a>
                        </div>
                    </article>

                    <article class="travel-summary-card">
                        <header>Address</header>
                        <div class="travel-address-summary">
                            <div>
                                <h3>Home</h3>
                                <p><?= e(trim($travelProfile['home_address_line1'] . ' ' . $travelProfile['home_address_line2'])) ?: 'No home address added yet.' ?></p>
                                <p><?= e(trim($travelProfile['home_city'] . ' ' . $travelProfile['home_state'] . ' ' . $travelProfile['home_pin_code'])) ?: '' ?></p>
                                <p><?= e($travelProfile['home_country'] ?: '-') ?></p>
                            </div>
                            <div>
                                <h3>Work</h3>
                                <p><?= e(trim($travelProfile['work_address_line1'] . ' ' . $travelProfile['work_address_line2'])) ?: 'No work address added yet.' ?></p>
                                <p><?= e(trim($travelProfile['work_city'] . ' ' . $travelProfile['work_state'] . ' ' . $travelProfile['work_pin_code'])) ?: '' ?></p>
                                <p><?= e($travelProfile['work_country'] ?: '-') ?></p>
                            </div>
                        </div>
                    </article>

                    <article class="travel-summary-card travel-emergency-card">
                        <header>Emergency Contacts</header>
                        <p>Add emergency contacts to be reached in case you have an emergency.</p>
                        <a class="settings-primary-action settings-anchor-action" href="#">+ Add Emergency Contact</a>
                    </article>
                </section>
            <?php endif; ?>
        </main>
    </div>
</div>
<?php render_footer(); ?>
