<?php
require_once dirname(__DIR__).'/config.php';
$u = current_user(); if(!$u){header('Location: ' . url('?login=1'));exit;}
?>
require_once dirname(__DIR__).'/config.php';
$session = requireAuth();
$user    = $session['user'];
?><!DOCTYPE html>
<html lang="ar" dir="rtl">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width,initial-scale=1">
  <title>الاستشارات القانونية — سودانا</title>
  <style>
    :root{--green:#00d97e;--bg:#f8fafc;--card:#fff;--text:#1e293b;--muted:#64748b;--border:#e2e8f0;--gold:#f59e0b}
    *{box-sizing:border-box;margin:0;padding:0}
    body{font-family:'Cairo',system-ui,sans-serif;background:var(--bg);color:var(--text);min-height:100vh}
    
    /* Header */
    .header{background:#fff;border-bottom:1px solid var(--border);padding:0 20px;height:56px;display:flex;align-items:center;justify-content:space-between;position:sticky;top:0;z-index:100;box-shadow:0 1px 4px rgba(0,0,0,.06)}
    .logo{font-weight:900;font-size:1.1rem;color:#1e293b;display:flex;align-items:center;gap:8px}
    .logo span{background:#a78bfa;color:#fff;width:28px;height:28px;border-radius:8px;display:flex;align-items:center;justify-content:center;font-size:.85rem}
    .back-btn{background:none;border:1px solid var(--border);color:var(--muted);padding:6px 14px;border-radius:8px;cursor:pointer;font-family:inherit;font-size:.82rem;display:flex;align-items:center;gap:6px;text-decoration:none}
    
    /* Hero */
    .hero{background:linear-gradient(135deg,#a78bfa18,#a78bfa05);border-bottom:1px solid var(--border);padding:28px 20px 22px}
    .hero h1{font-size:1.5rem;font-weight:900;color:#1e293b;margin-bottom:6px}
    .hero p{font-size:.88rem;color:var(--muted);max-width:480px;line-height:1.6}
    .hero-icon{font-size:2.5rem;margin-bottom:10px}
    
    /* Container */
    .container{max-width:900px;margin:0 auto;padding:20px}
    
    /* Cards */
    .card{background:var(--card);border:1px solid var(--border);border-radius:16px;padding:20px;margin-bottom:16px;box-shadow:0 1px 6px rgba(0,0,0,.04)}
    .card h2{font-size:1rem;font-weight:800;color:#1e293b;margin-bottom:14px;display:flex;align-items:center;gap:8px}
    
    /* Feature grid */
    .features{display:grid;grid-template-columns:repeat(auto-fit,minmax(160px,1fr));gap:12px;margin-bottom:16px}
    .feat{background:#f8fafc;border:1px solid var(--border);border-radius:12px;padding:14px;text-align:center;transition:.2s;cursor:pointer}
    .feat:hover{border-color:#a78bfa;transform:translateY(-2px);box-shadow:0 4px 12px #a78bfa22}
    .feat .feat-icon{font-size:1.4rem;margin-bottom:6px}
    .feat .feat-title{font-size:.78rem;font-weight:700;color:#1e293b;margin-bottom:3px}
    .feat .feat-desc{font-size:.68rem;color:var(--muted);line-height:1.4}
    
    /* Chat input */
    .chat-box{background:#f8fafc;border:1px solid var(--border);border-radius:12px;padding:14px}
    .chat-input{display:flex;gap:8px;margin-bottom:10px}
    .chat-input input{flex:1;background:#fff;border:1px solid var(--border);border-radius:8px;padding:10px 14px;font-family:inherit;font-size:.85rem;color:#1e293b}
    .chat-input input:focus{outline:none;border-color:#a78bfa}
    .chat-input button{background:#a78bfa;color:#fff;border:none;border-radius:8px;padding:10px 18px;cursor:pointer;font-family:inherit;font-weight:700;font-size:.85rem;white-space:nowrap}
    .chat-msgs{min-height:80px;max-height:300px;overflow-y:auto;font-size:.82rem;color:var(--muted)}
    .msg{padding:8px 12px;background:#fff;border:1px solid var(--border);border-radius:8px;margin-bottom:6px;line-height:1.5}
    .msg.ai{border-color:#a78bfa33;background:#a78bfa08}
    
    /* Quick actions */
    .quick-btns{display:flex;gap:8px;flex-wrap:wrap;margin-bottom:12px}
    .q-btn{background:#fff;border:1px solid var(--border);color:#1e293b;padding:7px 13px;border-radius:20px;cursor:pointer;font-family:inherit;font-size:.76rem;transition:.15s}
    .q-btn:hover{border-color:#a78bfa;color:#a78bfa}
    
    /* Responsive */
    @media(max-width:600px){
      .hero{padding:20px 16px 16px}
      .hero h1{font-size:1.2rem}
      .container{padding:14px}
      .features{grid-template-columns:repeat(2,1fr)}
    }
    
    /* Skeleton loader */
    .skel{background:linear-gradient(90deg,#f0f0f0 25%,#e8e8e8 50%,#f0f0f0 75%);background-size:200% 100%;animation:skel 1.4s infinite;border-radius:8px;height:18px;margin-bottom:8px}
    @keyframes skel{0%{background-position:200% 0}100%{background-position:-200% 0}}
    
    /* Animations */
    .fade-in{animation:fadeIn .4s ease}
    @keyframes fadeIn{from{opacity:0;transform:translateY(8px)}to{opacity:1;transform:translateY(0)}}
  </style>
</head>
<body>

<header class="header">
  <div class="logo"><span>⚖️</span> الاستشارات القانونية</div>
  <a href="/app/services.php" class="back-btn">← الرئيسية</a>
</header>

<div class="hero">
  <div class="hero-icon">⚖️</div>
  <h1>الاستشارات القانونية</h1>
  <p>مساعد ذكي متخصص — يفهم السياق السوداني ويوجهك للمتخصص المناسب</p>
</div>

<div class="container">

  <!-- Quick features -->
  <div class="card fade-in">
    <h2>⚡ ما يمكنك فعله</h2>
    <div class="features">
      <div class="feat" onclick="setQ('أحتاج محامياً')"><div class="feat-icon">👨‍⚖️</div><div class="feat-title">محامٍ الآن</div><div class="feat-desc">استشارة قانونية فورية</div></div>
        <div class="feat" onclick="setQ('مراجعة عقد')"><div class="feat-icon">📋</div><div class="feat-title">مراجعة عقد</div><div class="feat-desc">فهم العقود قبل التوقيع</div></div>
        <div class="feat" onclick="setQ('حقوقي في الوظيفة')"><div class="feat-icon">💼</div><div class="feat-title">قانون العمل</div><div class="feat-desc">حقوق العمال في السودان</div></div>
        <div class="feat" onclick="setQ('قانون الأسرة')"><div class="feat-icon">👨‍👩‍👧</div><div class="feat-title">قانون الأسرة</div><div class="feat-desc">زواج، طلاق، ميراث</div></div>
    </div>
  </div>

  <!-- AI Chat -->
  <div class="card fade-in">
    <h2>🤖 اسأل المساعد المتخصص</h2>
    <div class="quick-btns" id="quick-btns"></div>
    <div class="chat-box">
      <div class="chat-msgs" id="chat-msgs">
        <div class="msg ai">👋 أهلاً! أنا مساعد الاستشارات القانونية في سودانا. كيف يمكنني مساعدتك؟</div>
      </div>
      <div class="chat-input" style="margin-top:10px">
        <input type="text" id="chat-q" placeholder="اكتب سؤالك هنا..." onkeydown="if(event.key==='Enter')sendChat()">
        <button onclick="sendChat()">إرسال ←</button>
      </div>
    </div>
  </div>

</div>

<script>
const SERVICE = 'legal';
const QUICK_Q = ['كيف أرفع قضية في المحكمة؟','ما حقوقي في عقد العمل؟','أحتاج محامي عقارات','كيف أسجل شركة في السودان؟'];

// Load quick questions
document.getElementById('quick-btns').innerHTML = QUICK_Q.map(q=>
  `<button class="q-btn" onclick="setQ('${q}')">${q}</button>`
).join('');

function setQ(q){ document.getElementById('chat-q').value=q; sendChat(); }

async function sendChat(){
  const q = document.getElementById('chat-q').value.trim();
  if(!q) return;
  const msgs = document.getElementById('chat-msgs');
  msgs.innerHTML += `<div class="msg">${q}</div>`;
  document.getElementById('chat-q').value='';
  msgs.innerHTML += '<div class="msg ai skel" id="thinking" style="height:40px"></div>';
  msgs.scrollTop = msgs.scrollHeight;
  try{
    const r = await fetch('/api/visitor_ask.php',{
      method:'POST',
      headers:{'Content-Type':'application/json'},
      body: JSON.stringify({q, service: SERVICE, user_id: <?php echo json_encode($user['id']); ?>})
    });
    const d = await r.json();
    document.getElementById('thinking')?.remove();
    msgs.innerHTML += `<div class="msg ai fade-in">${d.answer || d.response || 'حدث خطأ'}</div>`;
    msgs.scrollTop = msgs.scrollHeight;
  }catch(e){
    document.getElementById('thinking')?.remove();
    msgs.innerHTML += '<div class="msg ai" style="color:#ef4444">خطأ في الاتصال — تحقق من اتصالك</div>';
  }
}
</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:#64748b;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>
</body>
</html>