<?php
session_start();
require_once '../config/database.php';

if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'admin') {
    header('Location: ../admin_login.php');
    exit();
}

$jadwal = $conn->query("
    SELECT s.*, k.nama_kapal, r.asal, r.tujuan 
    FROM schedules s 
    JOIN kapal k ON s.kapal_id = k.id 
    JOIN routes r ON s.route_id = r.id 
    ORDER BY s.tanggal DESC, s.jam_berangkat ASC
");
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Jadwal Kapal - Admin</title>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body { font-family: 'Inter', sans-serif; background: #f0f2f5; }
        /* Sidebar */
        .sidebar {
            position: fixed;
            left: 0;
            top: 0;
            width: 280px;
            height: 100%;
            background: linear-gradient(135deg, #1e3c72 0%, #2a5298 100%);
            color: white;
            transition: all 0.3s;
            z-index: 1000;
            box-shadow: 2px 0 10px rgba(0,0,0,0.1);
        }

        .sidebar-header {
            padding: 30px 20px;
            text-align: center;
            border-bottom: 1px solid rgba(255,255,255,0.1);
        }

        .sidebar-header i {
            font-size: 50px;
            margin-bottom: 10px;
        }

        .sidebar-header h3 {
            font-size: 20px;
            margin-top: 10px;
        }

        .sidebar-header p {
            font-size: 12px;
            opacity: 0.8;
            margin-top: 5px;
        }

        .sidebar-menu {
            padding: 20px 0;
        }

        .menu-item {
            padding: 12px 25px;
            margin: 5px 15px;
            border-radius: 12px;
            display: flex;
            align-items: center;
            gap: 15px;
            color: rgba(255,255,255,0.8);
            text-decoration: none;
            transition: all 0.3s;
        }

        .menu-item:hover, .menu-item.active {
            background: rgba(255,255,255,0.2);
            color: white;
            transform: translateX(5px);
        }

        .menu-item i {
            width: 24px;
            font-size: 18px;
        }
        .main-content { margin-left: 280px; padding: 20px 30px; }
        .top-bar {
            background: white; border-radius: 15px; padding: 15px 25px;
            margin-bottom: 25px; display: flex; justify-content: space-between;
            align-items: center;
        }
        .card { background: white; border-radius: 20px; padding: 25px; }
        .card-title { font-size: 20px; font-weight: 600; margin-bottom: 20px; border-bottom: 1px solid #e0e0e0; padding-bottom: 15px; }
        table { width: 100%; border-collapse: collapse; }
        th, td { padding: 12px; text-align: left; border-bottom: 1px solid #e0e0e0; }
        th { background: #f8f9fa; }
        .logout-btn { background: #dc2626; color: white; padding: 8px 16px; border-radius: 10px; text-decoration: none; }
        .status { padding: 4px 10px; border-radius: 20px; font-size: 11px; }
        .status-aktif { background: #d1fae5; color: #059669; }
    </style>
</head>
<body>
    <!-- Sidebar -->
    <div class="sidebar">
            <div class="sidebar-header">
                <i class="fas fa-user-shield"></i>
                <h3>Admin Panel</h3>
                <p>Samosir Wisata</p>
            </div>
            <div class="sidebar-menu">
                <a href="dashboard.php" class="menu-item">
                    <i class="fas fa-tachometer-alt"></i> Dashboard
                </a>
                <a href="kelola_kapal.php" class="menu-item">
                    <i class="fas fa-ship"></i> Kelola Kapal
                </a>
                <a href="verifikasi_kapal.php" class="menu-item">
                    <i class="fas fa-check-circle"></i> Verifikasi Kapal
                </a>
                <a href="kelola_pesanan.php" class="menu-item">
                    <i class="fas fa-ticket-alt"></i> Kelola Pesanan
                </a>
                <a href="jadwal.php" class="menu-item active">
                    <i class="fas fa-calendar-alt"></i> Jadwal Kapal
                </a>
                <a href="kelola_user.php" class="menu-item">
                    <i class="fas fa-users"></i> Kelola User
                </a>
                <a href="laporan.php" class="menu-item">
                    <i class="fas fa-chart-line"></i> Laporan
                </a>
            </div>
        </div>

    <div class="main-content">
        <div class="top-bar"><h2>Jadwal Keberangkatan Kapal</h2><a href="../logout.php" class="logout-btn"><i class="fas fa-sign-out-alt"></i> Logout</a></div>
        <div class="card">
            <div class="card-title"><i class="fas fa-calendar-alt"></i> Daftar Jadwal</div>
            <div class="table-responsive">
                <table>
                    <thead><tr><th>Kapal</th><th>Rute</th><th>Tanggal</th><th>Jam</th><th>Kuota</th><th>Status</th></tr></thead>
                    <tbody>
                        <?php while($row = $jadwal->fetch_assoc()): ?>
                        <tr>
                            <td><?php echo $row['nama_kapal']; ?></td>
                            <td><?php echo $row['asal']; ?> → <?php echo $row['tujuan']; ?></td>
                            <td><?php echo date('d/m/Y', strtotime($row['tanggal'])); ?></td>
                            <td><?php echo date('H:i', strtotime($row['jam_berangkat'])); ?> WIB</td>
                            <td><?php echo $row['kuota_tersedia']; ?> kursi</td>
                            <td><span class="status status-<?php echo $row['status']; ?>"><?php echo ucfirst($row['status']); ?></span></td>
                        </tr>
                        <?php endwhile; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</body>
</html>