<?php
session_start();
require_once "../includes/db.php";

if (!isset($_SESSION['admin'])) {
    header("Location: index.php");
    exit;
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $id       = (int)($_POST['id'] ?? 0);
    $name     = trim($_POST['name'] ?? '');
    $nic      = trim($_POST['nic_number'] ?? '');
    $course   = trim($_POST['course_name'] ?? '');
    $unit     = trim($_POST['unit_exam'] ?? '');
    $password = trim($_POST['password_plain'] ?? '');

    if ($id > 0 && $name && $nic && $course && $unit) {
        $stmt = $conn->prepare("UPDATE students 
                                SET name=?, nic_number=?, course_name=?, unit_exam=?, password_plain=? 
                                WHERE id=?");
        $stmt->bind_param("sssssi", $name, $nic, $course, $unit, $password, $id);
        $stmt->execute();
    }
}

header("Location: student_created_list.php");
exit;
