<?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('قضاياي');
?>
<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="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></tr></thead>
      <tbody>
      <?php foreach ($cases as $c):
        $st = CASE_STATUS[$c['status']] ?? ['label'=>$c['status'],'class'=>'secondary'];
      ?>
      <tr>
        <td><strong><?= htmlspecialchars($c['case_number']) ?></strong></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>
      </tr>
      <?php endforeach; ?>
      </tbody>
    </table></div>
    <?php endif; ?>
  </div>
</div></div>
<?php renderFoot(); ?>