<?php
/**
 * REVENDA — Vendor: Tour Guides
 * @version 1.0.0
 * Route: ?page=vendor-tour-guides
 */

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-guides')); 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_guides (operator_id, first_name, last_name, bio, years_experience, phone, email)
                 VALUES (?, ?, ?, ?, ?, ?, ?)",
                [$operatorId, trim($_POST['first_name']), trim($_POST['last_name']), trim($_POST['bio'] ?? ''), (int)($_POST['years_experience'] ?? 0), trim($_POST['phone'] ?? ''), trim($_POST['email'] ?? '')]
            );
            $msg = 'Guide added.';
        } catch (Throwable $e) { $msg = 'Error: ' . $e->getMessage(); }
    } elseif ($action === 'delete') {
        try { db()->execute("DELETE FROM tour_guides WHERE id = ? AND operator_id = ?", [(int)$_POST['id'], $operatorId]); $msg = 'Guide removed.'; }
        catch (Throwable $e) { $msg = 'Error: ' . $e->getMessage(); }
    } elseif ($action === 'toggle') {
        try { db()->execute("UPDATE tour_guides SET is_active = IF(is_active=1,0,1) WHERE id = ? AND operator_id = ?", [(int)$_POST['id'], $operatorId]); $msg = 'Status updated.'; }
        catch (Throwable $e) { $msg = 'Error: ' . $e->getMessage(); }
    }
}

try { $guides = db()->fetchAll("SELECT * FROM tour_guides WHERE operator_id = ? ORDER BY first_name", [$operatorId]); }
catch (Throwable $e) { $guides = []; }
if (!is_array($guides)) $guides = [];

$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; }
.vg-page { background: #0f0c29; min-height: 100vh; padding: 100px 0 60px; color: white; }
.vg-page .container { max-width: 1100px; margin: 0 auto; padding: 0 20px; }
.vg-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 16px; flex-wrap: wrap; margin-bottom: 24px; }
.vg-head h1 { font-size: 26px; font-weight: 800; margin: 0 0 4px; }
.vg-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; }
.vg-btn.primary { background: var(--grad-pink); border-color: transparent; color: white; }
.vg-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; }
.vg-section { background: var(--glass); border: 1px solid var(--border); border-radius: 18px; padding: 24px; margin-bottom: 20px; }
.vg-section h2 { font-size: 16px; font-weight: 800; margin: 0 0 18px; display: flex; align-items: center; gap: 10px; }
.vg-section h2 i { color: var(--pink); }
.vg-form { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.vg-form .full { grid-column: 1 / -1; }
.vg-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; }
.vg-field input, .vg-field textarea { 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; }
.vg-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 14px; }
.vg-card { background: rgba(255,255,255,0.02); border: 1px solid var(--border); border-radius: 14px; padding: 18px; display: flex; gap: 14px; align-items: flex-start; }
.vg-avatar { width: 52px; height: 52px; border-radius: 50%; background: var(--grad-pink); display: flex; align-items: center; justify-content: center; font-size: 20px; font-weight: 900; color: white; flex-shrink: 0; }
.vg-info { flex: 1; min-width: 0; }
.vg-name { font-size: 15px; font-weight: 800; color: white; margin: 0 0 3px; }
.vg-meta { font-size: 11.5px; color: var(--text-dim); margin-bottom: 8px; }
.vg-actions { display: flex; gap: 6px; }
.vg-actions button, .vg-actions a { 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: #cbd5e0; text-decoration: none; font-family: inherit; }
.vg-actions .del { color: var(--red); border-color: rgba(245,87,108,0.3); }
.vg-empty { text-align: center; padding: 30px; color: var(--text-dim); font-size: 13px; }
@media (max-width: 768px) { .vg-page { padding: 80px 0 60px; } .vg-form { grid-template-columns: 1fr; } }
</style>

<div class="vg-page">
    <div class="container">
        <div class="vg-head">
            <div>
                <h1>Tour Guides</h1>
                <p style="color:var(--text-dim);font-size:13.5px;margin:0;">Your guide roster. Assign them to departures.</p>
            </div>
            <a href="/?page=vendor-tour-dashboard" class="vg-btn"><i class="fas fa-arrow-left"></i> Dashboard</a>
        </div>

        <?php if ($msg): ?><div class="vg-msg"><i class="fas fa-check-circle"></i> <?= tourSafe($msg) ?></div><?php endif; ?>

        <div class="vg-section">
            <h2><i class="fas fa-user-plus"></i> Add a guide</h2>
            <form method="POST" class="vg-form">
                <input type="hidden" name="action" value="create">
                <div class="vg-field"><label>First name</label><input type="text" name="first_name" required></div>
                <div class="vg-field"><label>Last name</label><input type="text" name="last_name" required></div>
                <div class="vg-field"><label>Phone</label><input type="tel" name="phone"></div>
                <div class="vg-field"><label>Email</label><input type="email" name="email"></div>
                <div class="vg-field"><label>Years experience</label><input type="number" name="years_experience" min="0" value="0"></div>
                <div class="vg-field full"><label>Short bio</label><textarea name="bio" rows="3"></textarea></div>
                <div class="full" style="text-align:right;"><button type="submit" class="vg-btn primary"><i class="fas fa-plus"></i> Add guide</button></div>
            </form>
        </div>

        <div class="vg-section">
            <h2><i class="fas fa-users"></i> Team (<?= count($guides) ?>)</h2>
            <?php if (!empty($guides)): ?>
                <div class="vg-grid">
                    <?php foreach ($guides as $g): ?>
                        <div class="vg-card">
                            <div class="vg-avatar"><?= tourSafe(strtoupper(substr($g['first_name'], 0, 1))) ?></div>
                            <div class="vg-info">
                                <h3 class="vg-name"><?= tourSafe($g['first_name'] . ' ' . $g['last_name']) ?></h3>
                                <div class="vg-meta">
                                    <?php if (($g['years_experience'] ?? 0) > 0): ?><?= (int)$g['years_experience'] ?>y exp · <?php endif; ?>
                                    <?= !empty($g['is_active']) ? 'Active' : 'Inactive' ?>
                                </div>
                                <div class="vg-actions">
                                    <form method="POST" style="display:inline;"><input type="hidden" name="action" value="toggle"><input type="hidden" name="id" value="<?= (int)$g['id'] ?>"><button type="submit"><?= !empty($g['is_active']) ? 'Deactivate' : 'Activate' ?></button></form>
                                    <form method="POST" style="display:inline;" onsubmit="return confirm('Delete guide?');"><input type="hidden" name="action" value="delete"><input type="hidden" name="id" value="<?= (int)$g['id'] ?>"><button type="submit" class="del">Delete</button></form>
                                </div>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
            <?php else: ?>
                <div class="vg-empty">No guides 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;
?>