<?php
require_once __DIR__ . '/auth_helper.php';

if (!check_installed()) {
    header('Location: install.php');
    exit;
}

$user = currentUser();
if (!$user) {
    header('Location: index.php#login');
    exit;
}

$siteName  = 'pk999.com';
$amount    = round((float)($_GET['amount'] ?? 0), 2);
$methodId  = (int)($_GET['method_id'] ?? 0);
$minAmount = 100;
$maxAmount = 50000;

if ($amount < $minAmount || $amount > $maxAmount) {
    header('Location: deposit.php');
    exit;
}

$methods = payment_methods_for_country($user['country_code']);
$method = null;
foreach ($methods as $m) {
    if ((int)($m['id'] ?? 0) === $methodId) {
        $method = $m;
        break;
    }
}

if (!$method && $methods) {
    $method = $methods[0];
    $methodId = (int)$method['id'];
}

$error = '';
if (!$method) {
    $error = 'No payment method is active right now.';
}

$label        = trim((string)($method['label'] ?? 'Payment'));
$provider     = trim((string)($method['provider'] ?? $label));
$wallet       = trim((string)($method['wallet_number'] ?? ''));
$instructions = trim((string)($method['instructions'] ?? ''));

$detectText = strtolower($label . ' ' . $provider . ' ' . ($method['method_key'] ?? ''));
$isNagad = strpos($detectText, 'nagad') !== false || strpos($detectText, 'নগদ') !== false;
$isBkash = strpos($detectText, 'bkash') !== false || strpos($detectText, 'b-kash') !== false || strpos($detectText, 'বিকাশ') !== false;

$bankName = $isNagad ? 'NAGAD' : ($isBkash ? 'BKASH' : strtoupper($provider ?: $label));
$bankImg  = $isNagad ? 'banks/nagad.png' : ($isBkash ? 'banks/bKash.png' : 'banks/payment.png');
$bankTitle = $bankName . ' Deposit';

if ($instructions === '') {
    $instructions = 'এই ' . $bankName . ' নাম্বারে শুধুমাত্র ক্যাশআউট গ্রহণ করা হয়';
}

$amountText = number_format($amount, 2, '.', ',');
?>
<!DOCTYPE html>
<html lang="bn">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,minimum-scale=1,user-scalable=no">
<title>Payment - <?= e($siteName) ?></title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Noto+Sans+Bengali:wght@400;500;600;700;800;900&family=Poppins:wght@400;500;600;700;800;900&display=swap" rel="stylesheet">
<style>
*{box-sizing:border-box;-webkit-tap-highlight-color:transparent}
html,body{margin:0;padding:0;min-height:100%;background:#111;color:#111;font-family:'Noto Sans Bengali','Poppins',Arial,sans-serif;overscroll-behavior:none}
body{display:flex;justify-content:center;align-items:flex-start}
.pay-page{width:100%;max-width:680px;min-height:100vh;background:#fff;box-shadow:0 0 0 1px rgba(0,0,0,.05);overflow:hidden}
.pay-top{height:112px;background:#007449;color:#fff;display:flex;align-items:flex-start;justify-content:space-between;padding:8px 23px 12px 23px}
.amount-block{min-width:0;padding-top:0}
.amount-block .amount{font-family:'Poppins',Arial,sans-serif;font-size:25px;line-height:1.15;font-weight:900;letter-spacing:.2px;color:#fff;margin:0 0 3px 0;white-space:nowrap}
.amount-block .amount-rule{font-size:24px;line-height:1.45;font-weight:900;color:#fff;margin:0;max-width:320px}
.brand-block{padding-top:23px;text-align:right;display:flex;flex-direction:column;align-items:flex-end;gap:9px;min-width:142px}
.pay-service{display:flex;align-items:center;gap:9px;white-space:nowrap;color:#fff;font-family:'Poppins',Arial,sans-serif;font-weight:600;font-size:21px;letter-spacing:6px;line-height:1}
.pay-service .pay-badge{letter-spacing:0;background:#efffff;color:#006f48;font-size:16px;font-weight:900;border-radius:8px;padding:5px 8px;box-shadow:0 1px 2px rgba(0,0,0,.18)}
.lang-toggle{display:inline-flex;border-radius:4px;overflow:hidden;box-shadow:0 2px 5px rgba(0,0,0,.25);border:1px solid rgba(0,0,0,.18)}
.lang-toggle button{height:27px;border:0;padding:0 11px;font-family:Arial,sans-serif;font-size:13px;font-weight:700;color:#111;cursor:pointer;background:#fff}.lang-toggle button.active{background:#b8b8b8}
.pay-body{padding:38px 42px 36px 42px;background:#fff;min-height:calc(100vh - 112px)}
.danger-note{color:#c74c43;font-size:25px;font-weight:900;line-height:1.55;margin:3px 0 18px 0;letter-spacing:-.2px}
.bank-banner{height:116px;width:100%;background:#cf00ad;border-radius:7px;display:flex;align-items:center;padding:15px 16px;margin:0 0 4px 0;color:#fff;box-shadow:0 1px 0 rgba(0,0,0,.12);overflow:hidden}
.bank-logo-circle{width:85px;height:85px;border-radius:50%;background:#fff;border:1px solid #0d7c5b;display:flex;align-items:center;justify-content:center;overflow:hidden;flex:0 0 auto;margin-right:18px}.bank-logo-circle img{width:100%;height:100%;object-fit:cover;display:block}.bank-logo-fallback{display:none;color:#c00095;font-family:'Poppins',Arial,sans-serif;font-size:16px;font-weight:900;line-height:1;text-align:center;padding:5px}
.bank-banner-title{font-family:'Poppins','Noto Sans Bengali',Arial,sans-serif;font-size:25px;font-weight:800;letter-spacing:.2px;color:#fff;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
.wallet-title{font-size:24px;font-weight:900;color:#080808;line-height:1.25;margin:1px 0 4px 0}.wallet-title .star{color:#c00000}
.wallet-help{font-size:22px;font-weight:500;line-height:1.55;color:#121212;margin:0 0 6px 0}
.wallet-box{height:85px;background:#f1f1f1;display:flex;align-items:center;margin:0 0 16px 0;border:1px solid #eee}.wallet-input{border:0;background:transparent;outline:0;flex:1;height:100%;font-family:'Poppins',Arial,sans-serif;font-size:21px;font-weight:400;letter-spacing:1px;color:#333;text-align:left;padding:0 20px 0 52px;min-width:0}.copy-btn{width:72px;height:100%;border:0;background:transparent;display:flex;align-items:center;justify-content:center;cursor:pointer}.copy-img{
  width:45px;
  height:45px;
  position:relative;
  border:3px solid #168d6d;
  border-radius:4px;
}

.copy-img:before{
  content:"";
  position:absolute;
  width:28px;
  height:31px;
  left:7px;
  top:7px;
  border:3px solid #168d6d;
  border-radius:4px;
  background:transparent;
}

.copy-img:after{
  display:none !important;
  content:none !important;
}
.trx-label{font-size:24px;font-weight:900;line-height:1.35;margin:2px 0 16px;color:#070707}.trx-label .required{color:#d14b43}
.trx-input{width:100%;height:86px;border:3px solid #d20b16;border-radius:6px;background:#fff;outline:0;padding:0 18px;font-size:22px;font-weight:500;color:#111;font-family:'Noto Sans Bengali','Poppins',Arial,sans-serif}.trx-input::placeholder{color:#969696}.trx-input:focus{box-shadow:0 0 0 3px rgba(210,11,22,.09)}
.confirm-row{display:flex;justify-content:center;align-items:center;margin:24px 0 16px}.confirm-btn{width:230px;height:73px;border:1.5px solid #111;border-radius:12px;background:#fff;color:#111;font-size:23px;font-weight:500;font-family:'Noto Sans Bengali',Arial,sans-serif;cursor:pointer}.confirm-btn:active{transform:scale(.99)}.confirm-btn:disabled{opacity:.55;cursor:not-allowed}
.warning-box{font-size:16px;line-height:1.55;margin-top:0}.warning-box .w-title{font-size:21px;color:#101010;font-weight:900;line-height:1.2}.warning-box .w-red{color:#cf4c45;font-weight:900;font-size:17px;line-height:1.35}.warning-box .w-grey{color:#888;font-weight:500;font-size:16px;line-height:1.5;margin-top:2px}.pay-message{display:none;margin:14px 0 12px;padding:12px 14px;border-radius:7px;font-size:15px;font-weight:800;line-height:1.45}.pay-message.ok{display:block;background:#e7fff1;color:#006c32;border:1px solid #b8f0cd}.pay-message.err{display:block;background:#fff1f1;color:#b00000;border:1px solid #ffd2d2}.bottom-safe{height:22px;background:#fff}
.modal-layer{position:fixed;inset:0;background:rgba(0,0,0,.40);display:none;align-items:center;justify-content:center;z-index:99;padding:22px}.modal-layer.show{display:flex}.modal-card{width:min(430px,92vw);background:#fff;padding:30px 38px 22px;box-shadow:0 14px 35px rgba(0,0,0,.28)}.modal-title{font-size:30px;font-weight:400;margin:0 0 14px;color:#111}.modal-text{font-size:22px;line-height:1.35;margin:0 0 28px;color:#111}.modal-action{text-align:right}.modal-action button{border:0;background:#fff;color:#267ca0;font-size:24px;font-weight:500;padding:7px 5px;cursor:pointer;text-transform:uppercase}.error-card{margin:20px 0;padding:16px;border-radius:8px;background:#fff1f1;color:#b00000;border:1px solid #ffd0d0;font-size:18px;font-weight:800;line-height:1.45}
@media(max-width:560px){
  .pay-page{max-width:none;box-shadow:none}.pay-top{height:112px;padding:8px 23px 12px}.amount-block .amount{font-size:25px}.amount-block .amount-rule{font-size:24px;max-width:290px}.brand-block{padding-top:23px;min-width:118px}.pay-service{font-size:21px;letter-spacing:6px}.pay-service .pay-badge{font-size:16px;padding:5px 8px}.pay-body{padding:38px 42px 36px}.danger-note{font-size:25px;line-height:1.55}.bank-banner{height:116px;padding:15px 16px}.bank-logo-circle{width:85px;height:85px;margin-right:18px}.bank-banner-title{font-size:25px}.wallet-title{font-size:24px}.wallet-help{font-size:22px}.wallet-box{height:85px}.wallet-input{font-size:21px;padding-left:52px}.trx-label{font-size:24px}.trx-input{height:86px;font-size:22px}.confirm-btn{width:230px;height:73px;font-size:23px}.warning-box .w-title{font-size:21px}.warning-box .w-red{font-size:17px}.warning-box .w-grey{font-size:16px}
}
@media(max-width:430px){
  .pay-top{height:88px;padding:6px 16px 8px}.amount-block .amount{font-size:20px}.amount-block .amount-rule{font-size:19px;line-height:1.35;max-width:220px}.brand-block{padding-top:16px;min-width:102px;gap:6px}.pay-service{font-size:16px;letter-spacing:4px;gap:6px}.pay-service .pay-badge{font-size:12px;border-radius:6px;padding:4px 6px}.lang-toggle button{height:22px;font-size:11px;padding:0 9px}.pay-body{padding:30px 31px 28px;min-height:calc(100vh - 88px)}.danger-note{font-size:19px;line-height:1.55;margin-bottom:16px}.bank-banner{height:87px;padding:12px}.bank-logo-circle{width:64px;height:64px;margin-right:13px}.bank-logo-fallback{font-size:12px}.bank-banner-title{font-size:19px}.wallet-title{font-size:20px;margin-top:2px}.wallet-help{font-size:18px;line-height:1.55}.wallet-box{height:64px;margin-bottom:16px}.wallet-input{font-size:17px;padding-left:36px}.copy-btn{width:56px}.copy-img{transform:scale(.78)}.trx-label{font-size:19px;margin-bottom:13px}.trx-input{height:66px;border-width:2px;font-size:18px;padding:0 13px}.confirm-row{margin:20px 0 14px}.confirm-btn{width:174px;height:55px;font-size:19px;border-radius:10px}.warning-box .w-title{font-size:17px}.warning-box .w-red{font-size:14px}.warning-box .w-grey{font-size:13px}.modal-card{padding:25px 36px 20px}.modal-title{font-size:27px}.modal-text{font-size:19px}
}
@media(max-width:360px){.pay-body{padding-left:24px;padding-right:24px}.danger-note{font-size:18px}.wallet-help{font-size:17px}.bank-banner-title{font-size:17px}.pay-service{letter-spacing:3px}}
</style>
</head>
<body>
<div class="pay-page">
  <div class="pay-top">
    <div class="amount-block">
      <p class="amount">BDT <?= e($amountText) ?></p>
      <p class="amount-rule">কম বা বেশি ক্যাশআউট করবেন না</p>
    </div>
    <div class="brand-block">
      <div class="pay-service"><span class="pay-badge">PAY</span><span>SERVICE</span></div>
      <div class="lang-toggle" aria-label="Language">
        <button type="button">EN</button><button type="button" class="active">বাং</button>
      </div>
    </div>
  </div>

  <main class="pay-body">
    <?php if($error): ?>
      <div class="error-card"><?= e($error) ?></div>
    <?php else: ?>
      <div class="danger-note">আপনি যদি টাকার পরিমাণ পরিবর্তন করেন (BDT <?= e($amountText) ?>), আপনি ক্রেডিট পেতে সক্ষম হবেন না।</div>

      <div class="bank-banner">
        <div class="bank-logo-circle">
          <img src="<?= e($bankImg) ?>" alt="<?= e($bankName) ?>" onerror="this.style.display='none';this.nextElementSibling.style.display='block';">
          <span class="bank-logo-fallback"><?= e($bankName) ?></span>
        </div>
        <div class="bank-banner-title"><?= e($bankTitle) ?></div>
      </div>

      <div class="wallet-title">Wallet No<span class="star">*</span></div>
      <div class="wallet-help"><?= e($instructions) ?></div>
      <div class="wallet-box">
        <input class="wallet-input" id="walletNo" type="text" value="<?= e($wallet) ?>" readonly>
        <button class="copy-btn" type="button" onclick="copyWallet()" aria-label="Copy wallet number">
          <img src="icons/pay-page-copy.png" alt="Copy" onload="this.style.display='block';this.nextElementSibling.style.display='none';" onerror="this.style.display='none';this.nextElementSibling.style.display='block';">
          <span class="copy-img"></span>
        </button>
      </div>

      <form id="paymentForm">
        <input type="hidden" name="action" value="deposit_request">
        <input type="hidden" name="method_id" value="<?= (int)$methodId ?>">
        <input type="hidden" name="amount" value="<?= e(number_format($amount, 2, '.', '')) ?>">

        <div class="trx-label">ক্যাশআউটের TrxID নাম্বারটি লিখুন<span class="required">(প্রয়োজন)</span></div>
        <input class="trx-input" type="text" name="trxid" placeholder="TrxID অবশ্যই পূরণ করতে হবে!" required autocomplete="off">

        <div class="pay-message" id="payMsg"></div>
        <div class="confirm-row"><button class="confirm-btn" type="submit" id="confirmBtn">নিশ্চিত</button></div>
      </form>

      <div class="warning-box">
        <div class="w-title">সতর্কতাঃ:</div>
        <div class="w-red">লেনদেন আইডি সঠিকভাবে পূরণ করতে হবে, অন্যথায় স্কোর ব্যর্থ হবে! !</div>
        <div class="w-grey">অনুগ্রহ করে নিশ্চিত হয়ে নিন যে আপনি <?= e($bankName) ?> deposit ওয়ালেট নাম্বারে ক্যাশ আউট করছেন। এই নাম্বারের অন্য কোন ওয়ালেট থেকে ক্যাশ আউট করলে সেই টাকা পাওয়ার কোন সম্ভাবনা নাই</div>
      </div>
    <?php endif; ?>
  </main>
  <div class="bottom-safe"></div>
</div>

<div class="modal-layer" id="okModal">
  <div class="modal-card">
    <h2 class="modal-title">Confirmation</h2>
    <p class="modal-text" id="okText">Success! please wait for admin approval</p>
    <div class="modal-action"><button type="button" onclick="location.href='index.php#me'">GO</button></div>
  </div>
</div>

<script>
function showMsg(type, text){
  var box = document.getElementById('payMsg');
  if(!box) return;
  box.className = 'pay-message ' + (type === 'ok' ? 'ok' : 'err');
  box.textContent = text;
}
function showModal(text){
  var modal = document.getElementById('okModal');
  var txt = document.getElementById('okText');
  if(txt) txt.textContent = text || 'Success! please wait for admin approval';
  if(modal) modal.classList.add('show');
}
function copyWallet(){
  var el = document.getElementById('walletNo');
  if(!el) return;
  var val = el.value || '';
  if(navigator.clipboard && navigator.clipboard.writeText){
    navigator.clipboard.writeText(val).then(function(){ showMsg('ok','Wallet number copied'); });
  }else{
    el.focus(); el.select();
    try{ document.execCommand('copy'); showMsg('ok','Wallet number copied'); }catch(e){ showMsg('err','Copy failed'); }
  }
}
var form = document.getElementById('paymentForm');
if(form){
  form.addEventListener('submit', async function(e){
    e.preventDefault();
    var btn = document.getElementById('confirmBtn');
    if(btn){ btn.disabled = true; btn.textContent = 'Submitting...'; }
    showMsg('ok','Submitting request...');
    try{
      var response = await fetch('action.php', { method:'POST', body:new FormData(form) });
      var data = await response.json();
      if(data && data.success){
        form.reset();
        showModal(data.message || 'Success! please wait for admin approval');
      }else{
        showMsg('err', (data && (data.error || data.message)) ? (data.error || data.message) : 'Request failed');
        if(btn){ btn.disabled = false; btn.textContent = 'নিশ্চিত'; }
      }
    }catch(err){
      showMsg('err','Server error. Check action.php and database.');
      if(btn){ btn.disabled = false; btn.textContent = 'নিশ্চিত'; }
    }
  });
}
</script>
</body>
</html>
