<?php
require_once 'db.php';
require_once 'auth_check.php';
require_once 'currency.php';

if (session_status() === PHP_SESSION_NONE) session_start();

$uid = $user_id;

function get_setting_val($conn, $key, $default = '') {
    $stmt = $conn->prepare("SELECT key_value FROM settings WHERE key_name=? LIMIT 1");
    $stmt->bind_param("s", $key);
    $stmt->execute();
    $r = $stmt->get_result()->fetch_assoc();
    $stmt->close();
    return $r['key_value'] ?? $default;
}

$min_deposit = (float) get_setting_val($conn, 'min_deposit', '1000');
$max_deposit = (float) get_setting_val($conn, 'max_deposit', '10000000');

// ── Handle POST ───────────────────────────────────────────────
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $amount    = (float) ($_POST['amount']    ?? 0);
    $method_id = (int)   ($_POST['method_id'] ?? 0);
    $error     = '';

    if ($amount < $min_deposit) {
        $error = "Minimum deposit is " . number_format($min_deposit, 0) . " {$currency}.";
    } elseif ($amount > $max_deposit) {
        $error = "Maximum deposit is " . number_format($max_deposit, 0) . " {$currency}.";
    } elseif (!$method_id) {
        $error = 'Please select a payment method.';
    } elseif (empty($_FILES['proof']['name'])) {
        $error = 'Please attach your payment screenshot as proof of payment.';
    }

    $mrow = null;
    if (!$error) {
        $stmt = $conn->prepare("SELECT name FROM payment_methods WHERE id=? AND active=1 LIMIT 1");
        $stmt->bind_param("i", $method_id);
        $stmt->execute();
        $mrow = $stmt->get_result()->fetch_assoc();
        $stmt->close();
        if (!$mrow) $error = 'Invalid payment method selected.';
    }

    $proof_path = '';
    if (!$error) {
        $ext_ok = ['jpg','jpeg','png','gif','pdf'];
        $ext    = strtolower(pathinfo($_FILES['proof']['name'], PATHINFO_EXTENSION));
        if (!in_array($ext, $ext_ok)) {
            $error = 'Proof file must be JPG, PNG or PDF.';
        } else {
            $dir = 'uploads/proofs/';
            if (!is_dir($dir)) mkdir($dir, 0755, true);
            $fname = 'dep_' . $uid . '_' . time() . '_' . bin2hex(random_bytes(4)) . '.' . $ext;
            if (move_uploaded_file($_FILES['proof']['tmp_name'], $dir . $fname)) {
                $proof_path = $dir . $fname;
            } else {
                $error = 'Failed to upload screenshot. Please try again.';
            }
        }
    }

    if (!$error) {
        $method_name = $mrow['name'];
        $stmt = $conn->prepare("INSERT INTO deposits (user_id,amount,method,account_ref,txn_ref,proof_path,status,created_at)
                                VALUES (?,?,?,'','',?,'pending',NOW())");
        $stmt->bind_param("idss", $uid, $amount, $method_name, $proof_path);
        if ($stmt->execute()) {
            $stmt->close();
            $desc = "Deposit via {$method_name} – pending approval";
            $stmt2 = $conn->prepare("INSERT INTO account_logs (user_id,type,category,amount,description)
                                     VALUES (?,'income','deposit',?,?)");
            $stmt2->bind_param("ids", $uid, $amount, $desc);
            $stmt2->execute();
            $stmt2->close();
            $_SESSION['dep_success'] = 'Deposit submitted! Funds will appear in your Security Deposit after admin verification (usually within 30 minutes).';
            header('Location: deposit.php');
            exit;
        } else {
            $stmt->close();
            $error = 'Database error. Please try again or contact support.';
        }
    }

    $_SESSION['dep_error'] = $error;
    header('Location: deposit.php');
    exit;
}

// ── Flash messages ────────────────────────────────────────────
$success = $_SESSION['dep_success'] ?? '';
$error   = $_SESSION['dep_error']   ?? '';
unset($_SESSION['dep_success'], $_SESSION['dep_error']);

// ── User data ─────────────────────────────────────────────────
$ustmt = $conn->prepare("SELECT * FROM users WHERE id=? LIMIT 1");
$ustmt->bind_param("i", $uid);
$ustmt->execute();
$u = $ustmt->get_result()->fetch_assoc();
$ustmt->close();

$balance          = (float)($u['balance']          ?? 0);
$security_deposit = (float)($u['security_deposit'] ?? 0);

// ── Payment methods ───────────────────────────────────────────
$methods = [];
$mres = $conn->query("SELECT * FROM payment_methods WHERE active=1 ORDER BY sort_order ASC");
while ($row = $mres->fetch_assoc()) $methods[] = $row;

// ── Recent deposits ───────────────────────────────────────────
$recent_deps = [];
$dstmt = $conn->prepare("SELECT * FROM deposits WHERE user_id=? ORDER BY created_at DESC LIMIT 10");
$dstmt->bind_param("i", $uid);
$dstmt->execute();
$dres = $dstmt->get_result();
while ($row = $dres->fetch_assoc()) $recent_deps[] = $row;
$dstmt->close();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">
<title>WFM – Deposit</title>
<link href="https://fonts.googleapis.com/css2?family=Outfit:wght@400;500;600;700;800&display=swap" rel="stylesheet">
<style>
:root{
  --green:#2d7a3a;--green-lt:#3eaabb;
  --gold:#f5c518;--gold2:#c9960a;
  --text:#1a2a0a;--muted:#6a7a5a;--border:#e0ecd8;
  --r:14px;--r-sm:10px;
  --shadow:0 2px 16px rgba(0,50,0,.10);
}
*{margin:0;padding:0;box-sizing:border-box}
body{font-family:'Outfit',sans-serif;background:linear-gradient(155deg,#b8cba8 0%,#8faa7a 55%,#5a8050 100%);min-height:100vh;padding-bottom:90px}

/* Top Bar */
.top-bar{position:sticky;top:0;z-index:200;background:rgba(18,46,18,.85);backdrop-filter:blur(14px);
  padding:13px 16px;display:flex;align-items:center;gap:10px;border-bottom:1px solid rgba(255,255,255,.1)}
.top-bar h1{font-size:17px;font-weight:700;color:#fff;flex:1;letter-spacing:-.2px}
.tab-btns{display:flex;gap:6px}
.tab-btn{padding:5px 14px;border-radius:20px;font-size:12px;font-weight:700;
  text-decoration:none;border:1.5px solid rgba(255,255,255,.35);color:rgba(255,255,255,.75)}
.tab-btn.active{background:var(--gold);color:#7a5000;border-color:var(--gold)}

/* Balance Banner */
.bal-banner{margin:14px 14px 0;background:linear-gradient(135deg,#1e3a1e,#2d5a2d);
  border-radius:var(--r);padding:18px 20px;display:flex;
  position:relative;overflow:hidden;box-shadow:0 4px 20px rgba(0,0,0,.18)}
.bal-banner::after{content:'';position:absolute;top:-40px;right:-40px;width:130px;height:130px;
  border-radius:50%;background:rgba(255,255,255,.04)}
.bb-col{flex:1;text-align:center;position:relative;z-index:1}
.bb-col+.bb-col{border-left:1px solid rgba(255,255,255,.1)}
.bb-lbl{font-size:10px;font-weight:600;color:rgba(255,255,255,.5);text-transform:uppercase;letter-spacing:.7px;margin-bottom:8px}
.bb-val{font-size:21px;font-weight:800;color:#fff}
.bb-val small{font-size:11px;font-weight:400;opacity:.5;margin-left:3px}
.bb-note{font-size:10px;color:rgba(255,255,255,.38);margin-top:4px}

/* Info strip */
.info-strip{margin:8px 14px 0;background:rgba(245,197,24,.12);
  border:1px solid rgba(245,197,24,.35);border-radius:var(--r-sm);
  padding:11px 14px;display:flex;align-items:flex-start;gap:10px}
.info-strip svg{width:18px;height:18px;fill:var(--gold2);flex-shrink:0;margin-top:1px}
.info-strip p{font-size:12px;color:#5a4800;line-height:1.65;font-weight:500}
.info-strip strong{color:#3a2800}

/* Card */
.card{margin:8px 14px 0;background:#fff;border-radius:var(--r);padding:18px 16px;box-shadow:var(--shadow)}
.card-title{font-size:14px;font-weight:700;color:var(--text);margin-bottom:16px;
  display:flex;align-items:center;gap:8px}
.card-title .dot{width:8px;height:8px;border-radius:50%;
  background:linear-gradient(135deg,var(--green-lt),var(--green));flex-shrink:0}

/* Payment methods */
.method-grid{display:grid;grid-template-columns:repeat(3,1fr);gap:8px;margin-bottom:14px}
.method-option{position:relative}
.method-option input[type=radio]{position:absolute;opacity:0;width:0;height:0}
.method-label{display:flex;flex-direction:column;align-items:center;gap:5px;
  padding:10px 4px;border:2px solid var(--border);border-radius:var(--r-sm);
  cursor:pointer;text-align:center;transition:all .2s;min-height:70px;justify-content:center}
.method-label img{width:30px;height:30px;object-fit:contain}
.method-label .m-name{font-size:11px;font-weight:700;color:var(--text);line-height:1.2}
.method-option input:checked + .method-label{border-color:var(--green-lt);background:rgba(62,170,187,.08)}

.method-detail{display:none;background:#f2faf2;border:1px dashed #a0d0a8;
  border-radius:var(--r-sm);padding:12px 14px;margin-bottom:14px;
  font-size:12px;color:var(--text);line-height:1.75}
.method-detail.show{display:block}
.method-detail .md-title{font-size:13px;font-weight:700;color:var(--green);margin-bottom:4px}
.copy-btn{display:inline-flex;align-items:center;gap:5px;margin-top:8px;
  padding:5px 12px;border-radius:20px;background:var(--green);color:#fff;
  font-size:11px;font-weight:700;border:none;cursor:pointer;font-family:'Outfit',sans-serif}

/* Field */
.field{margin-bottom:14px}
.field label{display:block;font-size:11px;font-weight:700;color:var(--muted);
  margin-bottom:6px;text-transform:uppercase;letter-spacing:.5px}
.field input{width:100%;padding:13px 14px;border:2px solid var(--border);
  border-radius:var(--r-sm);font-family:'Outfit',sans-serif;font-size:14px;
  color:var(--text);background:#fff;outline:none;transition:border-color .2s}
.field input:focus{border-color:var(--green-lt)}
.field .hint{font-size:11px;color:var(--muted);margin-top:5px}

/* Quick amounts */
.qa-row{display:flex;flex-wrap:wrap;gap:6px;margin-bottom:10px}
.qa{padding:5px 14px;border-radius:20px;background:#f0f5ee;border:1.5px solid #d0e0c8;
  font-size:12px;font-weight:700;color:var(--green);cursor:pointer;transition:all .18s;white-space:nowrap}
.qa.sel,.qa:hover{background:var(--green);color:#fff;border-color:var(--green)}

/* ── Upload zone ── */
.upload-zone{
  border:2px dashed #c0d8b8;border-radius:var(--r-sm);
  padding:16px 14px;text-align:center;cursor:pointer;
  background:#f8fbf6;transition:all .2s;margin-bottom:14px;
  position:relative;
}
.upload-zone:hover,.upload-zone.has-file{border-color:var(--green-lt);background:#edf8f5}
.upload-zone input[type=file]{
  position:absolute;inset:0;width:100%;height:100%;
  opacity:0;cursor:pointer;z-index:2;
}
.uz-icon{width:32px;height:32px;fill:#a0c8a8;margin:0 auto 8px;display:block}
.uz-label{font-size:12px;color:var(--muted);font-weight:500;pointer-events:none}
.uz-label strong{color:#c00}

/* ── Screenshot preview ── */
.preview-wrap{
  display:none;
  margin-bottom:14px;
  border:2px solid var(--green-lt);
  border-radius:var(--r-sm);
  overflow:hidden;
  background:#edf8f5;
  position:relative;
}
.preview-wrap.show{display:block}
.preview-wrap img{
  width:100%;max-height:220px;object-fit:contain;
  display:block;background:#fff;
}
.preview-wrap .pdf-preview{
  padding:16px;text-align:center;font-size:13px;font-weight:600;color:var(--green);
}
.preview-wrap .pdf-preview svg{width:40px;height:40px;fill:var(--green);display:block;margin:0 auto 8px}
.preview-info{
  background:#edf8f5;padding:8px 12px;
  display:flex;align-items:center;justify-content:space-between;gap:8px;
}
.preview-info span{font-size:11px;color:var(--muted);font-weight:500;
  overflow:hidden;text-overflow:ellipsis;white-space:nowrap;flex:1}
.preview-remove{
  background:none;border:none;cursor:pointer;
  color:#c00;font-size:18px;font-weight:700;line-height:1;
  flex-shrink:0;padding:0 2px;
}

/* Submit button */
.btn-submit{width:100%;padding:15px;background:linear-gradient(90deg,#2d7a3a,#3eaabb);
  color:#fff;font-family:'Outfit',sans-serif;font-size:16px;font-weight:700;
  border:none;border-radius:var(--r);cursor:pointer;transition:opacity .2s;letter-spacing:.2px}
.btn-submit:hover{opacity:.88}
.btn-submit:active{opacity:.75;transform:scale(.98)}

/* Alert */
.alert{margin:8px 14px 0;padding:14px 16px;border-radius:var(--r-sm);font-size:13px;line-height:1.55;font-weight:500}
.alert-ok{background:#eafaf0;border:1px solid #9de0b8;color:#14532d}
.alert-err{background:#fff0f0;border:1px solid #f0b8b8;color:#7f1d1d}
.alert svg{width:16px;height:16px;vertical-align:middle;margin-right:6px}

/* History */
.dep-list{display:flex;flex-direction:column;gap:0}
.dep-item{display:flex;align-items:center;gap:10px;padding:11px 0;border-bottom:1px solid #f0f4ee}
.dep-item:last-child{border-bottom:none}
.dep-icon{width:38px;height:38px;border-radius:50%;background:#f0f8f0;
  display:flex;align-items:center;justify-content:center;flex-shrink:0}
.dep-icon svg{width:18px;height:18px;fill:var(--green)}
.dep-info{flex:1}
.dep-amount{font-size:15px;font-weight:800;color:var(--text)}
.dep-meta{font-size:11px;color:var(--muted);margin-top:2px}
.dep-badge{padding:3px 10px;border-radius:20px;font-size:10px;font-weight:700;
  text-transform:uppercase;letter-spacing:.4px;white-space:nowrap}
.dep-badge.pending{background:#fff8e0;color:#a07000}
.dep-badge.approved{background:#e6f9ef;color:#14532d}
.dep-badge.rejected{background:#fff0f0;color:#7f1d1d}
.empty-msg{text-align:center;padding:24px 0;color:var(--muted);font-size:13px}

/* Bottom Nav */
.bottom-nav{position:fixed;bottom:0;left:0;right:0;height:64px;background:#fff;
  border-top:1px solid #e0e0e0;display:flex;align-items:center;justify-content:space-around;z-index:1000}
.bn-item{display:flex;flex-direction:column;align-items:center;gap:3px;text-decoration:none;flex:1;padding:6px 0}
.bn-item .bn-icon{width:26px;height:26px;background-size:contain;background-repeat:no-repeat;background-position:center}
.bn-item span{font-size:10px;font-weight:600}
.bn-item.active span{color:#3eaabb}
.bn-item:not(.active) span{color:#aaa}
</style>
</head>
<body>

<!-- Top Bar -->
<div class="top-bar">
  <h1>Deposit</h1>
  <div class="tab-btns">
    <a href="deposit.php" class="tab-btn active">Deposit</a>
    <a href="withdraw.php" class="tab-btn">Withdraw</a>
  </div>
</div>

<!-- Balance Banner -->
<div class="bal-banner">
  <div class="bb-col">
    <div class="bb-lbl">Account Balance</div>
    <div class="bb-val"><?= number_format($balance,2) ?><small><?= $currency ?></small></div>
    <div class="bb-note">Earnings &amp; rewards</div>
  </div>
  <div class="bb-col">
    <div class="bb-lbl">Security Deposit</div>
    <div class="bb-val"><?= number_format($security_deposit,2) ?><small><?= $currency ?></small></div>
    <div class="bb-note">Deposit funds go here ↓</div>
  </div>
</div>

<!-- Info Strip -->
<div class="info-strip">
  <svg viewBox="0 0 24 24"><path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm1 15h-2v-6h2v6zm0-8h-2V7h2v2z"/></svg>
  <p>Deposits are credited to your <strong>Security Deposit</strong> after admin verification.<br>
  All task earnings, commissions, referral &amp; bonuses accumulate in your <strong>Account Balance</strong> — which you can withdraw freely.</p>
</div>

<!-- Alerts -->
<?php if ($success): ?>
<div class="alert alert-ok">
  <svg viewBox="0 0 24 24" fill="currentColor"><path d="M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z"/></svg>
  <?= htmlspecialchars($success) ?>
</div>
<?php elseif ($error): ?>
<div class="alert alert-err">
  <svg viewBox="0 0 24 24" fill="currentColor"><path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm1 15h-2v-2h2v2zm0-4h-2V7h2v6z"/></svg>
  <?= htmlspecialchars($error) ?>
</div>
<?php endif; ?>

<!-- Deposit Form -->
<div class="card">
  <div class="card-title"><span class="dot"></span> New Deposit Request</div>

  <form method="POST" enctype="multipart/form-data" id="depForm">

    <!-- ① Method -->
    <div class="field">
      <label>① Select Payment Method</label>
      <div class="method-grid">
        <?php foreach ($methods as $pm): ?>
        <div class="method-option">
          <input type="radio" name="method_id" id="m<?= $pm['id'] ?>"
                 value="<?= $pm['id'] ?>"
                 data-details="<?= htmlspecialchars($pm['details'] ?? '') ?>"
                 data-acct="<?= htmlspecialchars($pm['account_no'] ?? '') ?>"
                 data-name="<?= htmlspecialchars($pm['name']) ?>">
          <label class="method-label" for="m<?= $pm['id'] ?>">
            <?php if (!empty($pm['icon_url'])): ?>
            <img src="/hacker/<?= htmlspecialchars($pm['icon_url']) ?>"
                 alt="<?= htmlspecialchars($pm['name']) ?>"
                 onerror="this.style.display='none'">
            <?php else: ?>
            <svg viewBox="0 0 24 24" width="30" height="30" fill="#a0b8a0"><path d="M20 4H4c-1.11 0-2 .89-2 2v12c0 1.11.89 2 2 2h16c1.11 0 2-.89 2-2V6c0-1.11-.89-2-2-2zm0 14H4v-6h16v6zm0-10H4V6h16v2z"/></svg>
            <?php endif; ?>
            <span class="m-name"><?= htmlspecialchars($pm['name']) ?></span>
          </label>
        </div>
        <?php endforeach; ?>
        <?php if (empty($methods)): ?>
        <p style="font-size:13px;color:var(--muted);grid-column:1/-1;padding:8px 0">No payment methods configured. Contact support.</p>
        <?php endif; ?>
      </div>

      <!-- Dynamic instruction box -->
      <div class="method-detail" id="methodDetail">
        <div class="md-title" id="mDetailName"></div>
        <div id="mDetailBody"></div>
        <button type="button" class="copy-btn" onclick="copyAcct()">
          <svg viewBox="0 0 24 24" width="14" height="14" fill="#fff"><path d="M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.89 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z"/></svg>
          Copy Account Number
        </button>
      </div>
    </div>

    <!-- ② Amount -->
    <div class="field">
      <label>② Deposit Amount (<?= $currency ?>)</label>
      <div class="qa-row">
        <?php foreach ([1000,2000,5000,10000,20000,50000] as $p): ?>
        <span class="qa" onclick="setAmt(<?= $p ?>,this)"><?= number_format($p) ?></span>
        <?php endforeach; ?>
      </div>
      <input type="number" name="amount" id="amtInput"
             placeholder="Min <?= number_format($min_deposit,0) ?>"
             min="<?= $min_deposit ?>" max="<?= $max_deposit ?>"
             step="any" required>
      <p class="hint">Min: <?= number_format($min_deposit,0) ?> · Max: <?= number_format($max_deposit,0) ?> <?= $currency ?></p>
    </div>

    <!-- ③ Payment Screenshot -->
    <div class="field">
      <label>③ Payment Screenshot <span style="color:#c00;font-size:11px">(Required)</span></label>

      <!-- Preview area (shown after file chosen) -->
      <div class="preview-wrap" id="previewWrap">
        <img id="previewImg" src="" alt="Screenshot preview" style="display:none">
        <div class="pdf-preview" id="previewPdf" style="display:none">
          <svg viewBox="0 0 24 24"><path d="M14 2H6c-1.1 0-2 .9-2 2v16c0 1.1.9 2 2 2h12c1.1 0 2-.9 2-2V8l-6-6zm4 18H6V4h7v5h5v11z"/></svg>
          PDF file attached
        </div>
        <div class="preview-info">
          <span id="previewName">—</span>
          <button type="button" class="preview-remove" onclick="removeFile()" title="Remove">✕</button>
        </div>
      </div>

      <!-- Upload tap zone (hidden once file chosen) -->
      <div class="upload-zone" id="uploadZone">
        <input type="file" name="proof" id="proofFile"
               accept=".jpg,.jpeg,.png,.gif,.pdf" required>
        <svg class="uz-icon" viewBox="0 0 24 24">
          <path d="M9 16h6v-6h4l-7-7-7 7h4v6zm-4 2h14v2H5z"/>
        </svg>
        <div class="uz-label">
          Tap to attach payment screenshot<br>
          <strong>Required</strong> · JPG, PNG or PDF
        </div>
      </div>
    </div>

    <button type="submit" class="btn-submit">⬆ Submit Deposit Request</button>
  </form>
</div>

<!-- Recent Deposits -->
<div class="card" style="margin-bottom:12px">
  <div class="card-title"><span class="dot"></span> Recent Deposits</div>
  <?php if (empty($recent_deps)): ?>
    <div class="empty-msg">No deposit history yet.</div>
  <?php else: ?>
  <div class="dep-list">
    <?php foreach ($recent_deps as $d): ?>
    <div class="dep-item">
      <div class="dep-icon">
        <svg viewBox="0 0 24 24"><path d="M12 4l-1.41 1.41L16.17 11H4v2h12.17l-5.58 5.59L12 20l8-8z" transform="rotate(90,12,12)"/></svg>
      </div>
      <div class="dep-info">
        <div class="dep-amount">+<?= number_format($d['amount'],2) ?> <?= $currency ?></div>
        <div class="dep-meta"><?= htmlspecialchars($d['method']) ?> · <?= date('d M Y H:i', strtotime($d['created_at'])) ?></div>
      </div>
      <span class="dep-badge <?= htmlspecialchars($d['status']) ?>"><?= ucfirst(htmlspecialchars($d['status'])) ?></span>
    </div>
    <?php endforeach; ?>
  </div>
  <?php endif; ?>
</div>

<!-- Bottom Nav — dashboard points to admin_dashboard.php -->
<?php include 'navigation_bottom.php'; ?>

<script>
// ── Payment method select → show instructions ──
const radios   = document.querySelectorAll('input[name=method_id]');
const detailEl = document.getElementById('methodDetail');
const nameEl   = document.getElementById('mDetailName');
const bodyEl   = document.getElementById('mDetailBody');
let   acctVal  = '';

function activateMethod(r) {
  nameEl.textContent = r.dataset.name;
  bodyEl.innerHTML   = r.dataset.details;
  acctVal            = r.dataset.acct;
  detailEl.classList.add('show');
}
radios.forEach(r => {
  r.addEventListener('change', () => activateMethod(r));
  if (r.checked) activateMethod(r);
});

function copyAcct() {
  if (!acctVal) return;
  navigator.clipboard.writeText(acctVal).then(() => {
    const b = document.querySelector('.copy-btn');
    const orig = b.innerHTML;
    b.textContent = '✓ Copied!';
    setTimeout(() => { b.innerHTML = orig; }, 2000);
  });
}

// ── Quick amount picker ──
function setAmt(v, el) {
  document.getElementById('amtInput').value = v;
  document.querySelectorAll('.qa').forEach(q => q.classList.remove('sel'));
  el.classList.add('sel');
}

// ── Screenshot preview & remove ──
const proofFile   = document.getElementById('proofFile');
const previewWrap = document.getElementById('previewWrap');
const previewImg  = document.getElementById('previewImg');
const previewPdf  = document.getElementById('previewPdf');
const previewName = document.getElementById('previewName');
const uploadZone  = document.getElementById('uploadZone');

proofFile.addEventListener('change', function() {
  const file = this.files[0];
  if (!file) return;

  previewName.textContent = file.name;
  previewImg.style.display  = 'none';
  previewPdf.style.display  = 'none';

  if (file.type === 'application/pdf') {
    previewPdf.style.display = 'block';
  } else {
    const reader = new FileReader();
    reader.onload = function(e) {
      previewImg.src = e.target.result;
      previewImg.style.display = 'block';
    };
    reader.readAsDataURL(file);
  }

  previewWrap.classList.add('show');
  uploadZone.style.display = 'none';   // hide tap zone once file chosen
});

function removeFile() {
  proofFile.value = '';
  previewWrap.classList.remove('show');
  previewImg.src = '';
  previewImg.style.display = 'none';
  previewPdf.style.display = 'none';
  previewName.textContent  = '—';
  uploadZone.style.display = 'block';  // show tap zone again
}
</script>
</body>
</html>