<?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;--text:#1e293b;--muted:#64748b;--border:#e2e8f0;--red:#dc2626;--sb:env(safe-area-inset-bottom,0px)}
*{box-sizing:border-box;margin:0;padding:0;-webkit-tap-highlight-color:transparent}
body{font-family:'Cairo',system-ui,sans-serif;background:var(--bg);min-height:100vh;padding-bottom:calc(70px + var(--sb))}
.hdr{background:var(--dark);height:56px;display:flex;align-items:center;justify-content:space-between;padding:0 16px;position:sticky;top:0;z-index:100}
.hdr-t{color:#fff;font-weight:900;font-size:.95rem}.back{color:rgba(255,255,255,.6);font-size:.82rem;text-decoration:none}
.container{max-width:640px;margin:0 auto;padding:16px}
.card{background:var(--card);border:1px solid var(--border);border-radius:16px;padding:16px;margin-bottom:14px}
.card-title{font-size:.9rem;font-weight:800;color:var(--dark);margin-bottom:12px}
.btn{background:linear-gradient(135deg,var(--g),#059669);color:#fff;border:none;border-radius:12px;padding:12px 20px;font-family:inherit;font-weight:800;font-size:.88rem;cursor:pointer;width:100%;margin-top:8px}
.btn-out{background:transparent;color:var(--dark);border:1.5px solid var(--border);border-radius:12px;padding:10px 16px;font-family:inherit;font-weight:700;font-size:.82rem;cursor:pointer}
.field{margin-bottom:12px}
.field label{display:block;font-size:.72rem;font-weight:700;color:var(--muted);margin-bottom:4px}
.field input,.field select,.field textarea{width:100%;background:#f8fafc;border:1.5px solid var(--border);border-radius:10px;padding:10px 12px;font-family:inherit;font-size:15px}
.field textarea{resize:none;height:80px}
.badge{display:inline-block;padding:3px 10px;border-radius:20px;font-size:.68rem;font-weight:700}
.badge-g{background:#d1fae5;color:#065f46}.badge-r{background:#fee2e2;color:var(--red)}.badge-y{background:#fef3c7;color:#92400e}
.skel{background:linear-gradient(90deg,#f0f0f0 25%,#e8e8e8 50%,#f0f0f0 75%);background-size:200% 100%;animation:sk 1.2s infinite;border-radius:12px;height:60px;margin-bottom:10px}
@keyframes sk{0%{background-position:200% 0}100%{background-position:-200% 0}}

</style></head><body>
<header class="hdr">
  <div class="hdr-t">تتبع معاملة</div>
  <a href="/app/hub.php" class="back">← الهب</a>
</header>
<div class="container">

<div class="card">
  <div class="card-title">🔍 تتبع معاملة</div>
  <div class="field">
    <input type="text" id="tr-ref" placeholder="أدخل رقم المعاملة TRFxxxxxxxx" dir="ltr">
  </div>
  <button class="btn" onclick="trackTxn()">🔍 تتبع</button>
</div>
<div id="track-result" style="display:none">
  <div class="card">
    <div style="text-align:center;margin-bottom:14px">
      <div style="font-size:2rem" id="tr-icon">⏳</div>
      <div style="font-weight:900;font-size:1rem" id="tr-status-ar">—</div>
      <div style="font-size:.72rem;color:var(--muted)" id="tr-ref-display">—</div>
    </div>
    <div style="display:grid;gap:8px" id="tr-details"></div>
    <div id="tr-actions" style="margin-top:12px;display:flex;gap:8px"></div>
  </div>
</div>

</div>
<nav style="position:fixed;bottom:0;left:0;right:0;background:#fff;border-top:1px solid var(--border);display:flex;padding:8px 0 calc(8px + var(--sb));z-index:100">
  <a href="/app/hub.php" style="flex:1;text-align:center;text-decoration:none;color:#64748b;font-size:.6rem"><span style="font-size:1.1rem;display:block">&#127968;</span>الهب</a>
  <a href="/app/pay.php" style="flex:1;text-align:center;text-decoration:none;color:var(--g);font-size:.6rem"><span style="font-size:1.1rem;display:block">&#128179;</span>Pay</a>
  <a href="/app/services.php" style="flex:1;text-align:center;text-decoration:none;color:#64748b;font-size:.6rem"><span style="font-size:1.1rem;display:block">&#128295;</span>خدمات</a>
  <a href="/app/support.php" style="flex:1;text-align:center;text-decoration:none;color:#64748b;font-size:.6rem"><span style="font-size:1.1rem;display:block">&#127993;</span>الدعم</a>
  <a href="/app/profile.php" style="flex:1;text-align:center;text-decoration:none;color:#64748b;font-size:.6rem"><span style="font-size:1.1rem;display:block">&#128100;</span>حسابي</a>
</nav>
<script>

const refFromUrl = new URLSearchParams(location.search).get('txn_ref');
if(refFromUrl){document.getElementById('tr-ref').value=refFromUrl;trackTxn();}
const statusConfig = {
  pending:    {icon:'⏳',ar:'في الانتظار',color:'#f59e0b'},
  processing: {icon:'⚡',ar:'قيد المعالجة',color:'#3b82f6'},
  completed:  {icon:'✅',ar:'مكتملة',color:'#059669'},
  failed:     {icon:'❌',ar:'فاشلة',color:'#dc2626'},
  reversed:   {icon:'↩️',ar:'مُعكوسة',color:'#6b7280'},
  disputed:   {icon:'⚖️',ar:'متنازع عليها',color:'#f59e0b'},
};
async function trackTxn(){
  const ref = document.getElementById('tr-ref').value.trim();
  if(!ref){alert('أدخل رقم المعاملة');return;}
  try{
    const r = await fetch('/api/pay/track.php?txn_ref='+encodeURIComponent(ref));
    const d = await r.json();
    if(!d.ok){alert(d.error||'المعاملة غير موجودة');return;}
    const sc = statusConfig[d.status]||{icon:'❓',ar:d.status,color:'#64748b'};
    document.getElementById('tr-icon').textContent = sc.icon;
    document.getElementById('tr-status-ar').textContent = sc.ar;
    document.getElementById('tr-status-ar').style.color = sc.color;
    document.getElementById('tr-ref-display').textContent = d.txn_ref;
    const fmt = n => Number(n||0).toLocaleString('ar-SA');
    document.getElementById('tr-details').innerHTML = [
      ['النوع', d.type||'—'],
      ['المبلغ', fmt(d.amount_sdg)+' SDG'],
      ['الرسوم', fmt(d.fee_sdg)+' SDG'],
      ['البيان', d.narration||'—'],
      ['التاريخ', d.created_at||'—'],
      ['اكتمل', d.completed_at||'—'],
      ['سبب الفشل', d.failed_reason||'—'],
    ].filter(([k,v])=>v&&v!=='—').map(([k,v])=>`
      <div style="display:flex;justify-content:space-between;padding:8px;background:#f8fafc;border-radius:8px;font-size:.78rem">
        <span style="color:var(--muted)">${k}</span><span style="font-weight:600">${v}</span>
      </div>`).join('');
    const actions = [];
    if(d.can_reverse) actions.push('<a href="/app/pay.php" style="flex:1;background:#073328;color:#fff;border-radius:10px;padding:8px;text-align:center;font-size:.75rem;font-weight:700;text-decoration:none">↩️ عكس</a>');
    if(d.can_dispute) actions.push('<a href="/app/pay-dispute.php?txn_ref='+d.txn_ref+'" style="flex:1;background:#fef3c7;color:#92400e;border-radius:10px;padding:8px;text-align:center;font-size:.75rem;font-weight:700;text-decoration:none">⚖️ نزاع</a>');
    document.getElementById('tr-actions').innerHTML = actions.join('');
    document.getElementById('track-result').style.display='block';
  }catch(e){alert('تعذّر التتبع');}
}

</script>
</body></html>