<?php
require_once __DIR__ . '/../../config/constants.php';
require_once __DIR__ . '/../../config/database.php';
require_once __DIR__ . '/../../includes/auth.php';
require_once __DIR__ . '/../../includes/rbac.php';
require_once __DIR__ . '/../../includes/functions.php';
require_once __DIR__ . '/../../includes/layout.php';
require_once __DIR__ . '/../../includes/sidebar_helper.php';
requireLogin(); requireRole(ROLE_LAWYER); requireActiveLawyer();
$GLOBALS['BASE_URL_CSS'] = BASE_URL . '/public/assets/css/main.css';
$user = currentUser(); $pdo = db();
$cases = (function() use ($pdo,$user) {
    $s = $pdo->prepare("SELECT c.*,ct.name AS court_name FROM cases c JOIN case_lawyers cl ON c.id=cl.case_id LEFT JOIN courts ct ON c.court_id=ct.id WHERE cl.lawyer_id=? ORDER BY c.created_at DESC");
    $s->execute([$user['id']]); return $s->fetchAll();
})();
$notifCount = getUnreadNotifCount($user['id']);
renderHead('قضاياي');
?>
<style>
/* ── بطاقات الموبايل ── */
.case-card-mobile {
  display: none;
}
@media (max-width: 767px) {
  .case-table-desktop { display: none !important; }
  .case-card-mobile   { display: block; }

  .case-card {
    background: var(--white);
    border: 1px solid var(--border);
    border-radius: 14px;
    padding: 16px;
    margin-bottom: 12px;
    position: relative;
    transition: box-shadow .2s;
  }
  .case-card:active { box-shadow: 0 4px 16px rgba(0,0,0,.1); }

  .case-card-header {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    margin-bottom: 10px;
    gap: 8px;
  }
  .case-card-number {
    font-size: 15px;
    font-weight: 700;
    color: var(--navy);
    text-decoration: none;
    font-family: var(--font-amiri, serif);
  }
  .case-card-title {
    font-size: 13px;
    color: var(--text-mid);
    margin-bottom: 10px;
    line-height: 1.5;
  }
  .case-card-badges {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin-bottom: 10px;
  }
  .case-card-footer {
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-top: 1px solid var(--border);
    padding-top: 10px;
    margin-top: 4px;
  }
  .case-card-date {
    font-size: 12px;
    color: var(--text-muted);
  }
  .case-card-action {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    padding: 6px 14px;
    border-radius: 8px;
    border: 1px solid var(--gold);
    color: var(--navy);
    background: var(--gold-pale, #fdf8ec);
    font-size: 13px;
    font-weight: 600;
    text-decoration: none;
    transition: background .2s;
  }
  .case-card-action:active { background: var(--gold); }

  .panel-header { padding: 14px 16px; }
  .panel-title  { font-size: 15px; }
}
</style>

<div class="sidebar" id="sidebar"><?php buildSidebar('my_cases','lawyer'); ?></div>
<div class="sidebar-overlay" id="sidebarOverlay"></div>
<div class="main-wrap">
<?php renderTopbar('قضاياي', $notifCount); ?>
<div class="page-content">
  <div class="panel">
    <div class="panel-header">
      <div class="panel-title"><i class="bi bi-folder2-open"></i> قضاياي المسندة (<?= count($cases) ?>)</div>
    </div>

    <?php if (empty($cases)): ?>
    <div class="empty-state py-5">
      <i class="bi bi-folder-x"></i>
      <h5>لا توجد قضايا مسندة إليك حالياً</h5>
      <p>تواصل مع أمين الضبط لإضافتك لقضية.</p>
    </div>

    <?php else: ?>

    <!-- ════ جدول — شاشات كبيرة ════ -->
    <div class="case-table-desktop table-responsive">
      <table class="table dt-table">
        <thead>
          <tr>
            <th>رقم القضية</th><th>العنوان</th><th>النوع</th>
            <th>المدعي</th><th>المدعى عليه</th>
            <th>المرحلة</th><th>الحالة</th><th>التاريخ</th><th></th>
          </tr>
        </thead>
        <tbody>
        <?php foreach ($cases as $c):
          $st = CASE_STATUS[$c['status']] ?? ['label'=>$c['status'],'class'=>'secondary'];
        ?>
        <tr>
          <td><a href="case-view.php?id=<?= $c['id'] ?>" class="text-navy fw-700 text-decoration-none"><?= htmlspecialchars($c['case_number']) ?></a></td>
          <td><?= htmlspecialchars(mb_substr($c['title'],0,40,'UTF-8')) ?></td>
          <td><?= CASE_TYPES[$c['case_type']]??$c['case_type'] ?></td>
          <td><?= htmlspecialchars($c['plaintiff']) ?></td>
          <td><?= htmlspecialchars($c['defendant']) ?></td>
          <td><span class="badge bg-gold" style="color:var(--navy)"><?= STAGES[$c['stage']]??$c['stage'] ?></span></td>
          <td><span class="badge bg-<?= $st['class'] ?>"><?= $st['label'] ?></span></td>
          <td><?= date('d/m/Y',strtotime($c['filing_date'])) ?></td>
          <td><a href="case-view.php?id=<?= $c['id'] ?>" class="action-btn gold" title="عرض"><i class="bi bi-eye"></i></a></td>
        </tr>
        <?php endforeach; ?>
        </tbody>
      </table>
    </div>

    <!-- ════ بطاقات — شاشات الهاتف ════ -->
    <div class="case-card-mobile" style="padding:12px">
      <?php foreach ($cases as $c):
        $st = CASE_STATUS[$c['status']] ?? ['label'=>$c['status'],'class'=>'secondary'];
      ?>
      <div class="case-card">
        <div class="case-card-header">
          <a href="case-view.php?id=<?= $c['id'] ?>" class="case-card-number">
            <?= htmlspecialchars($c['case_number']) ?>
          </a>
          <span class="badge bg-<?= $st['class'] ?>"><?= $st['label'] ?></span>
        </div>

        <div class="case-card-title"><?= htmlspecialchars(mb_substr($c['title'],0,80,'UTF-8')) ?></div>

        <div class="case-card-badges">
          <span class="badge bg-gold" style="color:var(--navy)"><?= STAGES[$c['stage']]??$c['stage'] ?></span>
          <span class="badge bg-light text-dark border"><?= CASE_TYPES[$c['case_type']]??$c['case_type'] ?></span>
          <?php if (!empty($c['court_name'])): ?>
          <span class="badge bg-light text-dark border"><i class="bi bi-building me-1"></i><?= htmlspecialchars($c['court_name']) ?></span>
          <?php endif; ?>
        </div>

        <div style="font-size:12px;color:var(--text-muted);margin-bottom:8px">
          <span><i class="bi bi-person me-1"></i><?= htmlspecialchars($c['plaintiff']) ?></span>
          <span class="mx-2">↔</span>
          <span><?= htmlspecialchars($c['defendant']) ?></span>
        </div>

        <div class="case-card-footer">
          <span class="case-card-date"><i class="bi bi-calendar3 me-1"></i><?= date('d/m/Y',strtotime($c['filing_date'])) ?></span>
          <a href="case-view.php?id=<?= $c['id'] ?>" class="case-card-action">
            <i class="bi bi-eye"></i> عرض
          </a>
        </div>
      </div>
      <?php endforeach; ?>
    </div>

    <?php endif; ?>
  </div>
</div></div>
<?php renderFoot(); ?>