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

$id = (int)($_GET['id'] ?? 0);
if ($id <= 0) { die('Invalid ID'); }

$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'] ?? '');

$params = [$id_number,$student_number,$first_name,$last_name,$place_of_birth,$date_of_birth,$nationality,$email,$document_text];
$sql = "UPDATE students
        SET id_number=?, student_number=?, first_name=?, last_name=?, place_of_birth=?, date_of_birth=?, nationality=?, email=?, document_text=?";

if ($password !== '') {
  $sql .= ", password_hash=?";
  $params[] = password_hash($password, PASSWORD_DEFAULT);
}

$sql .= " WHERE id=?";
$params[] = $id;

$stmt = $pdo->prepare($sql);
$stmt->execute($params);

header('Location: ' . url_for('/admin/student_view.php?id=' . $id));
