<?php
// Include database connection code
include("includes/db.php");

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Collect form data
    $studentId = mysqli_real_escape_string($conn, $_POST['student_id']);
    $symbol_no = mysqli_real_escape_string($conn, $_POST['symbol_no']);
    $regd_id = mysqli_real_escape_string($conn, $_POST['regd_id']);
    $name = mysqli_real_escape_string($conn, $_POST['name']);
    $dob = mysqli_real_escape_string($conn, $_POST['dob']);
    $address = mysqli_real_escape_string($conn, $_POST['address']);
    $phone = mysqli_real_escape_string($conn, $_POST['phone']);
    $email = mysqli_real_escape_string($conn, $_POST['email']);
    $parentName = mysqli_real_escape_string($conn, $_POST['parent_name']);
    $parentPhone = mysqli_real_escape_string($conn, $_POST['parent_phone']);
    $class = mysqli_real_escape_string($conn, $_POST['studentClass']);
    $classId = mysqli_real_escape_string($conn, $_POST['Class_id']);
    $gender = $conn->real_escape_string($_POST["gender"]);
    $std_group = $conn->real_escape_string($_POST["group"]);

    // Check if a new photo is uploaded
    $existingPhoto = mysqli_real_escape_string($conn, $_POST['existing_photo']);
    $newPhoto = '';

    if (!empty($_FILES['photo']['name'])) {
        // Generate a unique photo name only when a new photo is uploaded
        $newPhoto = generateUniquePhotoName($regd_id, $name, $dob);
        $photoPath = "uploads/" . $newPhoto;
    }

    // SQL query to update student details in the database
    $query = "UPDATE students SET 
                regd_id ='$regd_id',
                symbol_no = $symbol_no,
                name = '$name', 
                dob = '$dob', 
                address = '$address', 
                phone = '$phone', 
                email = '$email', 
                parent_name = '$parentName', 
                parent_phone = '$parentPhone', 
                class = '$class', 
                Class_id = '$class',
                
                gender = '$gender',
                std_group = '$std_group'";

    // If a new photo is uploaded, update the query
    if (!empty($newPhoto)) {
        // Remove the existing photo if it exists and it's different from the new photo
        if (!empty($existingPhoto) && $existingPhoto != $newPhoto && file_exists("uploads/" . $existingPhoto)) {
            unlink("uploads/" . $existingPhoto);
        }

        // Upload the new photo
        move_uploaded_file($_FILES['photo']['tmp_name'], $photoPath);

        $query .= ", photo = '$newPhoto'";
    }

    $query .= " WHERE student_id = '$studentId'";

    if ($conn->query($query) === TRUE) {
        header("Location: student_detail.php?success=1");
    } else {
        echo "Error updating student details: " . $conn->error;
    }

    // Close the database connection
    $conn->close();
}

// Function to generate a unique photo name
function generateUniquePhotoName($regd_id, $name, $dob) {
    // Combine the elements and generate a hash
    $hash = $name . "-" . $regd_id . "-" . $dob;

    // Get the file extension from the original file name
    $extension = pathinfo($_FILES['photo']['name'], PATHINFO_EXTENSION);

    // Combine the hash, current timestamp, and extension to create a unique name
    $uniqueName = $hash . '_' . time() . '.' . $extension;

    return $uniqueName;
}
?>
