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

// Admin only
if (!isset($_SESSION['user_id']) || $_SESSION['user_role'] !== 'admin') {
    header('Location: ../login.php');
    exit();
}

$success = '';
$error = '';

// Load sessions (support both columns: name or session_name)
$sessions = [];
try {
    $stmt = $pdo->query("SELECT id, session_name AS label FROM academic_sessions ORDER BY is_current DESC, start_date DESC, id DESC");
    $sessions = $stmt->fetchAll(PDO::FETCH_ASSOC) ?: [];
    
    // Also get sessions that exist in the students table (to handle mismatched session names)
    $stmt2 = $pdo->query("SELECT DISTINCT session AS label FROM students WHERE session IS NOT NULL AND session <> '' ORDER BY session DESC");
    $student_sessions = $stmt2->fetchAll(PDO::FETCH_ASSOC) ?: [];
    
    // Merge sessions, avoiding duplicates
    $all_sessions = $sessions;
    foreach ($student_sessions as $student_session) {
        $found = false;
        foreach ($sessions as $session) {
            if ($session['label'] == $student_session['label']) {
                $found = true;
                break;
            }
        }
        if (!$found) {
            $all_sessions[] = ['id' => null, 'label' => $student_session['label']];
        }
    }
    $sessions = $all_sessions;
} catch (Throwable $e) {
    // fallback: distinct year/session string from students table
    try {
        $sessions = [];
        $rows = $pdo->query("SELECT DISTINCT session AS label FROM students WHERE session IS NOT NULL AND session <> '' ORDER BY session DESC")->fetchAll(PDO::FETCH_ASSOC) ?: [];
        foreach ($rows as $r) { $sessions[] = ['id' => null, 'label' => $r['label']]; }
    } catch (Throwable $e2) {}
}

// Load classes
$classes = [];
try {
    $classes = $pdo->query("SELECT id, name, section FROM classes WHERE status='active' ORDER BY name, section")->fetchAll(PDO::FETCH_ASSOC) ?: [];
} catch (Throwable $e) {}

function getStudentIds(PDO $pdo, int $classId, string $sessionLabel): array {
    $stmt = $pdo->prepare("SELECT id, user_id FROM students WHERE class_id = ? AND session = ? AND status = 'active'");
    $stmt->execute([$classId, $sessionLabel]);
    $rows = $stmt->fetchAll(PDO::FETCH_ASSOC) ?: [];
    return $rows;
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $action = $_POST['action'] ?? '';

    if ($action === 'reset_fees_only') {
        $session_label = trim($_POST['fee_reset_session_label'] ?? '');
        $class_id = (int)($_POST['fee_reset_class_id'] ?? 0);
        $confirm = trim($_POST['fee_reset_confirm'] ?? '');

        if ($session_label === '' || $class_id <= 0) {
            $error = 'ফি রিসেটের জন্য সেশন এবং ক্লাস নির্বাচন করুন।';
        } elseif ($confirm !== 'RESET FEES') {
            $error = 'নিশ্চিতকরণ টেক্সট সঠিক নয়। RESET FEES লিখুন।';
        } else {
            try {
                $pdo->beginTransaction();
                
                $students = getStudentIds($pdo, $class_id, $session_label);
                if (empty($students)) {
                    throw new Exception('এই সেশন ও ক্লাসে কোন সক্রিয় শিক্ষার্থী নেই।');
                }
                $student_ids = array_column($students, 'id');

                // Get fee IDs to delete from fee_payments
                $stmt = $pdo->prepare("SELECT id FROM fees WHERE student_id IN (" . implode(',', array_fill(0, count($student_ids), '?')) . ") AND (academic_year = ? OR academic_year IS NULL)");
                $stmt->execute(array_merge($student_ids, [$session_label]));
                $fee_ids = $stmt->fetchAll(PDO::FETCH_COLUMN);

                if (!empty($fee_ids)) {
                    $fee_ph = implode(',', array_fill(0, count($fee_ids), '?'));
                    
                    // Delete from fee_payments
                    $stmt = $pdo->prepare("DELETE FROM fee_payments WHERE fee_id IN ($fee_ph)");
                    $stmt->execute($fee_ids);

                    // Delete from fees
                    $stmt = $pdo->prepare("DELETE FROM fees WHERE id IN ($fee_ph)");
                    $stmt->execute($fee_ids);
                }

                $pdo->commit();
                $success = 'নির্বাচিত সেশন ও ক্লাসের ফি তথ্য সফলভাবে রিসেট হয়েছে।';
            } catch (Throwable $e) {
                if ($pdo->inTransaction()) $pdo->rollBack();
                $error = 'ফি রিসেট করার সময় ত্রুটি হয়েছে: ' . $e->getMessage();
            }
        }
    }

    if ($action === 'reset') {
        $session_label = trim($_POST['session_label'] ?? '');
        $class_id = (int)($_POST['class_id'] ?? 0);
        $confirm = trim($_POST['confirm'] ?? '');
        $which = $_POST['which'] ?? ['students','fees','results','attendance','subjects','library'];
        $delete_users = isset($_POST['delete_users']);

        if ($session_label === '' || $class_id <= 0) {
            $error = 'সেশন এবং ক্লাস নির্বাচন করুন।';
        } elseif ($confirm !== 'RESET') {
            $error = 'নিশ্চিতকরণ টেক্সট সঠিক নয়। RESET লিখুন।';
        } else {
            try {
                $pdo->beginTransaction();
                $students = getStudentIds($pdo, $class_id, $session_label);
                if (empty($students)) { throw new Exception('এই সেশন ও ক্লাসে কোন সক্রিয় শিক্ষার্থী নেই।'); }
                $student_ids = array_column($students, 'id');
                $user_ids = array_values(array_filter(array_column($students, 'user_id')));
                $ph = implode(',', array_fill(0, count($student_ids), '?'));

                // Results
                if (in_array('results', $which, true)) {
                    try {
                        $stmt = $pdo->prepare("DELETE FROM results WHERE session = ? AND student_id IN ($ph)");
                        $stmt->execute(array_merge([$session_label], $student_ids));
                    } catch (Throwable $e) { /* ignore if table missing */ }
                }
                // Fees (match by academic_year when possible, else by student only)
                if (in_array('fees', $which, true)) {
                    try {
                        // Prefer academic_year filter, but also match NULL values
                        $stmt = $pdo->prepare("DELETE FROM fees WHERE student_id IN ($ph) AND (academic_year = ? OR academic_year IS NULL)");
                        $stmt->execute(array_merge($student_ids, [$session_label]));
                    } catch (Throwable $e) { /* ignore */ }
                }
                // Attendance
                if (in_array('attendance', $which, true)) {
                    try {
                        $stmt = $pdo->prepare("DELETE FROM attendance WHERE student_id IN ($ph)");
                        $stmt->execute($student_ids);
                    } catch (Throwable $e) { /* ignore */ }
                }
                // Subject selections (table name used elsewhere)
                if (in_array('subjects', $which, true)) {
                    try {
                        $stmt = $pdo->prepare("DELETE FROM student_subjects WHERE student_id IN ($ph)");
                        $stmt->execute($student_ids);
                    } catch (Throwable $e) { /* ignore if not exists */ }
                }
                // Library issues
                if (in_array('library', $which, true)) {
                    try {
                        $stmt = $pdo->prepare("DELETE FROM library_issues WHERE student_id IN ($ph)");
                        $stmt->execute($student_ids);
                    } catch (Throwable $e) { /* ignore */ }
                }
                // Finally students
                if (in_array('students', $which, true)) {
                    $stmt = $pdo->prepare("DELETE FROM students WHERE id IN ($ph)");
                    $stmt->execute($student_ids);

                    if ($delete_users && !empty($user_ids)) {
                        $phu = implode(',', array_fill(0, count($user_ids), '?'));
                        try {
                            $pdo->prepare("DELETE FROM users WHERE id IN ($phu)")->execute($user_ids);
                        } catch (Throwable $e) { /* ignore */ }
                    }
                }
                $pdo->commit();
                $success = 'নির্বাচিত সেশন ও ক্লাসের তথ্য সফলভাবে রিসেট হয়েছে।';
            } catch (Throwable $e) {
                if ($pdo->inTransaction()) $pdo->rollBack();
                $error = 'ত্রুটি: ' . $e->getMessage();
            }
        }
    }
}
?>
<!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>
    body { font-family: 'Hind Siliguri', 'Noto Sans Bengali', Arial, sans-serif; }
    .danger { border-left: 5px solid #dc3545; }
    .card { border: 0; box-shadow: 0 6px 18px rgba(0,0,0,0.08); }
  </style>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-primary">
  <div class="container-fluid">
    <a class="navbar-brand" href="../index.php"><i class="fas fa-graduation-cap"></i> স্কুল ম্যানেজমেন্ট</a>
    <div class="navbar-nav ms-auto">
      <a class="nav-link" href="settings.php"><i class="fas fa-cogs"></i> সেটিংস</a>
      <a class="nav-link" href="../logout.php"><i class="fas fa-sign-out-alt"></i> লগআউট</a>
    </div>
  </div>
</nav>

<div class="container-fluid mt-4">
  <?php if ($success): ?><div class="alert alert-success"><?php echo htmlspecialchars($success); ?></div><?php endif; ?>
  <?php if ($error): ?><div class="alert alert-danger"><?php echo htmlspecialchars($error); ?></div><?php endif; ?>

  <div class="row g-3">
    <div class="col-lg-6">
      <div class="card danger">
        <div class="card-header bg-danger text-white"><strong><i class="fas fa-bomb"></i> সেশন/ক্লাস ভিত্তিক রিসেট</strong></div>
        <div class="card-body">
          <form method="post" onsubmit="return confirm('নিশ্চিত? এই কাজ পূর্বাবস্থায় ফেরানো যাবে না!');">
            <input type="hidden" name="action" value="reset">
            <div class="row g-2">
              <div class="col-md-6">
                <label class="form-label">সেশন</label>
                <select name="session_label" class="form-select" required>
                  <option value="">নির্বাচন করুন</option>
                  <?php foreach ($sessions as $s): ?>
                    <option value="<?php echo htmlspecialchars($s['label']); ?>"><?php echo htmlspecialchars($s['label']); ?></option>
                  <?php endforeach; ?>
                </select>
              </div>
              <div class="col-md-6">
                <label class="form-label">ক্লাস</label>
                <select name="class_id" class="form-select" required>
                  <option value="">নির্বাচন করুন</option>
                  <?php foreach ($classes as $c): ?>
                    <option value="<?php echo (int)$c['id']; ?>"><?php echo htmlspecialchars($c['name'].' - '.$c['section']); ?></option>
                  <?php endforeach; ?>
                </select>
              </div>
            </div>
            <div class="mt-3">
              <label class="form-label">শিক্ষার্থীদের তথ্য</label>
              <div id="reset_student_info" class="border rounded p-2 mb-3" style="max-height:200px; overflow:auto;">
                <div class="text-muted">ক্লাস ও সেশন নির্বাচন করুন।</div>
              </div>
            </div>
            <div class="mt-3">
              <label class="form-label">কোনগুলো রিসেট করবেন?</label>
              <div class="row">
                <div class="col-6">
                  <div class="form-check"><input class="form-check-input" type="checkbox" name="which[]" value="students" id="w_students" checked><label class="form-check-label" for="w_students">স্টুডেন্ট</label></div>
                  <div class="form-check"><input class="form-check-input" type="checkbox" name="which[]" value="fees" id="w_fees" checked><label class="form-check-label" for="w_fees">ফি</label></div>
                  <div class="form-check"><input class="form-check-input" type="checkbox" name="which[]" value="results" id="w_results" checked><label class="form-check-label" for="w_results">রেজাল্ট</label></div>
                </div>
                <div class="col-6">
                  <div class="form-check"><input class="form-check-input" type="checkbox" name="which[]" value="attendance" id="w_att" checked><label class="form-check-label" for="w_att">উপস্থিতি</label></div>
                  <div class="form-check"><input class="form-check-input" type="checkbox" name="which[]" value="subjects" id="w_subj" checked><label class="form-check-label" for="w_subj">বিষয় নির্বাচন</label></div>
                  <div class="form-check"><input class="form-check-input" type="checkbox" name="which[]" value="library" id="w_lib" checked><label class="form-check-label" for="w_lib">লাইব্রেরি</label></div>
                </div>
              </div>
              <div class="form-check mt-2">
                <input class="form-check-input" type="checkbox" name="delete_users" id="del_users">
                <label class="form-check-label" for="del_users">সংশ্লিষ্ট ইউজার অ্যাকাউন্টও ডিলিট করুন</label>
              </div>
            </div>
            <div class="mt-3">
              <label class="form-label">নিশ্চিতকরণ (RESET লিখুন)</label>
              <input class="form-control" name="confirm" placeholder="RESET" required>
            </div>
            <div class="mt-3 text-end">
              <button class="btn btn-danger"><i class="fas fa-trash"></i> রিসেট করুন</button>
            </div>
          </form>
        </div>
      </div>
    </div>

    <div class="col-lg-6">
      <div class="card">
        <div class="card-header bg-success text-white"><strong><i class="fas fa-level-up-alt"></i> শিক্ষার্থী প্রোমোশন</strong></div>
        <div class="card-body">
          <form method="post" onsubmit="return handlePromoteSubmit(this)">
            <input type="hidden" name="action" value="promote">
            <div class="row g-2">
              <div class="col-md-6">
                <label class="form-label">উৎস সেশন</label>
                <select name="from_session" id="from_session" class="form-select" required>
                  <option value="">নির্বাচন করুন</option>
                  <?php foreach ($sessions as $s): ?>
                    <option value="<?php echo htmlspecialchars($s['label']); ?>"><?php echo htmlspecialchars($s['label']); ?></option>
                  <?php endforeach; ?>
                </select>
              </div>
              <div class="col-md-6">
                <label class="form-label">উৎস ক্লাস</label>
                <select name="from_class" id="from_class" class="form-select" required>
                  <option value="">নির্বাচন করুন</option>
                  <?php foreach ($classes as $c): ?>
                    <option value="<?php echo (int)$c['id']; ?>"><?php echo htmlspecialchars($c['name'].' - '.$c['section']); ?></option>
                  <?php endforeach; ?>
                </select>
              </div>
            </div>
            <div class="row g-2 mt-2">
              <div class="col-md-6">
                <label class="form-label">গন্তব্য সেশন</label>
                <select name="to_session" id="to_session" class="form-select" required>
                  <option value="">নির্বাচন করুন</option>
                  <?php foreach ($sessions as $s): ?>
                    <option value="<?php echo htmlspecialchars($s['label']); ?>"><?php echo htmlspecialchars($s['label']); ?></option>
                  <?php endforeach; ?>
                </select>
              </div>
              <div class="col-md-6">
                <label class="form-label">গন্তব্য ক্লাস</label>
                <select name="to_class" id="to_class" class="form-select" required>
                  <option value="">নির্বাচন করুন</option>
                  <?php foreach ($classes as $c): ?>
                    <option value="<?php echo (int)$c['id']; ?>"><?php echo htmlspecialchars($c['name'].' - '.$c['section']); ?></option>
                  <?php endforeach; ?>
                </select>
              </div>
            </div>

            <div class="mt-3">
              <label class="form-label">শিক্ষার্থী নির্বাচন (ঐচ্ছিক)</label>
              <div class="d-flex gap-2 mb-2">
                <button type="button" class="btn btn-sm btn-outline-primary" id="btn_load_students">শিক্ষার্থী লোড করুন</button>
                <button type="button" class="btn btn-sm btn-outline-success" id="btn_select_all" disabled>সকল নির্বাচন</button>
                <button type="button" class="btn btn-sm btn-outline-secondary" id="btn_clear_all" disabled>সকল বাতিল</button>
              </div>
              <div class="small text-muted">উৎস সেশন ও ক্লাস নির্বাচন করলে স্বয়ংক্রিয়ভাবে তালিকা লোড হবে। নির্বাচন না করলে উৎসের সকল শিক্ষার্থী প্রোমোট হবে।</div>
              <div id="student_list" class="border rounded p-2 mt-2" style="max-height:260px; overflow:auto;">
                <div class="text-muted">তালিকা খালি।</div>
              </div>
            </div>

            <div class="form-check mt-3">
              <input class="form-check-input" type="checkbox" name="reset_roll" id="reset_roll">
              <label class="form-check-label" for="reset_roll">প্রোমোশনের পর রোল নাম্বার রিসেট করুন</label>
            </div>
            <div class="form-check mt-2">
              <input class="form-check-input" type="checkbox" name="reset_fees" id="reset_fees">
              <label class="form-check-label" for="reset_fees">প্রোমোশনের সময় পূর্বের সেশনের ফি রিসেট (ডিলিট) করুন</label>
            </div>

            <div class="mt-3">
              <label class="form-label">নিশ্চিতকরণ (PROMOTE লিখুন)</label>
              <input class="form-control" name="confirm_p" placeholder="PROMOTE" required>
            </div>
            <div class="mt-3 text-end">
              <button class="btn btn-success"><i class="fas fa-level-up-alt"></i> প্রোমোট করুন</button>
            </div>
          </form>

          <script>
            // Fetch and render students from source session+class
            const fromSessionEl = document.getElementById('from_session');
            const fromClassEl = document.getElementById('from_class');
            const btnLoad = document.getElementById('btn_load_students');
            const btnAll = document.getElementById('btn_select_all');
            const btnClear = document.getElementById('btn_clear_all');
            const listBox = document.getElementById('student_list');

            async function fetchStudents() {
              const s = fromSessionEl.value.trim();
              const c = fromClassEl.value.trim();
              if (!s || !c) {
                listBox.innerHTML = '<div class="text-muted">উৎস সেশন/ক্লাস নির্বাচন করুন।</div>';
                btnAll.disabled = true; btnClear.disabled = true;
                return;
              }
              listBox.innerHTML = '<div class="text-muted">লোড হচ্ছে...</div>';
              try {
                const url = `../api/get_students_advanced.php?session=${encodeURIComponent(s)}&class_id=${encodeURIComponent(c)}`;
                const res = await fetch(url, { credentials: 'same-origin' });
                const data = await res.json();
                renderStudentList(Array.isArray(data) ? data : []);
              } catch (e) {
                listBox.innerHTML = '<div class="text-danger">লোড করতে ব্যর্থ। পরে চেষ্টা করুন।</div>';
              }
            }

            function renderStudentList(items) {
              if (!items.length) {
                listBox.innerHTML = '<div class="text-muted">কোন শিক্ষার্থী পাওয়া যায়নি।</div>';
                btnAll.disabled = true; btnClear.disabled = true;
                return;
              }
              const frag = document.createDocumentFragment();
              const container = document.createElement('div');
              container.className = 'row row-cols-1 row-cols-md-2 g-1';
              items.forEach(st => {
                const col = document.createElement('div');
                col.className = 'col';
                const id = `st_${st.id}`;
                const label = document.createElement('label');
                label.className = 'form-check-label';
                label.setAttribute('for', id);
                const text = `${st.roll_number ? ('রোল: ' + st.roll_number + ' — ') : ''}${st.student_id ? ('আইডি: ' + st.student_id + ' — ') : ''}${(st.first_name||'') + ' ' + (st.last_name||'')}`;
                label.textContent = text;
                const cb = document.createElement('input');
                cb.type = 'checkbox';
                cb.className = 'form-check-input me-2';
                cb.name = 'student_ids[]';
                cb.value = st.id;
                cb.id = id;
                const wrap = document.createElement('div');
                wrap.className = 'form-check';
                wrap.appendChild(cb);
                wrap.appendChild(label);
                col.appendChild(wrap);
                container.appendChild(col);
              });
              frag.appendChild(container);
              listBox.innerHTML = '';
              listBox.appendChild(frag);
              btnAll.disabled = false; btnClear.disabled = false;
            }

            btnLoad?.addEventListener('click', fetchStudents);
            fromSessionEl?.addEventListener('change', fetchStudents);
            fromClassEl?.addEventListener('change', fetchStudents);

            // Reset section functionality
            const resetSessionEl = document.querySelector('select[name="session_label"]');
            const resetClassEl = document.querySelector('select[name="class_id"]');
            const resetInfoBox = document.getElementById('reset_student_info');
            
            async function fetchResetStudents() {
              const session = resetSessionEl?.value.trim();
              const classId = resetClassEl?.value.trim();
              
              if (!session || !classId) {
                resetInfoBox.innerHTML = '<div class="text-muted">ক্লাস ও সেশন নির্বাচন করুন।</div>';
                return;
              }
              
              resetInfoBox.innerHTML = '<div class="text-muted">লোড হচ্ছে...</div>';
              
              try {
                const url = `../api/get_students_advanced.php?session=${encodeURIComponent(session)}&class_id=${encodeURIComponent(classId)}`;
                const res = await fetch(url, { credentials: 'same-origin' });
                const data = await res.json();
                
                if (!Array.isArray(data) || data.length === 0) {
                  resetInfoBox.innerHTML = '<div class="text-muted">এই সেশন ও ক্লাসে কোন সক্রিয় শিক্ষার্থী নেই।</div>';
                  return;
                }
                
                // Display student count and list
                let html = `<div class="mb-2"><strong>মোট শিক্ষার্থী: ${data.length}</strong></div>`;
                html += '<div class="row row-cols-1 g-1">';
                
                data.slice(0, 20).forEach(student => {
                  const name = `${student.first_name || ''} ${student.last_name || ''}`.trim();
                  const roll = student.roll_number ? `রোল: ${student.roll_number}` : '';
                  const studentId = student.student_id ? `আইডি: ${student.student_id}` : '';
                  
                  html += '<div class="col">';
                  html += `<div class="border rounded p-2 small">${name}${roll ? ' — ' + roll : ''}${studentId ? ' — ' + studentId : ''}</div>`;
                  html += '</div>';
                });
                
                if (data.length > 20) {
                  html += `<div class="col"><div class="text-muted">... এবং আরো ${data.length - 20} জন</div></div>`;
                }
                
                html += '</div>';
                resetInfoBox.innerHTML = html;
              } catch (e) {
                resetInfoBox.innerHTML = '<div class="text-danger">তথ্য লোড করতে ব্যর্থ।</div>';
              }
            }
            
            resetSessionEl?.addEventListener('change', fetchResetStudents);
            resetClassEl?.addEventListener('change', fetchResetStudents);

            btnAll?.addEventListener('click', () => {
              listBox.querySelectorAll('input[type="checkbox"][name="student_ids[]"]').forEach(cb => cb.checked = true);
            });
            btnClear?.addEventListener('click', () => {
              listBox.querySelectorAll('input[type="checkbox"][name="student_ids[]"]').forEach(cb => cb.checked = false);
            });

            window.handlePromoteSubmit = function(form) {
              const totalSelected = form.querySelectorAll('input[name="student_ids[]"]:checked').length;
              const resetFees = form.querySelector('#reset_fees')?.checked;
              const msgLines = [];
              if (totalSelected > 0) {
                msgLines.push(`শুধুমাত্র নির্বাচিত ${totalSelected} জন শিক্ষার্থী প্রোমোট হবে।`);
              } else {
                msgLines.push('উৎস সেশন ও ক্লাসের সকল শিক্ষার্থী প্রোমোট হবে।');
              }
              msgLines.push(resetFees ? 'ফি রিসেট: হবে (পূর্বের সেশনের ফি মুছে যাবে)।' : 'ফি রিসেট: হবে না।');
              const ok = confirm(msgLines.join('\n'));
              if (!ok) return false;
              if (resetFees) {
                return confirm('সতর্কতা: ফি রিসেট করলে পূর্বের সেশনের ফি রেকর্ড স্থায়ীভাবে ডিলিট হবে। নিশ্চিত?');
              }
              return true;
            }
          </script>
        </div>
      </div>
    </div>
  </div>

  <div class="row g-3 mt-3">
    <div class="col-lg-6">
        <div class="card border-warning">
            <div class="card-header bg-warning text-dark"><strong><i class="fas fa-money-bill-wave"></i> শুধুমাত্র ফি রিসেট</strong></div>
            <div class="card-body">
                <p class="text-muted">এই টুলটি একটি নির্দিষ্ট ক্লাস ও সেশনের সকল ছাত্র-ছাত্রীর সকল ফি এবং পেমেন্টের রেকর্ড মুছে ফেলবে।</p>
                <form method="post" onsubmit="return confirm('আপনি কি নিশ্চিতভাবে ফি রিসেট করতে চান? এই কাজটি পূর্বাবস্থায় ফেরানো যাবে না!');">
                    <input type="hidden" name="action" value="reset_fees_only">
                    <div class="row g-2">
                        <div class="col-md-6">
                            <label class="form-label">সেশন</label>
                            <select name="fee_reset_session_label" class="form-select" required>
                                <option value="">নির্বাচন করুন</option>
                                <?php foreach ($sessions as $s): ?>
                                    <option value="<?php echo htmlspecialchars($s['label']); ?>"><?php echo htmlspecialchars($s['label']); ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label">ক্লাস</label>
                            <select name="fee_reset_class_id" class="form-select" required>
                                <option value="">নির্বাচন করুন</option>
                                <?php foreach ($classes as $c): ?>
                                    <option value="<?php echo (int)$c['id']; ?>"><?php echo htmlspecialchars($c['name'].'-'.$c['section']); ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                    </div>
                    <div class="mt-3">
                        <label class="form-label">নিশ্চিতকরণ (RESET FEES লিখুন)</label>
                        <input class="form-control" name="fee_reset_confirm" placeholder="RESET FEES" required>
                    </div>
                    <div class="form-check mt-3">
                        <input type="checkbox" class="form-check-input" id="final_confirmation" required>
                        <label class="form-check-label" for="final_confirmation">আমি বুঝি যে এই কাজটি নিশ্চিতভাবে ফি ডেটা মুছে ফেলতে চাই।</label>
                    </div>
                    <div class="mt-3 text-end">
                        <button class="btn btn-warning"><i class="fas fa-trash"></i> শুধুমাত্র ফি রিসেট করুন</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
  </div>
</div>
</body>
</html>