<?php
declare(strict_types=1);

if (session_status() !== PHP_SESSION_ACTIVE) {
    session_start();
}

require_once __DIR__ . '/db.php';
require_once __DIR__ . '/inc/layout.php';

if (empty($_SESSION['user_id'])) {
    header("Location: index.html?e=1");
    exit;
}

$pdo = db();
if (!$pdo instanceof PDO) {
    http_response_code(500);
    exit('DB not ready');
}

$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);

/* =========================
   Helpers
========================= */
function table_exists_local(PDO $pdo, string $table): bool
{
    $st = $pdo->prepare("
        SELECT COUNT(*)
        FROM information_schema.tables
        WHERE table_schema = DATABASE()
          AND table_name = ?
    ");
    $st->execute([$table]);
    return ((int)$st->fetchColumn()) > 0;
}

function column_exists_local(PDO $pdo, string $table, string $column): bool
{
    $st = $pdo->prepare("
        SELECT COUNT(*)
        FROM information_schema.columns
        WHERE table_schema = DATABASE()
          AND table_name = ?
          AND column_name = ?
    ");
    $st->execute([$table, $column]);
    return ((int)$st->fetchColumn()) > 0;
}
function status_label_ar(?string $status): string
{
    $s = strtoupper(trim((string)$status));

    $map = [
        'NEW'         => 'جديدة',
        'OPEN'        => 'مفتوحة',
        'WORK'        => 'قيد العمل',
        'IN_PROGRESS' => 'قيد العمل',
        'PENDING'     => 'معلقة',
        'CLOSED'      => 'مغلقة',
        'ARCHIVED'    => 'مؤرشفة',
        'POSTPONED'   => 'مؤجلة',
        'COMPLETED'   => 'مكتملة',
        'CANCELLED'   => 'ملغاة',
        'COURT'       => 'منظورة أمام المحكمة',
    ];

    return $map[$s] ?? ($s !== '' ? $s : 'غير محدد');
}
function status_class(?string $status): string
{
    $s = strtoupper(trim((string)$status));

    $map = [
        'NEW'         => 'st-new',
        'OPEN'        => 'st-open',
        'WORK'        => 'st-progress',
        'IN_PROGRESS' => 'st-progress',
        'PENDING'     => 'st-pending',
        'CLOSED'      => 'st-closed',
        'ARCHIVED'    => 'st-archived',
        'POSTPONED'   => 'st-pending',
        'COMPLETED'   => 'st-closed',
        'CANCELLED'   => 'st-archived',
        'COURT'       => 'st-open',
    ];

    return $map[$s] ?? 'st-default';
}function fmt_date(?string $v): string
{
    $v = trim((string)$v);

    if ($v === '' || $v === '0000-00-00' || $v === '0000-00-00 00:00:00') {
        return '—';
    }

    $ts = strtotime($v);
    if (!$ts) {
        return $v;
    }

    return date('Y-m-d h:i A', $ts);
}

function safe_back_url(): string
{
    $fallback = u('cases.php');

    if (!empty($_SERVER['HTTP_REFERER'])) {
        $ref = (string)$_SERVER['HTTP_REFERER'];
        $refHost = (string)(parse_url($ref, PHP_URL_HOST) ?? '');
        $myHost  = (string)($_SERVER['HTTP_HOST'] ?? '');

        if ($refHost !== '' && $myHost !== '' && strtolower($refHost) === strtolower($myHost)) {
            return $ref;
        }
    }

    return $fallback;
}

/* =========================
   Input / Session
========================= */
$caseId = (int)($_GET['case_id'] ?? $_GET['id'] ?? 0);
if ($caseId <= 0) {
    http_response_code(400);
    exit('Case ID missing');
}

$role            = current_role();
$isOwner         = is_owner();
$isAdmin         = is_admin();
$sessionBranchId = current_branch_id();

/* =========================
   Detect structure
========================= */
if (!table_exists_local($pdo, 'cases')) {
    http_response_code(500);
    exit('جدول cases غير موجود');
}

$hasBranchesTable = table_exists_local($pdo, 'branches');
$hasClientsTable  = table_exists_local($pdo, 'clients');

$hasCaseNo        = column_exists_local($pdo, 'cases', 'case_no');
$hasTitle         = column_exists_local($pdo, 'cases', 'title');
$hasStatus        = column_exists_local($pdo, 'cases', 'status');
$hasBranchId      = column_exists_local($pdo, 'cases', 'branch_id');
$hasClientName    = column_exists_local($pdo, 'cases', 'client_name');
$hasClientId      = column_exists_local($pdo, 'cases', 'client_id');
$hasCaseType      = column_exists_local($pdo, 'cases', 'case_type');
$hasCourt         = column_exists_local($pdo, 'cases', 'court');
$hasDescription   = column_exists_local($pdo, 'cases', 'description');
$hasNotes         = column_exists_local($pdo, 'cases', 'notes');
$hasNextSession   = column_exists_local($pdo, 'cases', 'next_session_date');
$hasCreatedAt     = column_exists_local($pdo, 'cases', 'created_at');
$hasUpdatedAt     = column_exists_local($pdo, 'cases', 'updated_at');
$hasSecret        = column_exists_local($pdo, 'cases', 'is_secret');

$clientsHasId     = $hasClientsTable && column_exists_local($pdo, 'clients', 'id');
$clientsHasNameAr = $hasClientsTable && column_exists_local($pdo, 'clients', 'name_ar');
$clientsHasName   = $hasClientsTable && column_exists_local($pdo, 'clients', 'name');

/* =========================
   Build query
========================= */
$selects = ["c.id"];
$selects[] = $hasCaseNo      ? "c.case_no" : "CAST(c.id AS CHAR) AS case_no";
$selects[] = $hasTitle       ? "c.title" : "'' AS title";
$selects[] = $hasStatus      ? "c.status" : "'OPEN' AS status";
$selects[] = $hasBranchId    ? "c.branch_id" : ((int)$sessionBranchId . " AS branch_id");
$selects[] = $hasCaseType    ? "c.case_type" : "'' AS case_type";
$selects[] = $hasCourt       ? "c.court" : "'' AS court";
$selects[] = $hasDescription ? "c.description" : "'' AS description";
$selects[] = $hasNotes       ? "c.notes" : "'' AS notes";
$selects[] = $hasNextSession ? "c.next_session_date" : "NULL AS next_session_date";
$selects[] = $hasCreatedAt   ? "c.created_at" : "NULL AS created_at";
$selects[] = $hasUpdatedAt   ? "c.updated_at" : "NULL AS updated_at";
$selects[] = $hasSecret      ? "COALESCE(c.is_secret,0) AS is_secret" : "0 AS is_secret";

if ($hasClientName) {
    $selects[] = "c.client_name";
} elseif ($hasClientId && $hasClientsTable && $clientsHasId && ($clientsHasNameAr || $clientsHasName)) {
    if ($clientsHasNameAr) {
        $selects[] = "COALESCE(cl.name_ar,'') AS client_name";
    } else {
        $selects[] = "COALESCE(cl.name,'') AS client_name";
    }
} else {
    $selects[] = "'' AS client_name";
}

$joins = [];
if (!$hasClientName && $hasClientId && $hasClientsTable && $clientsHasId && ($clientsHasNameAr || $clientsHasName)) {
    $joins[] = "LEFT JOIN clients cl ON cl.id = c.client_id";
}

$where  = ["c.id = ?"];
$params = [$caseId];

/* الموظف يشوف فقط قضايا فرعه */
if ($hasBranchId && !$isAdmin) {
    $where[]  = "c.branch_id = ?";
    $params[] = $sessionBranchId;
}

$sql = "
    SELECT " . implode(",\n", $selects) . "
    FROM cases c
    " . implode("\n", $joins) . "
    WHERE " . implode(' AND ', $where) . "
    LIMIT 1
";

$case = [];
try {
    $st = $pdo->prepare($sql);
    $st->execute($params);
    $case = (array)$st->fetch(PDO::FETCH_ASSOC);
} catch (Throwable $e) {
    $case = [];
}

if (empty($case)) {
    http_response_code(404);
    exit('القضية غير موجودة أو غير مصرح لك بعرضها');
}

/* القضية السرية للـ OWNER فقط */
if (!empty($case['is_secret']) && !$isOwner) {
    http_response_code(403);
    exit('هذه القضية سرية ولا يمكن فتحها.');
}

/* =========================
   Derived values
========================= */
$caseBranchId   = (int)($case['branch_id'] ?? $sessionBranchId);
$caseBranchName = branch_name_ar($caseBranchId);

if ($hasBranchesTable) {
    try {
        $st = $pdo->prepare("SELECT name_ar FROM branches WHERE id = ? LIMIT 1");
        $st->execute([$caseBranchId]);
        $caseBranchName = (string)($st->fetchColumn() ?: $caseBranchName);
    } catch (Throwable $e) {
    }
}

$filesCount = 0;
try {
    if (table_exists_local($pdo, 'case_files')) {
        $st = $pdo->prepare("SELECT COUNT(*) FROM case_files WHERE case_id = ?");
        $st->execute([$caseId]);
        $filesCount = (int)$st->fetchColumn();
    }
} catch (Throwable $e) {
    $filesCount = 0;
}

$caseNo      = trim((string)($case['case_no'] ?? $caseId));
$title       = trim((string)($case['title'] ?? 'بدون عنوان'));
$status      = (string)($case['status'] ?? '');
$statusAr    = status_label_ar($status);
$statusCss   = status_class($status);
$clientName  = trim((string)($case['client_name'] ?? ''));
$caseType    = trim((string)($case['case_type'] ?? ''));
$court       = trim((string)($case['court'] ?? ''));
$description = trim((string)($case['description'] ?? ''));
$notes       = trim((string)($case['notes'] ?? ''));
$nextSession = trim((string)($case['next_session_date'] ?? ''));
$createdAt   = trim((string)($case['created_at'] ?? ''));
$updatedAt   = trim((string)($case['updated_at'] ?? ''));
$isSecret    = !empty($case['is_secret']);
$backUrl     = safe_back_url();

/* =========================
   Render layout
========================= */
render_layout_start('تفاصيل القضية');
?>

<style>
  :root{
    --bg-main:#07171d;
    --bg-card:rgba(255,255,255,.05);
    --bg-card-2:rgba(255,255,255,.04);
    --line:rgba(255,255,255,.08);
    --line-soft:rgba(255,255,255,.05);
    --text:#ffffff;
    --muted:rgba(255,255,255,.68);
    --gold:#d4af37;
    --gold-2:#ffd76a;
    --gold-soft:rgba(212,175,55,.14);
    --danger:#f87171;
    --danger-soft:rgba(248,113,113,.12);
    --success:#34d399;
    --success-soft:rgba(52,211,153,.12);
    --blue:#60a5fa;
    --blue-soft:rgba(96,165,250,.12);
    --warning:#fbbf24;
    --warning-soft:rgba(251,191,36,.12);
    --shadow:0 18px 40px rgba(0,0,0,.16);
  }

  body{
    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%),
      #021d23;
  }

  .page-shell{
    display:grid;
    gap:14px;
  }

  .hero-card,
  .content-card{
    background:var(--bg-card);
    border:1px solid var(--line);
    border-radius:24px;
    box-shadow:var(--shadow);
    overflow:hidden;
    color:var(--text);
    backdrop-filter:blur(12px);
  }

  .hero-card.secret{
    border-color:rgba(248,113,113,.24);
    background:
      linear-gradient(180deg, rgba(127,29,29,.16), rgba(255,255,255,.04));
    box-shadow:0 14px 36px rgba(185,28,28,.14);
  }

  .secret-strip{
    height:6px;
    background:linear-gradient(90deg,#7f1d1d,#dc2626);
  }

  .hero-head{
    padding:24px;
    display:flex;
    align-items:flex-start;
    justify-content:space-between;
    gap:14px;
    flex-wrap:wrap;
  }

  .hero-title-wrap{
    min-width:0;
    flex:1 1 auto;
  }

  .case-no{
    color:var(--muted);
    font-size:14px;
    margin-bottom:8px;
  }

  .hero-title{
    margin:0;
    font-size:42px;
    line-height:1.15;
    color:#ffffff;
    word-break:break-word;
  }

  .hero-sub{
    margin-top:10px;
    display:flex;
    gap:10px;
    flex-wrap:wrap;
    color:var(--muted);
    font-size:15px;
  }

  .badges{
    display:flex;
    gap:8px;
    flex-wrap:wrap;
    justify-content:flex-end;
  }

  .badge{
    display:inline-flex;
    align-items:center;
    justify-content:center;
    min-height:38px;
    padding:0 14px;
    border-radius:999px;
    font-size:13px;
    font-weight:900;
    border:1px solid var(--line);
    background:rgba(255,255,255,.05);
    color:#ffffff;
    white-space:nowrap;
  }

  .badge.secret{
    background:var(--danger-soft);
    color:#fecaca;
    border-color:rgba(248,113,113,.22);
  }

  .st-new{
    background:rgba(34,211,238,.10);
    color:#67e8f9;
    border-color:rgba(34,211,238,.18);
  }

  .st-open{
    background:var(--blue-soft);
    color:var(--blue);
    border-color:rgba(96,165,250,.18);
  }

  .st-progress{
    background:var(--warning-soft);
    color:var(--warning);
    border-color:rgba(251,191,36,.20);
  }

  .st-pending{
    background:rgba(251,146,60,.12);
    color:#fdba74;
    border-color:rgba(251,146,60,.20);
  }

  .st-closed{
    background:var(--success-soft);
    color:var(--success);
    border-color:rgba(52,211,153,.18);
  }

  .st-archived{
    background:rgba(148,163,184,.14);
    color:#cbd5e1;
    border-color:rgba(148,163,184,.18);
  }

  .st-default{
    background:rgba(255,255,255,.05);
    color:#e2e8f0;
    border-color:var(--line);
  }

  .quick-stats{
    padding:0 24px 22px;
    display:grid;
    grid-template-columns:repeat(4,minmax(0,1fr));
    gap:12px;
  }

  .mini-stat{
    background:rgba(255,255,255,.04);
    border:1px solid var(--line);
    border-radius:18px;
    padding:14px;
  }

  .mini-stat .k{
    color:var(--muted);
    font-size:12px;
    margin-bottom:6px;
  }

  .mini-stat .v{
    color:#ffffff;
    font-size:20px;
    font-weight:900;
    line-height:1.2;
    word-break:break-word;
  }

.content-grid{
  display:grid;
  grid-template-columns:minmax(0, 1.7fr) minmax(320px, .8fr);
  gap:22px;
  align-items:start;
  width:100%;
}

  .card-head{
    padding:16px 18px;
    border-bottom:1px solid var(--line);
    display:flex;
    align-items:center;
    justify-content:space-between;
    gap:10px;
    background:rgba(255,255,255,.04);
  }

  .card-head .title{
    font-size:18px;
    color:#ffffff;
    font-weight:900;
  }

  .card-body{
    padding:18px;
  }

  .details-grid{
    display:grid;
    grid-template-columns:repeat(2,minmax(0,1fr));
    gap:12px;
  }

  .info{
    background:rgba(255,255,255,.04);
    border:1px solid var(--line);
    border-radius:18px;
    padding:14px;
    min-height:88px;
  }

  .info .k{
    color:var(--muted);
    font-size:12px;
    margin-bottom:7px;
  }

  .info .v{
    color:#ffffff;
    font-size:15px;
    line-height:1.7;
    word-break:break-word;
    white-space:pre-wrap;
    text-align:right;
    font-weight:700;
  }

  .text-block{
    background:rgba(255,255,255,.04);
    border:1px solid var(--line);
    border-radius:18px;
    padding:16px;
    color:#ffffff;
    font-size:15px;
    line-height:1.9;
    white-space:pre-wrap;
    text-align:right;
    min-height:120px;
  }

  .empty-text{
    color:var(--muted);
  }

  .actions-list{
    display:grid;
    gap:10px;
  }

  .action-btn{
    min-height:50px;
    border-radius:14px;
    border:1px solid var(--line);
    background:rgba(255,255,255,.05);
    display:flex;
    align-items:center;
    justify-content:center;
    gap:8px;
    font-weight:900;
    color:#ffffff;
    padding:12px 14px;
    transition:.18s ease;
  }

  .action-btn:hover{
    background:rgba(255,255,255,.08);
    transform:translateY(-1px);
  }

  .action-btn.primary{
    background:linear-gradient(135deg,#ffd76a,#d4af37);
    border-color:rgba(212,175,55,.25);
    color:#1d1600;
    box-shadow:0 12px 24px rgba(212,175,55,.16);
  }

  .action-btn.primary:hover{
    transform:translateY(-2px);
    box-shadow:0 16px 28px rgba(212,175,55,.22);
  }

  .action-btn.warn{
    background:rgba(251,191,36,.12);
    border-color:rgba(251,191,36,.22);
    color:#fbbf24;
  }

  .action-btn.danger{
    background:rgba(248,113,113,.10);
    border-color:rgba(248,113,113,.22);
    color:#fca5a5;
  }

  .top-local-actions{
    display:flex;
    gap:10px;
    flex-wrap:wrap;
  }

  .top-local-btn{
    display:inline-flex;
    align-items:center;
    justify-content:center;
    min-height:46px;
    padding:0 14px;
    border-radius:14px;
    border:1px solid var(--line);
    background:rgba(255,255,255,.05);
    color:#ffffff;
    font-weight:900;
    transition:.18s ease;
  }

  .top-local-btn:hover{
    background:rgba(255,255,255,.08);
    transform:translateY(-1px);
  }

  .top-local-btn.primary{
    background:linear-gradient(135deg,#ffd76a,#d4af37);
    border-color:rgba(212,175,55,.25);
    color:#1d1600;
    box-shadow:0 12px 24px rgba(212,175,55,.16);
  }

  .top-local-btn.primary:hover{
    transform:translateY(-2px);
    box-shadow:0 16px 28px rgba(212,175,55,.22);
  }

  .footerNote{
    margin-top:18px;
    padding:14px 18px;
    border-radius:16px;
    background:rgba(255,255,255,.04);
    border:1px solid rgba(255,255,255,.08);
    backdrop-filter:blur(8px);
    color:rgba(255,255,255,.75);
    font-size:13px;
  }

  .footer-inner{
    display:flex;
    justify-content:space-between;
    align-items:center;
    flex-wrap:wrap;
    gap:10px;
  }

  .footer-copy{
    color:#ffd76a !important;
    font-weight:700;
    letter-spacing:.3px;
  }

  .footer-system{
    font-weight:800;
    color:#d4af37;
    letter-spacing:.5px;
  }

  @media (max-width: 1180px){
    .quick-stats{
      grid-template-columns:repeat(2,minmax(0,1fr));
    }

    .content-grid{
      grid-template-columns:1fr;
    }
  }

  @media (max-width: 700px){
    .hero-title{
      font-size:28px;
    }

    .quick-stats{
      grid-template-columns:1fr;
    }

    .details-grid{
      grid-template-columns:1fr;
    }

    .hero-head,
    .quick-stats,
    .card-body{
      padding-right:14px;
      padding-left:14px;
    }

    .footer-inner{
      flex-direction:column;
      text-align:center;
    }
  }
  .page-shell{
  display:grid;
  gap:20px;
  max-width:1400px;
  margin:auto;
  padding:0 16px;
}
.hero-head{
  padding:30px;
}

.quick-stats{
  padding:0 30px 28px;
}

.card-body{
  padding:24px;
}
.hero-card,
.content-card{
  width:100%;
}
.page-shell{
  width:100% !important;
  max-width:none !important;
  margin:0 !important;
  padding:0 22px 30px !important;
  display:grid !important;
  gap:22px !important;
  box-sizing:border-box !important;
}

.hero-card,
.content-card{
  width:100% !important;
  max-width:none !important;
  box-sizing:border-box !important;
}

.content-grid{
  display:grid !important;
  grid-template-columns:minmax(0, 1.7fr) minmax(340px, .8fr) !important;
  gap:22px !important;
  align-items:start !important;
  width:100% !important;
}

.hero-head{
  padding:32px !important;
}

.quick-stats{
  padding:0 32px 30px !important;
  grid-template-columns:repeat(4,minmax(0,1fr)) !important;
}

.card-body{
  padding:26px !important;
}

.info{
  padding:18px !important;
}

.action-btn{
  min-height:58px !important;
}

@media (max-width:1180px){
  .content-grid{
    grid-template-columns:1fr !important;
  }

  .quick-stats{
    grid-template-columns:repeat(2,minmax(0,1fr)) !important;
  }
}

@media (max-width:700px){
  .page-shell{
    padding:0 12px 24px !important;
  }

  .quick-stats,
  .details-grid{
    grid-template-columns:1fr !important;
  }
}
</style>

<div class="page-shell">

  <div class="top-local-actions">
    <a class="top-local-btn" href="<?= h($backUrl) ?>">رجوع</a>
    <a class="top-local-btn primary" href="<?= h(u('case_files.php?id=' . $caseId)) ?>">المستندات</a>
    <?php if ($isAdmin): ?>
      <a class="top-local-btn" href="<?= h(u('case_edit.php?id=' . $caseId)) ?>">تعديل</a>
    <?php endif; ?>
  </div>

  <section class="hero-card <?= $isSecret ? 'secret' : '' ?>">
    <?php if ($isSecret): ?>
      <div class="secret-strip"></div>
    <?php endif; ?>

    <div class="hero-head">
      <div class="hero-title-wrap">
        <div class="case-no">رقم القضية: <?= h($caseNo) ?></div>
        <h1 class="hero-title"><?= h($title !== '' ? $title : 'بدون عنوان') ?></h1>
        <div class="hero-sub">
          <span>الفرع: <?= h($caseBranchName) ?></span>
          <?php if ($clientName !== ''): ?>
            <span>العميل: <?= h($clientName) ?></span>
          <?php endif; ?>
        </div>
      </div>

      <div class="badges">
        <?php if ($isSecret): ?>
          <span class="badge secret">قضية سرية</span>
        <?php endif; ?>
        <span class="badge <?= h($statusCss) ?>"><?= h($statusAr) ?></span>
      </div>
    </div>

    <div class="quick-stats">
      <div class="mini-stat">
        <div class="k">الحالة</div>
        <div class="v"><?= h($statusAr) ?></div>
      </div>

      <div class="mini-stat">
        <div class="k">المرفقات</div>
        <div class="v"><?= (int)$filesCount ?></div>
      </div>

      <div class="mini-stat">
        <div class="k">الجلسة القادمة</div>
        <div class="v"><?= h($nextSession !== '' ? fmt_date($nextSession) : '—') ?></div>
      </div>

      <div class="mini-stat">
        <div class="k">آخر تحديث</div>
        <div class="v"><?= h($updatedAt !== '' ? fmt_date($updatedAt) : ($createdAt !== '' ? fmt_date($createdAt) : '—')) ?></div>
      </div>
    </div>
  </section>

  <section class="content-grid">
    <div class="content-card">
      <div class="card-head">
        <div class="title">تفاصيل القضية</div>
      </div>

      <div class="card-body">
        <div class="details-grid">
          <div class="info">
            <div class="k">رقم القضية</div>
            <div class="v"><?= h($caseNo) ?></div>
          </div>

          <div class="info">
            <div class="k">عنوان القضية</div>
            <div class="v"><?= h($title !== '' ? $title : '—') ?></div>
          </div>

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

          <div class="info">
            <div class="k">الفرع</div>
            <div class="v"><?= h($caseBranchName) ?></div>
          </div>

          <div class="info">
            <div class="k">نوع القضية</div>
            <div class="v"><?= h($caseType !== '' ? $caseType : '—') ?></div>
          </div>

          <div class="info">
            <div class="k">المحكمة</div>
            <div class="v"><?= h($court !== '' ? $court : '—') ?></div>
          </div>

          <div class="info">
            <div class="k">الحالة</div>
            <div class="v"><?= h($statusAr) ?></div>
          </div>

          <div class="info">
            <div class="k">تاريخ الإنشاء</div>
            <div class="v"><?= h($createdAt !== '' ? fmt_date($createdAt) : '—') ?></div>
          </div>
        </div>

        <div style="height:14px"></div>

        <div class="info" style="min-height:auto">
          <div class="k">وصف القضية</div>
          <div class="text-block <?= $description === '' ? 'empty-text' : '' ?>">
            <?= h($description !== '' ? $description : 'لا يوجد وصف لهذه القضية.') ?>
          </div>
        </div>

        <div style="height:14px"></div>

        <div class="info" style="min-height:auto">
          <div class="k">ملاحظات</div>
          <div class="text-block <?= $notes === '' ? 'empty-text' : '' ?>">
            <?= h($notes !== '' ? $notes : 'لا توجد ملاحظات.') ?>
          </div>
        </div>
      </div>
    </div>

    <div class="content-card">
      <div class="card-head">
        <div class="title">إجراءات سريعة</div>
      </div>

      <div class="card-body">
        <div class="actions-list">
          <a class="action-btn primary" href="<?= h(u('case_files.php?id=' . $caseId)) ?>">فتح مستندات القضية</a>

          <?php if ($isAdmin): ?>
            <a class="action-btn warn" href="<?= h(u('case_edit.php?id=' . $caseId)) ?>">تعديل بيانات القضية</a>
          <?php endif; ?>

          <a class="action-btn" href="<?= h(u('cases.php')) ?>">العودة إلى قائمة القضايا</a>

          <?php if ($isSecret): ?>
            <div class="action-btn danger" style="cursor:default">
              هذه القضية مصنفة كقضية سرية
            </div>
          <?php endif; ?>
        </div>
      </div>
    </div>
  </section>

</div>

<?php render_layout_end(); ?>