<?php
/**
 * riwayat_pencairan.php - Halaman Riwayat Pencairan Dana untuk Supir
 */
$activeMenu = 'riwayatPencairan';
$user_id = $_SESSION['user_id'] ?? 0;

// Ambil data supir
$stmt = $this->pdo->prepare("SELECT * FROM users WHERE id = ?");
$stmt->execute([$user_id]);
$supir = $stmt->fetch(PDO::FETCH_ASSOC);

// Hitung notifikasi
$unreadCount = 0;
try {
    $stmt = $this->pdo->prepare("SELECT COUNT(*) as total FROM notifications WHERE user_id = ? AND is_read = 0");
    $stmt->execute([$user_id]);
    $unreadCount = $stmt->fetch(PDO::FETCH_ASSOC)['total'] ?? 0;
} catch (Exception $e) {}

// Hitung pesanan masuk
$pesananMasuk = 0;
try {
    $stmt = $this->pdo->prepare("
        SELECT COUNT(*) as total FROM pesanan p 
        JOIN kapal k ON p.kapal_id = k.id 
        WHERE k.pemilik_id = ? AND p.status_pesanan = 'pending'
    ");
    $stmt->execute([$user_id]);
    $pesananMasuk = $stmt->fetch(PDO::FETCH_ASSOC)['total'] ?? 0;
} catch (Exception $e) {}

include __DIR__ . '/../layouts/header.php';
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Riwayat Pencairan - Reservasi Kapal</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/all.min.css">
    <style>
        :root {
            --primary: #0f3b5e;
            --primary-light: #1a5276;
            --primary-dark: #0a2a42;
            --secondary: #e67e22;
            --success: #22c55e;
            --danger: #ef4444;
            --warning: #f59e0b;
            --info: #3b82f6;
            --gray-50: #f8fafc;
            --gray-100: #f1f5f9;
            --gray-200: #e2e8f0;
            --gray-300: #cbd5e1;
            --gray-400: #94a3b8;
            --gray-500: #64748b;
            --gray-600: #475569;
            --gray-700: #334155;
            --gray-800: #1e293b;
            --gray-900: #0f172a;
            --shadow: 0 4px 20px rgba(0,0,0,0.08);
            --radius: 12px;
            --transition: all 0.3s ease;
        }

        * { margin: 0; padding: 0; box-sizing: border-box; }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: var(--gray-50);
            color: var(--gray-800);
            display: flex;
            min-height: 100vh;
        }

        /* ========== SIDEBAR ========== */
        .sidebar {
            width: 250px;
            background: linear-gradient(180deg, var(--primary-dark), var(--primary));
            color: #fff;
            min-height: 100vh;
            padding: 1.5rem 0;
            position: sticky;
            top: 0;
            height: 100vh;
            overflow-y: auto;
            flex-shrink: 0;
            z-index: 100;
        }

        .sidebar-brand {
            padding: 0 1.5rem 1.5rem;
            border-bottom: 1px solid rgba(255,255,255,0.1);
            margin-bottom: 1.5rem;
        }

        .sidebar-brand h2 {
            font-size: 1.2rem;
            font-weight: 700;
            display: flex;
            align-items: center;
            gap: 0.5rem;
            margin: 0;
        }

        .sidebar-brand h2 i { color: var(--secondary); }

        .sidebar-user {
            padding: 0 1.5rem 1.5rem;
            border-bottom: 1px solid rgba(255,255,255,0.1);
            margin-bottom: 1rem;
            display: flex;
            align-items: center;
            gap: 0.8rem;
        }

        .sidebar-user .avatar {
            width: 42px;
            height: 42px;
            border-radius: 50%;
            background: var(--secondary);
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: 700;
            font-size: 1rem;
            color: #fff;
            flex-shrink: 0;
            overflow: hidden;
        }

        .sidebar-user .avatar img {
            width: 100%;
            height: 100%;
            object-fit: cover;
        }

        .sidebar-user .user-info .name {
            font-weight: 600;
            font-size: 0.9rem;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
        }

        .sidebar-user .user-info .role {
            font-size: 0.7rem;
            opacity: 0.7;
        }

        .sidebar-menu {
            list-style: none;
            padding: 0 0.8rem;
        }

        .sidebar-menu li { margin-bottom: 0.2rem; }

        .sidebar-menu li a {
            display: flex;
            align-items: center;
            gap: 0.8rem;
            padding: 0.7rem 1rem;
            color: rgba(255,255,255,0.7);
            text-decoration: none;
            border-radius: 8px;
            transition: var(--transition);
            font-size: 0.85rem;
        }

        .sidebar-menu li a:hover,
        .sidebar-menu li a.active {
            background: rgba(255,255,255,0.15);
            color: #fff;
        }

        .sidebar-menu li a i { width: 20px; text-align: center; }

        .sidebar-menu li a .badge {
            margin-left: auto;
            background: var(--danger);
            color: #fff;
            padding: 0.1rem 0.5rem;
            border-radius: 20px;
            font-size: 0.6rem;
            font-weight: 600;
        }

        .sidebar-menu .divider {
            height: 1px;
            background: rgba(255,255,255,0.1);
            margin: 0.8rem 0;
        }

        .sidebar-menu .logout a { color: rgba(255,255,255,0.5); }
        .sidebar-menu .logout a:hover { color: #fff; background: rgba(239,68,68,0.2); }

        /* ========== MAIN CONTENT ========== */
        .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 {
            margin-bottom: 1.5rem;
        }

        .content-header h2 {
            font-size: 1.4rem;
            font-weight: 700;
            color: #1a202c;
            margin: 0;
            display: flex;
            align-items: center;
            gap: 0.8rem;
        }

        .content-header h2 i { color: var(--primary); }

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

        .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;
            text-transform: uppercase;
            white-space: nowrap;
        }

        .badge-success { background: #d1fae5; color: #065f46; }
        .badge-warning { background: #fef3c7; color: #92400e; }
        .badge-danger { background: #fee2e2; color: #991b1b; }
        .badge-info { background: #dbeafe; color: #1e40af; }

        .table-wrapper {
            background: #fff;
            border-radius: 12px;
            overflow-x: auto;
            border: 1px solid #e2e8f0;
        }

        .table-header {
            padding: 0.8rem 1.2rem;
            border-bottom: 1px solid #e2e8f0;
            display: flex;
            justify-content: space-between;
            align-items: center;
            flex-wrap: wrap;
            gap: 0.5rem;
        }

        .table-header h3 {
            font-size: 1rem;
            font-weight: 600;
            color: #2d3748;
            display: flex;
            align-items: center;
            gap: 0.5rem;
            margin: 0;
        }

        .table-count {
            font-size: 0.85rem;
            color: #718096;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            font-size: 0.9rem;
            min-width: 700px;
        }

        table th {
            background: #f7fafc;
            color: #4a5568;
            font-weight: 600;
            padding: 0.7rem 1rem;
            text-align: left;
            border-bottom: 2px solid #e2e8f0;
            font-size: 0.75rem;
            text-transform: uppercase;
            letter-spacing: 0.3px;
        }

        table td {
            padding: 0.7rem 1rem;
            border-bottom: 1px solid #e2e8f0;
            vertical-align: middle;
        }

        table tr:hover td {
            background: #f7fafc;
        }

        .empty-state {
            text-align: center;
            padding: 2.5rem 1.5rem;
            background: #f7fafc;
            border-radius: 12px;
        }

        .empty-state .empty-icon {
            font-size: 3.5rem;
            display: block;
            margin-bottom: 0.5rem;
            opacity: 0.5;
        }

        .empty-state h3 {
            font-size: 1.2rem;
            font-weight: 700;
            color: #2d3748;
            margin-bottom: 0.2rem;
        }

        .empty-state p {
            color: #718096;
            font-size: 0.9rem;
            margin: 0;
        }

        .stat-total {
            background: #ebf4ff;
            padding: 1rem 1.5rem;
            border-radius: 12px;
            margin-bottom: 1.5rem;
            display: inline-block;
            border: 1px solid #dbeafe;
        }

        .stat-total .label {
            color: #4a5568;
            font-size: 0.85rem;
            display: flex;
            align-items: center;
            gap: 0.3rem;
        }

        .stat-total .value {
            font-size: 1.5rem;
            font-weight: 700;
            color: #2b6cb0;
        }

        /* ========== RESPONSIVE ========== */
        @media (max-width: 768px) {
            .sidebar {
                width: 60px;
                padding: 1rem 0;
                position: fixed;
                left: 0;
                top: 0;
                height: 100vh;
                z-index: 1000;
                overflow-y: auto;
            }
            
            .sidebar-brand h2 span,
            .sidebar-user .user-info, 
            .sidebar-menu li a span { 
                display: none; 
            }
            
            .sidebar-brand { 
                padding: 0 0.5rem 1rem; 
                text-align: center; 
            }
            
            .sidebar-brand h2 i {
                font-size: 1.5rem;
            }
            
            .sidebar-user { 
                padding: 0 0.5rem 1rem; 
                justify-content: center; 
            }
            
            .sidebar-user .avatar { 
                width: 36px; 
                height: 36px; 
                font-size: 0.8rem; 
            }
            
            .sidebar-menu li a { 
                padding: 0.6rem; 
                justify-content: center; 
            }
            
            .sidebar-menu li a i { 
                margin: 0; 
                font-size: 1.1rem; 
            }
            
            .sidebar-menu li a .badge { 
                display: none; 
            }
            
            .dashboard-wrapper {
                flex-direction: column;
                padding: 0 12px;
                margin-top: 1rem;
            }

            .main-content {
                padding: 1rem;
            }

            .content-header {
                flex-direction: column;
                align-items: flex-start;
            }

            table {
                font-size: 0.8rem;
                min-width: 600px;
            }

            table th,
            table td {
                padding: 0.4rem 0.6rem;
            }

            .stat-total {
                width: 100%;
            }
        }

        @media (max-width: 480px) {
            .main-content {
                padding: 0.8rem;
            }

            .content-header h2 {
                font-size: 1.1rem;
            }

            table {
                font-size: 0.7rem;
                min-width: 500px;
            }

            table th,
            table td {
                padding: 0.3rem 0.4rem;
            }

            .badge {
                font-size: 0.55rem;
                padding: 0.1rem 0.4rem;
            }

            .stat-total .value {
                font-size: 1.2rem;
            }
        }
    </style>
</head>
<body>

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

    <main class="main-content">
        <div class="content-header">
            <h2><i class="fas fa-money-bill-wave" style="color:#2b6cb0;"></i> Riwayat Pencairan</h2>
            <p>Riwayat pencairan dana yang telah dilakukan.</p>
        </div>

        <div class="stat-total">
            <div class="label"><i class="fas fa-money-bill-wave"></i> Total Dana Dicairkan</div>
            <div class="value">Rp <?= number_format($totalDicairkan ?? 0, 0, ',', '.') ?></div>
        </div>

        <?php if (empty($riwayat)): ?>
            <div class="table-wrapper">
                <div class="empty-state">
                    <span class="empty-icon">💰</span>
                    <h3>Belum Ada Pencairan</h3>
                    <p>Belum ada riwayat pencairan dana.</p>
                </div>
            </div>
        <?php else: ?>
            <div class="table-wrapper">
                <div class="table-header">
                    <h3><i class="fas fa-list"></i> Daftar Pencairan</h3>
                    <span class="table-count">Total: <?= count($riwayat) ?> pencairan</span>
                </div>
                <table>
                    <thead>
                        <tr>
                            <th>#</th>
                            <th>Kode Pesanan</th>
                            <th>Kapal</th>
                            <th>Tanggal Berangkat</th>
                            <th>Nominal</th>
                            <th>Tanggal Cair</th>
                            <th>Status</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach($riwayat as $i => $r): ?>
                            <tr>
                                <td><?= $i + 1 ?></td>
                                <td><strong><?= htmlspecialchars($r['kode_pesanan'] ?? '-') ?></strong></td>
                                <td><?= htmlspecialchars($r['nama_kapal'] ?? '-') ?></td>
                                <td><?= date('d/m/Y', strtotime($r['tanggal_berangkat'] ?? $r['created_at'])) ?></td>
                                <td><strong>Rp <?= number_format($r['nominal_diterima_supir'] ?? 0, 0, ',', '.') ?></strong></td>
                                <td><?= isset($r['tanggal_pencairan']) && $r['tanggal_pencairan'] != '0000-00-00 00:00:00' ? date('d/m/Y H:i', strtotime($r['tanggal_pencairan'])) : '-' ?></td>
                                <td>
                                    <span class="badge badge-success"><i class="fas fa-check-circle"></i> Cair</span>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        <?php endif; ?>
    </main>
</div>

</body>
</html>

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