<?php
include("db.php");
// header("Content-Type: application/json; charset=utf-8");
// mysqli_set_charset($db, "utf8");

// $db → اتصال mysqli
// $idPublicUser → رقم المستخدم الحالي

$method = $_SERVER['REQUEST_METHOD'];

if ($method == "GET") {

    $txt = "SELECT 
                name_company,
                logo_company,
                email_company,
                type_company,
                tax_number_company,
                tax_company,
                phone_company,
                address_company
            FROM users 
            WHERE id_user = '$idPublicUser'
            LIMIT 1";

    $q = mysqli_query($db, $txt);
    $row = mysqli_fetch_assoc($q);

    echo json_encode([
        "ok" => true,
        "data" => $row
    ], JSON_UNESCAPED_UNICODE);
    exit;
}


if ($method == "POST") {

    $name_company       = $_POST['name_company'] ?? "";
    $type_company       = $_POST['type_company'] ?? "";
    $tax_number_company = $_POST['tax_number_company'] ?? "";
    $tax_company        = $_POST['tax_company'] ?? "";
    $phone_company      = $_POST['phone_company'] ?? "";
    $email_company      = $_POST['email_company'] ?? "";
    $address_company    = $_POST['address_company'] ?? "";

    // -------------------------
    // معالجة اللوجو Base64
    // -------------------------
    $logoBase64 = "";

    if (isset($_FILES['logo_company']) && $_FILES['logo_company']['error'] == 0) {

        $tmp = $_FILES['logo_company']['tmp_name'];
        $mime = mime_content_type($tmp);
        $fileData = file_get_contents($tmp);
        $base64 = base64_encode($fileData);

        $logoBase64 = "data:$mime;base64,$base64";
    }

    // هل يوجد سجل؟
    $check = mysqli_query($db, "SELECT id_user, logo_company FROM users WHERE id_user='$idPublicUser' LIMIT 1");

    if (mysqli_num_rows($check) > 0) {

        $row = mysqli_fetch_assoc($check);

        // لو مفيش لوجو جديد، سيب القديم
        if ($logoBase64 == "") {
            $logoBase64 = $row["logo_company"];
        }

        $txt = "
            UPDATE users SET
                name_company      = '$name_company',
                logo_company      = '" . mysqli_real_escape_string($db, $logoBase64) . "',
                email_company     = '$email_company',
                type_company      = '$type_company',
                tax_number_company = '$tax_number_company',
                tax_company       = '$tax_company',
                phone_company     = '$phone_company',
                address_company   = '$address_company'
            WHERE id_user = '$idPublicUser'
        ";

        mysqli_query($db, $txt);

    } else {

        $txt = "
            INSERT INTO users (
                id_user,
                name_company,
                logo_company,
                email_company,
                type_company,
                tax_number_company,
                tax_company,
                phone_company,
                address_company,
                date_add
            ) VALUES (
                '$idPublicUser',
                '$name_company',
                '" . mysqli_real_escape_string($db, $logoBase64) . "',
                '$email_company',
                '$type_company',
                '$tax_number_company',
                '$tax_company',
                '$phone_company',
                '$address_company',
                NOW()
            )
        ";

        mysqli_query($db, $txt);
    }

    echo json_encode([
        "ok" => true,
        "msg" => "تم حفظ بيانات الشركة",
        "logo_base64" => $logoBase64
    ], JSON_UNESCAPED_UNICODE);

    exit;
}

// طريقة غير مدعومة
echo json_encode(["ok"=>false, "msg"=>"Method Not Allowed"]);
exit;
