<?php
/**
 * saldo.php - Halaman Saldo Wisatawan
 * DIPERBAIKI - Menampilkan saldo, riwayat, dan tombol tarik saldo
 * DITAMBAHKAN - Tombol Tarik Saldo dan informasi saldo
 */
$activeMenu = 'saldo';
$pageTitle = 'Saldo Saya - Wisatawan';
include __DIR__ . '/../layouts/header.php';

// Data dari controller
$saldo = $saldo ?? 0;
$riwayat = $riwayat ?? [];
$user = $user ?? [];
$user_id = $user_id ?? $_SESSION['user_id'] ?? 0;
$unreadCount = $unreadCount ?? 0;
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Saldo Saya - Wisatawan</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/all.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800;900&display=swap" rel="stylesheet">
    
    <style>
        /* ============================================
           ROOT VARIABLES
           ============================================ */
        :root {
            --primary: #2b6cb0;
            --primary-light: #4299e1;
            --primary-dark: #1a4a7a;
            --success: #22c55e;
            --success-bg: #d1fae5;
            --warning: #f59e0b;
            --warning-bg: #fef3c7;
            --danger: #ef4444;
            --danger-bg: #fee2e2;
            --info: #3b82f6;
            --info-bg: #dbeafe;
            --gray-50: #f7fafc;
            --gray-100: #edf2f7;
            --gray-200: #e2e8f0;
            --gray-300: #cbd5e0;
            --gray-400: #a0aec0;
            --gray-500: #718096;
            --gray-600: #4a5568;
            --gray-700: #2d3748;
            --gray-800: #1a202c;
            --gray-900: #171923;
            --shadow: 0 4px 20px rgba(0,0,0,0.06);
            --shadow-lg: 0 10px 40px rgba(0,0,0,0.1);
            --shadow-hover: 0 8px 30px rgba(0,0,0,0.12);
            --radius: 16px;
            --radius-sm: 10px;
            --transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
        }

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

        body {
            font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
            background: #f7fafc;
            color: var(--gray-800);
            line-height: 1.6;
        }

        /* ============================================
           DASHBOARD WRAPPER
           ============================================ */
        .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: #ffffff;
            border-radius: var(--radius);
            padding: 1.8rem;
            box-shadow: var(--shadow);
            border: 1px solid var(--gray-200);
            animation: fadeInUp 0.5s ease;
        }

        @keyframes fadeInUp {
            from { opacity: 0; transform: translateY(30px); }
            to { opacity: 1; transform: translateY(0); }
        }

        /* ============================================
           HEADER
           ============================================ */
        .content-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            flex-wrap: wrap;
            gap: 1rem;
            margin-bottom: 1.5rem;
            padding-bottom: 1rem;
            border-bottom: 2px solid var(--gray-100);
        }

        .content-header-left h2 {
            font-size: 1.5rem;
            font-weight: 800;
            color: var(--gray-900);
            display: flex;
            align-items: center;
            gap: 0.6rem;
        }

        .content-header-left h2 i {
            color: var(--primary);
            background: var(--primary-bg);
            padding: 0.4rem;
            border-radius: var(--radius-sm);
        }

        .content-header-left p {
            color: var(--gray-500);
            font-size: 0.9rem;
            margin-top: 0.2rem;
        }

        .header-actions {
            display: flex;
            gap: 0.6rem;
            flex-wrap: wrap;
        }

        /* ============================================
           BUTTONS
           ============================================ */
        .btn {
            display: inline-flex;
            align-items: center;
            gap: 0.5rem;
            padding: 0.5rem 1.2rem;
            border: none;
            border-radius: var(--radius-sm);
            font-weight: 600;
            font-size: 0.85rem;
            cursor: pointer;
            transition: var(--transition);
            text-decoration: none;
            font-family: inherit;
        }

        .btn:hover {
            transform: translateY(-2px);
            box-shadow: var(--shadow-lg);
        }

        .btn-primary {
            background: linear-gradient(135deg, var(--primary), var(--primary-dark));
            color: #fff;
        }

        .btn-primary:hover {
            background: linear-gradient(135deg, var(--primary-dark), #0d2b4f);
            color: #fff;
        }

        .btn-success {
            background: linear-gradient(135deg, var(--success), #16a34a);
            color: #fff;
        }

        .btn-success:hover {
            background: linear-gradient(135deg, #16a34a, #0d7a3a);
            color: #fff;
        }

        .btn-outline {
            background: transparent;
            color: var(--gray-600);
            border: 2px solid var(--gray-200);
        }

        .btn-outline:hover {
            background: var(--gray-50);
            border-color: var(--gray-400);
        }

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

        .btn-lg {
            padding: 0.7rem 1.5rem;
            font-size: 1rem;
        }

        /* ============================================
           SALDO CARD
           ============================================ */
        .saldo-card {
            background: linear-gradient(135deg, var(--primary), var(--primary-dark));
            color: #fff;
            padding: 1.8rem 2rem;
            border-radius: var(--radius);
            margin-bottom: 1.5rem;
            display: flex;
            justify-content: space-between;
            align-items: center;
            flex-wrap: wrap;
            gap: 1rem;
            box-shadow: 0 8px 30px rgba(43, 108, 176, 0.3);
        }

        .saldo-card .saldo-left {
            display: flex;
            align-items: center;
            gap: 1.2rem;
        }

        .saldo-card .saldo-icon {
            font-size: 2.5rem;
            background: rgba(255,255,255,0.15);
            width: 64px;
            height: 64px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .saldo-card .saldo-info .saldo-label {
            font-size: 0.8rem;
            opacity: 0.7;
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }

        .saldo-card .saldo-info .saldo-amount {
            font-size: 2.2rem;
            font-weight: 800;
            line-height: 1.2;
        }

        .saldo-card .saldo-actions {
            display: flex;
            gap: 0.8rem;
            flex-wrap: wrap;
        }

        .saldo-card .saldo-actions .btn {
            padding: 0.6rem 1.2rem;
            font-size: 0.85rem;
        }

        .btn-withdraw {
            background: rgba(255,255,255,0.2);
            color: #fff;
            border: 1px solid rgba(255,255,255,0.3);
        }

        .btn-withdraw:hover {
            background: rgba(255,255,255,0.3);
            color: #fff;
        }

        .btn-topup {
            background: #fff;
            color: var(--primary);
        }

        .btn-topup:hover {
            background: #f7fafc;
            color: var(--primary-dark);
        }

        /* ============================================
           STATS MINI
           ============================================ */
        .stats-mini {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
            gap: 0.8rem;
            margin-bottom: 1.5rem;
        }

        .stat-mini-item {
            background: #fff;
            padding: 0.8rem 1rem;
            border-radius: var(--radius-sm);
            border: 1px solid var(--gray-200);
            text-align: center;
            transition: var(--transition);
        }

        .stat-mini-item:hover {
            border-color: var(--primary);
            transform: translateY(-2px);
            box-shadow: var(--shadow);
        }

        .stat-mini-number {
            font-size: 1.1rem;
            font-weight: 700;
            display: block;
            line-height: 1.2;
            color: var(--gray-900);
        }

        .stat-mini-label {
            font-size: 0.6rem;
            color: var(--gray-500);
            font-weight: 600;
            text-transform: uppercase;
            letter-spacing: 0.3px;
        }

        /* ============================================
           RIWAYAT SALDO
           ============================================ */
        .riwayat-section {
            margin-top: 1.5rem;
        }

        .riwayat-section .section-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 0.8rem;
            flex-wrap: wrap;
            gap: 0.5rem;
        }

        .riwayat-section .section-header h3 {
            font-size: 1rem;
            font-weight: 700;
            color: var(--gray-800);
            display: flex;
            align-items: center;
            gap: 0.5rem;
        }

        .riwayat-section .section-header h3 i {
            color: var(--primary);
        }

        .riwayat-list {
            display: flex;
            flex-direction: column;
            gap: 0.6rem;
        }

        .riwayat-item {
            display: flex;
            align-items: center;
            gap: 1rem;
            padding: 0.8rem 1.2rem;
            background: #fff;
            border-radius: var(--radius-sm);
            border: 1px solid var(--gray-200);
            transition: var(--transition);
        }

        .riwayat-item:hover {
            border-color: var(--primary-light);
            box-shadow: var(--shadow);
        }

        .riwayat-item .riwayat-icon {
            width: 44px;
            height: 44px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 1.2rem;
            flex-shrink: 0;
        }

        .riwayat-item .riwayat-icon.masuk {
            background: var(--success-bg);
            color: #065f46;
        }

        .riwayat-item .riwayat-icon.keluar {
            background: var(--danger-bg);
            color: #991b1b;
        }

        .riwayat-item .riwayat-content {
            flex: 1;
            min-width: 0;
        }

        .riwayat-item .riwayat-content .riwayat-title {
            font-weight: 600;
            font-size: 0.9rem;
            color: var(--gray-800);
        }

        .riwayat-item .riwayat-content .riwayat-desc {
            font-size: 0.8rem;
            color: var(--gray-500);
            margin-top: 0.1rem;
        }

        .riwayat-item .riwayat-content .riwayat-time {
            font-size: 0.7rem;
            color: var(--gray-400);
            margin-top: 0.1rem;
        }

        .riwayat-item .riwayat-amount {
            font-weight: 700;
            font-size: 1rem;
            flex-shrink: 0;
            text-align: right;
        }

        .riwayat-item .riwayat-amount.masuk {
            color: #065f46;
        }

        .riwayat-item .riwayat-amount.keluar {
            color: #991b1b;
        }

        /* ============================================
           EMPTY STATE
           ============================================ */
        .empty-state {
            text-align: center;
            padding: 2rem 1.5rem;
            background: var(--gray-50);
            border-radius: var(--radius-sm);
            border: 2px dashed var(--gray-200);
        }

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

        .empty-state p {
            color: var(--gray-500);
            font-size: 0.9rem;
        }

        /* ============================================
           ALERT
           ============================================ */
        .alert {
            display: flex;
            align-items: center;
            gap: 10px;
            padding: 0.8rem 1.2rem;
            border-radius: var(--radius-sm);
            margin-bottom: 1rem;
            border-left: 4px solid transparent;
            animation: slideDown 0.4s ease;
        }

        .alert-success {
            background: var(--success-bg);
            color: #065f46;
            border-left-color: var(--success);
        }

        .alert-danger {
            background: var(--danger-bg);
            color: #991b1b;
            border-left-color: var(--danger);
        }

        .alert-close {
            margin-left: auto;
            background: none;
            border: none;
            font-size: 1.2rem;
            cursor: pointer;
            color: inherit;
            opacity: 0.5;
            padding: 0 4px;
            transition: var(--transition);
        }

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

        /* ============================================
           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;
            }

            .saldo-card {
                flex-direction: column;
                text-align: center;
                padding: 1.5rem;
            }

            .saldo-card .saldo-left {
                flex-direction: column;
            }

            .saldo-card .saldo-actions {
                width: 100%;
                justify-content: center;
            }

            .riwayat-item {
                flex-wrap: wrap;
                padding: 0.8rem 1rem;
            }

            .riwayat-item .riwayat-amount {
                width: 100%;
                text-align: left;
                margin-top: 0.2rem;
            }

            .stats-mini {
                grid-template-columns: repeat(2, 1fr);
            }
        }

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

            .saldo-card .saldo-info .saldo-amount {
                font-size: 1.6rem;
            }

            .stats-mini {
                grid-template-columns: 1fr 1fr;
                gap: 0.5rem;
            }

            .riwayat-item .riwayat-icon {
                width: 36px;
                height: 36px;
                font-size: 1rem;
            }
        }
    </style>
</head>
<body>

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

    <main class="main-content">
        <!-- HEADER -->
        <div class="content-header">
            <div class="content-header-left">
                <h2>
                    <i class="fas fa-wallet"></i>
                    Saldo Saya
                </h2>
                <p>
                    <i class="fas fa-coins"></i> 
                    Kelola saldo aplikasi Anda
                </p>
            </div>
            <div class="header-actions">
                <a href="?page=wisatawan&action=booking" class="btn btn-primary btn-sm">
                    <i class="fas fa-ship"></i> Booking
                </a>
            </div>
        </div>

        <!-- ALERT 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; ?>

        <!-- SALDO CARD -->
        <div class="saldo-card">
            <div class="saldo-left">
                <div class="saldo-icon">
                    <i class="fas fa-wallet"></i>
                </div>
                <div class="saldo-info">
                    <div class="saldo-label">Total Saldo</div>
                    <div class="saldo-amount">Rp <?= number_format($saldo, 0, ',', '.') ?></div>
                </div>
            </div>
            <div class="saldo-actions">
                <!-- 🔥 TOMBOL TARIK SALDO -->
                <a href="?page=wisatawan&action=tarikSaldo" class="btn btn-withdraw">
                    <i class="fas fa-money-bill-wave"></i> Tarik Saldo
                </a>
                <a href="?page=wisatawan&action=booking" class="btn btn-topup">
                    <i class="fas fa-ship"></i> Booking
                </a>
            </div>
        </div>

        <!-- STATS MINI -->
        <?php 
            $totalMasuk = 0;
            $totalKeluar = 0;
            foreach ($riwayat as $r) {
                if ($r['jenis'] == 'masuk') {
                    $totalMasuk += (int) $r['jumlah'];
                } else {
                    $totalKeluar += (int) $r['jumlah'];
                }
            }
        ?>
        <div class="stats-mini">
            <div class="stat-mini-item">
                <span class="stat-mini-number" style="color:#065f46;">Rp <?= number_format($totalMasuk, 0, ',', '.') ?></span>
                <span class="stat-mini-label">⬆️ Total Masuk</span>
            </div>
            <div class="stat-mini-item">
                <span class="stat-mini-number" style="color:#991b1b;">Rp <?= number_format($totalKeluar, 0, ',', '.') ?></span>
                <span class="stat-mini-label">⬇️ Total Keluar</span>
            </div>
            <div class="stat-mini-item">
                <span class="stat-mini-number" style="color:#2b6cb0;"><?= count($riwayat) ?></span>
                <span class="stat-mini-label">📋 Total Transaksi</span>
            </div>
            <div class="stat-mini-item">
                <span class="stat-mini-number" style="color:#f59e0b;">Rp <?= number_format($saldo, 0, ',', '.') ?></span>
                <span class="stat-mini-label">💰 Saldo Saat Ini</span>
            </div>
        </div>

        <!-- RIWAYAT SALDO -->
        <div class="riwayat-section">
            <div class="section-header">
                <h3><i class="fas fa-history"></i> Riwayat Saldo</h3>
                <?php if (!empty($riwayat)): ?>
                    <span style="font-size:0.8rem; color:var(--gray-500);">
                        <?= count($riwayat) ?> transaksi
                    </span>
                <?php endif; ?>
            </div>

            <?php if (empty($riwayat)): ?>
                <div class="empty-state">
                    <span class="empty-icon">📭</span>
                    <p>Belum ada riwayat saldo.</p>
                    <p style="font-size:0.85rem;">Lakukan transaksi untuk mulai mencatat saldo.</p>
                </div>
            <?php else: ?>
                <div class="riwayat-list">
                    <?php foreach ($riwayat as $r): 
                        $isMasuk = ($r['jenis'] == 'masuk');
                        $icon = $isMasuk ? 'fa-arrow-down' : 'fa-arrow-up';
                        $iconClass = $isMasuk ? 'masuk' : 'keluar';
                        $amountClass = $isMasuk ? 'masuk' : 'keluar';
                        $sign = $isMasuk ? '+' : '-';
                        $time = isset($r['created_at']) ? date('d/m/Y H:i', strtotime($r['created_at'])) : '-';
                        $keterangan = $r['keterangan'] ?? 'Transaksi';
                        $tipe = $r['tipe'] ?? '';
                        
                        // Label tipe
                        $typeLabels = [
                            'refund' => ['label' => '🔄 Refund', 'color' => '#065f46'],
                            'withdraw' => ['label' => '🏦 Penarikan', 'color' => '#991b1b'],
                            'topup' => ['label' => '💳 Top Up', 'color' => '#065f46'],
                            'booking' => ['label' => '⛵ Booking', 'color' => '#1e40af'],
                            'bonus' => ['label' => '🎁 Bonus', 'color' => '#f59e0b'],
                        ];
                        $typeLabel = $typeLabels[$tipe]['label'] ?? '📝 Transaksi';
                    ?>
                        <div class="riwayat-item">
                            <div class="riwayat-icon <?= $iconClass ?>">
                                <i class="fas <?= $icon ?>"></i>
                            </div>
                            <div class="riwayat-content">
                                <div class="riwayat-title">
                                    <?= $typeLabel ?>
                                    <span style="font-weight:400; font-size:0.8rem; color:var(--gray-500);">
                                        - <?= htmlspecialchars($keterangan) ?>
                                    </span>
                                </div>
                                <div class="riwayat-time">
                                    <i class="fas fa-clock"></i> <?= $time ?>
                                </div>
                            </div>
                            <div class="riwayat-amount <?= $amountClass ?>">
                                <?= $sign ?> Rp <?= number_format((int) $r['jumlah'], 0, ',', '.') ?>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
            <?php endif; ?>
        </div>

        <!-- INFORMASI -->
        <div style="margin-top:1.5rem; padding:1rem 1.2rem; background:#dbeafe; border-radius:10px; border-left:4px solid #2b6cb0;">
            <p style="font-size:0.85rem; color:#1e40af; margin:0;">
                <i class="fas fa-info-circle"></i> 
                <strong>Saldo dapat digunakan untuk:</strong> 
                Pembayaran booking kapal dan ditarik ke rekening bank (minimal Rp 50.000).
            </p>
        </div>
    </main>
</div>

<script>
    // Auto-hide alert setelah 5 detik
    document.addEventListener('DOMContentLoaded', function() {
        const alerts = document.querySelectorAll('.alert');
        alerts.forEach(alert => {
            setTimeout(() => {
                alert.style.transition = 'opacity 0.5s';
                alert.style.opacity = '0';
                setTimeout(() => alert.remove(), 500);
            }, 5000);
        });
    });

    console.log('✅ Saldo loaded');
</script>

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