<?php
require_once 'config/database.php';

echo "Student Form Debug\n";
echo "==================\n";

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    echo "POST Data Received:\n";
    echo "-------------------\n";
    
    foreach ($_POST as $key => $value) {
        echo "$key: " . (is_array($value) ? json_encode($value) : $value) . "\n";
    }
    
    echo "\nSpecific Values:\n";
    echo "----------------\n";
    echo "Session: '" . ($_POST['session'] ?? 'NOT SET') . "'\n";
    echo "Roll Number: '" . ($_POST['roll_number'] ?? 'NOT SET') . "'\n";
    echo "Class ID: '" . ($_POST['class_id'] ?? 'NOT SET') . "'\n";
    echo "Group Name: '" . ($_POST['group_name'] ?? 'NOT SET') . "'\n";
    
    // Test the INSERT query parameters
    echo "\nINSERT Query Parameters (in order):\n";
    echo "------------------------------------\n";
    
    $user_id = null;
    $student_id = '2024TEST';
    $first_name = $_POST['first_name'] ?? '';
    $last_name = $_POST['last_name'] ?? '';
    $email = $_POST['email'] ?? '';
    $phone = $_POST['phone'] ?? '';
    $address = $_POST['address'] ?? '';
    $date_of_birth = $_POST['date_of_birth'] ?? '';
    $gender = $_POST['gender'] ?? '';
    $class_id = $_POST['class_id'] ?? '';
    $session = $_POST['session'] ?? '';
    $group_name = $_POST['group_name'] ?? '';
    $roll_number = $_POST['roll_number'] ?? '';
    $admission_date = $_POST['admission_date'] ?? '';
    $guardian_name = $_POST['guardian_name'] ?? '';
    $guardian_phone = $_POST['guardian_phone'] ?? '';
    $guardian_email = $_POST['guardian_email'] ?? '';
    $guardian_address = $_POST['guardian_address'] ?? '';
    $photo_path = null;
    
    $params = [
        $user_id, $student_id, $first_name, $last_name, $email, $phone, $address,
        $date_of_birth, $gender, $class_id, $session, $group_name, $roll_number, $admission_date,
        $guardian_name, $guardian_phone, $guardian_email, $guardian_address, $photo_path
    ];
    
    $columns = [
        'user_id', 'student_id', 'first_name', 'last_name', 'email', 'phone', 'address',
        'date_of_birth', 'gender', 'class_id', 'session', 'group_name', 'roll_number', 'admission_date',
        'guardian_name', 'guardian_phone', 'guardian_email', 'guardian_address', 'photo'
    ];
    
    for ($i = 0; $i < count($columns); $i++) {
        echo ($i + 1) . ". " . $columns[$i] . ": '" . ($params[$i] ?? 'NULL') . "'\n";
    }
    
    echo "\nKey Values Check:\n";
    echo "-----------------\n";
    echo "Position 11 (session): '" . $params[10] . "'\n";
    echo "Position 13 (roll_number): '" . $params[12] . "'\n";
    
    if ($params[10] != $session || $params[12] != $roll_number) {
        echo "❌ MISMATCH DETECTED!\n";
    } else {
        echo "✅ Parameters match correctly\n";
    }
    
} else {
    echo "No POST data received. This script should be called from the student add form.\n";
    echo "\nTo test:\n";
    echo "1. Go to students/add.php\n";
    echo "2. Fill the form\n";
    echo "3. Change the form action to point to this script\n";
    echo "4. Submit the form\n";
}

echo "\n" . str_repeat('=', 50) . "\n";
echo "Debug script completed.\n";
?>