<?php
// /htdocs/pages/vendor/rides-drivers.php — DRIVER MANAGEMENT
if (!defined('APP_START')) { http_response_code(403); exit('Direct access denied'); }

$vendor = getVendor();
if (!$vendor) { redirect('/?page=dashboard'); exit; }
$db = db();
$provider = $db->fetchOne("SELECT * FROM ride_providers WHERE vendor_id = ? LIMIT 1", [(int)$vendor['id']]);
if (!$provider) { redirect('/?page=vendor-rides'); exit; }

$drivers = $db->fetchAll(
    "SELECT d.*, v.make, v.model, v.license_plate
     FROM ride_drivers d
     LEFT JOIN ride_driver_vehicles dv ON dv.driver_id = d.id AND dv.is_current = 1
     LEFT JOIN ride_vehicles v ON v.id = dv.vehicle_id
     WHERE d.provider_id = ?
     ORDER BY d.full_name",
    [(int)$provider['id']]
) ?: [];

$pendingApps = $db->fetchAll(
    "SELECT * FROM ride_driver_applications
     WHERE provider_id = ? AND status = 'pending'
     ORDER BY created_at DESC",
    [(int)$provider['id']]
) ?: [];

$pageTitle = 'Drivers — ' . $provider['name'];
require_once __DIR__ . '/../../includes/header.php';
?>

<style>
.rd-page { background: #0f0c29; min-height: 100vh; padding: 100px 0 80px; color: white; }
.rd-page .container { max-width: 1400px; margin: 0 auto; padding: 0 20px; }
.rd-head { display: flex; justify-content: space-between; align-items: flex-start; flex-wrap: wrap; gap: 16px; margin-bottom: 22px; }
.rd-head h1 { font-size: 28px; font-weight: 800; margin-bottom: 4px; }
.rd-head p { color: #a0aec0; font-size: 13px; }
.rd-nav { display: flex; gap: 6px; background: rgba(255,255,255,0.03); border: 1px solid rgba(255,255,255,0.08); border-radius: 12px; padding: 4px; flex-wrap: wrap; }
.rd-nav a { padding: 8px 14px; border-radius: 8px; font-size: 12px; font-weight: 700; color: #a0aec0; text-decoration: none; display: inline-flex; align-items: center; gap: 6px; }
.rd-nav a:hover { background: rgba(255,255,255,0.05); color: white; }
.rd-nav a.active { background: linear-gradient(135deg, #667eea, #764ba2); color: white; }
.rd-section { background: rgba(255,255,255,0.03); border: 1px solid rgba(255,255,255,0.08); border-radius: 16px; padding: 22px; margin-bottom: 20px; }
.rd-section h2 { font-size: 16px; font-weight: 800; margin-bottom: 16px; display: flex; align-items: center; gap: 8px; }
.rd-drivers { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 14px; }
.rd-driver { background: rgba(0,0,0,0.2); border: 1px solid rgba(255,255,255,0.08); border-radius: 14px; padding: 18px; }
.rd-driver .head { display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 12px; }
.rd-driver .avatar { width: 44px; height: 44px; border-radius: 50%; background: linear-gradient(135deg, #667eea, #764ba2); display: flex; align-items: center; justify-content: center; font-size: 18px; font-weight: 800; flex-shrink: 0; }
.rd-driver .info { flex: 1; margin-left: 12px; }
.rd-driver .name { font-size: 15px; font-weight: 800; }
.rd-driver .phone { font-size: 12px; color: #a0aec0; margin-top: 2px; }
.rd-driver .meta { font-size: 11px; color: #a0aec0; line-height: 1.7; padding-top: 10px; border-top: 1px solid rgba(255,255,255,0.06); }
.rd-badge { padding: 3px 10px; border-radius: 50px; font-size: 10px; font-weight: 800; text-transform: uppercase; }
.rd-badge.online { background: rgba(0,184,148,0.15); color: #00b894; }
.rd-badge.offline { background: rgba(160,174,192,0.15); color: #a0aec0; }
.rd-empty { text-align: center; padding: 40px 20px; color: #a0aec0; font-size: 13px; }
.rd-empty i { font-size: 40px; opacity: 0.3; display: block; margin-bottom: 12px; color: #a3bffa; }
.rd-app-row { padding: 14px; background: rgba(0,0,0,0.2); border-radius: 10px; margin-bottom: 8px; display: flex; justify-content: space-between; align-items: center; gap: 12px; }
.rd-app-row .name { font-weight: 700; font-size: 14px; }
.rd-app-row .detail { font-size: 12px; color: #a0aec0; margin-top: 2px; }
</style>

<div class="rd-page">
    <div class="container">

        <div class="rd-head">
            <div>
                <h1>👤 Drivers</h1>
                <p><?= htmlspecialchars($provider['name']) ?> · <?= count($drivers) ?> active driver<?= count($drivers) === 1 ? '' : 's' ?></p>
            </div>
            <div class="rd-nav">
                <a href="/?page=vendor-rides"><i class="fas fa-tachometer-alt"></i> Dashboard</a>
                <a href="/?page=vendor-rides-bookings"><i class="fas fa-calendar-check"></i> Bookings</a>
                <a href="/?page=vendor-rides-fleet"><i class="fas fa-car"></i> Fleet</a>
                <a href="/?page=vendor-rides-drivers" class="active"><i class="fas fa-user-tie"></i> Drivers</a>
                <a href="/?page=vendor-rides-reports"><i class="fas fa-chart-line"></i> Reports</a>
                <a href="/?page=vendor-rides-settings"><i class="fas fa-cog"></i> Settings</a>
            </div>
        </div>

        <?php if (!empty($pendingApps)): ?>
            <div class="rd-section" style="background: rgba(246,173,85,0.05); border-color: rgba(246,173,85,0.3);">
                <h2><i class="fas fa-user-clock" style="color: #f6ad55;"></i> Pending applications (<?= count($pendingApps) ?>)</h2>
                <?php foreach ($pendingApps as $a): ?>
                    <div class="rd-app-row">
                        <div>
                            <div class="name"><?= htmlspecialchars($a['full_name']) ?></div>
                            <div class="detail"><?= htmlspecialchars($a['phone']) ?> · License <?= htmlspecialchars($a['license_number']) ?> · Applied <?= date('M j', strtotime($a['created_at'])) ?></div>
                        </div>
                        <a href="/?page=admin-ride-applications" style="padding: 8px 14px; background: rgba(246,173,85,0.2); color: #f6ad55; border-radius: 8px; text-decoration: none; font-size: 11px; font-weight: 800;">
                            Review <i class="fas fa-arrow-right"></i>
                        </a>
                    </div>
                <?php endforeach; ?>
                <p style="font-size: 11px; color: #718096; margin-top: 8px;">Applications are reviewed by Revenda admins before approval.</p>
            </div>
        <?php endif; ?>

        <div class="rd-section">
            <h2><i class="fas fa-users" style="color: #a3bffa;"></i> Active Drivers</h2>
            <?php if (empty($drivers)): ?>
                <div class="rd-empty">
                    <i class="fas fa-user-tie"></i>
                    <p>No drivers yet. Drivers can apply via <a href="/?page=driver-register" style="color: #a3bffa;">the public registration form</a>.</p>
                </div>
            <?php else: ?>
                <div class="rd-drivers">
                    <?php foreach ($drivers as $d):
                        $initials = strtoupper(substr($d['full_name'], 0, 1));
                    ?>
                        <div class="rd-driver">
                            <div class="head">
                                <div class="avatar"><?= $initials ?></div>
                                <div class="info">
                                    <div class="name"><?= htmlspecialchars($d['full_name']) ?></div>
                                    <div class="phone"><?= htmlspecialchars($d['phone']) ?></div>
                                </div>
                                <span class="rd-badge <?= $d['is_online'] ? 'online' : 'offline' ?>"><?= $d['is_online'] ? 'Online' : 'Offline' ?></span>
                            </div>
                            <div class="meta">
                                <div>⭐ <?= number_format((float)$d['rating'], 1) ?> · <?= (int)$d['total_rides'] ?> rides</div>
                                <div>License: <?= htmlspecialchars($d['license_number'] ?: '—') ?></div>
                                <?php if ($d['license_plate']): ?>
                                    <div>Vehicle: <?= htmlspecialchars(($d['make'] ?? '') . ' ' . ($d['model'] ?? '') . ' · ' . $d['license_plate']) ?></div>
                                <?php endif; ?>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
            <?php endif; ?>
        </div>

    </div>
</div>

<?php require_once __DIR__ . '/../../includes/footer.php'; ?>