<?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>الوكلاء — Sudanna Pay</title>
<style>:root{--g:#00d97e;--dark:#073328;--bg:#f0fdf4;--card:#fff;--text:#1e293b;--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','Noto Sans Arabic',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}
.search-bar{background:var(--dark);padding:0 16px 12px}
.search-input{width:100%;background:rgba(255,255,255,.1);border:1px solid rgba(255,255,255,.2);border-radius:10px;padding:10px 14px;color:#fff;font-family:inherit;font-size:16px}
.search-input::placeholder{color:rgba(255,255,255,.4)}
.container{max-width:640px;margin:0 auto;padding:14px}
.agent-card{background:var(--card);border:1.5px solid var(--border);border-radius:14px;padding:14px;margin-bottom:10px;display:flex;align-items:center;gap:12px}
.agent-icon{width:44px;height:44px;border-radius:12px;background:#f0fdf4;display:flex;align-items:center;justify-content:center;font-size:1.4rem;flex-shrink:0}
.agent-name{font-weight:700;font-size:.88rem}.agent-city{font-size:.75rem;color:var(--muted)}
.agent-code{font-family:monospace;font-size:.75rem;color:var(--g);font-weight:700;margin-top:4px}
.agent-actions{margin-right:auto;display:flex;gap:6px}
.btn-cashin{background:#d1fae5;color:#065f46;border:none;border-radius:8px;padding:7px 12px;font-family:inherit;font-size:.73rem;font-weight:700;cursor:pointer}
.btn-cashout{background:#fee2e2;color:#991b1b;border:none;border-radius:8px;padding:7px 12px;font-family:inherit;font-size:.73rem;font-weight:700;cursor:pointer}
.empty{text-align:center;padding:40px 16px;color:var(--muted)}.empty-icon{font-size:3rem;margin-bottom:10px}
.info-banner{background:#f0fdf4;border:1px solid #bbf7d0;border-radius:12px;padding:12px;margin-bottom:14px;font-size:.8rem;line-height:1.7;color:var(--dark)}
.btn-apply{display:block;background:linear-gradient(135deg,var(--g),#059669);color:#fff;border:none;border-radius:10px;padding:11px;width:100%;font-family:inherit;font-weight:700;font-size:.88rem;cursor:pointer;margin-top:10px;text-align:center;text-decoration:none}
.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">🏪 وكلاء Sudanna Pay</div><a href="/app/pay.php" class="back">← Pay</a></header>
<div class="search-bar"><input type="text" class="search-input" placeholder="ابحث بالمدينة..." id="city-search" oninput="searchAgents(this.value)"></div>
<div class="container">
<div class="info-banner">
💡 الوكيل = فرع Sudanna Pay في حيّك<br>
تقدر تشحن أو تسحب أموالك منه نقداً.<br>
عمولة السحب والشحن: 0.5% فقط.
<a href="/app/provider-register.php?domain=agent" class="btn-apply">سجِّل كوكيل — واكسب من كل معاملة ✅</a>
</div>
<div id="agents-list"><p style="color:var(--muted);text-align:center;padding:20px">⏳ جاري التحميل...</p></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/pay-history.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>
let searchTimeout;
async function searchAgents(city){
  clearTimeout(searchTimeout);
  searchTimeout=setTimeout(async()=>{
    const r=await fetch('/api/agents/list.php'+(city?'?city='+encodeURIComponent(city):''));
    const d=await r.json();
    const list=d.agents||[];
    const el=document.getElementById('agents-list');
    if(!list.length){el.innerHTML='<div class="empty"><div class="empty-icon">🏪</div><p>لا وكلاء في هذه المنطقة بعد</p></div>';return;}
    el.innerHTML=list.map(a=>`<div class="agent-card">
      <div class="agent-icon">🏪</div>
      <div style="flex:1;min-width:0">
        <div class="agent-name">${a.business_name}</div>
        <div class="agent-city">📍 ${a.city||''} ${a.governorate?'— '+a.governorate:''}</div>
        <div class="agent-code">${a.agent_code}</div>
      </div>
      <div class="agent-actions">
        <button class="btn-cashin" onclick="doTransaction('cashin','${a.agent_code}')">+ شحن</button>
        <button class="btn-cashout" onclick="doTransaction('cashout','${a.agent_code}')">- سحب</button>
      </div>
    </div>`).join('');
  },400);
}
function doTransaction(type, agentCode){
  const amt=parseFloat(prompt(type==='cashin'?'كم تريد أن يشحن الوكيل في محفظتك؟ (SDG)':'كم تريد سحبه نقداً؟ (SDG)'));
  if(!amt||amt<100){alert('الحد الأدنى 100 SDG');return;}
  if(type==='cashout'){
    const pin=prompt('أدخل الرقم السري للسحب');
    if(!pin)return;
    fetch('/api/pay/cashout.php',{method:'POST',headers:{'Content-Type':'application/json'},
      body:JSON.stringify({agent_code:agentCode,amount:amt,pin})})
    .then(r=>r.json()).then(d=>{
      if(d.ok)alert('✅ طلب السحب مُرسَل للوكيل. اذهب إليه الآن.');
      else alert('❌ '+(d.message||d.error));
    });
  } else {
    alert('🏪 أعطِ الوكيل هذا الكود: '+agentCode+'
والمبلغ: '+Number(amt).toLocaleString('ar-SA')+' SDG
سيشحن محفظتك فوراً.');
  }
}
searchAgents('');
</script></body></html>