<?php
/**
 * pesanan_masuk.php - Halaman Pesanan Masuk (Supir)
 * Menampilkan daftar pesanan yang menunggu verifikasi dari supir.
 * Mengikuti template dashboard
 */
include __DIR__ . '/../layouts/header.php';
$activeMenu = 'pesananMasuk';

if (!isset($unreadCount)) $unreadCount = 0;
if (!isset($pesanan)) $pesanan = [];
?>

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

    <main class="main-content">
        <div class="content-header">
            <h2>📩 Pesanan Masuk</h2>
            <p>Daftar pesanan yang menunggu verifikasi dari Anda. Segera verifikasi untuk memproses pesanan.</p>
        </div>

        <!-- FLASH MESSAGE -->
        <?php if(isset($_SESSION['flash_message'])): ?>
            <div class="alert alert-success">✅ <?= htmlspecialchars($_SESSION['flash_message']) ?><?php unset($_SESSION['flash_message']); ?></div>
        <?php endif; ?>
        <?php if(isset($_SESSION['error_message'])): ?>
            <div class="alert alert-danger">❌ <?= htmlspecialchars($_SESSION['error_message']) ?><?php unset($_SESSION['error_message']); ?></div>
        <?php endif; ?>

        <!-- ===== DAFTAR PESANAN ===== -->
        <?php if(empty($pesanan)): ?>
            <div class="empty-state">
                <div class="empty-icon">📭</div>
                <h3>Tidak Ada Pesanan Masuk</h3>
                <p>Belum ada pesanan baru yang masuk untuk kapal Anda. Tenang, kami akan memberi notifikasi jika ada pesanan.</p>
                <a href="?page=supir&action=dashboard" class="btn btn-primary" style="margin-top:1rem;">Kembali ke Dashboard</a>
            </div>
        <?php else: ?>
            <!-- Statistik singkat -->
            <div class="stats-mini">
                <div class="stat-mini" style="border-color:#f59e0b;">
                    <span class="stat-mini-number" style="color:#f59e0b;"><?= count($pesanan) ?></span>
                    <span class="stat-mini-label">Total Pesanan</span>
                </div>
                <div class="stat-mini" style="border-color:#10b981;">
                    <span class="stat-mini-number" style="color:#10b981;">
                        <?php 
                            $private = array_filter($pesanan, function($p) { 
                                return $p['jenis_booking'] == 'private'; 
                            });
                            echo count($private);
                        ?>
                    </span>
                    <span class="stat-mini-label">Private</span>
                </div>
                <div class="stat-mini" style="border-color:#8b5cf6;">
                    <span class="stat-mini-number" style="color:#8b5cf6;">
                        <?php 
                            $shared = array_filter($pesanan, function($p) { 
                                return $p['jenis_booking'] == 'shared'; 
                            });
                            echo count($shared);
                        ?>
                    </span>
                    <span class="stat-mini-label">Shared Trip</span>
                </div>
            </div>

            <div class="table-section">
                <div class="table-header">
                    <h3>📋 Daftar Pesanan Masuk</h3>
                    <span class="table-count">Total: <?= count($pesanan) ?> pesanan</span>
                </div>
                <div class="table-wrapper">
                    <table>
                        <thead>
                            <tr>
                                <th>#</th>
                                <th>Kode Pesanan</th>
                                <th>Wisatawan</th>
                                <th>Kapal</th>
                                <th>Tanggal</th>
                                <th>Durasi</th>
                                <th>Jumlah</th>
                                <th>Total</th>
                                <th>Tipe</th>
                                <th>Aksi</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach($pesanan as $i => $p): ?>
                                <tr>
                                    <td><?= $i+1 ?></td>
                                    <td>
                                        <strong><?= htmlspecialchars($p['kode_pesanan']) ?></strong>
                                        <?php if($p['jenis_booking'] == 'shared'): ?>
                                            <br><span class="badge badge-shared">🔄 Shared</span>
                                        <?php endif; ?>
                                    </td>
                                    <td>
                                        <?= htmlspecialchars($p['wisatawan'] ?? '-') ?>
                                        <?php if(!empty($p['no_hp'])): ?>
                                            <br><span style="font-size:0.7rem; color:var(--gray-400);">📱 <?= htmlspecialchars($p['no_hp']) ?></span>
                                        <?php endif; ?>
                                    </td>
                                    <td><strong><?= htmlspecialchars($p['nama_kapal']) ?></strong></td>
                                    <td><?= date('d/m/Y', strtotime($p['tanggal_berangkat'])) ?></td>
                                    <td><?= $p['durasi_jam'] ?> jam</td>
                                    <td><?= $p['jumlah_tiket'] ?> org</td>
                                    <td style="font-weight:600; color:var(--primary);">Rp <?= number_format($p['total_harga'], 0, ',', '.') ?></td>
                                    <td>
                                        <?php if($p['jenis_booking'] == 'private'): ?>
                                            <span class="badge badge-private">🚤 Private</span>
                                        <?php else: ?>
                                            <span class="badge badge-shared">🔄 Shared</span>
                                        <?php endif; ?>
                                    </td>
                                    <td>
                                        <a href="?page=supir&action=verifikasiPesanan&id=<?= $p['id'] ?>" class="btn btn-sm btn-primary">🔍 Verifikasi</a>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        <?php endif; ?>
    </main>
</div>

<style>
    .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 {
        margin-bottom: 1.5rem;
    }
    .content-header h2 {
        font-size: 1.4rem;
        font-weight: 700;
        color: var(--gray-900);
        margin-bottom: 0.2rem;
    }
    .content-header p {
        color: var(--gray-500);
        font-size: 0.95rem;
    }

    .alert {
        padding: 0.8rem 1.2rem;
        border-radius: 10px;
        margin-bottom: 1.2rem;
        font-weight: 500;
        border-left: 4px solid transparent;
    }
    .alert-success {
        background: #d1fae5;
        color: #065f46;
        border-left-color: var(--success);
    }
    .alert-danger {
        background: #fee2e2;
        color: #991b1b;
        border-left-color: var(--danger);
    }

    /* ===== STATS MINI ===== */
    .stats-mini {
        display: flex;
        gap: 1rem;
        margin-bottom: 1.5rem;
        flex-wrap: wrap;
    }
    .stat-mini {
        background: #fff;
        border: 1px solid var(--gray-200);
        border-radius: 10px;
        padding: 0.8rem 1.5rem;
        text-align: center;
        flex: 1;
        min-width: 100px;
        border-top: 3px solid var(--primary);
    }
    .stat-mini .stat-mini-number {
        display: block;
        font-size: 1.4rem;
        font-weight: 700;
        color: var(--gray-900);
    }
    .stat-mini .stat-mini-label {
        font-size: 0.75rem;
        color: var(--gray-500);
    }

    /* ===== TABLE ===== */
    .table-section {
        margin-top: 0.5rem;
    }
    .table-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 1rem;
        flex-wrap: wrap;
        gap: 0.5rem;
    }
    .table-header h3 {
        font-size: 1.05rem;
        font-weight: 700;
        color: var(--gray-800);
        margin: 0;
    }
    .table-count {
        font-size: 0.85rem;
        color: var(--gray-500);
    }
    .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-private {
        background: #dbeafe;
        color: #1e40af;
    }
    .badge-shared {
        background: #fef3c7;
        color: #92400e;
    }

    /* ===== EMPTY STATE ===== */
    .empty-state {
        text-align: center;
        padding: 2.5rem 1.5rem;
        background: var(--gray-50);
        border-radius: 14px;
        border: 1px solid var(--gray-200);
    }
    .empty-state .empty-icon {
        font-size: 3.5rem;
        display: block;
        margin-bottom: 0.5rem;
    }
    .empty-state h3 {
        font-size: 1.2rem;
        font-weight: 700;
        color: var(--gray-800);
        margin-bottom: 0.2rem;
    }
    .empty-state p {
        color: var(--gray-500);
        font-size: 0.9rem;
        margin: 0;
    }

    /* ===== BUTTON ===== */
    .btn {
        display: inline-flex;
        align-items: center;
        gap: 0.4rem;
        padding: 0.4rem 1rem;
        border: none;
        border-radius: 6px;
        font-weight: 600;
        font-size: 0.8rem;
        cursor: pointer;
        transition: var(--transition);
        text-decoration: none;
        white-space: nowrap;
    }
    .btn:hover {
        transform: translateY(-1px);
        box-shadow: 0 4px 12px rgba(0,0,0,0.08);
        text-decoration: none;
    }
    .btn-primary {
        background: var(--primary);
        color: #fff;
    }
    .btn-primary:hover {
        background: var(--primary-light);
        color: #fff;
    }
    .btn-sm {
        padding: 0.2rem 0.6rem;
        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-mini {
            flex-direction: column;
        }
        .stat-mini {
            min-width: auto;
        }
        .table-header {
            flex-direction: column;
            align-items: flex-start;
        }
        table {
            font-size: 0.8rem;
        }
        table th,
        table td {
            padding: 0.4rem 0.6rem;
        }
        .btn-sm {
            font-size: 0.6rem;
            padding: 0.15rem 0.4rem;
        }
    }

    @media (max-width: 480px) {
        .main-content {
            padding: 0.8rem;
        }
        .empty-state {
            padding: 1.5rem 1rem;
        }
        .empty-state .empty-icon {
            font-size: 2.5rem;
        }
        .btn {
            font-size: 0.7rem;
            padding: 0.3rem 0.7rem;
        }
    }
</style>

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