<?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:0 0 calc(80px + 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;font-size:.95rem}.back{color:rgba(255,255,255,.6);font-size:.82rem;text-decoration:none}
.container{max-width:540px;margin:20px auto;padding:0 16px}
.card{background:var(--card);border:1px solid var(--border);border-radius:20px;padding:24px}
.card-title{font-size:1rem;font-weight:900;color:var(--dark);margin-bottom:20px;text-align:center}
.field{margin-bottom:16px}
.field label{display:block;font-size:.75rem;font-weight:700;color:var(--muted);margin-bottom:6px}
.field input{width:100%;background:#f8fafc;border:1.5px solid var(--border);border-radius:12px;padding:12px 14px;font-family:inherit;font-size:15px;color:#1e293b}
.field input:focus{outline:none;border-color:var(--g)}
.amount-display{text-align:center;font-size:2.4rem;font-weight:900;color:var(--dark);padding:12px 0;letter-spacing:-1px}
.fee-info{text-align:center;font-size:.75rem;color:var(--muted);margin-bottom:16px}
.btn{background:linear-gradient(135deg,var(--g),#059669);color:#fff;border:none;border-radius:14px;padding:15px;width:100%;font-family:inherit;font-weight:900;font-size:1rem;cursor:pointer}
.btn:disabled{opacity:.5;cursor:not-allowed}
.quick-amounts{display:flex;gap:8px;flex-wrap:wrap;margin-bottom:14px}
.qa-btn{background:#f0fdf4;border:1px solid #bbf7d0;border-radius:8px;padding:7px 14px;font-size:.78rem;font-weight:700;cursor:pointer;color:var(--dark)}
.success{background:#d1fae5;border:1px solid #6ee7b7;border-radius:12px;padding:16px;text-align:center;display:none}
</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="field"><label>رقم الجوال أو رقم الحساب</label>
      <input type="text" id="to" placeholder="0912345678 أو SD2025XXXXXXXX" oninput="checkRecipient()"></div>
    <div class="amount-display" id="amt-display">0 SDG</div>
    <div class="quick-amounts">
      <div class="qa-btn" onclick="setAmount(500)">500</div>
      <div class="qa-btn" onclick="setAmount(1000)">1,000</div>
      <div class="qa-btn" onclick="setAmount(5000)">5,000</div>
      <div class="qa-btn" onclick="setAmount(10000)">10,000</div>
      <div class="qa-btn" onclick="setAmount(50000)">50,000</div>
    </div>
    <div class="field"><label>المبلغ (SDG)</label>
      <input type="number" id="amount" placeholder="أدخل المبلغ" min="10" oninput="updateFee()"></div>
    <div class="fee-info" id="fee-info">أدخل المبلغ لحساب الرسوم</div>
    <div class="field"><label>ملاحظة (اختياري)</label>
      <input type="text" id="narration" placeholder="مثال: إيجار — مصاريف..." maxlength="80"></div>
    <div class="field"><label>رقم PIN السري</label>
      <input type="password" id="pin" maxlength="6" placeholder="4-6 أرقام"></div>
    <button class="btn" id="send-btn" onclick="doTransfer()">↑ أرسل الآن</button>
    <div class="success" id="success-box">
      <div style="font-size:1.6rem">✅</div>
      <div style="font-weight:900;font-size:1rem;margin:8px 0">تم الإرسال بنجاح!</div>
      <div id="success-info" style="font-size:.82rem;color:var(--muted)"></div>
    </div>
  </div>
</div>
<script>
const fmtSDG=n=>Number(n||0).toLocaleString('ar-SA');
function setAmount(n){document.getElementById('amount').value=n;updateFee();}
function updateFee(){
  const a=parseFloat(document.getElementById('amount').value)||0;
  const amtEl=document.getElementById('amt-display');
  const feeEl=document.getElementById('fee-info');
  amtEl.textContent=fmtSDG(a)+' SDG';
  let fee=0;let pct='مجاني';
  if(a>=100000){fee=a*.003;pct='0.3%';}
  else if(a>=10000){fee=a*.005;pct='0.5%';}
  feeEl.textContent=fee>0?'رسوم التحويل: '+fmtSDG(fee)+' SDG ('+pct+')':'التحويل مجاني أقل من 10,000 SDG';
}
function checkRecipient(){
  const val=document.getElementById('to').value.trim();
  if(val.length>=9) document.getElementById('to').style.borderColor='var(--g)';
}
async function doTransfer(){
  const to=document.getElementById('to').value.trim();
  const amount=parseFloat(document.getElementById('amount').value)||0;
  const pin=document.getElementById('pin').value.trim();
  const narr=document.getElementById('narration').value.trim();
  if(!to||amount<10||!pin){alert('أكمل جميع البيانات المطلوبة');return;}
  const btn=document.getElementById('send-btn');
  btn.disabled=true;btn.textContent='جاري الإرسال...';
  try{
    const r=await fetch('/api/pay/transfer.php',{method:'POST',headers:{'Content-Type':'application/json'},
      body:JSON.stringify({to_phone:to,to_account:to,amount_sdg:amount,pin,narration:narr,idempotency_key:'tf_'+Date.now()})});
    const d=await r.json();
    if(d.ok){
      document.getElementById('success-box').style.display='block';
      document.getElementById('success-info').textContent='المرجع: '+d.txn_ref+' | '+fmtSDG(amount)+' SDG';
      btn.style.display='none';
    }else{
      alert('❌ '+(d.message||d.error||'حدث خطأ'));
      btn.disabled=false;btn.textContent='↑ أرسل الآن';
    }
  }catch(e){alert('❌ تعذّر الاتصال');btn.disabled=false;btn.textContent='↑ أرسل الآن';}
}
</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>
<script src="/assets/pricing-loader.js" defer></script>
</body></html>
