<?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;-webkit-tap-highlight-color:transparent}
body{font-family:'Cairo',system-ui,sans-serif;background:var(--bg);min-height:100vh;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;position:sticky;top:0;z-index:100}
.logo{color:#fff;font-weight:900}.back{color:rgba(255,255,255,.6);font-size:.82rem;text-decoration:none}
.filters{display:flex;gap:8px;padding:12px 16px;overflow-x:auto;background:var(--dark)}
.f-btn{background:rgba(255,255,255,.1);border:none;border-radius:20px;padding:6px 14px;color:rgba(255,255,255,.7);font-family:inherit;font-size:.72rem;cursor:pointer;white-space:nowrap}
.f-btn.active{background:var(--g);color:#000;font-weight:700}
.container{max-width:640px;margin:0 auto;padding:12px 16px}
.txn-card{background:var(--card);border:1px solid var(--border);border-radius:12px;padding:12px;display:flex;align-items:center;gap:10px;margin-bottom:8px}
.txn-icon{width:42px;height:42px;border-radius:12px;display:flex;align-items:center;justify-content:center;font-size:1.2rem;flex-shrink:0}
.txn-in{background:#d1fae5}.txn-out{background:#fee2e2}.txn-neutral{background:#f1f5f9}
.txn-info{flex:1}
.txn-narr{font-size:.83rem;font-weight:700;margin-bottom:2px}
.txn-meta{font-size:.7rem;color:var(--muted)}
.txn-amt{text-align:left;white-space:nowrap}
.amt-in{color:#059669;font-weight:900;font-size:.9rem}
.amt-out{color:#dc2626;font-weight:900;font-size:.9rem}
.txn-ref{font-size:.65rem;color:var(--muted);margin-top:2px}
.skel{background:linear-gradient(90deg,#f0f0f0 25%,#e8e8e8 50%,#f0f0f0 75%);background-size:200% 100%;animation:sk 1.2s infinite;border-radius:12px;height:64px;margin-bottom:8px}
@keyframes sk{0%{background-position:200% 0}100%{background-position:-200% 0}}
.bottom-nav{position:fixed;bottom:0;left:0;right:0;background:#fff;border-top:1px solid var(--border);display:flex;padding:8px 0 calc(8px + var(--safe-bottom));z-index:100}
.nav-item{flex:1;display:flex;flex-direction:column;align-items:center;gap:3px;text-decoration:none;color:var(--muted);font-size:.62rem;padding:4px 0}
.nav-item.active{color:var(--g)}.nav-icon{font-size:1.2rem}
</style></head><body>
<header class="hdr"><div class="logo">📋 سجل المعاملات</div><a href="/app/pay.php" class="back">← Pay</a></header>
<div class="filters">
  <button class="f-btn active" onclick="load('',this)">الكل</button>
  <button class="f-btn" onclick="load('transfer',this)">تحويل</button>
  <button class="f-btn" onclick="load('bill',this)">فواتير</button>
  <button class="f-btn" onclick="load('invest',this)">استثمار</button>
  <button class="f-btn" onclick="load('rosca',this)">صندوق</button>
  <button class="f-btn" onclick="load('cashin',this)">شحن</button>
  <button class="f-btn" onclick="load('cashout',this)">سحب</button>
</div>
<div class="container" id="list"><div class="skel"></div><div class="skel"></div><div class="skel"></div></div>
<nav class="bottom-nav">
  <a href="/app/dashboard.php" class="nav-item"><span class="nav-icon">🏠</span><span>الرئيسية</span></a>
  <a href="/app/pay.php" class="nav-item active"><span class="nav-icon">💳</span><span>Pay</span></a>
  <a href="/app/invest/index.php" class="nav-item"><span class="nav-icon">💹</span><span>استثمار</span></a>
  <a href="/app/profile.php" class="nav-item"><span class="nav-icon">👤</span><span>حسابي</span></a>
</nav>
<script>
const fmtSDG=n=>Number(n||0).toLocaleString('ar-SA');
const icons={transfer:'💸',bill:'📄',cashin:'⬇️',cashout:'⬆️',invest:'💹',rosca:'🔄',zakat:'🕌',gift:'🎁',merchant:'🏪',qr_pay:'📷',refund:'↩️'};
const typeAr={transfer:'تحويل',bill:'فاتورة',cashin:'شحن',cashout:'سحب',invest:'استثمار',rosca:'صندوق',zakat:'زكاة',gift:'هدية',merchant:'تاجر',qr_pay:'QR',refund:'استرداد'};

async function load(type,btn){
  if(btn){document.querySelectorAll('.f-btn').forEach(b=>b.classList.remove('active'));btn.classList.add('active');}
  const el=document.getElementById('list');
  el.innerHTML='<div class="skel"></div><div class="skel"></div>';
  try{
    const r=await fetch('/api/pay/history.php?limit=30'+(type?'&type='+type:''));
    const d=await r.json();
    const txns=d.data||[];
    if(!txns.length){el.innerHTML='<p style="text-align:center;padding:24px;color:var(--muted)">لا معاملات</p>';return;}
    el.innerHTML=txns.map(t=>{
      const isIn=t.direction==='in';
      const dt=new Date(t.created_at.replace(' ','T'));
      const dtStr=dt.toLocaleDateString('ar-SA',{day:'numeric',month:'short',year:'numeric',hour:'2-digit',minute:'2-digit'});
      return`<div class="txn-card">
        <div class="txn-icon ${isIn?'txn-in':t.direction==='out'?'txn-out':'txn-neutral'}">${icons[t.type]||'💳'}</div>
        <div class="txn-info">
          <div class="txn-narr">${t.narration||(typeAr[t.type]||t.type)}</div>
          <div class="txn-meta">${dtStr}</div>
          <div class="txn-ref"># ${t.txn_ref}</div>
        </div>
        <div class="txn-amt">
          <div class="${isIn?'amt-in':'amt-out'}">${isIn?'+':'-'}${fmtSDG(t.amount_sdg)}</div>
          <div style="font-size:.65rem;color:var(--muted);text-align:left">SDG</div>
        </div>
      </div>`;}).join('');
  }catch(e){el.innerHTML='<p style="text-align:center;padding:24px;color:var(--muted)">تعذّر التحميل</p>';}
}

load('');
</script></body></html>
