<?php
/**
 * tarik_saldo.php - Halaman Tarik Saldo Wisatawan
 */
$activeMenu = 'saldo';
$pageTitle = 'Tarik Saldo - Wisatawan';
include __DIR__ . '/../layouts/header.php';

// Data dari controller
$saldo = $saldo ?? 0;
$riwayatWithdraw = $riwayatWithdraw ?? [];
$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>Tarik Saldo - 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 {
            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); }
        }

        .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;
        }

        .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-secondary {
            background: var(--gray-200);
            color: var(--gray-600);
        }

        .btn-secondary:hover {
            background: var(--gray-300);
            color: var(--gray-800);
        }

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

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

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

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

        .btn-danger:hover {
            background: linear-gradient(135deg, #dc2626, #b91c1c);
            color: #fff;
        }

        /* SALDO CARD */
        .saldo-card {
            background: linear-gradient(135deg, var(--primary), var(--primary-dark));
            color: #fff;
            padding: 1.5rem 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-label {
            font-size: 0.85rem;
            opacity: 0.8;
        }

        .saldo-card .saldo-amount {
            font-size: 2rem;
            font-weight: 700;
        }

        /* FORM */
        .withdraw-box {
            background: #fff;
            padding: 1.5rem;
            border-radius: var(--radius);
            border: 1px solid var(--gray-200);
            margin-bottom: 1.5rem;
        }

        .withdraw-box h3 {
            font-size: 1.1rem;
            font-weight: 700;
            color: var(--gray-800);
            margin-bottom: 1.2rem;
            display: flex;
            align-items: center;
            gap: 0.5rem;
        }

        .withdraw-box h3 i {
            color: var(--primary);
        }

        .form-group {
            margin-bottom: 1rem;
        }

        .form-group label {
            display: block;
            font-weight: 600;
            font-size: 0.85rem;
            color: var(--gray-700);
            margin-bottom: 0.3rem;
        }

        .form-group label .required {
            color: var(--danger);
        }

        .form-control {
            width: 100%;
            padding: 0.7rem 0.8rem;
            border: 1.5px solid var(--gray-200);
            border-radius: var(--radius-sm);
            font-size: 0.9rem;
            transition: var(--transition);
            font-family: inherit;
            background: #fff;
        }

        .form-control:focus {
            outline: none;
            border-color: var(--primary);
            box-shadow: 0 0 0 3px rgba(43, 108, 176, 0.1);
        }

        .form-hint {
            font-size: 0.7rem;
            color: var(--gray-400);
            margin-top: 0.2rem;
            display: block;
        }

        .form-row {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 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-pending {
            background: var(--warning-bg);
            color: #92400e;
        }

        .badge-proses {
            background: var(--info-bg);
            color: #1e40af;
        }

        .badge-selesai {
            background: var(--success-bg);
            color: #065f46;
        }

        .badge-gagal {
            background: var(--danger-bg);
            color: #991b1b;
        }

        /* TABLE */
        .table-responsive {
            overflow-x: auto;
        }

        .table {
            width: 100%;
            border-collapse: collapse;
            font-size: 0.85rem;
        }

        .table thead {
            background: var(--gray-50);
        }

        .table th {
            padding: 0.6rem 0.8rem;
            text-align: left;
            font-weight: 600;
            color: var(--gray-600);
            border-bottom: 2px solid var(--gray-200);
            font-size: 0.7rem;
            text-transform: uppercase;
            letter-spacing: 0.3px;
        }

        .table td {
            padding: 0.6rem 0.8rem;
            border-bottom: 1px solid var(--gray-100);
        }

        .table tr:hover td {
            background: var(--gray-50);
        }

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

        /* INFO */
        .info-box {
            margin-top: 1.5rem;
            padding: 1rem 1.2rem;
            background: var(--info-bg);
            border-radius: var(--radius-sm);
            border-left: 4px solid var(--info);
        }

        .info-box p {
            font-size: 0.85rem;
            color: #1e40af;
            margin: 0;
        }

        .info-box p i {
            margin-right: 0.3rem;
        }

        /* SECTION TITLE */
        .section-title {
            font-size: 1rem;
            font-weight: 700;
            color: var(--gray-800);
            margin: 1.5rem 0 0.8rem;
            display: flex;
            align-items: center;
            gap: 0.5rem;
        }

        .section-title i {
            color: var(--primary);
        }

        /* 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;
            }
            .form-row {
                grid-template-columns: 1fr;
            }
        }

        @media (max-width: 480px) {
            .main-content {
                padding: 0.8rem;
            }
            .withdraw-box {
                padding: 1rem;
            }
            .saldo-card .saldo-amount {
                font-size: 1.5rem;
            }
        }
    </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-money-bill-wave"></i>
                    Tarik Saldo
                </h2>
                <p>
                    <i class="fas fa-hand-holding-usd"></i> 
                    Tarik saldo aplikasi Anda ke rekening bank
                </p>
            </div>
            <div>
                <a href="?page=wisatawan&action=saldo" class="btn btn-secondary">
                    <i class="fas fa-arrow-left"></i> Kembali ke Saldo
                </a>
            </div>
        </div>

        <!-- ALERT -->
        <?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>
                <div class="saldo-label"><i class="fas fa-wallet"></i> Saldo Saat Ini</div>
                <div class="saldo-amount">Rp <?= number_format($saldo, 0, ',', '.') ?></div>
            </div>
            <div style="font-size:0.85rem; opacity:0.8;">
                <i class="fas fa-info-circle"></i> Minimal penarikan: Rp 50.000
            </div>
        </div>

        <!-- FORM TARIK SALDO -->
        <div class="withdraw-box">
            <h3><i class="fas fa-hand-holding-usd"></i> Form Penarikan Saldo</h3>
            
            <form method="POST" action="?page=wisatawan&action=tarikSaldo" id="formWithdraw">
                <div class="form-group">
                    <label>Jumlah Penarikan <span class="required">*</span></label>
                    <input type="number" name="jumlah" class="form-control" 
                           placeholder="Masukkan jumlah penarikan" 
                           min="50000" max="<?= $saldo ?>" required
                           oninput="this.value = this.value.replace(/[^0-9]/g, '')">
                    <small class="form-hint">Maksimal: Rp <?= number_format($saldo, 0, ',', '.') ?></small>
                </div>

                <div class="form-group">
                    <label>Metode Penarikan <span class="required">*</span></label>
                    <select name="metode" class="form-control" required>
                        <option value="bank">🏦 Transfer Bank</option>
                        <option value="ewallet">📱 E-Wallet</option>
                    </select>
                </div>

                <div class="form-group">
                    <label>Nama Bank <span class="required">*</span></label>
                    <select name="bank" class="form-control" required>
                        <option value="">-- Pilih Bank --</option>
                        <option value="BCA">BCA</option>
                        <option value="Mandiri">Mandiri</option>
                        <option value="BNI">BNI</option>
                        <option value="BRI">BRI</option>
                        <option value="Danamon">Danamon</option>
                        <option value="CIMB Niaga">CIMB Niaga</option>
                        <option value="Permata">Permata</option>
                        <option value="BTN">BTN</option>
                        <option value="Bank Syariah Indonesia">Bank Syariah Indonesia</option>
                        <option value="Lainnya">Lainnya</option>
                    </select>
                </div>

                <div class="form-group">
                    <label>Nomor Rekening <span class="required">*</span></label>
                    <input type="text" name="nomor_rekening" class="form-control" 
                           placeholder="Masukkan nomor rekening" required>
                    <small class="form-hint">Masukkan nomor rekening dengan benar</small>
                </div>

                <div class="form-group">
                    <label>Nama Pemilik Rekening <span class="required">*</span></label>
                    <input type="text" name="nama_pemilik" class="form-control" 
                           placeholder="Masukkan nama pemilik rekening" 
                           value="<?= htmlspecialchars($user['nama'] ?? '') ?>" required>
                </div>

                <div style="display:flex; gap:0.8rem; flex-wrap:wrap; margin-top:0.5rem;">
                    <button type="submit" class="btn btn-success btn-lg" id="btnWithdraw">
                        <i class="fas fa-paper-plane"></i> Ajukan Penarikan
                    </button>
                    <a href="?page=wisatawan&action=saldo" class="btn btn-secondary btn-lg">
                        <i class="fas fa-times"></i> Batal
                    </a>
                </div>
            </form>
        </div>

        <!-- RIWAYAT PENARIKAN -->
        <?php if (!empty($riwayatWithdraw)): ?>
            <h4 class="section-title"><i class="fas fa-history"></i> Riwayat Penarikan</h4>
            <div class="table-responsive">
                <table class="table">
                    <thead>
                        <tr>
                            <th>Tanggal</th>
                            <th>Jumlah</th>
                            <th>Bank</th>
                            <th>No. Rekening</th>
                            <th>Status</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($riwayatWithdraw as $row): ?>
                            <tr>
                                <td><?= date('d/m/Y H:i', strtotime($row['created_at'])) ?></td>
                                <td><strong>Rp <?= number_format($row['jumlah'], 0, ',', '.') ?></strong></td>
                                <td><?= htmlspecialchars($row['bank'] ?? '-') ?></td>
                                <td><?= htmlspecialchars($row['nomor_rekening'] ?? '-') ?></td>
                                <td>
                                    <?php if ($row['status'] == 'pending'): ?>
                                        <span class="badge badge-pending"><i class="fas fa-clock"></i> Menunggu</span>
                                    <?php elseif ($row['status'] == 'proses'): ?>
                                        <span class="badge badge-proses"><i class="fas fa-spinner"></i> Diproses</span>
                                    <?php elseif ($row['status'] == 'selesai'): ?>
                                        <span class="badge badge-selesai"><i class="fas fa-check"></i> Selesai</span>
                                    <?php elseif ($row['status'] == 'gagal'): ?>
                                        <span class="badge badge-gagal"><i class="fas fa-times"></i> Gagal</span>
                                    <?php endif; ?>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        <?php endif; ?>

        <!-- INFORMASI -->
        <div class="info-box">
            <p>
                <i class="fas fa-info-circle"></i> 
                <strong>Catatan:</strong> Penarikan saldo akan diproses dalam 1-3 hari kerja. 
                Pastikan data rekening yang Anda masukkan sudah benar.
            </p>
        </div>
    </main>
</div>

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

    // Validasi form sebelum submit
    document.getElementById('formWithdraw')?.addEventListener('submit', function(e) {
        const jumlah = this.querySelector('input[name="jumlah"]');
        const saldo = <?= $saldo ?>;
        const minimal = 50000;
        const maxSaldo = <?= $saldo ?>;
        
        if (!jumlah.value || parseInt(jumlah.value) < minimal) {
            e.preventDefault();
            alert('Minimal penarikan Rp 50.000');
            jumlah.focus();
            return false;
        }
        
        if (parseInt(jumlah.value) > maxSaldo) {
            e.preventDefault();
            alert('Saldo tidak mencukupi. Saldo Anda: Rp ' + new Intl.NumberFormat('id-ID').format(maxSaldo));
            jumlah.focus();
            return false;
        }
        
        if (!confirm('⚠️ Yakin ingin menarik saldo sebesar Rp ' + new Intl.NumberFormat('id-ID').format(parseInt(jumlah.value)) + '?\n\nPastikan data rekening Anda sudah benar!')) {
            e.preventDefault();
            return false;
        }
        
        // Disable button untuk mencegah double submit
        const btn = document.getElementById('btnWithdraw');
        if (btn) {
            btn.disabled = true;
            btn.innerHTML = '<i class="fas fa-spinner fa-spin"></i> Memproses...';
        }
    });
</script>

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