<?php
require __DIR__ . '/_bootstrap.php';

$id_number      = trim($_POST['id_number'] ?? '');
$student_number = trim($_POST['student_number'] ?? '');
$first_name     = trim($_POST['first_name'] ?? '');
$last_name      = trim($_POST['last_name'] ?? '');
$place_of_birth = trim($_POST['place_of_birth'] ?? '');
$date_of_birth  = trim($_POST['date_of_birth'] ?? '');
$nationality    = trim($_POST['nationality'] ?? '');
$email          = trim($_POST['email'] ?? '');
$password       = $_POST['password'] ?? '';
$document_text  = trim($_POST['document_text'] ?? '');

if ($id_number==='' || $student_number==='' || $first_name==='' || $last_name==='' ||
    $place_of_birth==='' || $date_of_birth==='' || $nationality==='' || $email==='' || $password==='') {
  die('All fields are required.');
}

$hash = password_hash($password, PASSWORD_DEFAULT);

$stmt = $pdo->prepare("INSERT INTO students
  (id_number, student_number, first_name, last_name, place_of_birth, date_of_birth, nationality, email, password_hash, document_text)
  VALUES (?,?,?,?,?,?,?,?,?,?)");
$stmt->execute([$id_number,$student_number,$first_name,$last_name,$place_of_birth,$date_of_birth,$nationality,$email,$hash,$document_text]);

$studentId = (int)$pdo->lastInsertId();
header('Location: ' . url_for('/admin/student_view.php?id=' . $studentId));
