<?php
require_once("../../includes/config.php");

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    try {
        // Clear existing grades to refresh the scale
        $pdo->exec("TRUNCATE TABLE grading_system");

        $stmt = $pdo->prepare("INSERT INTO grading_system (grade_name, min_percentage, max_percentage, remarks) VALUES (?, ?, ?, ?)");

        // Loops through the arrays sent by the form
        foreach ($_POST['grade_name'] as $key => $name) {
            if (!empty($name)) {
                $stmt->execute([
                    $_POST['grade_name'][$key],
                    $_POST['min_percentage'][$key],
                    $_POST['max_percentage'][$key],
                    $_POST['remarks'][$key]
                ]);
            }
        }
        header("Location: ../../result.php?status=success");
    } catch (PDOException $e) {
        die("Error: " . $e->getMessage());
    }
}
?>