<?php
include __DIR__ . '/../layouts/header.php';
$activeMenu = 'request_nonaktifkan';
?>

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

    <main class="main-content">
        <div class="content-header">
            <h2>⛔ Request Nonaktifkan Kapal</h2>
            <p>Kelola request nonaktifkan kapal dari supir.</p>
        </div>

        <?php if(isset($_SESSION['flash_success'])): ?>
            <div class="alert alert-success">✅ <?= htmlspecialchars($_SESSION['flash_success']) ?></div>
            <?php unset($_SESSION['flash_success']); ?>
        <?php endif; ?>

        <?php if(isset($_SESSION['flash_error'])): ?>
            <div class="alert alert-danger">❌ <?= htmlspecialchars($_SESSION['flash_error']) ?></div>
            <?php unset($_SESSION['flash_error']); ?>
        <?php endif; ?>

        <!-- Request Menunggu -->
        <?php if(!empty($requests)): ?>
            <div class="card">
                <h3>📋 Request Menunggu</h3>
                <div class="table-responsive">
                    <table class="table">
                        <thead>
                            <tr>
                                <th>Kapal</th>
                                <th>Supir</th>
                                <th>No HP</th>
                                <th>Tanggal Request</th>
                                <th>Aksi</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach($requests as $req): ?>
                            <tr>
                                <td>
                                    <strong><?= htmlspecialchars($req['nama_kapal']) ?></strong><br>
                                    <small><?= ucfirst($req['jenis']) ?> | <?= $req['kapasitas'] ?> org</small>
                                </td>
                                <td><?= htmlspecialchars($req['pemilik_nama']) ?></td>
                                <td><?= htmlspecialchars($req['pemilik_no_hp'] ?? '-') ?></td>
                                <td><?= date('d/m/Y H:i', strtotime($req['hapus_request_at'])) ?></td>
                                <td>
                                    <form method="POST" action="?page=admin&action=prosesRequestNonaktifkan" style="display:flex; gap:0.5rem; flex-wrap:wrap;">
                                        <input type="hidden" name="kapal_id" value="<?= $req['id'] ?>">
                                        
                                        <!-- Tombol Terima -->
                                        <div>
                                            <input type="hidden" name="action" value="terima">
                                            <input type="text" name="alasan" placeholder="Alasan nonaktif" required class="form-control" style="width:140px; padding:0.2rem 0.5rem; font-size:0.8rem;">
                                            <button type="submit" class="btn btn-sm btn-danger" onclick="return confirm('Yakin menyetujui nonaktifkan kapal ini?')">
                                                ⛔ Terima
                                            </button>
                                        </div>
                                        
                                        <!-- Tombol Tolak -->
                                        <div>
                                            <input type="hidden" name="action" value="tolak">
                                            <input type="text" name="alasan" placeholder="Alasan tolak" required class="form-control" style="width:140px; padding:0.2rem 0.5rem; font-size:0.8rem;">
                                            <button type="submit" class="btn btn-sm btn-secondary" onclick="return confirm('Yakin menolak request ini?')">
                                                ❌ Tolak
                                            </button>
                                        </div>
                                    </form>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        <?php else: ?>
            <div class="alert alert-info">📭 Tidak ada request nonaktifkan yang menunggu.</div>
        <?php endif; ?>

        <!-- Riwayat Request -->
        <?php if(!empty($history)): ?>
            <div class="card" style="margin-top:2rem;">
                <h3>📜 Riwayat Request</h3>
                <div class="table-responsive">
                    <table class="table">
                        <thead>
                            <tr>
                                <th>Kapal</th>
                                <th>Supir</th>
                                <th>Status</th>
                                <th>Tanggal</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach($history as $h): ?>
                            <tr>
                                <td><?= htmlspecialchars($h['nama_kapal']) ?></td>
                                <td><?= htmlspecialchars($h['pemilik_nama']) ?></td>
                                <td>
                                    <?php if($h['hapus_request'] == 'disetujui'): ?>
                                        <span class="badge badge-danger">⛔ Dinonaktifkan</span>
                                    <?php else: ?>
                                        <span class="badge badge-secondary">❌ Ditolak</span>
                                    <?php endif; ?>
                                </td>
                                <td><?= date('d/m/Y H:i', strtotime($h['hapus_request_at'])) ?></td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        <?php endif; ?>
    </main>
</div>

<style>
    .badge-success { background: #d1fae5; color: #065f46; padding:0.2rem 0.6rem; border-radius:4px; font-size:0.75rem; }
    .badge-warning { background: #fef3c7; color: #92400e; padding:0.2rem 0.6rem; border-radius:4px; font-size:0.75rem; }
    .badge-danger { background: #fee2e2; color: #991b1b; padding:0.2rem 0.6rem; border-radius:4px; font-size:0.75rem; }
    .badge-secondary { background: #e5e7eb; color: #4b5563; padding:0.2rem 0.6rem; border-radius:4px; font-size:0.75rem; }
    
    .btn-sm { padding: 0.2rem 0.6rem; font-size: 0.75rem; border-radius: 4px; cursor: pointer; border: none; }
    .btn-danger { background: #dc2626; color: #fff; }
    .btn-danger:hover { background: #b91c1c; }
    .btn-secondary { background: #6b7280; color: #fff; }
    .btn-secondary:hover { background: #4b5563; }
    .form-control { border: 1px solid #d1d5db; border-radius: 4px; }
    
    .table { width: 100%; border-collapse: collapse; }
    .table th, .table td { padding: 0.5rem 0.8rem; border-bottom: 1px solid #e5e7eb; text-align: left; }
    .table th { background: #f3f4f6; font-weight: 600; }
</style>

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