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

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

$active_page = 'verifikasi_kapal';

// Proses verifikasi (setujui / tolak)
if (isset($_GET['approve']) && is_numeric($_GET['approve'])) {
    $id = intval($_GET['approve']);
    $conn->query("UPDATE kapal SET status_verifikasi = 'verified' WHERE id = $id");
    header('Location: verifikasi_kapal.php');
    exit();
}
if (isset($_GET['reject']) && is_numeric($_GET['reject'])) {
    $id = intval($_GET['reject']);
    $conn->query("UPDATE kapal SET status_verifikasi = 'rejected' WHERE id = $id");
    header('Location: verifikasi_kapal.php');
    exit();
}

// Ambil kapal yang statusnya pending (atau semua jika kolom status_verifikasi tidak ada)
// Cek apakah kolom status_verifikasi ada
$checkColumn = $conn->query("SHOW COLUMNS FROM kapal LIKE 'status_verifikasi'");
if ($checkColumn && $checkColumn->num_rows > 0) {
    $kapal = $conn->query("SELECT * FROM kapal WHERE status_verifikasi = 'pending' ORDER BY id DESC");
} else {
    // Jika kolom tidak ada, tampilkan semua kapal sebagai pending (agar bisa diverifikasi)
    $kapal = $conn->query("SELECT * FROM kapal ORDER BY id DESC");
}

$total_pending = $kapal->num_rows;
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <title>Verifikasi Kapal - Admin Samosir Wisata</title>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800&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{position:fixed;left:0;top:0;width:280px;height:100%;background:linear-gradient(135deg,#1e3c72,#2a5298);color:#fff}.sidebar-header{padding:30px 20px;text-align:center;border-bottom:1px solid rgba(255,255,255,0.1)}.sidebar-header i{font-size:50px}.sidebar-header h3{font-size:20px;margin-top:10px}.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}.menu-item.active,.menu-item:hover{background:rgba(255,255,255,0.2);color:#fff}.main-content{margin-left:280px;padding:20px 30px}.top-bar{background:#fff;border-radius:15px;padding:15px 25px;margin-bottom:25px;display:flex;justify-content:space-between;align-items:center}.page-title h2{font-size:24px;color:#333}.user-avatar{width:45px;height:45px;background:linear-gradient(135deg,#667eea,#764ba2);border-radius:50%;display:flex;align-items:center;justify-content:center;color:#fff;font-weight:bold}.logout-btn{background:#dc2626;color:#fff;padding:8px 16px;border-radius:10px;text-decoration:none}.welcome-card{background:linear-gradient(135deg,#1e3c72,#2a5298);border-radius:20px;padding:25px 30px;margin-bottom:30px;color:#fff;display:flex;justify-content:space-between;flex-wrap:wrap}.stats-grid{display:grid;grid-template-columns:repeat(auto-fit,minmax(240px,1fr));gap:20px;margin-bottom:30px}.stat-card{background:#fff;padding:20px;border-radius:20px;position:relative}.stat-card::before{content:'';position:absolute;top:0;left:0;width:100%;height:4px;background:linear-gradient(135deg,#1e3c72,#2a5298);border-radius:20px 20px 0 0}.stat-value{font-size:28px;font-weight:700}.card{background:#fff;border-radius:20px;padding:20px;margin-bottom:25px}.card-header{border-bottom:1px solid #e0e0e0;padding-bottom:15px;margin-bottom:20px}.card-title{font-size:18px;font-weight:600}table{width:100%;border-collapse:collapse}th,td{padding:12px;border-bottom:1px solid #e0e0e0;text-align:left}.btn-approve{background:#d1fae5;color:#059669;padding:5px 12px;border-radius:6px;text-decoration:none;display:inline-block;margin-right:5px}.btn-reject{background:#fee2e2;color:#dc2626;padding:5px 12px;border-radius:6px;text-decoration:none}@media(max-width:768px){.sidebar{transform:translateX(-100%)}.main-content{margin-left:0}}
    </style>
</head>
<body>
<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 active"><i class="fas fa-check-circle"></i> Verifikasi Kapal</a>
        <a href="request_hapus_kapal.php" class="menu-item"><i class="fas fa-trash-alt"></i> Request Hapus</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"><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">
        <div class="page-title"><h2>Verifikasi Kapal</h2><p><i class="fas fa-check-circle"></i> Verifikasi kapal wisata sebelum diaktifkan</p></div>
        <div class="user-info"><div class="user-avatar">A</div><span>Admin Utama</span><a href="../logout.php" class="logout-btn"><i class="fas fa-sign-out-alt"></i> Logout</a></div>
    </div>
    <div class="welcome-card">
        <div class="welcome-text"><h1>Verifikasi Kapal ⚓</h1><p>Setujui atau tolak kapal yang didaftarkan oleh pemilik</p></div>
        <div class="welcome-date"><div class="date"><i class="fas fa-calendar-alt"></i> <?= date('l, d F Y') ?></div><div class="time"><i class="fas fa-clock"></i> <?= date('H:i') ?> WIB</div></div>
    </div>
    <div class="stats-grid">
        <div class="stat-card"><div class="stat-header"><div class="stat-value"><?= $total_pending ?></div><i class="fas fa-clock"></i></div><div class="stat-label">Menunggu Verifikasi</div></div>
    </div>
    <div class="card">
        <div class="card-header"><div class="card-title"><i class="fas fa-list"></i> Daftar Kapal Perlu Verifikasi</div></div>
        <div class="table-responsive">
            <table>
                <thead><tr><th>No</th><th>Nama Kapal</th><th>Jenis</th><th>Kapasitas</th><th>Harga/Tiket</th><th>Aksi</th></tr></thead>
                <tbody>
                    <?php if ($kapal && $kapal->num_rows > 0): $no=1; while($row=$kapal->fetch_assoc()): ?>
                    <tr>
                        <td><?= $no++ ?></td>
                        <td><strong><?= htmlspecialchars($row['nama_kapal'] ?? '') ?></strong></td>
                        <td><?= ucfirst($row['jenis'] ?? '') ?></td>
                        <td><?= $row['kapasitas'] ?? 0 ?> orang</td>
                        <td>Rp <?= number_format($row['harga_per_tiket'] ?? 50000,0,',','.') ?></td>
                        <td>
                            <a href="?approve=<?= $row['id'] ?>" class="btn-approve" onclick="return confirm('Setujui kapal <?= htmlspecialchars($row['nama_kapal'] ?? '') ?>?')"><i class="fas fa-check"></i> Setujui</a>
                            <a href="?reject=<?= $row['id'] ?>" class="btn-reject" onclick="return confirm('Tolak kapal <?= htmlspecialchars($row['nama_kapal'] ?? '') ?>?')"><i class="fas fa-times"></i> Tolak</a>
                        </td>
                    </tr>
                    <?php endwhile; else: ?>
                    <tr><td colspan="6" style="text-align:center">Tidak ada kapal yang menunggu verifikasi.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>
</body>
</html>