<?php
declare(strict_types=1);

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

date_default_timezone_set('Asia/Dubai');

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

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

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

$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);

$today = date('Y-m-d');
$role  = strtoupper((string)($_SESSION['role'] ?? 'STAFF'));
$branchId = (int)($_SESSION['branch_id'] ?? 1);

$sql = "
    SELECT
        cs.id,
        cs.case_id,
        c.case_no,
        c.title,
        c.client_name,
        c.lawyer_name,
        cs.court_name,
        cs.session_type,
        cs.session_date,
        cs.session_time,
        cs.status,
        b.name_ar AS branch_name
    FROM case_sessions cs
    INNER JOIN cases c ON c.id = cs.case_id
    LEFT JOIN branches b ON b.id = c.branch_id
    WHERE cs.session_date = ?
";

$params = [$today];

if (!in_array($role, ['OWNER', 'ADMIN'], true)) {
    $sql .= " AND c.branch_id = ? ";
    $params[] = $branchId;
}

$sql .= " ORDER BY cs.session_time ASC, cs.id DESC ";

$st = $pdo->prepare($sql);
$st->execute($params);
$rows = $st->fetchAll();

render_layout_start('جلسات اليوم');
?>

<style>
.today-page{color:#0f2740}
.today-page *{box-sizing:border-box}
.today-page .wrap{max-width:1200px;margin:0 auto;padding:18px}
.today-page .head{display:flex;justify-content:space-between;align-items:end;gap:12px;flex-wrap:wrap;margin-bottom:18px}
.today-page h1{margin:0;color:#fff;font-size:30px;font-weight:800}
.today-page .sub{color:rgba(255,255,255,.72);font-size:14px}
.today-page .actions{display:flex;gap:10px;flex-wrap:wrap}
.today-page .btn{display:inline-flex;align-items:center;justify-content:center;height:42px;padding:0 16px;border-radius:14px;text-decoration:none;font-weight:800;border:1px solid #dfe7f1;background:#fff;color:#0f2740}
.today-page .card{background:#fff;border:1px solid #e6edf5;border-radius:20px;box-shadow:0 10px 30px rgba(15,39,64,.07);overflow:hidden}
.today-page .cardHead{padding:18px;border-bottom:1px solid #edf2f7;font-weight:800}
.today-page table{width:100%;border-collapse:collapse}
.today-page th,.today-page td{padding:14px 12px;border-bottom:1px solid #edf2f7;text-align:right}
.today-page th{background:#f8fbff;color:#557087;font-size:13px}
.today-page td{font-size:14px;color:#0f2740}
.today-page .empty{padding:30px;text-align:center;color:#6f8094}
</style>

<div class="today-page">
  <div class="wrap">
    <div class="head">
      <div>
        <h1>جلسات اليوم</h1>
        <div class="sub">الجلسات بتاريخ <?= h($today) ?></div>
      </div>
      <div class="actions">
        <a class="btn" href="hearings.php">رجوع</a>
        <a class="btn" href="hearings_print.php?today=1" target="_blank">طباعة جلسات اليوم</a>
      </div>
    </div>

    <div class="card">
      <div class="cardHead">عدد جلسات اليوم: <?= count($rows) ?></div>

      <?php if (!$rows): ?>
        <div class="empty">لا توجد جلسات اليوم.</div>
      <?php else: ?>
        <table>
          <thead>
            <tr>
              <th>القضية</th>
              <th>العميل</th>
              <th>المحكمة</th>
              <th>نوع الجلسة</th>
              <th>الوقت</th>
              <th>المحامي</th>
              <th>الفرع</th>
            </tr>
          </thead>
          <tbody>
            <?php foreach ($rows as $r): ?>
              <tr>
                <td><?= h((string)$r['title']) ?> <br><small>رقم: <?= h((string)$r['case_no']) ?></small></td>
                <td><?= h((string)$r['client_name']) ?></td>
                <td><?= h((string)$r['court_name']) ?></td>
                <td><?= h((string)$r['session_type']) ?></td>
                <td><?= h((string)$r['session_time']) ?></td>
                <td><?= h((string)$r['lawyer_name']) ?></td>
                <td><?= h((string)($r['branch_name'] ?? '-')) ?></td>
              </tr>
            <?php endforeach; ?>
          </tbody>
        </table>
      <?php endif; ?>
    </div>
  </div>
</div>

<?php render_layout_end(); ?>