<?php
session_start();
header('Content-Type: application/json');

require_once 'db_connect.php';

if (!isset($_SESSION['loggedin']) || $_SESSION['role'] !== 'admin') {
    echo json_encode(['status' => 'error', 'message' => 'غير مصرح']); exit;
}

$school_name = $_SESSION['school_name'];
$stage_name = trim($_POST['stage_name'] ?? '');
$department_name = trim($_POST['department_name'] ?? '');
$section_name = trim($_POST['section_name'] ?? '');

if (empty($stage_name)) {
    echo json_encode(['status' => 'error', 'message' => 'المرحلة مطلوبة!']); exit;
}

try {
    $stmt = $conn->prepare("INSERT INTO school_tags (school_name, stage_name, department_name, section_name) VALUES (?, ?, ?, ?)");
    $stmt->execute([$school_name, $stage_name, $department_name, $section_name]);
    
    echo json_encode(['status' => 'success']);
} catch (PDOException $e) {
    echo json_encode(['status' => 'error', 'message' => 'حدث خطأ: ' . $e->getMessage()]);
}
?>