<?php
/**
 * dashboard.php - Halaman Dashboard Supir
 * Menampilkan ringkasan data kapal, pesanan, dan statistik.
 */
include __DIR__ . '/../layouts/header.php';
$activeMenu = 'dashboard';

// ===== FALLBACK DATA =====
if (!isset($unreadCount)) $unreadCount = 0;
if (!isset($kapal)) $kapal = [];
if (!isset($totalKapal)) $totalKapal = count($kapal);
if (!isset($totalPending)) {
    $totalPending = 0;
    foreach ($kapal as $k) {
        if ($k['status_verifikasi'] == 'pending') $totalPending++;
    }
}
if (!isset($totalTerverifikasi)) {
    $totalTerverifikasi = 0;
    foreach ($kapal as $k) {
        if ($k['status_verifikasi'] == 'verified') $totalTerverifikasi++;
    }
}
$totalPesanan = $totalPesanan ?? 0;
$pesananTerbaru = $pesananTerbaru ?? [];
?>

<div class="dashboard-wrapper">
    <?php include __DIR__ . '/../layouts/sidebar_supir.php'; ?>

    <main class="main-content">
        <!-- HEADER -->
        <div class="content-header">
            <div>
                <h1>👋 Selamat datang, <?= htmlspecialchars($_SESSION['nama'] ?? 'Pengguna') ?></h1>
                <p>Kelola kapal dan pesanan Anda dengan mudah.</p>
            </div>
            <div class="header-actions">
                <span class="date-badge">📅 <?= date('d F Y') ?></span>
            </div>
        </div>

        <!-- ===== STATISTIK ===== -->
        <div class="stats-grid">
            <div class="stat-card blue">
                <div class="stat-icon">⛵</div>
                <div>
                    <div class="stat-number"><?= $totalKapal ?></div>
                    <div class="stat-label">Total Kapal</div>
                </div>
            </div>
            <div class="stat-card yellow">
                <div class="stat-icon">⏳</div>
                <div>
                    <div class="stat-number"><?= $totalPending ?></div>
                    <div class="stat-label">Menunggu Verifikasi</div>
                </div>
            </div>
            <div class="stat-card green">
                <div class="stat-icon">✅</div>
                <div>
                    <div class="stat-number"><?= $totalTerverifikasi ?></div>
                    <div class="stat-label">Terverifikasi</div>
                </div>
            </div>
            <div class="stat-card gray">
                <div class="stat-icon">📩</div>
                <div>
                    <div class="stat-number"><?= $totalPesanan ?></div>
                    <div class="stat-label">Pesanan Masuk</div>
                </div>
            </div>
        </div>

        <!-- ===== AKSI CEPAT ===== -->
        <div class="quick-actions-section">
            <h3>🚀 Aksi Cepat</h3>
            <div class="quick-actions-grid">
                <a href="?page=supir&action=kelolaKapal" class="quick-action-card">
                    <span class="quick-icon">➕</span>
                    <div>
                        <h4>Tambah Kapal</h4>
                        <p>Daftarkan kapal baru</p>
                    </div>
                </a>
                <a href="?page=supir&action=pesananMasuk" class="quick-action-card">
                    <span class="quick-icon">📩</span>
                    <div>
                        <h4>Pesanan Masuk</h4>
                        <p>Lihat & verifikasi</p>
                    </div>
                </a>
                <a href="?page=supir&action=sharedTrip" class="quick-action-card">
                    <span class="quick-icon">🔄</span>
                    <div>
                        <h4>Shared Trip</h4>
                        <p>Buat shared trip</p>
                    </div>
                </a>
                <a href="?page=supir&action=jadwal" class="quick-action-card">
                    <span class="quick-icon">📅</span>
                    <div>
                        <h4>Jadwal</h4>
                        <p>Atur jadwal kapal</p>
                    </div>
                </a>
            </div>
        </div>

        <!-- ===== PESANAN TERBARU ===== -->
        <?php if(!empty($pesananTerbaru)): ?>
            <div class="table-section">
                <div class="table-header">
                    <h3>📋 Pesanan Terbaru</h3>
                    <a href="?page=supir&action=pesananMasuk" class="btn btn-sm btn-secondary">Lihat Semua →</a>
                </div>
                <div class="table-wrapper">
                    <table>
                        <thead>
                            <tr>
                                <th>Kode</th>
                                <th>Wisatawan</th>
                                <th>Kapal</th>
                                <th>Tanggal</th>
                                <th>Total</th>
                                <th>Status</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach($pesananTerbaru as $p): 
                                $badge = match($p['status_pesanan']) {
                                    'pending' => 'badge-pending',
                                    'confirmed' => 'badge-confirmed',
                                    'completed' => 'badge-completed',
                                    'cancelled' => 'badge-danger',
                                    default => 'badge-pending'
                                };
                                $statusText = match($p['status_pesanan']) {
                                    'pending' => '⏳ Pending',
                                    'confirmed' => '✅ Dikonfirmasi',
                                    'completed' => '🎉 Selesai',
                                    'cancelled' => '❌ Dibatalkan',
                                    default => '⏳ Pending'
                                };
                            ?>
                                <tr>
                                    <td><strong><?= htmlspecialchars($p['kode_pesanan']) ?></strong></td>
                                    <td><?= htmlspecialchars($p['wisatawan'] ?? '-') ?></td>
                                    <td><?= htmlspecialchars($p['nama_kapal']) ?></td>
                                    <td><?= date('d/m/Y', strtotime($p['tanggal_berangkat'] ?? $p['created_at'])) ?></td>
                                    <td style="font-weight:600; color:var(--primary);">Rp <?= number_format($p['total_harga'], 0, ',', '.') ?></td>
                                    <td><span class="badge <?= $badge ?>"><?= $statusText ?></span></td>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        <?php endif; ?>

        <!-- ===== TIPS ===== -->
        <div class="tips-section">
            <h3>💡 Tips untuk Supir Kapal</h3>
            <div class="tips-grid">
                <div class="tip-card">
                    <span class="tip-icon">✅</span>
                    <div>
                        <h4>Verifikasi Kapal</h4>
                        <p>Pastikan data kapal lengkap agar cepat diverifikasi admin.</p>
                    </div>
                </div>
                <div class="tip-card">
                    <span class="tip-icon">💰</span>
                    <div>
                        <h4>Harga Kompetitif</h4>
                        <p>Atur harga sesuai rute dan fasilitas untuk menarik wisatawan.</p>
                    </div>
                </div>
                <div class="tip-card">
                    <span class="tip-icon">📱</span>
                    <div>
                        <h4>Respons Cepat</h4>
                        <p>Segera verifikasi pesanan yang masuk untuk kepuasan pelanggan.</p>
                    </div>
                </div>
                <div class="tip-card">
                    <span class="tip-icon">🔄</span>
                    <div>
                        <h4>Shared Trip</h4>
                        <p>Buat shared trip untuk mengisi kapasitas kosong dan meningkatkan pendapatan.</p>
                    </div>
                </div>
            </div>
        </div>
    </main>
</div>

<style>
    /* ===== LAYOUT ===== */
    .dashboard-wrapper {
        display: flex;
        gap: 24px;
        max-width: 1200px;
        margin: 1.5rem auto 0;
        padding: 0 20px;
        width: 100%;
        flex: 1;
        align-items: flex-start;
    }

    .main-content {
        flex: 1;
        min-width: 0;
        background: #fff;
        border-radius: 14px;
        padding: 1.8rem;
        box-shadow: 0 4px 20px rgba(0,0,0,0.06);
        border: 1px solid var(--gray-200);
    }

    .content-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        flex-wrap: wrap;
        gap: 1rem;
        margin-bottom: 1.5rem;
    }
    .content-header h1 {
        font-size: 1.5rem;
        font-weight: 700;
        color: var(--gray-900);
        margin-bottom: 0.2rem;
    }
    .content-header p {
        color: var(--gray-500);
        font-size: 0.95rem;
        margin: 0;
    }
    .date-badge {
        background: var(--gray-100);
        padding: 0.3rem 1rem;
        border-radius: 20px;
        font-size: 0.8rem;
        color: var(--gray-600);
        white-space: nowrap;
    }

    /* ===== STATS ===== */
    .stats-grid {
        display: grid;
        grid-template-columns: repeat(4, 1fr);
        gap: 1rem;
        margin-bottom: 1.5rem;
    }
    .stat-card {
        background: #fff;
        padding: 1rem 1.2rem;
        border-radius: 12px;
        box-shadow: 0 2px 12px rgba(0,0,0,0.04);
        display: flex;
        align-items: center;
        gap: 1rem;
        border-left: 4px solid var(--primary);
        border: 1px solid var(--gray-200);
        transition: var(--transition);
    }
    .stat-card:hover {
        transform: translateY(-2px);
        box-shadow: 0 8px 30px rgba(0,0,0,0.06);
    }
    .stat-card .stat-icon {
        font-size: 1.8rem;
        opacity: 0.6;
    }
    .stat-card .stat-number {
        font-size: 1.6rem;
        font-weight: 700;
        color: var(--gray-900);
        line-height: 1.2;
    }
    .stat-card .stat-label {
        color: var(--gray-500);
        font-size: 0.75rem;
        font-weight: 500;
    }
    .stat-card.blue { border-left-color: var(--primary); }
    .stat-card.yellow { border-left-color: var(--secondary); }
    .stat-card.green { border-left-color: var(--success); }
    .stat-card.gray { border-left-color: var(--gray-400); }

    /* ===== QUICK ACTIONS ===== */
    .quick-actions-section {
        background: var(--gray-50);
        border-radius: 14px;
        padding: 1.2rem 1.5rem;
        margin-bottom: 1.5rem;
        border: 1px solid var(--gray-200);
    }
    .quick-actions-section h3 {
        font-size: 1.05rem;
        font-weight: 700;
        color: var(--gray-800);
        margin-bottom: 0.8rem;
    }
    .quick-actions-grid {
        display: grid;
        grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
        gap: 0.8rem;
    }
    .quick-action-card {
        display: flex;
        align-items: center;
        gap: 0.8rem;
        padding: 0.8rem 1rem;
        background: #fff;
        border-radius: 10px;
        border: 1px solid var(--gray-200);
        transition: var(--transition);
        text-decoration: none;
    }
    .quick-action-card:hover {
        border-color: var(--primary);
        transform: translateY(-2px);
        box-shadow: 0 4px 12px rgba(0,0,0,0.04);
        text-decoration: none;
    }
    .quick-action-card .quick-icon {
        font-size: 1.8rem;
    }
    .quick-action-card h4 {
        font-size: 0.9rem;
        font-weight: 600;
        color: var(--gray-800);
        margin: 0;
    }
    .quick-action-card p {
        font-size: 0.75rem;
        color: var(--gray-500);
        margin: 0;
    }

    /* ===== TABLE ===== */
    .table-section {
        margin-bottom: 1.5rem;
    }
    .table-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 0.8rem;
        flex-wrap: wrap;
        gap: 0.5rem;
    }
    .table-header h3 {
        font-size: 1.05rem;
        font-weight: 700;
        color: var(--gray-800);
        margin: 0;
    }
    .table-wrapper {
        background: #fff;
        border-radius: 14px;
        overflow-x: auto;
        border: 1px solid var(--gray-200);
    }
    table {
        width: 100%;
        border-collapse: collapse;
        font-size: 0.9rem;
    }
    table th {
        background: var(--gray-50);
        color: var(--gray-700);
        font-weight: 600;
        padding: 0.7rem 1rem;
        text-align: left;
        border-bottom: 2px solid var(--gray-200);
        font-size: 0.75rem;
        text-transform: uppercase;
        letter-spacing: 0.3px;
    }
    table td {
        padding: 0.7rem 1rem;
        border-bottom: 1px solid var(--gray-100);
        vertical-align: middle;
    }
    table tr:hover td {
        background: var(--gray-50);
    }

    /* ===== BADGE ===== */
    .badge {
        display: inline-block;
        padding: 0.15rem 0.6rem;
        border-radius: 20px;
        font-size: 0.65rem;
        font-weight: 600;
        text-transform: uppercase;
    }
    .badge-pending { background: #fef3c7; color: #92400e; }
    .badge-confirmed { background: #dbeafe; color: #1e40af; }
    .badge-completed { background: #d1fae5; color: #065f46; }
    .badge-danger { background: #fee2e2; color: #991b1b; }

    /* ===== TIPS ===== */
    .tips-section {
        background: var(--gray-50);
        border-radius: 14px;
        padding: 1.2rem 1.5rem;
        border: 1px solid var(--gray-200);
    }
    .tips-section h3 {
        font-size: 1.05rem;
        font-weight: 700;
        color: var(--gray-800);
        margin-bottom: 0.8rem;
    }
    .tips-grid {
        display: grid;
        grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
        gap: 0.8rem;
    }
    .tip-card {
        display: flex;
        gap: 0.8rem;
        padding: 0.6rem 0.8rem;
        background: #fff;
        border-radius: 10px;
        border: 1px solid var(--gray-200);
        transition: var(--transition);
    }
    .tip-card:hover {
        border-color: var(--primary);
    }
    .tip-card .tip-icon {
        font-size: 1.3rem;
        flex-shrink: 0;
        margin-top: 2px;
    }
    .tip-card h4 {
        font-size: 0.85rem;
        font-weight: 600;
        color: var(--gray-800);
        margin-bottom: 0.1rem;
    }
    .tip-card p {
        font-size: 0.8rem;
        color: var(--gray-500);
        margin: 0;
    }

    /* ===== BUTTON ===== */
    .btn {
        display: inline-flex;
        align-items: center;
        gap: 0.4rem;
        padding: 0.4rem 0.8rem;
        border: none;
        border-radius: 6px;
        font-weight: 600;
        font-size: 0.8rem;
        cursor: pointer;
        transition: var(--transition);
        text-decoration: none;
    }
    .btn:hover {
        transform: translateY(-1px);
        box-shadow: 0 4px 12px rgba(0,0,0,0.08);
        text-decoration: none;
    }
    .btn-secondary {
        background: var(--gray-200);
        color: var(--gray-700);
    }
    .btn-secondary:hover {
        background: var(--gray-300);
        color: var(--gray-800);
    }
    .btn-sm {
        padding: 0.2rem 0.5rem;
        font-size: 0.7rem;
    }

    /* ===== RESPONSIVE ===== */
    @media (max-width: 768px) {
        .dashboard-wrapper {
            flex-direction: column;
            padding: 0 12px;
            margin-top: 1rem;
        }
        .main-content {
            padding: 1rem;
        }
        .stats-grid {
            grid-template-columns: 1fr 1fr;
            gap: 0.7rem;
        }
        .stat-card .stat-number {
            font-size: 1.2rem;
        }
        .quick-actions-grid {
            grid-template-columns: 1fr 1fr;
        }
        .tips-grid {
            grid-template-columns: 1fr;
        }
        table {
            font-size: 0.8rem;
        }
        table th, table td {
            padding: 0.4rem 0.6rem;
        }
        .content-header {
            flex-direction: column;
            align-items: flex-start;
        }
    }

    @media (max-width: 480px) {
        .stats-grid {
            grid-template-columns: 1fr;
        }
        .quick-actions-grid {
            grid-template-columns: 1fr;
        }
        .main-content {
            padding: 0.8rem;
        }
        .quick-action-card {
            padding: 0.6rem 0.8rem;
        }
        .quick-action-card .quick-icon {
            font-size: 1.4rem;
        }
        .tips-grid {
            grid-template-columns: 1fr;
        }
        .date-badge {
            font-size: 0.7rem;
            padding: 0.2rem 0.7rem;
        }
    }
</style>

<?php include __DIR__ . '/../layouts/footer.php'; ?>