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

// 1. استقبال البيانات
$slug = $_POST['slug'] ?? '';
$title = $conn->real_escape_string($_POST['title'] ?? '');
$name = $conn->real_escape_string($_POST['name'] ?? '');
$phone = $conn->real_escape_string($_POST['phone'] ?? '');
$spec = $conn->real_escape_string($_POST['spec'] ?? '');
$password = $conn->real_escape_string($_POST['password'] ?? '');

$country = $conn->real_escape_string($_POST['country'] ?? 'العراق');
$bio = $conn->real_escape_string($_POST['bio'] ?? '');
$experience = $conn->real_escape_string($_POST['experience'] ?? '');
$achievements = $conn->real_escape_string($_POST['achievements'] ?? '');

if (empty($slug) || empty($phone) || empty($name)) {
    echo json_encode(['status' => 'error', 'message' => 'الاسم والموبايل ضروريان.']);
    exit;
}

$update_parts = [
    "title = '$title'",
    "phone = '$phone'",
    "specialization = '$spec'",
    "country = '$country'",
    "bio = '$bio'",
    "experience = '$experience'",
    "achievements = '$achievements'"
];

if (!empty($password)) {
    $update_parts[] = "password = '$password'";
}

// 2. معالجة اللوجو القديم
if (isset($_FILES['logo']) && $_FILES['logo']['error'] === UPLOAD_ERR_OK) {
    $dir = 'uploads/logos/';
    if (!is_dir($dir)) mkdir($dir, 0777, true);
    $ext = pathinfo($_FILES['logo']['name'], PATHINFO_EXTENSION);
    $filename = uniqid('logo_') . '.' . $ext;
    if (move_uploaded_file($_FILES['logo']['tmp_name'], $dir . $filename)) {
        $update_parts[] = "logo = '" . $dir . $filename . "'";
    }
}

// 3. معالجة الصورة الشخصية
if (isset($_FILES['personal_photo']) && $_FILES['personal_photo']['error'] === UPLOAD_ERR_OK) {
    $dir = 'uploads/personal/';
    if (!is_dir($dir)) mkdir($dir, 0777, true);
    $ext = pathinfo($_FILES['personal_photo']['name'], PATHINFO_EXTENSION);
    $filename = uniqid('profile_') . '.' . $ext;
    if (move_uploaded_file($_FILES['personal_photo']['tmp_name'], $dir . $filename)) {
        $update_parts[] = "personal_photo = '" . $dir . $filename . "'";
    }
}

// 4. معالجة صور المعرض (تعدد الصور)
if (isset($_FILES['gallery']) && !empty($_FILES['gallery']['name'][0])) {
    $dir = 'uploads/gallery/';
    if (!is_dir($dir)) mkdir($dir, 0777, true);
    
    $gallery_paths = [];
    foreach ($_FILES['gallery']['tmp_name'] as $key => $tmp_name) {
        if ($_FILES['gallery']['error'][$key] === UPLOAD_ERR_OK) {
            $ext = pathinfo($_FILES['gallery']['name'][$key], PATHINFO_EXTENSION);
            $filename = uniqid('gal_') . '.' . $ext;
            if (move_uploaded_file($tmp_name, $dir . $filename)) {
                $gallery_paths[] = $dir . $filename;
            }
        }
    }
    if (!empty($gallery_paths)) {
        $gallery_json = $conn->real_escape_string(json_encode($gallery_paths));
        $update_parts[] = "gallery = '$gallery_json'";
    }
}

// 5. التحديث النهائي في القاعدة
$sql = "UPDATE teachers SET " . implode(', ', $update_parts) . " WHERE teacher_name = '$slug'";

if ($conn->query($sql)) {
    echo json_encode(['status' => 'success']);
} else {
    echo json_encode(['status' => 'error', 'message' => 'فشل التحديث: ' . $conn->error]);
}

$conn->close();
?>