<?php
session_start();
require_once '../config/database.php';
require_once '../includes/site_settings.php';

if (!isset($_SESSION['user_id'])) { header('Location: ../login.php'); exit(); }

$exam_id    = $_GET['exam_id']    ?? '';
$student_id = $_GET['student_id'] ?? '';

$settings     = get_all_settings($pdo);
$site_name    = $settings['site_name']    ?? 'প্রতিষ্ঠানের নাম';
$site_address = $settings['site_address'] ?? 'ঠিকানা, ফোন: ০১৭xxxxxxxx';
$logo_image   = $settings['logo_image']   ?? '';
$logo_size    = isset($settings['logo_size']) ? intval($settings['logo_size']) : 100;

$exam_details  = null;
$exam_subjects = [];
if ($exam_id) {
    $stmt = $pdo->prepare("SELECT e.*, c.name as class_name, c.section FROM exams e JOIN classes c ON e.class_id = c.id WHERE e.id = ?");
    $stmt->execute([$exam_id]);
    $exam_details = $stmt->fetch();
    if ($exam_details) {
        if ($student_id) {
            $stmt = $pdo->prepare("SELECT es.*, s.name as subject_name, s.code as subject_code FROM exam_subjects es JOIN subjects s ON es.subject_id = s.id JOIN student_subjects ss ON es.subject_id = ss.subject_id WHERE es.exam_id = ? AND ss.student_id = ? ORDER BY es.exam_date, es.start_time");
            $stmt->execute([$exam_id, $student_id]);
        } else {
            $stmt = $pdo->prepare("SELECT es.*, s.name as subject_name, s.code as subject_code FROM exam_subjects es JOIN subjects s ON es.subject_id = s.id WHERE es.exam_id = ? ORDER BY es.exam_date, es.start_time");
            $stmt->execute([$exam_id]);
        }
        $exam_subjects = $stmt->fetchAll();
    }
}

$student = null;
if ($student_id) {
    $stmt = $pdo->prepare("SELECT s.*, c.name as class_name, c.section FROM students s JOIN classes c ON s.class_id = c.id WHERE s.id = ?");
    $stmt->execute([$student_id]);
    $student = $stmt->fetch();
}

$seat_arrangement = null;
if ($exam_id && $student_id) {
    $stmt = $pdo->prepare("SELECT sa.* FROM seat_arrangements sa WHERE sa.exam_id = ? AND sa.student_id = ? LIMIT 1");
    $stmt->execute([$exam_id, $student_id]);
    $seat_arrangement = $stmt->fetch();
}

$all_exams = $pdo->query("SELECT e.id, e.name, c.name as class_name, c.section, e.session, e.group_name FROM exams e JOIN classes c ON e.class_id = c.id ORDER BY e.created_at DESC")->fetchAll();

$students = [];
if ($exam_id && $exam_details) {
    $params = [$exam_details['class_id'], $exam_details['session']];
    if ($exam_details['group_name']) {
        $stmt = $pdo->prepare("SELECT s.id, s.student_id, s.first_name, s.last_name, s.roll_number, s.guardian_name FROM students s WHERE s.class_id = ? AND s.session = ? AND (s.group_name = ? OR UPPER(s.group_name) = UPPER(?)) AND s.status = 'active' ORDER BY s.roll_number");
        $params[] = $exam_details['group_name']; $params[] = $exam_details['group_name'];
    } else {
        $stmt = $pdo->prepare("SELECT s.id, s.student_id, s.first_name, s.last_name, s.roll_number, s.guardian_name FROM students s WHERE s.class_id = ? AND s.session = ? AND s.status = 'active' ORDER BY s.roll_number");
    }
    $stmt->execute($params);
    $students = $stmt->fetchAll();
}

// Photo path
$photo_src = ''; $photo_ok = false;
if ($student && !empty($student['photo'])) {
    foreach (['../'.$student['photo'], $student['photo'], '../uploads/students/'.basename($student['photo'])] as $p) {
        if (file_exists($p)) { $photo_ok = true; $photo_src = $p; break; }
    }
}

$days_bn = ['Sunday'=>'রবিবার','Monday'=>'সোমবার','Tuesday'=>'মঙ্গলবার','Wednesday'=>'বুধবার','Thursday'=>'বৃহস্পতিবার','Friday'=>'শুক্রবার','Saturday'=>'শনিবার'];

// Group subjects by date
$by_date = [];
foreach ($exam_subjects as $r) {
    $d = date('Y-m-d', strtotime($r['exam_date']));
    $by_date[$d][] = $r;
}
?>
<!DOCTYPE html>
<html lang="bn">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>হাজিরা পত্র</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Hind+Siliguri:wght@300;400;500;600;700&display=swap" rel="stylesheet">
<style>
*,*::before,*::after{font-family:'Hind Siliguri',sans-serif!important}
i[class*="fa-"],i[class*="fa-"]::before{font-family:'Font Awesome 6 Free','FontAwesome'!important;font-weight:900!important}
:root{--navy:#1a1a2e;--purple:#7209b7;--teal:#06d6a0;--orange:#fb8500;--pink:#e63946;--green:#006747;--blue:#4361ee;--gold:#ffd700;--cert-padding:14px}
body{background:#f0f2f8;color:#1a1a2e}

/* TOP BAR */
.top-bar{background:linear-gradient(135deg,var(--navy),#16213e);padding:11px 24px;display:flex;align-items:center;justify-content:space-between;box-shadow:0 4px 20px rgba(0,0,0,.25)}
.top-bar .brand{color:#fff;font-size:1.05rem;font-weight:700;text-decoration:none;display:flex;align-items:center;gap:9px}
.top-bar .brand i{color:var(--gold)}
.top-bar .nav-links{display:flex;gap:5px;flex-wrap:wrap}
.top-bar .nav-links a{color:rgba(255,255,255,.8);text-decoration:none;padding:5px 11px;border-radius:20px;font-size:.8rem;transition:all .2s}
.top-bar .nav-links a:hover{background:rgba(255,255,255,.15);color:#fff}

/* FILTER CARD */
.filter-card{background:#fff;border-radius:16px;box-shadow:0 6px 24px rgba(0,0,0,.08);overflow:hidden;margin-bottom:20px}
.filter-card-header{background:linear-gradient(135deg,var(--green),#004d35);padding:13px 20px;color:#fff;display:flex;align-items:center;gap:10px}
.filter-card-header h5{margin:0;font-size:.98rem;font-weight:700}
.filter-card-body{padding:18px 20px}
.form-label{font-size:.78rem;font-weight:700;color:#555;margin-bottom:3px}
.form-control,.form-select{border-radius:10px!important;border:1.5px solid #e0e0e0;font-size:.86rem;padding:7px 11px;transition:border-color .2s,box-shadow .2s}
.form-control:focus,.form-select:focus{border-color:var(--green);box-shadow:0 0 0 3px rgba(6,214,160,.12)}

/* divider */
.filter-divider{display:flex;align-items:center;gap:8px;margin:12px 0 10px;color:#bbb;font-size:.75rem;font-weight:700;text-transform:uppercase;letter-spacing:.5px}
.filter-divider::before,.filter-divider::after{content:'';flex:1;height:1px;background:#ebebeb}

/* BUTTONS */
.btn-view{background:linear-gradient(135deg,var(--green),#004d35);color:#fff;border:none;border-radius:10px;padding:8px 22px;font-weight:700;font-size:.88rem;transition:all .2s}
.btn-view:hover{opacity:.88;transform:translateY(-1px);color:#fff}
.btn-print{background:linear-gradient(135deg,var(--orange),#c96a00);color:#fff;border:none;border-radius:10px;padding:8px 20px;font-weight:700;font-size:.88rem;transition:all .2s}
.btn-print:hover{opacity:.88;color:#fff}
.btn-reset{background:#f0f0f0;color:#555;border:none;border-radius:10px;padding:8px 14px;font-size:.86rem;transition:all .2s}
.btn-reset:hover{background:#e0e0e0}
.btn-quick{background:linear-gradient(135deg,var(--blue),var(--purple));color:#fff;border:none;border-radius:10px;padding:7px 16px;font-weight:700;font-size:.84rem;transition:all .2s}
.btn-quick:hover{opacity:.88;color:#fff}

/* CERT CARD */
.cert-card{background:#fff;border-radius:16px;box-shadow:0 6px 24px rgba(0,0,0,.1);overflow:hidden;margin-bottom:24px}

/* CERT HEADER — logo left, address center, photo right */
.cert-header{background:linear-gradient(135deg,var(--green),#004d35);padding:var(--cert-padding) 18px;display:grid;grid-template-columns:90px 1fr 90px;align-items:center;gap:14px;justify-items:center}
.cert-header .hdr-logo{justify-self:start}
.cert-header .hdr-logo img{max-height:64px;width:auto;object-fit:contain}
.cert-header .hdr-logo .logo-ph{width:56px;height:56px;background:rgba(255,255,255,.18);border-radius:50%;display:flex;align-items:center;justify-content:center;color:rgba(255,255,255,.6);font-size:1.4rem}
.cert-header .hdr-center{text-align:center;justify-self:center}
.cert-header .hdr-center h2{color:#f5f8ff;font-size:1.08rem;font-weight:800;margin:0;line-height:1.25}
.cert-header .hdr-center p{color:rgba(245,248,255,.85);font-size:.78rem;margin:3px 0 0;line-height:1.4}
.cert-header .hdr-center .cert-title{background:linear-gradient(135deg,rgba(6,214,160,.85),rgba(2,138,91,.85));color:#f8fbff;border-radius:20px;padding:4px 14px;font-size:.8rem;font-weight:800;display:inline-block;margin-top:8px}
.cert-header .hdr-right{justify-self:end}
.cert-header .hdr-right .photo-box{width:70px;height:90px;border-radius:10px;border:2px solid rgba(255,255,255,.35);overflow:hidden;display:flex;align-items:center;justify-content:center;background:rgba(255,255,255,.12);flex-shrink:0}
.cert-header .hdr-right .photo-box img{width:100%;height:100%;object-fit:cover}
.cert-header .hdr-right .photo-box i{font-size:1.8rem;color:rgba(255,255,255,.4)}

/* CERT BODY */
.cert-body{padding:calc(var(--cert-padding) + 2px) 18px}

/* student info grid */
.stu-grid{display:grid;grid-template-columns:1fr 1fr;gap:10px;background:linear-gradient(135deg,#f0faf5,#e8f5e9);border-radius:10px;padding:calc(var(--cert-padding) - 1px);margin-bottom:14px}
.info-item{display:flex;flex-direction:row;align-items:baseline;gap:6px;flex-wrap:wrap;margin-bottom:6px}
.info-item:last-child{margin-bottom:0}
.info-item .lbl{font-size:.76rem;font-weight:700;color:#777;white-space:nowrap;min-width:110px;flex-shrink:0}
.info-item .lbl::after{content:':'}
.info-item .val{font-size:.88rem;font-weight:700;color:var(--navy)}
.roll-badge{background:linear-gradient(135deg,var(--green),#004d35);color:#fff;border-radius:8px;padding:2px 10px;font-size:.78rem;font-weight:800;display:inline-block}
.group-badge{background:linear-gradient(135deg,var(--orange),#c96a00);color:#fff;border-radius:8px;padding:2px 9px;font-size:.76rem;font-weight:700;display:inline-block}

/* exam table */
.att-table{width:100%;border-collapse:collapse;font-size:.82rem;margin-bottom:12px}
.att-table thead tr{background:linear-gradient(135deg,var(--green),#004d35)}
.att-table thead th{color:#fff;padding:8px 10px;text-align:center;font-weight:700;font-size:.76rem;border:none}
.att-table tbody tr{border-bottom:1px solid #f0f0f0}
.att-table tbody tr:nth-child(even){background:#f9fffe}
.att-table tbody td{padding:8px 10px;text-align:center;vertical-align:middle;border:1px solid #e8e8e8}
.att-table tbody td.subj-name{text-align:left;font-weight:600}
.att-table tbody td.sig-cell{min-width:90px;height:38px}

/* signature */
.sig-row{display:grid;grid-template-columns:1fr 1fr 1fr;gap:14px;margin-top:12px;font-size:.78rem}
.sig-item{text-align:center}
.sig-line{border-top:1px solid #999;margin:24px auto 3px;width:80%}

/* seat badge */
.seat-badge{background:linear-gradient(135deg,var(--blue),var(--purple));color:#fff;border-radius:8px;padding:2px 9px;font-size:.76rem;font-weight:700;display:inline-block}

/* empty state */
.empty-state{text-align:center;padding:60px 20px;color:#aaa}
.empty-state i{font-size:3.5rem;margin-bottom:14px;display:block;opacity:.3}

/* PRINT */
@media print{
  .no-print{display:none!important}
  body{background:#fff;font-size:10px;-webkit-print-color-adjust:exact;color-adjust:exact}
  .cert-card{box-shadow:none;border:1px solid #000;border-radius:0;background:#fff}
  .cert-header{display:grid;grid-template-columns:auto 1fr auto;align-items:center;gap:10px;padding:10px 14px;background:transparent;border:none;-webkit-print-color-adjust:exact;color-adjust:exact}
  .cert-header .hdr-logo .logo-ph{background:transparent;color:#000}
  .cert-header .hdr-center{color:#000}
  .cert-header .hdr-center h2{color:#000}
  .cert-header .hdr-center p{color:#333}
  .cert-header .hdr-center .cert-title{background:#000;color:#fff}
  .cert-header .hdr-right .photo-box{background:transparent;border-color:#000}
  .stu-grid{background:#fff;border:1px solid #000;-webkit-print-color-adjust:exact;color-adjust:exact}
  .att-table thead tr{background:#000;color:#fff;-webkit-print-color-adjust:exact;color-adjust:exact}
  .att-table thead th{color:#fff}
  .att-table tbody tr:nth-child(even){background:#f8f8f8}
  .att-table tbody tr{border-color:#ccc}
  .att-table tbody td{border-color:#ccc}
  .roll-badge,.group-badge,.seat-badge{background:#000;color:#fff;-webkit-print-color-adjust:exact;color-adjust:exact}
}
</style>
</head>
<body>

<!-- TOP BAR -->
<div class="top-bar no-print">
  <a href="../index.php" class="brand"><i class="fas fa-graduation-cap"></i> স্কুল ম্যানেজমেন্ট সিস্টেম</a>
  <div class="nav-links">
    <a href="../index.php"><i class="fas fa-home"></i> হোম</a>
    <a href="routine.php"><i class="fas fa-calendar"></i> রুটিন</a>
    <a href="admit_card.php"><i class="fas fa-id-card"></i> এডমিট কার্ড</a>
    <a href="attendance.php"><i class="fas fa-clipboard-check"></i> হাজিরা</a>
    <a href="bulk_attendance_certificate.php"><i class="fas fa-copy"></i> বাল্ক হাজিরা</a>
  </div>
</div>

<div class="container-fluid py-4 px-4">

<!-- FILTER CARD -->
<div class="filter-card no-print">
  <div class="filter-card-header">
    <i class="fas fa-file-alt fa-lg"></i>
    <h5>হাজিরা পত্র — একক শিক্ষার্থী</h5>
  </div>
  <div class="filter-card-body">

    <!-- Quick ID search -->
    <form method="GET" class="mb-3">
      <div class="row g-2 align-items-end">
        <div class="col-auto">
          <label class="form-label"><i class="fas fa-bolt me-1" style="color:var(--gold)"></i>দ্রুত অনুসন্ধান (ID দিয়ে)</label>
        </div>
        <div class="col-md-3">
          <input type="number" class="form-control" name="exam_id" placeholder="পরীক্ষা ID" value="<?php echo htmlspecialchars($exam_id); ?>">
        </div>
        <div class="col-md-3">
          <input type="number" class="form-control" name="student_id" placeholder="ছাত্র/ছাত্রী ID" value="<?php echo htmlspecialchars($student_id); ?>">
        </div>
        <div class="col-auto">
          <button type="submit" class="btn-quick"><i class="fas fa-search me-1"></i>খুঁজুন</button>
        </div>
      </div>
    </form>

    <div class="filter-divider"><i class="fas fa-list"></i> অথবা ড্রপডাউন থেকে নির্বাচন করুন</div>

    <!-- Dropdown search -->
    <form method="GET" id="mainForm">
      <div class="row g-3 align-items-end">
        <div class="col-md-5">
          <label class="form-label"><i class="fas fa-clipboard-list me-1" style="color:var(--green)"></i>পরীক্ষা নির্বাচন <span class="text-danger">*</span></label>
          <select class="form-select" id="exam_id_sel" name="exam_id" required>
            <option value="">— পরীক্ষা নির্বাচন করুন —</option>
            <?php foreach ($all_exams as $ex): ?>
              <option value="<?php echo $ex['id']; ?>" <?php echo ($exam_id == $ex['id']) ? 'selected' : ''; ?>>
                <?php echo htmlspecialchars($ex['name'].' — '.$ex['class_name'].' ('.$ex['section'].')'); ?>
                <?php if ($ex['group_name']): ?> · <?php echo htmlspecialchars($ex['group_name']); ?><?php endif; ?>
                [<?php echo htmlspecialchars($ex['session']); ?>]
              </option>
            <?php endforeach; ?>
          </select>
        </div>
        <div class="col-md-5">
          <label class="form-label"><i class="fas fa-user-graduate me-1" style="color:var(--blue)"></i>ছাত্র/ছাত্রী নির্বাচন <span class="text-danger">*</span></label>
          <select class="form-select" id="student_id_sel" name="student_id" required <?php echo !$exam_id ? 'disabled' : ''; ?>>
            <option value="">— প্রথমে পরীক্ষা নির্বাচন করুন —</option>
            <?php if ($exam_id && !empty($students)): ?>
              <?php foreach ($students as $stu): ?>
                <option value="<?php echo $stu['id']; ?>" <?php echo ($student_id == $stu['id']) ? 'selected' : ''; ?>>
                  <?php echo htmlspecialchars($stu['first_name'].' '.$stu['last_name']); ?>
                  (রোল: <?php echo htmlspecialchars($stu['roll_number']); ?>)
                </option>
              <?php endforeach; ?>
            <?php endif; ?>
          </select>
        </div>
        <div class="col-md-2 d-flex gap-2">
          <button type="submit" class="btn-view flex-fill"><i class="fas fa-eye me-1"></i>দেখুন</button>
          <a href="attendance_certificate.php" class="btn-reset"><i class="fas fa-redo"></i></a>
        </div>
      </div>
    </form>

    <div class="row g-3 align-items-center mt-3">
      <div class="col-md-3">
        <label class="form-label">কাস্টম প্যাডিং</label>
      </div>
      <div class="col-md-7">
        <input type="range" class="form-range" id="paddingSlider" min="6" max="30" step="2" value="14">
      </div>
      <div class="col-md-2 text-end">
        <span id="paddingValue" class="fw-bold">14px</span>
      </div>
    </div>

    <?php if ($exam_id && $student_id && $exam_details && $student): ?>
      <div class="mt-3 d-flex justify-content-end">
        <button class="btn-print" onclick="window.print()"><i class="fas fa-print me-1"></i>প্রিন্ট করুন</button>
      </div>
    <?php endif; ?>

  </div>
</div>

<?php if ($exam_id && $student_id && $exam_details && $student): ?>

<!-- CERTIFICATE -->
<div class="cert-card">

  <!-- Header: logo left | name+address center | photo right -->
  <div class="cert-header">
    <div class="hdr-logo">
      <?php if ($logo_image): ?>
        <img src="../<?php echo htmlspecialchars($logo_image); ?>" alt="Logo" style="max-height:<?php echo $logo_size; ?>px">
      <?php else: ?>
        <div class="logo-ph"><i class="fas fa-graduation-cap"></i></div>
      <?php endif; ?>
    </div>
    <div class="hdr-center">
      <h2><?php echo htmlspecialchars($site_name); ?></h2>
      <p><?php echo htmlspecialchars($site_address); ?></p>
      <div class="cert-title"><i class="fas fa-clipboard-check me-1"></i>হাজিরা পত্র — <?php echo htmlspecialchars($exam_details['name']); ?></div>
    </div>
    <div class="hdr-right">
      <div class="photo-box">
        <?php if ($photo_ok): ?>
          <img src="<?php echo htmlspecialchars($photo_src); ?>" alt="ছবি">
        <?php else: ?>
          <i class="fas fa-user"></i>
        <?php endif; ?>
      </div>
    </div>
  </div>

  <!-- Body -->
  <div class="cert-body">

    <!-- Student Info -->
    <div class="stu-grid">
      <div>
        <div class="info-item">
          <span class="lbl">ছাত্র/ছাত্রীর নাম</span>
          <span class="val"><?php echo htmlspecialchars($student['first_name'].' '.$student['last_name']); ?></span>
        </div>
        <div class="info-item">
          <span class="lbl">শ্রেণি</span>
          <span class="val"><?php echo htmlspecialchars($student['class_name'].' '.$student['section']); ?></span>
        </div>
        <div class="info-item">
          <span class="lbl">গ্রুপ</span>
          <span class="val">
            <?php if (!empty($student['group_name'])): ?>
              <span class="group-badge"><?php echo htmlspecialchars($student['group_name']); ?></span>
            <?php else: ?>সাধারণ<?php endif; ?>
          </span>
        </div>
      </div>
      <div>
        <div class="info-item">
          <span class="lbl">রোল নম্বর</span>
          <span class="val"><span class="roll-badge"><?php echo htmlspecialchars($student['roll_number']); ?></span></span>
        </div>
        <div class="info-item">
          <span class="lbl">আইডি নম্বর</span>
          <span class="val"><?php echo htmlspecialchars($student['student_id']); ?></span>
        </div>
        <div class="info-item">
          <span class="lbl">অভিভাবকের নাম</span>
          <span class="val"><?php echo htmlspecialchars($student['guardian_name'] ?? '—'); ?></span>
        </div>
      </div>
    </div>

    <!-- Exam Table -->
    <table class="att-table">
      <thead>
        <tr>
          <th style="width:12%">তারিখ</th>
          <th style="width:11%">দিন</th>
          <th style="width:35%">বিষয়</th>
          <th style="width:21%">ছাত্র/ছাত্রীর স্বাক্ষর</th>
          <th style="width:21%">কক্ষ পরিদর্শকের স্বাক্ষর</th>
          <?php if ($seat_arrangement): ?><th style="width:10%">রুম/সিট</th><?php endif; ?>
        </tr>
      </thead>
      <tbody>
        <?php foreach ($by_date as $dk => $rows):
          $subj_parts = [];
          foreach ($rows as $r) $subj_parts[] = $r['subject_name'].($r['subject_code'] ? ' ('.$r['subject_code'].')' : '');
        ?>
        <tr>
          <td><?php echo date('d/m/Y', strtotime($dk)); ?></td>
          <td><?php echo $days_bn[date('l', strtotime($dk))]; ?></td>
          <td class="subj-name"><?php echo implode(' / ', $subj_parts); ?></td>
          <td class="sig-cell"></td>
          <td class="sig-cell"></td>
          <?php if ($seat_arrangement): ?>
            <td><span class="seat-badge"><?php echo htmlspecialchars(($seat_arrangement['room_number']??'').'/'.($seat_arrangement['seat_number']??'')); ?></span></td>
          <?php endif; ?>
        </tr>
        <?php endforeach; ?>
        <?php if (empty($by_date)): ?>
          <tr><td colspan="6" style="text-align:center;color:#aaa;font-style:italic">কোনো বিষয় পাওয়া যায়নি</td></tr>
        <?php endif; ?>
      </tbody>
    </table>

    <!-- Signature -->
    <div class="sig-row">
      <div class="sig-item"><div class="sig-line"></div><p>পরীক্ষার্থীর স্বাক্ষর</p></div>
      <div class="sig-item"><div class="sig-line"></div><p>কক্ষ পরিদর্শকের স্বাক্ষর</p></div>
      <div class="sig-item"><div class="sig-line"></div><p>প্রধান শিক্ষক / পরীক্ষা নিরীক্ষক</p></div>
    </div>

  </div>
</div>

<?php elseif ($exam_id && $student_id): ?>
<div class="empty-state no-print">
  <i class="fas fa-exclamation-triangle" style="color:var(--orange)"></i>
  <p class="fw-bold fs-5">পরীক্ষা বা ছাত্র/ছাত্রীর তথ্য পাওয়া যায়নি</p>
  <small>সঠিক পরীক্ষা ও শিক্ষার্থী নির্বাচন করুন</small>
</div>
<?php else: ?>
<div class="empty-state no-print">
  <i class="fas fa-file-alt" style="color:var(--green)"></i>
  <p class="fw-bold fs-5">পরীক্ষা ও শিক্ষার্থী নির্বাচন করুন</p>
  <small>উপরের ফিল্টার থেকে পরীক্ষা ও শিক্ষার্থী বেছে নিন</small>
</div>
<?php endif; ?>

</div><!-- /container-fluid -->

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
<script>
document.getElementById('exam_id_sel').addEventListener('change', function() {
    const sel = document.getElementById('student_id_sel');
    sel.disabled = !this.value;
    if (!this.value) {
        sel.innerHTML = '<option value="">— প্রথমে পরীক্ষা নির্বাচন করুন —</option>';
        return;
    }
    this.form.submit();
});

document.getElementById('student_id_sel').addEventListener('change', function() {
    if (this.value && document.getElementById('exam_id_sel').value) {
        this.form.submit();
    }
});

const paddingSlider = document.getElementById('paddingSlider');
const paddingValue = document.getElementById('paddingValue');
if (paddingSlider && paddingValue) {
    const setPadding = (value) => {
        document.documentElement.style.setProperty('--cert-padding', value + 'px');
        paddingValue.textContent = value + 'px';
    };
    setPadding(paddingSlider.value);
    paddingSlider.addEventListener('input', function() {
        setPadding(this.value);
    });
}
</script>
</body>
</html>
