<?php
/**
 * detail_pesanan.php - Halaman Detail Pesanan Admin
 */
$activeMenu = 'kelolaPesanan';
$pageTitle = 'Detail Pesanan - Admin';
include __DIR__ . '/../layouts/header.php';

// Data dari controller
$pesanan = $pesanan ?? [];
$notifications = $notifications ?? [];
$unreadCount = $unreadCount ?? 0;

if (empty($pesanan)) {
    ?>
    <div class="alert alert-danger">
        <i class="fas fa-exclamation-circle"></i>
        Pesanan tidak ditemukan.
    </div>
    <a href="?page=admin&action=kelolaPesanan" class="btn btn-primary">← Kembali ke Daftar Pesanan</a>
    <?php
    include __DIR__ . '/../layouts/footer.php';
    exit;
}

// Mapping status
$statusMap = [
    'pending' => ['badge-warning', 'Menunggu Verifikasi', 'fa-clock'],
    'confirmed' => ['badge-success', 'Dikonfirmasi', 'fa-check-circle'],
    'completed' => ['badge-info', 'Selesai', 'fa-flag-checkered'],
    'cancelled' => ['badge-danger', 'Dibatalkan', 'fa-times-circle'],
    'dikonfirmasi' => ['badge-success', 'Dikonfirmasi', 'fa-check-circle'],
    'selesai' => ['badge-info', 'Selesai', 'fa-flag-checkered'],
    'dibatalkan' => ['badge-danger', 'Dibatalkan', 'fa-times-circle'],
];

$paymentMap = [
    'belum_bayar' => ['badge-warning', 'Belum Bayar', 'fa-clock'],
    'menunggu_konfirmasi' => ['badge-warning', 'Menunggu Konfirmasi', 'fa-hourglass-half'],
    'lunas' => ['badge-success', 'Lunas', 'fa-check-circle'],
    'gagal' => ['badge-danger', 'Gagal', 'fa-times-circle'],
];

$status = $statusMap[$pesanan['status_pesanan'] ?? 'pending'] ?? ['badge-secondary', ucfirst($pesanan['status_pesanan'] ?? 'Unknown'), 'fa-circle'];
$payment = $paymentMap[$pesanan['status_pembayaran'] ?? 'belum_bayar'] ?? ['badge-secondary', ucfirst($pesanan['status_pembayaran'] ?? 'Unknown'), 'fa-circle'];
?>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/all.min.css">

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

    <main class="main-content">
        <!-- HEADER -->
        <div class="content-header">
            <div>
                <h2><i class="fas fa-file-invoice" style="color:#2b6cb0;"></i> Detail Pesanan</h2>
                <p>Detail lengkap pesanan kapal.</p>
            </div>
            <a href="?page=admin&action=kelolaPesanan" class="btn btn-secondary">
                <i class="fas fa-arrow-left"></i> Kembali
            </a>
        </div>

        <!-- FLASH MESSAGES -->
        <?php if(isset($_SESSION['flash_success'])): ?>
            <div class="alert alert-success">
                <i class="fas fa-check-circle"></i>
                <?= htmlspecialchars($_SESSION['flash_success']) ?>
                <button class="alert-close" onclick="this.parentElement.remove()">&times;</button>
                <?php unset($_SESSION['flash_success']); ?>
            </div>
        <?php endif; ?>

        <?php if(isset($_SESSION['flash_error'])): ?>
            <div class="alert alert-danger">
                <i class="fas fa-exclamation-circle"></i>
                <?= htmlspecialchars($_SESSION['flash_error']) ?>
                <button class="alert-close" onclick="this.parentElement.remove()">&times;</button>
                <?php unset($_SESSION['flash_error']); ?>
            </div>
        <?php endif; ?>

        <!-- ========================================== -->
        <!-- STATUS BANNER -->
        <!-- ========================================== -->
        <div class="status-banner">
            <div class="status-icon">
                <i class="fas <?= $status[2] ?>" style="font-size:2rem;"></i>
            </div>
            <div class="status-info">
                <div class="status-label">Status Pesanan</div>
                <div class="status-value">
                    <span class="badge <?= $status[0] ?>" style="font-size:1rem; padding:0.3rem 1rem;">
                        <i class="fas <?= $status[2] ?>"></i> <?= $status[1] ?>
                    </span>
                    <span class="badge <?= $payment[0] ?>" style="font-size:1rem; padding:0.3rem 1rem; margin-left:0.5rem;">
                        <i class="fas <?= $payment[2] ?>"></i> <?= $payment[1] ?>
                    </span>
                </div>
            </div>
            <div class="status-code">
                <small>Kode: <strong><?= htmlspecialchars($pesanan['kode_pesanan'] ?? '-') ?></strong></small>
            </div>
        </div>

        <!-- ========================================== -->
        <!-- DETAIL GRID -->
        <!-- ========================================== -->
        <div class="detail-grid">
            <!-- Kiri: Informasi Pesanan -->
            <div class="detail-left">
                <div class="info-card">
                    <h3><i class="fas fa-info-circle"></i> Informasi Pesanan</h3>
                    
                    <div class="info-row">
                        <span class="info-label">Kode Pesanan</span>
                        <span class="info-value"><strong><?= htmlspecialchars($pesanan['kode_pesanan'] ?? '-') ?></strong></span>
                    </div>
                    
                    <div class="info-row">
                        <span class="info-label">Kapal</span>
                        <span class="info-value"><strong><?= htmlspecialchars($pesanan['nama_kapal'] ?? '-') ?></strong></span>
                    </div>
                    
                    <div class="info-row">
                        <span class="info-label">Tanggal Berangkat</span>
                        <span class="info-value"><?= isset($pesanan['tanggal_berangkat']) ? date('d/m/Y', strtotime($pesanan['tanggal_berangkat'])) : '-' ?></span>
                    </div>
                    
                    <div class="info-row">
                        <span class="info-label">Durasi Sewa</span>
                        <span class="info-value"><?= $pesanan['durasi_hari'] ?? ceil(($pesanan['durasi_jam'] ?? 24) / 24) ?> hari</span>
                    </div>
                    
                    <div class="info-row">
                        <span class="info-label">Jumlah Penumpang</span>
                        <span class="info-value"><?= $pesanan['jumlah_tiket'] ?? 1 ?> orang</span>
                    </div>
                    
                    <div class="info-row">
                        <span class="info-label">Total Harga</span>
                        <span class="info-value harga">Rp <?= number_format($pesanan['total_harga'] ?? 0, 0, ',', '.') ?></span>
                    </div>
                    
                    <div class="info-row">
                        <span class="info-label">Rute</span>
                        <span class="info-value"><?= htmlspecialchars($pesanan['route_asal'] ?? '-') ?> → <?= htmlspecialchars($pesanan['route_tujuan'] ?? '-') ?></span>
                    </div>
                    
                    <?php if(!empty($pesanan['catatan'])): ?>
                    <div class="info-row">
                        <span class="info-label">Catatan</span>
                        <span class="info-value"><?= htmlspecialchars($pesanan['catatan']) ?></span>
                    </div>
                    <?php endif; ?>
                    
                    <?php if(!empty($pesanan['payment_type'])): ?>
                    <div class="info-row">
                        <span class="info-label">Metode Pembayaran</span>
                        <span class="info-value">
                            <span class="badge badge-info" style="background:#dbeafe; color:#1e40af;">
                                <i class="fas fa-credit-card"></i> <?= strtoupper($pesanan['payment_type']) ?>
                            </span>
                        </span>
                    </div>
                    <?php endif; ?>
                </div>

                <!-- Informasi Kapal -->
                <div class="info-card">
                    <h3><i class="fas fa-ship"></i> Informasi Kapal</h3>
                    
                    <div class="info-row">
                        <span class="info-label">Nama Kapal</span>
                        <span class="info-value"><strong><?= htmlspecialchars($pesanan['nama_kapal'] ?? '-') ?></strong></span>
                    </div>
                    
                    <div class="info-row">
                        <span class="info-label">Pelabuhan Asal</span>
                        <span class="info-value"><?= htmlspecialchars($pesanan['pelabuhan_asal'] ?? '-') ?></span>
                    </div>
                    
                    <div class="info-row">
                        <span class="info-label">Pelabuhan Tujuan</span>
                        <span class="info-value"><?= htmlspecialchars($pesanan['pelabuhan_tujuan'] ?? '-') ?></span>
                    </div>
                    
                    <div class="info-row">
                        <span class="info-label">Pemilik Kapal</span>
                        <span class="info-value"><?= htmlspecialchars($pesanan['pemilik_nama'] ?? '-') ?></span>
                    </div>
                    
                    <?php if(!empty($pesanan['pemilik_hp'])): ?>
                    <div class="info-row">
                        <span class="info-label">Kontak Pemilik</span>
                        <span class="info-value"><?= htmlspecialchars($pesanan['pemilik_hp']) ?></span>
                    </div>
                    <?php endif; ?>
                </div>
            </div>

            <!-- Kanan: Data Wisatawan -->
            <div class="detail-right">
                <div class="info-card">
                    <h3><i class="fas fa-user"></i> Data Wisatawan</h3>
                    
                    <div class="info-row">
                        <span class="info-label">Nama Lengkap</span>
                        <span class="info-value"><strong><?= htmlspecialchars($pesanan['wisatawan_nama'] ?? $pesanan['nama_lengkap'] ?? '-') ?></strong></span>
                    </div>
                    
                    <div class="info-row">
                        <span class="info-label">Email</span>
                        <span class="info-value"><?= htmlspecialchars($pesanan['wisatawan_email'] ?? $pesanan['email'] ?? '-') ?></span>
                    </div>
                    
                    <div class="info-row">
                        <span class="info-label">Nomor HP</span>
                        <span class="info-value"><?= htmlspecialchars($pesanan['wisatawan_hp'] ?? $pesanan['no_hp'] ?? '-') ?></span>
                    </div>
                    
                    <div class="info-row">
                        <span class="info-label">NIK KTP</span>
                        <span class="info-value"><?= htmlspecialchars($pesanan['nik_ktp'] ?? '-') ?></span>
                    </div>
                </div>

                <!-- Informasi Keuangan -->
                <div class="info-card" style="margin-top:1rem;">
                    <h3><i class="fas fa-money-bill-wave"></i> Informasi Keuangan</h3>
                    
                    <div class="info-row">
                        <span class="info-label">Total Harga</span>
                        <span class="info-value harga">Rp <?= number_format($pesanan['total_harga'] ?? 0, 0, ',', '.') ?></span>
                    </div>
                    
                    <?php if(!empty($pesanan['komisi_admin'])): ?>
                    <div class="info-row">
                        <span class="info-label">Komisi Admin</span>
                        <span class="info-value">Rp <?= number_format($pesanan['komisi_admin'] ?? 0, 0, ',', '.') ?></span>
                    </div>
                    <?php endif; ?>
                    
                    <?php if(!empty($pesanan['nominal_diterima_supir'])): ?>
                    <div class="info-row">
                        <span class="info-label">Diterima Supir</span>
                        <span class="info-value" style="color:#22c55e; font-weight:600;">
                            Rp <?= number_format($pesanan['nominal_diterima_supir'] ?? 0, 0, ',', '.') ?>
                        </span>
                    </div>
                    <?php endif; ?>
                    
                    <?php if(!empty($pesanan['status_pencairan'])): ?>
                    <div class="info-row">
                        <span class="info-label">Status Pencairan</span>
                        <span class="info-value">
                            <?php if($pesanan['status_pencairan'] == 'cair'): ?>
                                <span class="badge badge-success">
                                    <i class="fas fa-check-circle"></i> Sudah Cair
                                </span>
                            <?php elseif($pesanan['status_pencairan'] == 'proses'): ?>
                                <span class="badge badge-warning">
                                    <i class="fas fa-hourglass-half"></i> Proses
                                </span>
                            <?php else: ?>
                                <span class="badge badge-secondary">
                                    <i class="fas fa-clock"></i> Belum Cair
                                </span>
                            <?php endif; ?>
                        </span>
                    </div>
                    <?php endif; ?>
                </div>

                <!-- Status Claim -->
                <?php if(!empty($pesanan['status_claim']) && $pesanan['status_claim'] != 'tidak'): ?>
                <div class="info-card" style="margin-top:1rem; border-left: 4px solid #f59e0b;">
                    <h3><i class="fas fa-hand-holding-usd" style="color:#f59e0b;"></i> Status Claim</h3>
                    
                    <div class="info-row">
                        <span class="info-label">Status Claim</span>
                        <span class="info-value">
                            <?php if($pesanan['status_claim'] == 'menunggu'): ?>
                                <span class="badge badge-warning">
                                    <i class="fas fa-clock"></i> Menunggu
                                </span>
                            <?php elseif($pesanan['status_claim'] == 'diterima'): ?>
                                <span class="badge badge-success">
                                    <i class="fas fa-check-circle"></i> Diterima
                                </span>
                            <?php elseif($pesanan['status_claim'] == 'ditolak'): ?>
                                <span class="badge badge-danger">
                                    <i class="fas fa-times-circle"></i> Ditolak
                                </span>
                            <?php endif; ?>
                        </span>
                    </div>
                    
                    <?php if(!empty($pesanan['alasan_claim'])): ?>
                    <div class="info-row">
                        <span class="info-label">Alasan</span>
                        <span class="info-value"><?= htmlspecialchars($pesanan['alasan_claim']) ?></span>
                    </div>
                    <?php endif; ?>
                </div>
                <?php endif; ?>
            </div>
        </div>

        <!-- ========================================== -->
        <!-- RIWAYAT NOTIFIKASI -->
        <!-- ========================================== -->
        <?php if(!empty($notifications)): ?>
        <div class="info-card" style="margin-top:1.5rem;">
            <h3><i class="fas fa-bell"></i> Riwayat Notifikasi</h3>
            <div class="notification-list">
                <?php foreach($notifications as $notif): ?>
                    <div class="notification-item">
                        <div class="notif-icon">
                            <i class="fas <?= strpos($notif['title'] ?? '', '✅') !== false ? 'fa-check-circle text-success' : (strpos($notif['title'] ?? '', '❌') !== false ? 'fa-times-circle text-danger' : 'fa-info-circle text-info') ?>"></i>
                        </div>
                        <div class="notif-content">
                            <div class="notif-title"><?= htmlspecialchars($notif['title'] ?? 'Notifikasi') ?></div>
                            <div class="notif-message"><?= nl2br(htmlspecialchars($notif['message'] ?? '')) ?></div>
                            <div class="notif-time"><?= isset($notif['created_at']) ? date('d/m/Y H:i', strtotime($notif['created_at'])) : '-' ?></div>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
        </div>
        <?php endif; ?>

        <!-- ========================================== -->
        <!-- TOMBOL AKSI -->
        <!-- ========================================== -->
        <div class="action-section" style="margin-top:1.5rem; display:flex; gap:0.5rem; flex-wrap:wrap;">
            <a href="?page=admin&action=kelolaPesanan" class="btn btn-secondary">
                <i class="fas fa-arrow-left"></i> Kembali
            </a>
            
            <?php if(($pesanan['status_pembayaran'] ?? '') == 'lunas' && ($pesanan['status_pencairan'] ?? '') == 'belum'): ?>
                <a href="?page=admin&action=pencairanDana" class="btn btn-success">
                    <i class="fas fa-money-bill-wave"></i> Cairkan Dana
                </a>
            <?php endif; ?>
            
            <?php if(($pesanan['status_claim'] ?? '') == 'menunggu'): ?>
                <a href="?page=admin&action=claimPengembalian" class="btn btn-warning">
                    <i class="fas fa-hand-holding-usd"></i> Proses Claim
                </a>
            <?php endif; ?>
        </div>
    </main>
</div>

<style>
/* ============================================
   LAYOUT
   ============================================ */
.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 #e2e8f0;
}

.content-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1.5rem;
    flex-wrap: wrap;
    gap: 10px;
}

.content-header h2 {
    font-size: 1.4rem;
    font-weight: 700;
    color: #1a202c;
    margin: 0;
}

.content-header p {
    color: #718096;
    font-size: 0.95rem;
    margin: 0;
}

.alert {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 0.8rem 1.2rem;
    border-radius: 10px;
    margin-bottom: 1rem;
    border-left: 4px solid transparent;
}

.alert-success {
    background: #d1fae5;
    color: #065f46;
    border-left-color: #22c55e;
}

.alert-danger {
    background: #fee2e2;
    color: #991b1b;
    border-left-color: #ef4444;
}

.alert-close {
    margin-left: auto;
    background: none;
    border: none;
    font-size: 1rem;
    cursor: pointer;
    color: inherit;
    opacity: 0.6;
    padding: 0 4px;
}

.alert-close:hover {
    opacity: 1;
}

/* ============================================
   STATUS BANNER
   ============================================ */
.status-banner {
    background: linear-gradient(135deg, #f7fafc, #edf2f7);
    border-radius: 12px;
    padding: 1.2rem 1.5rem;
    margin-bottom: 1.5rem;
    border: 1px solid #e2e8f0;
    display: flex;
    align-items: center;
    gap: 1rem;
    flex-wrap: wrap;
}

.status-icon {
    font-size: 2rem;
    color: #2b6cb0;
}

.status-info {
    flex: 1;
}

.status-label {
    font-size: 0.75rem;
    color: #718096;
    text-transform: uppercase;
    letter-spacing: 0.3px;
}

.status-value {
    display: flex;
    gap: 0.5rem;
    flex-wrap: wrap;
    margin-top: 0.1rem;
}

.status-code {
    font-size: 0.85rem;
    color: #718096;
}

/* ============================================
   DETAIL GRID
   ============================================ */
.detail-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1.5rem;
    margin-bottom: 1.5rem;
}

.info-card {
    background: #fff;
    border-radius: 12px;
    padding: 1.2rem 1.5rem;
    border: 1px solid #e2e8f0;
}

.info-card h3 {
    font-size: 1rem;
    font-weight: 700;
    color: #2d3748;
    margin-bottom: 0.8rem;
    padding-bottom: 0.5rem;
    border-bottom: 1px solid #e2e8f0;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.info-row {
    display: flex;
    justify-content: space-between;
    padding: 0.3rem 0;
    border-bottom: 1px solid #f7fafc;
}

.info-row:last-child {
    border-bottom: none;
}

.info-label {
    color: #718096;
    font-size: 0.85rem;
    font-weight: 500;
}

.info-value {
    font-size: 0.85rem;
    color: #1a202c;
    text-align: right;
}

.info-value.harga {
    font-weight: 700;
    color: #2b6cb0;
    font-size: 1rem;
}

/* ============================================
   BADGE
   ============================================ */
.badge {
    display: inline-flex;
    align-items: center;
    gap: 0.2rem;
    padding: 0.15rem 0.6rem;
    border-radius: 20px;
    font-size: 0.65rem;
    font-weight: 600;
}

.badge-success {
    background: #d1fae5;
    color: #065f46;
}

.badge-danger {
    background: #fee2e2;
    color: #991b1b;
}

.badge-warning {
    background: #fef3c7;
    color: #92400e;
}

.badge-info {
    background: #dbeafe;
    color: #1e40af;
}

.badge-secondary {
    background: #e5e7eb;
    color: #4b5563;
}

/* ============================================
   BUTTON
   ============================================ */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    padding: 0.5rem 1.2rem;
    border: none;
    border-radius: 8px;
    font-weight: 600;
    font-size: 0.85rem;
    cursor: pointer;
    transition: all 0.3s ease;
    text-decoration: none;
}

.btn:hover {
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(0,0,0,0.08);
}

.btn-primary {
    background: #2b6cb0;
    color: #fff;
}

.btn-primary:hover {
    background: #1a4a7a;
    color: #fff;
}

.btn-secondary {
    background: #e2e8f0;
    color: #4a5568;
}

.btn-secondary:hover {
    background: #cbd5e0;
    color: #1a202c;
}

.btn-success {
    background: #22c55e;
    color: #fff;
}

.btn-success:hover {
    background: #16a34a;
    color: #fff;
}

.btn-warning {
    background: #f59e0b;
    color: #fff;
}

.btn-warning:hover {
    background: #d97706;
    color: #fff;
}

.btn-sm {
    padding: 0.25rem 0.8rem;
    font-size: 0.75rem;
}

/* ============================================
   NOTIFICATION LIST
   ============================================ */
.notification-list {
    max-height: 300px;
    overflow-y: auto;
}

.notification-item {
    display: flex;
    gap: 0.8rem;
    padding: 0.6rem 0;
    border-bottom: 1px solid #f7fafc;
}

.notification-item:last-child {
    border-bottom: none;
}

.notif-icon {
    font-size: 1.2rem;
    flex-shrink: 0;
    margin-top: 0.1rem;
}

.text-success { color: #22c55e; }
.text-danger { color: #ef4444; }
.text-info { color: #2b6cb0; }

.notif-content {
    flex: 1;
}

.notif-title {
    font-weight: 600;
    font-size: 0.85rem;
    color: #1a202c;
}

.notif-message {
    font-size: 0.8rem;
    color: #4a5568;
    white-space: pre-wrap;
    word-break: break-word;
}

.notif-time {
    font-size: 0.65rem;
    color: #a0aec0;
    margin-top: 0.1rem;
}

/* ============================================
   ACTION SECTION
   ============================================ */
.action-section {
    display: flex;
    gap: 0.5rem;
    flex-wrap: wrap;
}

/* ============================================
   RESPONSIVE
   ============================================ */
@media (max-width: 768px) {
    .dashboard-wrapper {
        flex-direction: column;
        padding: 0 12px;
        margin-top: 1rem;
    }
    .main-content {
        padding: 1rem;
    }
    .content-header {
        flex-direction: column;
        align-items: flex-start;
    }
    .detail-grid {
        grid-template-columns: 1fr;
    }
    .status-banner {
        flex-direction: column;
        align-items: flex-start;
    }
    .info-row {
        flex-direction: column;
        align-items: flex-start;
        gap: 0.1rem;
    }
    .info-value {
        text-align: left;
    }
    .action-section {
        flex-direction: column;
    }
    .action-section .btn {
        width: 100%;
        justify-content: center;
    }
}

@media (max-width: 480px) {
    .main-content {
        padding: 0.8rem;
    }
    .info-card {
        padding: 0.8rem 1rem;
    }
}
</style>

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