<?php
/**
 * jadwal.php - Halaman Jadwal Kapal (Supir)
 * Mengikuti template dashboard
 */
include __DIR__ . '/../layouts/header.php';
$activeMenu = 'jadwal';

if (!isset($unreadCount)) $unreadCount = 0;
if (!isset($kapalList)) $kapalList = [];
if (!isset($schedules)) $schedules = [];
if (!isset($routes)) $routes = [];
?>

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

    <main class="main-content">
        <div class="content-header">
            <h2>📅 Jadwal Kapal</h2>
            <p>Atur jadwal keberangkatan kapal Anda dengan mudah.</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; ?>

        <!-- ===== FORM TAMBAH JADWAL ===== -->
        <?php if(empty($kapalList)): ?>
            <div class="alert alert-info">Anda belum memiliki kapal terverifikasi. Silakan daftarkan kapal dan tunggu verifikasi admin.</div>
        <?php else: ?>
            <div class="form-card">
                <h3>➕ Tambah Jadwal</h3>
                <form method="POST" action="" id="formTambahJadwal">
                    <input type="hidden" name="tambah_jadwal" value="1">
                    
                    <div class="form-grid">
                        <div class="form-group">
                            <label>Pilih Kapal <span class="required">*</span></label>
                            <select name="kapal_id" required>
                                <option value="">-- Pilih Kapal --</option>
                                <?php foreach($kapalList as $k): ?>
                                    <option value="<?= $k['id'] ?>"><?= htmlspecialchars($k['nama_kapal']) ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <div class="form-group">
                            <label>Pilih Rute <span class="required">*</span></label>
                            <select name="route_id" required>
                                <option value="">-- Pilih Rute --</option>
                                <?php foreach($routes as $r): ?>
                                    <option value="<?= $r['id'] ?>"><?= htmlspecialchars($r['asal']) ?> → <?= htmlspecialchars($r['tujuan']) ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <div class="form-group">
                            <label>Tanggal <span class="required">*</span></label>
                            <input type="date" name="tanggal" min="<?= date('Y-m-d') ?>" required>
                        </div>
                        <div class="form-group">
                            <label>Jam Berangkat <span class="required">*</span></label>
                            <input type="time" name="jam_berangkat" required>
                        </div>
                        <div class="form-group">
                            <label>Kuota Tersedia <span class="required">*</span></label>
                            <input type="number" name="kuota_tersedia" min="1" placeholder="Jumlah kursi" required>
                        </div>
                        <div class="form-group">
                            <label>Harga (Rp) <span class="required">*</span></label>
                            <input type="number" name="harga_jual" min="1" placeholder="50000" required>
                            <small style="color:var(--gray-400); font-size:0.7rem;">Harga per orang untuk jadwal ini.</small>
                        </div>
                    </div>

                    <div class="form-actions">
                        <button type="submit" class="btn btn-primary">💾 Simpan Jadwal</button>
                    </div>
                </form>
            </div>
        <?php endif; ?>

        <!-- ===== DAFTAR JADWAL ===== -->
        <?php if(empty($schedules)): ?>
            <div class="empty-state">
                <div class="empty-icon">📅</div>
                <h3>Belum Ada Jadwal</h3>
                <p>Tambahkan jadwal baru di atas untuk kapal Anda.</p>
            </div>
        <?php else: ?>
            <div class="table-section">
                <div class="table-header">
                    <h3>📋 Daftar Jadwal</h3>
                    <span class="table-count">Total: <?= count($schedules) ?> jadwal</span>
                </div>
                <div class="table-wrapper">
                    <table>
                        <thead>
                            <tr>
                                <th>#</th>
                                <th>Kapal</th>
                                <th>Rute</th>
                                <th>Tanggal</th>
                                <th>Jam</th>
                                <th>Kuota</th>
                                <th>Harga</th>
                                <th>Status</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach($schedules as $i => $s): 
                                $statusBadge = match($s['status']) {
                                    'aktif' => 'badge-success',
                                    'delay' => 'badge-warning',
                                    'batal' => 'badge-danger',
                                    'selesai' => 'badge-secondary',
                                    default => 'badge-pending'
                                };
                                $statusText = match($s['status']) {
                                    'aktif' => '✅ Aktif',
                                    'delay' => '⏳ Delay',
                                    'batal' => '❌ Batal',
                                    'selesai' => '🎉 Selesai',
                                    default => '⏳ Pending'
                                };
                            ?>
                                <tr>
                                    <td><?= $i+1 ?></td>
                                    <td><strong><?= htmlspecialchars($s['nama_kapal']) ?></strong></td>
                                    <td><?= htmlspecialchars($s['asal']) ?> → <?= htmlspecialchars($s['tujuan']) ?></td>
                                    <td><?= date('d/m/Y', strtotime($s['tanggal'])) ?></td>
                                    <td><?= htmlspecialchars($s['jam_berangkat']) ?></td>
                                    <td><?= $s['kuota_tersedia'] ?> org</td>
                                    <td style="font-weight:600; color:var(--primary);">Rp <?= number_format($s['harga_jual'], 0, ',', '.') ?></td>
                                    <td><span class="badge <?= $statusBadge ?>"><?= $statusText ?></span></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);
    }
    .alert-info {
        background: #dbeafe;
        color: #1e40af;
        border-left-color: var(--primary);
    }

    .form-card {
        background: #fff;
        border-radius: 14px;
        padding: 1.5rem;
        border: 1px solid var(--gray-200);
        margin-bottom: 1.5rem;
    }
    .form-card h3 {
        font-size: 1.1rem;
        font-weight: 700;
        color: var(--gray-900);
        margin-bottom: 0.8rem;
    }
    .form-grid {
        display: grid;
        grid-template-columns: 1fr 1fr 1fr;
        gap: 0.8rem;
        margin-bottom: 0.8rem;
    }
    .form-group {
        margin-bottom: 0.5rem;
    }
    .form-group label {
        display: block;
        font-weight: 600;
        font-size: 0.85rem;
        color: var(--gray-700);
        margin-bottom: 0.2rem;
    }
    .form-group .required {
        color: var(--danger);
    }
    .form-group input,
    .form-group select {
        width: 100%;
        padding: 0.5rem 0.7rem;
        border: 1.5px solid var(--gray-200);
        border-radius: 6px;
        font-size: 0.9rem;
        background: #fff;
        transition: var(--transition);
        font-family: inherit;
    }
    .form-group input:focus,
    .form-group select:focus {
        outline: none;
        border-color: var(--primary);
        box-shadow: 0 0 0 3px rgba(15,59,94,0.08);
    }
    .form-group small {
        display: block;
        color: var(--gray-400);
        font-size: 0.7rem;
        margin-top: 0.2rem;
    }

    .form-actions {
        display: flex;
        gap: 0.8rem;
        margin-top: 0.5rem;
        flex-wrap: wrap;
    }

    .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 {
        display: inline-block;
        padding: 0.15rem 0.6rem;
        border-radius: 20px;
        font-size: 0.65rem;
        font-weight: 600;
        text-transform: uppercase;
    }
    .badge-success {
        background: #d1fae5;
        color: #065f46;
    }
    .badge-warning {
        background: #fef3c7;
        color: #92400e;
    }
    .badge-danger {
        background: #fee2e2;
        color: #991b1b;
    }
    .badge-secondary {
        background: #e2e8f0;
        color: #475569;
    }
    .badge-pending {
        background: #fef3c7;
        color: #92400e;
    }

    .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;
    }

    .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;
    }

    @media (max-width: 768px) {
        .dashboard-wrapper {
            flex-direction: column;
            padding: 0 12px;
            margin-top: 1rem;
        }
        .main-content {
            padding: 1rem;
        }
        .form-grid {
            grid-template-columns: 1fr 1fr;
            gap: 0.5rem;
        }
        .table-header {
            flex-direction: column;
            align-items: flex-start;
        }
        table {
            font-size: 0.8rem;
        }
        table th,
        table td {
            padding: 0.4rem 0.6rem;
        }
    }

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

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