<?php
//////////////// POST ACTION TO DELETE REGISTERED STUDENT FROM A PARTICULAR CLASS, BUT IT'S NOT WORKING FOR NOW //////
if (isset($_POST['student_delete_btn'])) {
    $delete_id = $_POST['student_hidden_id'];

    $query = " DELETE FROM `students` WHERE `id` = '{$delete_id}' ";
    $run_query = mysqli_query($connection, $query);

    if ($run_query == true) {
        $_SESSION['msg'] = "Student Deleted Successfully";
        $_SESSION['type'] = "warning";
    } else {
        $_SESSION['msg'] = "Student Record Could Not Deleted";
        $_SESSION['type'] = "error";
    }
    // Redirect once after logic
    header("Location: view_students");
    exit();
}
?>


<?php
if (isset($_POST['student_edit_btn'])) {
    $edit_student_id = $_POST['edit_student_id'];

    // Sanitize
    $edit_student_gender = inject_checker($connection, $_POST['edit_student_gender']);
    $edit_student_firstname = inject_checker($connection, $_POST['edit_student_firstname']);    
    $edit_student_lastname = inject_checker($connection, $_POST['edit_student_lastname']);
    $edit_student_othername = inject_checker($connection, $_POST['edit_student_othername']);
    $edit_student_dob = inject_checker($connection, $_POST['edit_student_dob']);
    $edit_student_address = inject_checker($connection, $_POST['edit_student_address']);
    $edit_guardian_name = inject_checker($connection, ucwords($_POST['edit_guardian_name']));
    $edit_guardian_occupation = inject_checker($connection, ucwords($_POST['edit_guardian_occupation']));
    $edit_guardian_phone = inject_checker($connection, $_POST['edit_guardian_phone']);
    $edit_student_state = $_POST['edit_student_state'];
    $edit_student_lga = $_POST['edit_student_lga'];
    $edit_student_nationality = $_POST['edit_student_nationality'];
    $edit_height = inject_checker($connection, $_POST['edit_height']);
    $edit_weight = inject_checker($connection, $_POST['edit_weight']);
    $edit_club = inject_checker($connection, $_POST['edit_club']);
    $edit_student_class = $_POST['edit_student_class'];
    $edit_student_class_level = $_POST['edit_student_class_level'];
    $edit_health_conditions = $_POST['edit_health_conditions'];

    // Validation...

    // Image Upload
    $image_location = "";
    if (!empty($_FILES['edit_student_passport']['name'])) {
        $name = $_FILES['edit_student_passport']['name'];
        $tmp_name = $_FILES['edit_student_passport']['tmp_name'];
        $location = 'uploads/students/';
        if (move_uploaded_file($tmp_name, $location . $name)) {
            $image_location = $location . $name;
        }
    }

    $query = "UPDATE `students` SET
                `gender` = '{$edit_student_gender}',
                `firstname` = '{$edit_student_firstname}',
                `lastname` = '{$edit_student_lastname}',
                `othername` = '{$edit_student_othername}',
                `dob` = '{$edit_student_dob}',
                `address` = '{$edit_student_address}',
                `guardian_name` = '{$edit_guardian_name}',
                `guardian_occupation` = '{$edit_guardian_occupation}',
                `guardian_phone` = '{$edit_guardian_phone}',
                `state` = '{$edit_student_state}',
                `lga` = '{$edit_student_lga}',
                `nationality` = '{$edit_student_nationality}',
                `height` = '{$edit_height}',
                `weight` = '{$edit_weight}',
                `club` = '{$edit_club}',
                `class` = '{$edit_student_class}',
                `level` = '{$edit_student_class_level}',
                `health_conditions` = '{$edit_health_conditions}'";

    if (!empty($image_location)) {
        $query .= ", `passport` = '{$image_location}'";
    }

    $query .= " WHERE `id` = '{$edit_student_id}'";

    $run_query = mysqli_query($connection, $query);

    if ($run_query) {
        $_SESSION['msg'] = "Student Info Updated Successfully";
        $_SESSION['type'] = "success";
    } else {
        $_SESSION['msg'] = "Student Info Update Failed";
        $_SESSION['type'] = "error";
    }

    header("Location: view_students");
    exit();
}
?>


<?php
if (isset($_POST['print_student_slip'])) {
    $print_id = $_POST['student_hidden_id'];

    $query = "SELECT * FROM `students` WHERE `id` = '{$print_id}'";
    $run_query = mysqli_query($connection, $query);

    if (mysqli_num_rows($run_query) == 1) {
        $result = mysqli_fetch_assoc($run_query);
        $student_id = $result['id'];

        // Optionally still store in session if needed
        $_SESSION['slip_id'] = $student_id;

        // Redirect using GET parameter
        header("Location: slip.php?id={$student_id}");
        exit();
    }
    // Optional: Redirect back if validation fails
    header("Location: view_students");
    exit();
}
?>


<?php
//////////////// DEFAULT NULL VALUES ///////////////
$error = array();
$date = date('d/M/Y');
$select = "--select--";
$time = date('h:m:s');
?>
<?php
if (isset($_POST['student_reg_btn'])) {
    // Collect and sanitize inputs
    $reg_student_gender = inject_checker($connection, $_POST['reg_student_gender']);
    $reg_student_firstname = inject_checker($connection, $_POST['reg_student_firstname']);
    $reg_student_othername = inject_checker($connection, $_POST['reg_student_othername']);
    $role = (int) $_POST['role'];
    $reg_student_lastname = inject_checker($connection, $_POST['reg_student_lastname']);
    $height = inject_checker($connection, $_POST['height']);
    $weight = inject_checker($connection, $_POST['weight']);
    $club = inject_checker($connection, $_POST['club']);
    $reg_student_dob = inject_checker($connection, $_POST['reg_student_dob']);
    $health_conditions = inject_checker($connection, $_POST['health_conditions']);
    $reg_student_address = inject_checker($connection, $_POST['reg_student_address']);
    $guardian_name = inject_checker($connection, ucwords($_POST['guardian_name']));
    $guardian_occupation = inject_checker($connection, ucwords($_POST['guardian_occupation']));
    $guardian_phone = inject_checker($connection, $_POST['guardian_phone']);
    $reg_student_state = inject_checker($connection, $_POST['reg_student_state']);
    $reg_number = inject_checker($connection, $_POST['reg_number']);
    $class_level = inject_checker($connection, $_POST['class_level']);
    $reg_student_lga = inject_checker($connection, $_POST['reg_student_lga']);
    $reg_student_nationality = inject_checker($connection, $_POST['reg_student_nationality']);
    $reg_student_tc = inject_checker($connection, $_POST['reg_student_tc']);

    $gen_password = "1234";
    $hashed_password = password_hash($gen_password, PASSWORD_DEFAULT);
    $date = date("Y-m-d");

    // Validate fields
    if (
        $reg_student_gender === "Select" || empty($reg_student_firstname) || empty($reg_number) ||
        empty($reg_student_lastname) || empty($reg_student_dob) || empty($height) || empty($weight) ||
        empty($club) || empty($health_conditions) || empty($reg_student_address) ||
        $reg_student_state === "States" || $reg_student_nationality === "Country" ||
        $class_level === "Select Level" || $reg_student_tc === "Select Class"
    ) {
        $_SESSION['msg'] = "All required fields must be filled correctly.";
        $_SESSION['type'] = "error";
        header("Location: view_students");
        exit();
    }

    // Handle image upload
    $image_location = "uploads/students/none.jpg";
    if (!empty($_FILES['file']['name'])) {
        $location = 'uploads/students/';
        $name = $_FILES['file']['name'];
        $tmp_name = $_FILES['file']['tmp_name'];

        if (move_uploaded_file($tmp_name, $location . $name)) {
            $image_location = $location . $name;
        }
    }

    // Prepare insert statement
    $stmt = $connection->prepare("
        INSERT INTO students 
        (gender, firstname, othername, lastname, dob, height, weight, club, health_conditions, address,
         guardian_name, guardian_occupation, guardian_phone, state, lga, nationality, class, level,
         reg_number, role, gen_password, passport, date_of_reg)
        VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
    ");

    $stmt->bind_param(
        "sssssssssssssssssssisss",
        $reg_student_gender, $reg_student_firstname, $reg_student_othername, $reg_student_lastname,
        $reg_student_dob, $height, $weight, $club, $health_conditions, $reg_student_address,
        $guardian_name, $guardian_occupation, $guardian_phone, $reg_student_state, $reg_student_lga,
        $reg_student_nationality, $reg_student_tc, $class_level, $reg_number, $role,
        $hashed_password, $image_location, $date
    );

    if ($stmt->execute()) {
        $_SESSION['msg'] = "Registration successful | Reg No: {$reg_number} | Password: {$gen_password}";
        $_SESSION['type'] = "success";
    } else {
        $_SESSION['msg'] = "Registration failed: " . $stmt->error;
        $_SESSION['type'] = "error";
    }

    $stmt->close();
    header("Location: view_students");
    exit();
}
?>

