<?php
require_once dirname(__DIR__).'/config.php';
$u=current_user();if(!$u){header('Location: ' . url('?login=1'));exit;}
?><!DOCTYPE html><html lang="ar" dir="rtl"><head>
<meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1,viewport-fit=cover">
<title>استقبال مال — سودانا</title>
<style>
:root{--g:#00d97e;--dark:#073328;--bg:#f0fdf4;--card:#fff;--muted:#64748b;--border:#e2e8f0;--safe-bottom:env(safe-area-inset-bottom,0px)}
*{box-sizing:border-box;margin:0;padding:0}
body{font-family:'Cairo',system-ui,sans-serif;background:var(--bg);min-height:100vh;padding-bottom:calc(70px + env(safe-area-inset-bottom,0px));padding-bottom:calc(70px + var(--safe-bottom))}
.hdr{background:var(--dark);height:56px;display:flex;align-items:center;justify-content:space-between;padding:0 16px}
.logo{color:#fff;font-weight:900}.back{color:rgba(255,255,255,.6);font-size:.82rem;text-decoration:none}
.container{max-width:480px;margin:0 auto;padding:20px 16px}
.card{background:var(--card);border:1px solid var(--border);border-radius:20px;padding:24px;text-align:center}
.card-title{font-size:1rem;font-weight:900;color:var(--dark);margin-bottom:20px}
.qr-frame{width:200px;height:200px;background:var(--dark);border-radius:16px;margin:0 auto 16px;display:flex;align-items:center;justify-content:center;position:relative;overflow:hidden}
.qr-inner{color:rgba(255,255,255,.7);font-size:.75rem;text-align:center;padding:12px;line-height:1.7}
.account-no{font-size:1rem;font-weight:900;color:var(--dark);letter-spacing:2px;margin:12px 0 4px;font-family:monospace}
.account-name{font-size:.82rem;color:var(--muted);margin-bottom:16px}
.amount-input{width:100%;background:#f8fafc;border:1.5px solid var(--border);border-radius:12px;padding:12px;text-align:center;font-family:inherit;font-size:1.1rem;font-weight:700;margin-bottom:12px}
.btn{background:linear-gradient(135deg,var(--g),#059669);color:#fff;border:none;border-radius:12px;padding:12px;width:100%;font-family:inherit;font-weight:800;font-size:.9rem;cursor:pointer;margin-bottom:8px}
.btn-copy{background:#f8fafc;border:1.5px solid var(--border);border-radius:12px;padding:12px;width:100%;font-family:inherit;font-weight:700;font-size:.85rem;cursor:pointer}
</style></head><body>
<header class="hdr"><div class="logo">↓ استقبال مال</div><a href="/app/pay.php" class="back">← Pay</a></header>
<div class="container">
  <div class="card">
    <div class="card-title">📷 رمز الاستقبال</div>
    <div class="qr-frame" id="qr-frame"><div class="qr-inner" id="qr-content">جاري توليد QR...</div></div>
    <div class="account-no" id="acc-no">—</div>
    <div class="account-name" id="acc-name">—</div>
    <input type="number" class="amount-input" id="req-amount" placeholder="المبلغ (اختياري)" min="0">
    <button class="btn" onclick="generateQR()">🔄 توليد QR جديد</button>
    <button class="btn-copy" onclick="copyAccount()">📋 نسخ رقم الحساب</button>
  </div>
</div>
<script>
async function loadDash(){
  try{
    const r=await fetch('/api/pay/dashboard.php');
    const d=await r.json();
    const data=d.data||d;
    if(data.account){
      document.getElementById('acc-no').textContent=data.account.account_no;
      document.getElementById('acc-name').textContent='<?= htmlspecialchars($u["name"]??$u["phone"]??'') ?>';
    }
  }catch(e){}
}
async function generateQR(){
  const amount=parseFloat(document.getElementById('req-amount').value)||null;
  const el=document.getElementById('qr-content');
  el.textContent='جاري التوليد...';
  try{
    const r=await fetch('/api/pay/qr_generate.php',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({type:'dynamic',amount_sdg:amount})});
    const d=await r.json();
    if(d.ok){
      const qrToken=d.qr_token;
      document.getElementById('qr-frame').innerHTML=`<div style="background:#fff;padding:12px;border-radius:8px;width:180px;height:180px;display:flex;flex-direction:column;align-items:center;justify-content:center;gap:4px"><div style="font-size:3rem">🔲</div><div style="font-size:.65rem;color:#1e293b;text-align:center;word-break:break-all">${qrToken.substring(0,20)}...</div></div>`;
      document.getElementById('acc-no').textContent=d.account_no||document.getElementById('acc-no').textContent;
    }
  }catch(e){el.textContent='تعذّر التوليد';}
}
function copyAccount(){
  const txt=document.getElementById('acc-no').textContent;
  navigator.clipboard?.writeText(txt).then(()=>alert('✅ تم نسخ رقم الحساب: '+txt));
}
loadDash();
generateQR();
</script>
<nav style="position:fixed;bottom:0;left:0;right:0;background:#fff;border-top:1px solid #e2e8f0;display:flex;padding:8px 0 calc(8px + env(safe-area-inset-bottom,0px));z-index:100">
  <a href="/app/hub.php" style="flex:1;display:flex;flex-direction:column;align-items:center;gap:2px;text-decoration:none;color:#64748b;font-size:.6rem;font-family:inherit;padding:4px 0"><span style="font-size:1.1rem">&#127968;</span><span>الهب</span></a>
  <a href="/app/pay.php" style="flex:1;display:flex;flex-direction:column;align-items:center;gap:2px;text-decoration:none;color:#00d97e;font-size:.6rem;font-family:inherit;padding:4px 0"><span style="font-size:1.1rem">&#128179;</span><span>Pay</span></a>
  <a href="/app/invest/index.php" style="flex:1;display:flex;flex-direction:column;align-items:center;gap:2px;text-decoration:none;color:#64748b;font-size:.6rem;font-family:inherit;padding:4px 0"><span style="font-size:1.1rem">&#128185;</span><span>استثمار</span></a>
  <a href="/app/community.php" style="flex:1;display:flex;flex-direction:column;align-items:center;gap:2px;text-decoration:none;color:#64748b;font-size:.6rem;font-family:inherit;padding:4px 0"><span style="font-size:1.1rem">&#127968;</span><span>مجتمع</span></a>
  <a href="/app/profile.php" style="flex:1;display:flex;flex-direction:column;align-items:center;gap:2px;text-decoration:none;color:#64748b;font-size:.6rem;font-family:inherit;padding:4px 0"><span style="font-size:1.1rem">&#128100;</span><span>حسابي</span></a>
</nav>
</body></html>
