<?php

// Check if the request method is POST
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
     $json = file_get_contents('php://input');
    $data = json_decode($json, true);
    function Getemployid()
{
    $strings = '123456789';
    $stringLength = strlen($strings);
    $newStrings = '';
    for ($i = 0; $i < 6; $i++) {
        $newStrings .= $strings[rand(0, $stringLength - 1)];
    }
    return $newStrings;
}
$em_id = Getemployid();
    // Retrieve the POST parameters
   // Retrieve the POST parameters
$employ_name_ar = isset($data["employ_name_ar"]) ? $data["employ_name_ar"] : "";
    $employ_name_en = isset($data["employ_name_en"]) ? $data["employ_name_en"] : "";
    $mobile_number = isset($data["mobile_number"]) ? $data["mobile_number"] : "";
    $employ_email = isset($data["employ_email"]) ? $data["employ_email"] : "";
    $username = isset($data["username"]) ? $data["username"] : "";
    $password = isset($data["password"]) ? $data["password"] : "";
 $conn = new mysqli('localhost', 'marsalog_mahran', 'gX-;hjZN-B*u', 'marsalog_shipping_clearance_db');

    if ($conn->connect_error) {
        die('Connection failed: ' . $conn->connect_error);
    }
    // Validate the required parameters
    if (empty($employ_name_ar) || empty($employ_name_en) || empty($employ_email) || empty($username) || empty($password)) {
        $success = false;
        $response = array(
            'success' => $success,
            'msg' => "One or more parameters are missing.",
            'status' => "unsuccess",
        );
    } else {
        // Create the SQL queries with the POST parameters
        $sql = "INSERT INTO hr_employ_tbl(employ_id, system_id, user_id_top_manager, company_id, dept_id, job_id, job_number, employ_stat, employ_name_ar, employ_name_en, mobile_number, employ_email, office_employ, employee_number_fingerprint,company_lat,company_lng) VALUES ('$em_id','2222','572548','467986','957958','951342','1036','active_employ','$employ_name_ar','$employ_name_en','$mobile_number','$employ_email','office_employee','6666','24.831096','46.789141')";
        $sql1 = "INSERT INTO user_tbl(user_id, name_ar, name_en, username, password, user_mobile, user_email) VALUES ('$em_id','$employ_name_ar','$employ_name_en','$username','$password','$mobile_number','$employ_email')";
        $sql2 = "INSERT INTO waseet_company_user_tbl(system_id, user_id, company_id, user_type) VALUES ('2222','$em_id','467986','Employee')";

        // Execute the SQL queries
        if ($conn->query($sql) === TRUE && $conn->query($sql1) === TRUE && $conn->query($sql2) === TRUE) {
            $success = true;
            $response = array(
                'success' => $success,
                'msg' => "تمت الإضافة بنجاح",
                'status' => "success",
            );
        } else {
            $success = false;
            $response = array(
                'success' => $success,
                'msg' => "هناك خطأ الرجاء اعادة المحاولة",
                'status' => "unsuccess",
            );
        }
    }
} else {
    $success = false;
    $response = array(
        'success' => $success,
        'msg' => "Invalid request method. Expected POST.",
        'status' => "unsuccess",
    );
}

echo json_encode($response, JSON_UNESCAPED_UNICODE);
?>