<?php
require_once 'api_helper.php';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_POST['deposit_submit'])) {
        $res = apiPost('/deposit', [
            'channel' => $_POST['method'],
            'amount' => $_POST['amount'],
            'walletNo' => $_POST['phone'],
            'trxId' => $_POST['trx_id']
        ]);
        echo '<script>alert("ডিপোজিট রিকোয়েস্ট জমা দেওয়া হয়েছে!");window.location.href="wallet.php";</script>';
    }
    if (isset($_POST['gift_submit'])) {
        $res = apiPost('/gift', [
            'phone' => $_POST['gift_phone'],
            'amount' => $_POST['gift_amount']
        ]);
        if ($res['success']) {
            echo '<script>alert("গিফট পাঠানো হয়েছে!");window.location.href="wallet.php";</script>';
        } else {
            echo '<script>alert("Error: ' . addslashes($res['error']) . '");</script>';
        }
    }
}

include 'partials/header.php';

$showDeposit = isset($_GET['deposit']) && $_GET['deposit'] == '1';
$showGift = isset($_GET['gift']) && $_GET['gift'] == '1';
?>
<div class="space-y-6 pb-28">

  <div class="flex items-center gap-3 border-b border-slate-800 pb-3">
    <button onclick="history.back()" class="p-1 px-2.5 rounded-lg bg-slate-800/100 hover:bg-slate-700 text-slate-100">
      <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="inline mr-1"><path d="m12 19-7-7 7-7"/><path d="M19 12H5"/></svg>
      Back
    </button>
    <div>
      <h2 class="text-[#f8fafc] text-lg font-black tracking-tight leading-none">Wallet</h2>
      <span class="text-xs text-slate-400 font-bold uppercase font-mono">Balance Details</span>
    </div>
  </div>

  <div class="bg-slate-900 border border-slate-800 rounded-2xl p-5 shadow-2xl relative overflow-hidden space-y-4">
    <div class="text-center">
      <span class="text-[10px] text-slate-400 font-bold uppercase tracking-widest block font-mono leading-none mb-1">AVAILABLE BALANCE</span>
      <span class="text-white font-extrabold text-4xl block tracking-tight">৳ <?= number_format($user['balance'] + $user['winning_balance']) ?></span>
      <div class="mt-2.5 inline-block bg-slate-950/80 border border-slate-800 rounded-full px-4 py-1">
        <span class="text-xs text-slate-400 font-bold">Winning Balance: </span>
        <span class="text-[#facc15] font-black font-mono text-xs">৳ <?= number_format($user['winning_balance']) ?></span>
      </div>
    </div>

    <div class="grid grid-cols-3 gap-2 pt-2 text-center">
      <a href="?deposit=<?= $showDeposit ? '0' : '1' ?>" class="bg-green-600 hover:bg-green-700 text-white font-extrabold text-xs py-3.5 px-1 rounded-xl shadow-lg border border-green-500/30 flex items-center justify-center gap-1.5 transition-transform active:scale-95 no-underline" id="btn-wallet-deposit">
        <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="3" stroke-linecap="round" stroke-linejoin="round"><path d="M5 12h14"/><path d="M12 5v14"/></svg>
        DEPOSIT
      </a>
      <a href="withdraw.php" class="bg-[#dc2626] hover:bg-red-700 text-white font-extrabold text-xs py-3.5 px-1 rounded-xl shadow-lg border border-red-500/30 flex items-center justify-center gap-1.5 transition-transform active:scale-95 no-underline" id="btn-wallet-withdraw">
        <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="2" y="6" width="20" height="12" rx="2"/><circle cx="12" cy="12" r="2"/><path d="M6 12h.01M18 12h.01"/></svg>
        WITHDRAW
      </a>
      <a href="?gift=<?= $showGift ? '0' : '1' ?>" class="bg-[#2563eb] hover:bg-blue-700 text-white font-extrabold text-xs py-3.5 px-1 rounded-xl shadow-lg border border-blue-500/30 flex items-center justify-center gap-1.5 transition-transform active:scale-95 no-underline" id="btn-wallet-gift">
        <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="22" y1="2" x2="11" y2="13"/><polygon points="22 2 15 22 11 13 2 9 22 2"/></svg>
        GIFT
      </a>
    </div>
  </div>

  <?php if ($showDeposit): ?>
  <form method="POST" action="" class="bg-[#131b2e] border-2 border-green-500/30 p-5 rounded-2xl space-y-4 shadow-xl">
    <h4 class="text-green-400 font-black text-sm uppercase tracking-wider border-b border-slate-800 pb-2">Add Money / ডিপোজিট করুন</h4>

    <div class="grid grid-cols-3 gap-2 text-center">
      <?php
      $methods = ['bKash', 'Nagad', 'Rocket'];
      $selected = $_POST['method'] ?? 'bKash';
      foreach ($methods as $m):
        $isSel = $selected === $m;
      ?>
      <div onclick="document.getElementById('method_<?= $m ?>').checked=true;this.closest('.grid').querySelectorAll('div').forEach(d=>d.classList.remove('border-green-500','bg-green-500/5','text-white','font-bold'));this.classList.add('border-green-500','bg-green-500/5','text-white','font-bold')" class="p-2.5 rounded-xl border-2 cursor-pointer transition-all <?= $isSel ? 'border-green-500 bg-green-500/5 text-white font-bold' : 'border-slate-800 text-slate-400' ?>">
        <span class="text-xs block font-bold"><?= $m ?></span>
        <input type="radio" name="method" id="method_<?= $m ?>" value="<?= $m ?>" class="hidden" <?= $isSel ? 'checked' : '' ?>>
      </div>
      <?php endforeach; ?>
    </div>

    <div class="bg-slate-950 p-3 rounded-xl border border-slate-800/80 text-xs">
      <span class="text-slate-400 block font-bold leading-tight uppercase font-mono">মরচেন্ট / সেন্ড মানি নম্বর:</span>
      <span class="text-[#facc15] font-black text-sm select-all tracking-wide block mt-1" id="merchantDisplay">01880541682 (Merchant Pay)</span>
      <p class="text-slate-500 mt-1.5 text-[10px] leading-tight font-sans">💡 প্রথমে এই নম্বরটি লক্ষ্য করুন। আপনার ওয়ালেট অ্যাপ (bkash/nagad) থেকে টাকা পরিশোধ করে নিচে ট্রানজেকশন সাবমিট করুন।</p>
    </div>

    <div class="space-y-3 text-xs font-semibold">
      <div class="space-y-1">
        <label class="text-slate-400 font-bold block uppercase font-mono">Amount / টাকার পরিমাণ (৳)</label>
        <input type="number" name="amount" value="<?= htmlspecialchars($_POST['amount'] ?? '500') ?>" placeholder="500" required class="w-full bg-slate-950 border border-slate-800 rounded-lg px-3 py-2.5 text-white font-bold focus:outline-none focus:border-green-500">
      </div>
      <div class="space-y-1">
        <label class="text-slate-400 font-bold block uppercase font-mono">Sender Mobile No / আপনার ফোন নম্বর</label>
        <input type="tel" name="phone" value="<?= htmlspecialchars($_POST['phone'] ?? '') ?>" placeholder="0171XXXXXXXX" required class="w-full bg-slate-950 border border-slate-800 rounded-lg px-3 py-2.5 text-white font-bold focus:outline-none focus:border-green-500">
      </div>
      <div class="space-y-1">
        <label class="text-slate-400 font-bold block uppercase font-mono">Transaction ID (TrxID) / ট্রানজেকশন আইডি</label>
        <input type="text" name="trx_id" value="<?= htmlspecialchars($_POST['trx_id'] ?? '') ?>" placeholder="AX89K7L90P" required class="w-full bg-slate-950 border border-slate-800 rounded-lg px-3 py-2.5 text-white font-bold uppercase focus:outline-none focus:border-green-500 font-mono">
      </div>
    </div>

    <button type="submit" name="deposit_submit" class="w-full bg-green-500 hover:bg-green-600 text-slate-950 font-black tracking-wider uppercase py-3 rounded-xl transition-all">CONFIRM REQUEST / সাবমিট করুন</button>
  </form>
  <?php endif; ?>

  <?php if ($showGift): ?>
  <form method="POST" action="" class="bg-[#131b2e] border-2 border-blue-500/30 p-5 rounded-2xl space-y-4 shadow-xl">
    <h4 class="text-blue-400 font-black text-sm uppercase tracking-wider border-b border-slate-800 pb-2">Gift to Friend / বন্ধুকে গিফট পাঠান</h4>

    <div class="space-y-3 text-xs font-semibold">
      <div class="space-y-1">
        <label class="text-slate-400 font-bold block uppercase font-mono">Friends Wallet/Phone Number</label>
        <input type="tel" name="gift_phone" value="<?= htmlspecialchars($_POST['gift_phone'] ?? '') ?>" placeholder="01XXXXXXXXX" required class="w-full bg-slate-950 border border-slate-800 rounded-lg px-3 py-2.5 text-white font-bold focus:outline-none focus:border-blue-500">
      </div>
      <div class="space-y-1">
        <label class="text-slate-400 font-bold block uppercase font-mono">Amount / স্থানান্তর টাকা</label>
        <input type="number" name="gift_amount" value="<?= htmlspecialchars($_POST['gift_amount'] ?? '') ?>" placeholder="100" required class="w-full bg-slate-950 border border-slate-800 rounded-lg px-3 py-2.5 text-white font-bold focus:outline-none focus:border-blue-500">
      </div>
    </div>

    <button type="submit" name="gift_submit" class="w-full bg-blue-500 hover:bg-blue-600 text-white font-black tracking-wider uppercase py-3 rounded-xl transition-all">SEND GIFT</button>
  </form>
  <?php endif; ?>

  <div class="space-y-2 border-y border-slate-800 pb-4 pt-1">
    <div onclick="alert('টাকা ডিপোজিট করার জন্য bKash বা Nagad সিলেক্ট করুন, আমাদের মার্চেন্ট নম্বরে পেমেন্ট সম্পূর্ণ করে TrxID ও অ্যামাউন্ট সাবমিট করুন। ১২ সেকেন্ডে অটোমেটিক রিকোয়েস্ট এক্সেপ্ট হয়ে যাবে!')" class="flex items-center justify-between p-3.5 bg-slate-900 border border-slate-850 hover:bg-slate-850 rounded-xl cursor-pointer">
      <span class="text-slate-300 font-bold text-xs leading-none">কিভাবে টাকা ডিপোজিট করবেন?</span>
      <span class="text-[10px] text-red-500 bg-red-500/10 hover:bg-red-500/20 px-3 py-1 rounded font-black uppercase font-mono transition-transform">ভিডিও দেখুন</span>
    </div>
    <div onclick="alert('ওয়ালেট পেজে \'GIFT\' এ ক্লিক করুন, বন্ধুর মোবাইল নম্বর ও পেমেন্ট দেওয়ার অ্যামাউন্ট লিখে পাঠান।')" class="flex items-center justify-between p-3.5 bg-slate-900 border border-slate-850 hover:bg-slate-850 rounded-xl cursor-pointer">
      <span class="text-slate-300 font-bold text-xs leading-none">কিভাবে বন্ধুকে গিফট পাঠাবেন?</span>
      <span class="text-[10px] text-red-500 bg-red-500/10 hover:bg-red-500/20 px-3 py-1 rounded font-black uppercase font-mono transition-transform">ভিডিও দেখুন</span>
    </div>
  </div>

  <div class="bg-[#131b2e] border border-slate-850 p-5 rounded-2xl shadow-lg space-y-4">
    <div class="flex items-center justify-between border-b border-slate-800 pb-2">
      <h4 class="text-white text-xs font-black tracking-widest uppercase font-mono">MINI STATEMENTS</h4>
      <span class="text-[10px] text-slate-400 font-bold">Latest activity</span>
    </div>

    <?php if (empty($transactions)): ?>
    <div class="py-12 text-center flex flex-col items-center justify-center text-slate-500">
      <svg xmlns="http://www.w3.org/2000/svg" width="48" height="48" viewBox="0 0 24 24" fill="none" stroke="#475569" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="mb-3 animate-bounce"><polyline points="22 12 16 12 14 15 10 15 8 12 2 12"/><path d="M5.45 5.11 2 12v6a2 2 0 0 0 2 2h16a2 2 0 0 0 2-2v-6l-3.45-6.89A2 2 0 0 0 16.76 4H7.24a2 2 0 0 0-1.79 1.11z"/></svg>
      <p class="text-xs font-bold font-sans">No transactions available.</p>
      <button onclick="alert('আপনার একাউন্টে বর্তমানে কোনো লেনদেন সম্পাদিত হয়নি। ম্যাচ খেলুন বা উইথড্র/ডিপোজিট করুন।')" class="mt-3 text-sky-400 text-xs font-bold uppercase underline">See all...</button>
    </div>
    <?php else: ?>
    <div class="space-y-3.5 max-h-72 overflow-y-auto pr-1">
      <?php foreach ($transactions as $tx):
        $isDeduction = in_array($tx['type'], ['WITHDRAW', 'ENTRY_FEE', 'GIFT_SENT']);
      ?>
      <div class="flex items-center justify-between bg-slate-950 p-3 rounded-xl border border-slate-800/60">
        <div class="space-y-0.5">
          <span class="text-white font-bold text-xs block truncate max-w-[180px]"><?= htmlspecialchars($tx['description']) ?></span>
          <span class="text-[9px] text-slate-500 block font-mono font-medium"><?= htmlspecialchars($tx['date']) ?></span>
          <?php if (!empty($tx['transactionId'])): ?>
          <span class="text-[8px] bg-slate-900 text-slate-400 px-1 rounded block w-max uppercase font-mono">TrxID: <?= htmlspecialchars($tx['transactionId']) ?></span>
          <?php endif; ?>
        </div>
        <div class="text-right flex flex-col items-end gap-1">
          <span class="font-mono font-black text-sm <?= $isDeduction ? 'text-rose-500' : 'text-emerald-400' ?>"><?= $isDeduction ? '-' : '+' ?> ৳ <?= number_format($tx['amount']) ?></span>
          <span class="text-[8px] font-black px-1.5 py-0.5 rounded uppercase <?= $tx['status'] === 'SUCCESS' ? 'bg-green-500/10 text-green-400' : 'bg-yellow-500/10 text-yellow-500' ?>"><?= htmlspecialchars($tx['status']) ?></span>
        </div>
      </div>
      <?php endforeach; ?>
    </div>
    <?php endif; ?>
  </div>
</div>

<script>
<?php if ($showDeposit): ?>
(function(){
  var methods = {'bKash':'01880541682 (Merchant Pay)','Nagad':'01974054168 (Send Money)','Rocket':'01714054168-4 (Send Money)'};
  var radios = document.querySelectorAll('input[name="method"]');
  var display = document.getElementById('merchantDisplay');
  radios.forEach(function(r){
    r.addEventListener('change', function(){
      display.textContent = methods[this.value] || '';
    });
  });
})();
<?php endif; ?>
</script>

<?php include 'partials/footer.php'; ?>
