<?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;--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">فتح نزاع — Sudanna Pay</div>
  <a href="/app/hub.php" class="back">← الهب</a>
</header>
<div class="container">

<div class="card">
  <div class="card-title">⚖️ فتح نزاع على معاملة</div>
  <div class="field"><label>رقم المعاملة *</label>
    <input type="text" id="d-ref" placeholder="TRF20241028..." dir="ltr" id="d-txnref"></div>
  <div class="field"><label>سبب النزاع *</label>
    <select id="d-reason">
      <option value="not_received">التحويل لم يصل للمستلم</option>
      <option value="wrong_amount">المبلغ خاطئ</option>
      <option value="unauthorized">معاملة غير مصرح بها</option>
      <option value="duplicate">معاملة مكررة</option>
      <option value="service_not_delivered">الخدمة لم تُقدَّم</option>
      <option value="refund_request">طلب استرداد</option>
      <option value="other">أخرى</option>
    </select></div>
  <div class="field"><label>الوصف التفصيلي *</label>
    <textarea id="d-desc" placeholder="اشرح ما حدث بالتفصيل..."></textarea></div>
  <button class="btn" onclick="submitDispute()">⚖️ رفع النزاع</button>
</div>
<div class="card" id="d-status-card" style="display:none">
  <div class="card-title">📋 نزاعاتي</div>
  <div id="d-list"><div class="skel"></div></div>
</div>
<div style="background:#fef3c7;border:1px solid #fbbf24;border-radius:12px;padding:12px;margin-bottom:12px;font-size:.78rem;color:#92400e">
  ⏱️ نراجع النزاع خلال 48 ساعة عمل. احتفظ بأدلتك.
</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 txnFromUrl = new URLSearchParams(location.search).get('txn_ref');
if(txnFromUrl) document.getElementById('d-txnref').value = txnFromUrl;
async function submitDispute(){
  const ref  = document.getElementById('d-txnref')?.value.trim() || document.getElementById('d-ref')?.value.trim();
  const reason = document.getElementById('d-reason').value;
  const desc = document.getElementById('d-desc').value.trim();
  if(!ref||!desc){alert('أدخل رقم المعاملة والوصف');return;}
  const btn = document.querySelector('.btn');
  btn.disabled=true; btn.textContent='جاري الإرسال...';
  try{
    const r = await fetch('/api/pay/dispute.php',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({txn_ref:ref,reason,description:desc})});
    const d = await r.json();
    if(d.ok||d.dispute_id){alert('✅ تم رفع النزاع #'+d.dispute_id+'. سنتواصل خلال 48 ساعة.');}
    else alert('❌ '+(d.message||d.error));
  }catch(e){alert('تعذّر الإرسال');}
  btn.disabled=false; btn.textContent='⚖️ رفع النزاع';
}

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