<?php
/**
 * REVENDA — Vendor: Tour Vehicles
 * @version 1.0.0
 * Route: ?page=vendor-tour-vehicles
 */

if (!defined('APP_START')) { http_response_code(403); exit('Direct access denied'); }
if (!function_exists('isLoggedIn') || !isLoggedIn()) {
    redirect('/?page=login&redirect=' . urlencode('/?page=vendor-tour-vehicles')); exit;
}

require_once dirname(__DIR__, 2) . '/includes/tour_operators.php';

$userId = (int)($_SESSION['user_id'] ?? 0);
try { $operator = db()->fetchOne("SELECT * FROM tour_operators WHERE vendor_id = ? LIMIT 1", [$userId]); }
catch (Throwable $e) { $operator = null; }
if (!$operator) { redirect('/?page=vendor-tour-onboarding'); exit; }
$operatorId = (int)$operator['id'];

$msg = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $action = $_POST['action'] ?? '';
    if ($action === 'create') {
        try {
            db()->execute(
                "INSERT INTO tour_vehicles (operator_id, name, type, make, model, year, registration, capacity, luggage_capacity)
                 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
                [$operatorId, trim($_POST['name']), $_POST['type'] ?? 'suv', trim($_POST['make'] ?? ''), trim($_POST['model'] ?? ''), (int)($_POST['year'] ?? 0) ?: null, trim($_POST['registration'] ?? ''), max(1, (int)($_POST['capacity'] ?? 4)), (int)($_POST['luggage_capacity'] ?? 2)]
            );
            $msg = 'Vehicle added.';
        } catch (Throwable $e) { $msg = 'Error: ' . $e->getMessage(); }
    } elseif ($action === 'delete') {
        try { db()->execute("DELETE FROM tour_vehicles WHERE id = ? AND operator_id = ?", [(int)$_POST['id'], $operatorId]); $msg = 'Vehicle removed.'; }
        catch (Throwable $e) { $msg = 'Error: ' . $e->getMessage(); }
    }
}

try { $vehicles = db()->fetchAll("SELECT * FROM tour_vehicles WHERE operator_id = ? ORDER BY name", [$operatorId]); }
catch (Throwable $e) { $vehicles = []; }
if (!is_array($vehicles)) $vehicles = [];

$headerPath = dirname(__DIR__, 2) . '/includes/header.php';
if (is_file($headerPath)) require_once $headerPath;
?>

<style>
:root { --grad-pink: linear-gradient(135deg, #f093fb 0%, #f5576c 100%); --glass: rgba(255,255,255,0.03); --border: rgba(255,255,255,0.08); --text-dim: #a0aec0; --text-hi: #a3bffa; --green: #00b894; --red: #f5576c; --pink: #f093fb; }
.vv-page { background: #0f0c29; min-height: 100vh; padding: 100px 0 60px; color: white; }
.vv-page .container { max-width: 1100px; margin: 0 auto; padding: 0 20px; }
.vv-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 16px; flex-wrap: wrap; margin-bottom: 24px; }
.vv-head h1 { font-size: 26px; font-weight: 800; margin: 0 0 4px; }
.vv-btn { padding: 11px 18px; border-radius: 10px; font-size: 12.5px; font-weight: 800; text-decoration: none; text-transform: uppercase; letter-spacing: 0.04em; display: inline-flex; align-items: center; gap: 7px; border: 1px solid var(--border); background: rgba(255,255,255,0.04); color: #cbd5e0; cursor: pointer; font-family: inherit; }
.vv-btn.primary { background: var(--grad-pink); border-color: transparent; color: white; }
.vv-msg { padding: 12px 18px; border-radius: 12px; background: rgba(0,184,148,0.1); border: 1px solid rgba(0,184,148,0.35); color: #81e6d9; font-size: 13px; font-weight: 700; margin-bottom: 20px; }
.vv-section { background: var(--glass); border: 1px solid var(--border); border-radius: 18px; padding: 24px; margin-bottom: 20px; }
.vv-section h2 { font-size: 16px; font-weight: 800; margin: 0 0 18px; display: flex; align-items: center; gap: 10px; }
.vv-section h2 i { color: var(--pink); }
.vv-form { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 12px; }
.vv-form .full { grid-column: 1 / -1; }
.vv-field label { display: block; font-size: 10.5px; font-weight: 800; text-transform: uppercase; letter-spacing: 0.05em; color: var(--text-dim); margin-bottom: 6px; }
.vv-field input, .vv-field select { width: 100%; padding: 11px 14px; background: rgba(0,0,0,0.25); border: 1px solid rgba(255,255,255,0.1); border-radius: 10px; color: white; font-size: 13.5px; font-family: inherit; outline: none; box-sizing: border-box; }
.vv-field select option { background: #1a1a2e; color: white; }
.vv-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 14px; }
.vv-card { background: rgba(255,255,255,0.02); border: 1px solid var(--border); border-radius: 14px; padding: 16px; }
.vv-card .name { font-size: 15px; font-weight: 800; color: white; margin: 0 0 6px; }
.vv-card .meta { font-size: 12px; color: var(--text-dim); margin-bottom: 10px; }
.vv-card .actions { display: flex; gap: 6px; }
.vv-card .actions button { padding: 5px 10px; border-radius: 6px; font-size: 10.5px; font-weight: 800; cursor: pointer; text-transform: uppercase; letter-spacing: 0.03em; border: 1px solid var(--border); background: rgba(255,255,255,0.04); color: var(--red); font-family: inherit; }
.vv-empty { text-align: center; padding: 30px; color: var(--text-dim); font-size: 13px; }
@media (max-width: 768px) { .vv-page { padding: 80px 0 60px; } .vv-form { grid-template-columns: 1fr; } }
</style>

<div class="vv-page">
    <div class="container">
        <div class="vv-head">
            <div>
                <h1>Vehicles</h1>
                <p style="color:var(--text-dim);font-size:13.5px;margin:0;">Your fleet. Assign them to departures.</p>
            </div>
            <a href="/?page=vendor-tour-dashboard" class="vv-btn"><i class="fas fa-arrow-left"></i> Dashboard</a>
        </div>

        <?php if ($msg): ?><div class="vv-msg"><i class="fas fa-check-circle"></i> <?= tourSafe($msg) ?></div><?php endif; ?>

        <div class="vv-section">
            <h2><i class="fas fa-plus-circle"></i> Add a vehicle</h2>
            <form method="POST" class="vv-form">
                <input type="hidden" name="action" value="create">
                <div class="vv-field"><label>Name / label</label><input type="text" name="name" required placeholder="e.g. Land Cruiser #1"></div>
                <div class="vv-field">
                    <label>Type</label>
                    <select name="type">
                        <?php foreach (['sedan'=>'Sedan','suv'=>'SUV','minivan'=>'Minivan','bus'=>'Bus','coaster'=>'Coaster','4x4'=>'4x4','landcruiser'=>'Land Cruiser','boat'=>'Boat','other'=>'Other'] as $k => $lbl): ?>
                            <option value="<?= $k ?>"><?= $lbl ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="vv-field"><label>Make</label><input type="text" name="make"></div>
                <div class="vv-field"><label>Model</label><input type="text" name="model"></div>
                <div class="vv-field"><label>Year</label><input type="number" name="year" min="1950" max="2099"></div>
                <div class="vv-field"><label>Registration</label><input type="text" name="registration"></div>
                <div class="vv-field"><label>Passenger capacity</label><input type="number" name="capacity" min="1" value="4"></div>
                <div class="vv-field"><label>Luggage capacity</label><input type="number" name="luggage_capacity" min="0" value="2"></div>
                <div class="full" style="text-align:right;"><button type="submit" class="vv-btn primary"><i class="fas fa-plus"></i> Add vehicle</button></div>
            </form>
        </div>

        <div class="vv-section">
            <h2><i class="fas fa-car"></i> Fleet (<?= count($vehicles) ?>)</h2>
            <?php if (!empty($vehicles)): ?>
                <div class="vv-grid">
                    <?php foreach ($vehicles as $v): ?>
                        <div class="vv-card">
                            <h3 class="name"><?= tourSafe($v['name']) ?></h3>
                            <div class="meta">
                                <?= tourSafe($v['type']) ?>
                                <?= !empty($v['capacity']) ? ' · ' . (int)$v['capacity'] . ' seats' : '' ?>
                                <?= !empty($v['registration']) ? ' · ' . tourSafe($v['registration']) : '' ?>
                            </div>
                            <div class="actions">
                                <form method="POST" onsubmit="return confirm('Delete vehicle?');"><input type="hidden" name="action" value="delete"><input type="hidden" name="id" value="<?= (int)$v['id'] ?>"><button type="submit">Delete</button></form>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
            <?php else: ?>
                <div class="vv-empty">No vehicles yet. Add one above.</div>
            <?php endif; ?>
        </div>
    </div>
</div>

<?php
$footerPath = dirname(__DIR__, 2) . '/includes/footer.php';
if (is_file($footerPath)) require_once $footerPath;
?>