<?php
session_start();
if (!isset($_SESSION['admin_logged_in'])) { header('Location: ../index.php'); exit(); }
if (file_exists(__DIR__ . '/../../db.php')) {
    require_once __DIR__ . '/../../db.php';
} elseif (file_exists(__DIR__ . '/../db.php')) {
    require_once __DIR__ . '/../db.php';
}

if (file_exists(__DIR__ . '/../../result_db_setup.php')) {
    require_once __DIR__ . '/../../result_db_setup.php';
} elseif (file_exists(__DIR__ . '/../result_db_setup.php')) {
    require_once __DIR__ . '/../result_db_setup.php';
} elseif (file_exists(__DIR__ . '/result_db_setup.php')) {
    require_once __DIR__ . '/result_db_setup.php';
}
setupResultTables($conn);
$msg=''; $err='';

if ($_POST['action']??''=='add') {
    $sid=intval($_POST['session_id']); $tn=$conn->real_escape_string(trim($_POST['term_name']));
    $tc=$conn->real_escape_string(trim($_POST['term_code'])); $do=intval($_POST['display_order']);
    $conn->query("INSERT INTO result_terms (session_id,term_name,term_code,display_order) VALUES ($sid,'$tn','$tc',$do)");
    if($conn->affected_rows>0) $msg="Term added!"; else $err=$conn->error;
}
if (isset($_GET['del'])) { $conn->query("DELETE FROM result_terms WHERE id=".intval($_GET['del'])); $msg="Deleted."; }

$terms=$conn->query("SELECT rt.*,rs.session_name FROM result_terms rt JOIN result_sessions rs ON rt.session_id=rs.id ORDER BY rs.is_active DESC,rt.display_order");
$sessions=$conn->query("SELECT * FROM result_sessions ORDER BY is_active DESC,id DESC");
?>
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>Exam Terms | Result</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/all.min.css">
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
<?php include __DIR__.'/_sidebar_style.php'; ?></head><body>
<?php include __DIR__.'/_sidebar.php'; ?>
<div class="main">
<div class="topbar"><h4><i class="fas fa-list-ol me-2" style="color:var(--primary)"></i>Exam Terms</h4>
<button class="btn btn-primary btn-sm" data-bs-toggle="modal" data-bs-target="#addModal"><i class="fas fa-plus me-1"></i>Add Term</button></div>
<?php if($msg): ?><div class="alert alert-success"><?=$msg?></div><?php endif; ?>
<?php if($err): ?><div class="alert alert-danger"><?=$err?></div><?php endif; ?>
<div class="card-box"><table class="table mb-0">
<thead><tr><th>#</th><th>Session</th><th>Term Name</th><th>Code</th><th>Order</th><th>Actions</th></tr></thead>
<tbody><?php $i=0; while($t=$terms->fetch_assoc()): $i++; ?>
<tr><td><?=$i?></td>
<td><span class="badge bg-info"><?=htmlspecialchars($t['session_name'])?></span></td>
<td class="fw-600"><?=htmlspecialchars($t['term_name'])?></td>
<td><code><?=htmlspecialchars($t['term_code'])?></code></td>
<td><?=$t['display_order']?></td>
<td>
<a href="marks_entry.php?term_id=<?=$t['id']?>" class="btn btn-sm btn-outline-primary" style="font-size:11px">Entry Marks</a>
<a href="?del=<?=$t['id']?>" class="btn btn-sm btn-outline-danger ms-1" style="font-size:11px" onclick="return confirm('Delete?')"><i class="fas fa-trash"></i></a>
</td></tr>
<?php endwhile; ?></tbody></table></div></div>
<div class="modal fade" id="addModal" tabindex="-1"><div class="modal-dialog"><div class="modal-content">
<div class="modal-header" style="background:linear-gradient(135deg,var(--primary),#1a6ba0);color:#fff">
<h5 class="modal-title">Add Exam Term</h5><button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button></div>
<form method="POST"><input type="hidden" name="action" value="add"><div class="modal-body">
<div class="mb-3"><label class="form-label fw-600">Session *</label><select name="session_id" class="form-select" required>
<?php while($s=$sessions->fetch_assoc()): ?><option value="<?=$s['id']?>"><?=htmlspecialchars($s['session_name'])?> <?=$s['is_active']?'(Active)':''?></option><?php endwhile; ?></select></div>
<div class="mb-3"><label class="form-label fw-600">Term Name *</label><input type="text" name="term_name" class="form-control" required placeholder="e.g. Term 1 (April-September)"></div>
<div class="row g-3"><div class="col-6"><label class="form-label fw-600">Term Code *</label><input type="text" name="term_code" class="form-control" required placeholder="TERM1"></div>
<div class="col-6"><label class="form-label fw-600">Display Order</label><input type="number" name="display_order" class="form-control" value="1"></div></div>
</div><div class="modal-footer"><button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-primary">Save Term</button></div></form></div></div></div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script></body></html>
