<?php
mysqli_report(MYSQLI_REPORT_OFF);
header('Content-Type: application/json; charset=utf-8');

require_once 'db_connect.php';

if (!isset($_POST['school_id']) || empty($_POST['school_id'])) {
    echo json_encode(['status' => 'error', 'message' => 'بيانات المدرسة غير متوفرة!']);
    exit;
}

$school_id = intval($_POST['school_id']);

$working_days = '';
if (isset($_POST['working_days']) && is_array($_POST['working_days'])) {
    $working_days = implode(',', $_POST['working_days']);
}

// البيانات الأساسية
$school_name = $conn->real_escape_string($_POST['school_name'] ?? '');
$manager_name = $conn->real_escape_string($_POST['manager_name'] ?? '');
$email = $conn->real_escape_string($_POST['email'] ?? '');
$admin_email = $conn->real_escape_string($_POST['admin_email'] ?? '');
$accounting_email = $conn->real_escape_string($_POST['accounting_email'] ?? '');
$ui_language = $conn->real_escape_string($_POST['ui_language'] ?? 'ar');
$ui_font = $conn->real_escape_string($_POST['ui_font'] ?? 'Tajawal');
$timezone = $conn->real_escape_string($_POST['timezone'] ?? 'Asia/Baghdad');

// الألوان والخلفيات والأزرار
$primary_color = $conn->real_escape_string($_POST['primary_color'] ?? '#ff4d79');
$secondary_color = $conn->real_escape_string($_POST['secondary_color'] ?? '#6b1024');
$bg_type = $conn->real_escape_string($_POST['bg_type'] ?? 'solid');
$bg_color_1 = $conn->real_escape_string($_POST['bg_color_1'] ?? '#090204');
$bg_color_2 = $conn->real_escape_string($_POST['bg_color_2'] ?? '#1e293b');
$btn_bg_color = $conn->real_escape_string($_POST['btn_bg_color'] ?? '#ff4d79');
$btn_text_color = $conn->real_escape_string($_POST['btn_text_color'] ?? '#ffffff');
$text_color = $conn->real_escape_string($_POST['text_color'] ?? '#ffffff');

// إضافات الظل والإطار وعناوين النوافذ والـ Radius
$border_thickness = $conn->real_escape_string($_POST['border_thickness'] ?? '1');
$enable_shadow = $conn->real_escape_string($_POST['enable_shadow'] ?? '1');
$shadow_color = $conn->real_escape_string($_POST['shadow_color'] ?? '#000000');
$modal_title_color = $conn->real_escape_string($_POST['modal_title_color'] ?? '#ff4d79');
$modal_radius = $conn->real_escape_string($_POST['modal_radius'] ?? '15'); // انحناء النوافذ
$btn_radius = $conn->real_escape_string($_POST['btn_radius'] ?? '8'); // انحناء الأزرار

// الواتساب
$whatsapp_number = $conn->real_escape_string($_POST['whatsapp_number'] ?? '');
$whatsapp_provider = $conn->real_escape_string($_POST['whatsapp_provider'] ?? '');
$whatsapp_api_url = $conn->real_escape_string($_POST['whatsapp_api_url'] ?? '');
$whatsapp_api_token = $conn->real_escape_string($_POST['whatsapp_api_token'] ?? '');

$query = "UPDATE schools SET 
    school_name = '$school_name', manager_name = '$manager_name',
    email = '$email', admin_email = '$admin_email', accounting_email = '$accounting_email',
    primary_color = '$primary_color', secondary_color = '$secondary_color',
    ui_language = '$ui_language', ui_font = '$ui_font', 
    bg_type = '$bg_type', bg_color_1 = '$bg_color_1', bg_color_2 = '$bg_color_2',
    btn_bg_color = '$btn_bg_color', btn_text_color = '$btn_text_color', text_color = '$text_color',
    border_thickness = '$border_thickness', enable_shadow = '$enable_shadow', 
    shadow_color = '$shadow_color', modal_title_color = '$modal_title_color',
    modal_radius = '$modal_radius', btn_radius = '$btn_radius',
    timezone = '$timezone', working_days = '$working_days',
    whatsapp_number = '$whatsapp_number', whatsapp_provider = '$whatsapp_provider',
    whatsapp_api_url = '$whatsapp_api_url', whatsapp_api_token = '$whatsapp_api_token'
    WHERE id = $school_id";

if ($conn->query($query)) {
    echo json_encode(['status' => 'success', 'message' => 'تم حفظ الإعدادات والهوية البصرية بالكامل بنجاح!']);
} else {
    echo json_encode(['status' => 'error', 'message' => 'حدث خطأ أثناء الحفظ: ' . $conn->error]);
}
?>