<?php
declare(strict_types=1);
require_once __DIR__ . '/db.php';

function h($s): string { return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8'); }

/* دعم حالتين: db.php يعطي $pdo أو يعطي دالة db() */
if (!isset($pdo) || !($pdo instanceof PDO)) {
  if (function_exists('db')) {
    $pdo = db();
  }
}

/* حالة عربية (متوافق مع PHP 7+) */
function status_ar(string $s): string {
  $s = strtoupper(trim($s));
  switch ($s) {
    case 'NEW':    return 'جديدة';
    case 'WORK':   return 'قيد العمل';
    case 'COURT':  return 'في المحكمة';
    case 'CLOSED': return 'مغلقة';
    default:       return $s !== '' ? $s : '—';
  }
}

/* إدخال الكود: GET فقط (أسهل للعميل والرابط) */
$code = strtoupper(trim((string)($_GET['code'] ?? '')));
$code = preg_replace('/[^A-Z0-9]/', '', $code);

$case = null;
$dbErr = '';

try {
  if (!isset($pdo) || !($pdo instanceof PDO)) {
    throw new RuntimeException('اتصال قاعدة البيانات غير متوفر.');
  }
  $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

  if ($code !== '') {
    $st = $pdo->prepare("
     SELECT
  id, case_no, title, client_name, lawyer_name AS responsible_employee, status,
  next_hearing_date, client_notes
FROM cases
      WHERE client_access_code = ?
      LIMIT 1
    ");
    $st->execute([$code]);
    $case = $st->fetch(PDO::FETCH_ASSOC) ?: null;
  }
} catch (Throwable $e) {
  $dbErr = $e->getMessage();
}

$logo = "https://hamdanlegalgroup.ae/wp-content/uploads/2026/02/logomail.png";
?>
<!doctype html>
<html lang="ar" dir="rtl">
<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width,initial-scale=1" />
  <title>متابعة القضية</title>
  <link href="https://fonts.googleapis.com/css2?family=Tajawal:wght@400;500;700;800&display=swap" rel="stylesheet">
<style>
  :root{
    --bg1:#07171d;
    --bg2:#0b2633;
    --bg3:#021d23;

    --card:rgba(255,255,255,.05);
    --card-2:rgba(255,255,255,.04);
    --line:rgba(255,255,255,.08);
    --line-soft:rgba(255,255,255,.05);

    --text:rgba(255,255,255,.94);
    --muted:rgba(255,255,255,.68);

    --gold:#d4af37;
    --gold-2:#ffd76a;
    --gold-soft:rgba(212,175,55,.14);

    --teal:#0f766e;
    --teal2:#0f4f66;

    --danger:#f87171;
    --danger-soft:rgba(248,113,113,.12);

    --success:#34d399;
    --success-soft:rgba(52,211,153,.12);

    --shadow:0 18px 40px rgba(0,0,0,.16);
    --ring:0 0 0 4px rgba(212,175,55,.12);
  }

  *{box-sizing:border-box}

  html,body{
    margin:0;
    padding:0;
  }

  body{
    font-family:"Tajawal",system-ui,-apple-system,Segoe UI,Arial,sans-serif;
    color:var(--text);
    background:
      radial-gradient(circle at 20% 20%, rgba(10,77,87,.85), transparent 40%),
      radial-gradient(circle at 80% 80%, rgba(13,106,122,.55), transparent 40%),
      var(--bg3);
  }

  a{
    color:inherit;
    text-decoration:none;
  }

  .wrap{
    max-width:920px;
    margin:28px auto;
    padding:0 16px;
  }

  .shell{
    background:rgba(255,255,255,.05);
    border:1px solid rgba(255,255,255,.08);
    border-radius:24px;
    box-shadow:var(--shadow);
    overflow:hidden;
    backdrop-filter:blur(12px);
  }

  .head{
    padding:18px 18px 12px;
    border-bottom:1px solid var(--line);
    background:
      radial-gradient(900px 220px at 50% -30%, rgba(212,175,55,.16), transparent 70%),
      linear-gradient(180deg, rgba(255,255,255,.06), rgba(255,255,255,.03));
  }

  .brand{
    display:flex;
    align-items:center;
    justify-content:space-between;
    gap:12px;
    flex-wrap:wrap;
  }

  .brandLeft{
    display:flex;
    align-items:center;
    gap:12px;
    min-width:0;
  }

  .logo{
    width:46px;
    height:46px;
    border-radius:14px;
    background:rgba(255,255,255,.06);
    border:1px solid rgba(255,255,255,.08);
    display:flex;
    align-items:center;
    justify-content:center;
    overflow:hidden;
    box-shadow:0 10px 25px rgba(0,0,0,.08);
  }

  .logo img{
    width:100%;
    height:100%;
    object-fit:contain;
    padding:8px;
  }

  .titles{
    display:flex;
    flex-direction:column;
    gap:2px;
    min-width:0;
  }

  .t1{
    margin:0;
    font-size:20px;
    font-weight:900;
    color:#fff;
    line-height:1.2;
  }

  .t2{
    color:var(--muted);
    font-size:13px;
  }

  .badgeTop{
    display:inline-flex;
    align-items:center;
    gap:8px;
    padding:10px 12px;
    border-radius:999px;
    border:1px solid rgba(255,255,255,.08);
    background:rgba(255,255,255,.05);
    box-shadow:0 10px 25px rgba(0,0,0,.08);
    font-weight:900;
    color:#fff;
  }

  .dot{
    width:8px;
    height:8px;
    border-radius:50%;
    background:var(--gold);
    box-shadow:0 0 0 4px rgba(212,175,55,.14);
  }

  .sub{
    margin-top:10px;
    color:var(--muted);
    font-size:13.5px;
    line-height:1.8;
  }

  .formBar{
    padding:14px 18px;
    border-bottom:1px solid var(--line);
    background:rgba(255,255,255,.03);
    display:flex;
    gap:10px;
    flex-wrap:wrap;
    align-items:center;
  }

  .inp{
    flex:1 1 320px;
    height:48px;
    border-radius:999px;
    border:1px solid var(--line);
    padding:0 16px;
    background:rgba(255,255,255,.05);
    outline:none;
    font-size:14px;
    color:#fff;
  }

  .inp::placeholder{
    color:rgba(255,255,255,.45);
  }

  .inp:focus{
    border-color:rgba(212,175,55,.45);
    box-shadow:var(--ring);
    background:rgba(255,255,255,.06);
  }

  .btn{
    height:48px;
    border-radius:999px;
    border:1px solid rgba(212,175,55,.25);
    padding:0 18px;
    cursor:pointer;
    font-weight:900;
    color:#1d1600;
    background:linear-gradient(135deg,#ffd76a,#d4af37);
    box-shadow:0 14px 28px rgba(212,175,55,.18);
    transition:.18s ease;
    font-family:inherit;
  }

  .btn:hover{
    transform:translateY(-1px);
    box-shadow:0 16px 30px rgba(212,175,55,.24);
  }

  .body{
    padding:18px;
  }

  .note{
    border:1px dashed rgba(255,255,255,.16);
    background:rgba(255,255,255,.04);
    border-radius:16px;
    padding:14px;
    color:var(--muted);
    line-height:1.9;
  }

  .note.err{
    border-style:solid;
    border-color:rgba(248,113,113,.24);
    background:rgba(127,29,29,.18);
    color:#fecaca;
  }

  .grid{
    display:grid;
    grid-template-columns:repeat(3,1fr);
    gap:12px;
    margin-top:14px;
  }

  @media(max-width:880px){
    .grid{
      grid-template-columns:repeat(2,1fr);
    }
  }

  @media(max-width:620px){
    .grid{
      grid-template-columns:1fr;
    }
  }

  .item{
    border:1px solid var(--line);
    border-radius:16px;
    padding:12px 12px 11px;
    background:linear-gradient(180deg, rgba(255,255,255,.06), rgba(255,255,255,.03));
    box-shadow:0 10px 22px rgba(0,0,0,.06);
    position:relative;
    overflow:hidden;
    backdrop-filter:blur(10px);
  }

  .item:before{
    content:"";
    position:absolute;
    inset:-40px -40px auto auto;
    width:160px;
    height:160px;
    border-radius:50%;
    background:radial-gradient(circle at 30% 30%, rgba(212,175,55,.12), transparent 65%);
    pointer-events:none;
  }

  .k{
    color:var(--muted);
    font-size:12.8px;
  }

  .v{
    margin-top:6px;
    font-weight:900;
    color:#fff;
    line-height:1.5;
    word-break:break-word;
  }

  .statusPill{
    display:inline-flex;
    align-items:center;
    gap:8px;
    padding:8px 12px;
    border-radius:999px;
    border:1px solid rgba(255,255,255,.08);
    background:rgba(255,255,255,.05);
    font-weight:900;
    color:#fff;
  }

  .stDot{
    width:8px;
    height:8px;
    border-radius:50%;
  }

  .st-new .stDot{
    background:#0ea5e9;
    box-shadow:0 0 0 4px rgba(14,165,233,.12);
  }

  .st-work .stDot{
    background:#f59e0b;
    box-shadow:0 0 0 4px rgba(245,158,11,.12);
  }

  .st-court .stDot{
    background:#8b5cf6;
    box-shadow:0 0 0 4px rgba(139,92,246,.12);
  }

  .st-closed .stDot{
    background:#10b981;
    box-shadow:0 0 0 4px rgba(16,185,129,.12);
  }

  .boxFull{
    margin-top:12px;
    border:1px solid var(--line);
    background:linear-gradient(180deg, rgba(255,255,255,.06), rgba(255,255,255,.03));
    border-radius:18px;
    padding:14px;
    box-shadow:0 12px 28px rgba(0,0,0,.06);
    backdrop-filter:blur(10px);
  }

  .boxTitle{
    font-weight:900;
    color:#fff;
    margin:0 0 8px 0;
  }

  .boxTxt{
    color:#eef2f7;
    line-height:2;
    white-space:pre-wrap;
  }

  .foot{
    padding:12px 18px;
    border-top:1px solid var(--line);
    color:var(--muted);
    font-size:12.5px;
    background:rgba(255,255,255,.03);
  }
</style>
</head>

<body>
  <div class="wrap">
    <div class="shell">
      <div class="head">
        <div class="brand">
          <div class="brandLeft">
            <div class="logo"><img src="<?=h($logo)?>" alt="HLG"></div>
            <div class="titles">
              <h1 class="t1">متابعة القضية</h1>
              <div class="t2">أدخل كود الوصول لعرض بيانات قضيتك</div>
            </div>
          </div>

          <div class="badgeTop" title="عرض آمن">
            <span class="dot"></span>
            <span>عرض آمن</span>
          </div>
        </div>

        <div class="sub">
          هذه الصفحة تعرض بيانات القضية المرتبطة بالكود فقط. إذا واجهت مشكلة في الكود تواصل مع المكتب.
        </div>
      </div>

      <form class="formBar" method="get" action="">
        <input class="inp" name="code" autocomplete="off"
               placeholder="مثال: A7K9P2X3QZ" value="<?=h($code)?>">
        <button class="btn" type="submit">عرض</button>
      </form>

      <div class="body">
        <?php if ($dbErr !== ''): ?>
          <div class="note err">خطأ اتصال/استعلام. <?=h($dbErr)?></div>

        <?php elseif ($code === ''): ?>
          <div class="note">الرجاء إدخال كود الوصول ثم اضغط عرض.</div>

        <?php elseif (!$case): ?>
          <div class="note">الكود غير صحيح أو غير مفعّل. تأكد من كتابته كما هو.</div>

        <?php else: ?>
          <?php
            $status = strtoupper((string)($case['status'] ?? ''));
            $stClass = 'st-new';
            if ($status === 'WORK') $stClass = 'st-work';
            elseif ($status === 'COURT') $stClass = 'st-court';
            elseif ($status === 'CLOSED') $stClass = 'st-closed';

            $nextDate = trim((string)($case['next_hearing_date'] ?? ''));
            $nextDate = $nextDate !== '' ? $nextDate : '—';

$clientName = trim((string)($case['client_name'] ?? ''));
$responsibleEmployee = trim((string)($case['responsible_employee'] ?? ''));
$clientNotes = trim((string)($case['client_notes'] ?? ''));          ?>

          <div class="grid">
            <div class="item">
              <div class="k">رقم القضية</div>
              <div class="v"><?=h((string)($case['case_no'] ?? '—'))?></div>
            </div>

            <div class="item">
              <div class="k">نوع القضية</div>
              <div class="v"><?=h((string)($case['title'] ?? '—'))?></div>
            </div>

            <div class="item">
              <div class="k">الحالة</div>
              <div class="v">
                <span class="statusPill <?=h($stClass)?>">
                  <span class="stDot"></span>
                  <span><?=h(status_ar((string)($case['status'] ?? '')))?></span>
                </span>
              </div>
            </div>

            <div class="item">
              <div class="k">اسم العميل</div>
              <div class="v"><?= $clientName !== '' ? h($clientName) : '—' ?></div>
            </div>

            <div class="item">
  <div class="k">الموظف المسؤول</div>
  <div class="v"><?= $responsibleEmployee !== '' ? h($responsibleEmployee) : '—' ?></div>
</div>

            <div class="item">
              <div class="k">الجلسة القادمة</div>
              <div class="v"><?=h($nextDate)?></div>
            </div>

            <div class="item">
              <div class="k">كود الوصول</div>
              <div class="v"><?=h($code)?></div>
            </div>
          </div>

          <?php if ($clientNotes !== ''): ?>
            <div class="boxFull">
              <div class="boxTitle">ملاحظات للعميل</div>
              <div class="boxTxt"><?=nl2br(h($clientNotes))?></div>
            </div>
          <?php endif; ?>

        <?php endif; ?>
      </div>

      <div class="foot">مجموعة حمدان القانونية © جميع الحقوق محفوظة</div>
    </div>
  </div>
</body>
</html>