<?php
header('Content-Type: application/json; charset=utf-8');
require_once 'db_connect.php';

$teacher_slug = $conn->real_escape_string($_POST['teacher_slug'] ?? '');
$name = $conn->real_escape_string($_POST['name'] ?? '');
$phone = $conn->real_escape_string($_POST['phone'] ?? '');
$stage = $conn->real_escape_string($_POST['stage'] ?? '');

if (empty($teacher_slug) || empty($name) || empty($phone)) {
    echo json_encode(['status' => 'error', 'message' => 'بيانات مفقودة.']);
    exit;
}

$sql = "INSERT INTO join_requests (teacher_slug, student_name, student_phone, stage, status) 
        VALUES ('$teacher_slug', '$name', '$phone', '$stage', 'pending')";

if ($conn->query($sql)) {
    echo json_encode(['status' => 'success']);
} else {
    echo json_encode(['status' => 'error', 'message' => $conn->error]);
}
$conn->close();
?>