<?php
header('content-type:application/json');
include($_SERVER['DOCUMENT_ROOT'] . "/API-Others/connection/business-conn.php");
include($_SERVER['DOCUMENT_ROOT'] . "/API-Others/connection/conn.php");

$unique_user_id = addslashes(isset($_POST['unique_user_id']) ? $_POST['unique_user_id'] : '');
$profile_photo = addslashes(isset($_POST['profile_photo']) ? $_POST['profile_photo'] : '');
$fullname = addslashes(isset($_POST['fullname']) ? $_POST['fullname'] : '');
$email = addslashes(isset($_POST['email']) ? $_POST['email'] : '');
$phone = addslashes(isset($_POST['phone']) ? $_POST['phone'] : '');
$nationality = addslashes(isset($_POST['nationality']) ? $_POST['nationality'] : '');
$living_town = addslashes(isset($_POST['living_town']) ? $_POST['living_town'] : '');
$address = addslashes(isset($_POST['address']) ? $_POST['address'] : '');
$status = addslashes(isset($_POST['status']) ? $_POST['status'] : '');



$sql = ("UPDATE users SET profile_photo = '$profile_photo', nationality = '$nationality', living_town = '$living_town', fullname = '$fullname', email = '$email', phone = '$phone', address = '$address', status = '$status' WHERE unique_user_id = '$unique_user_id'");
if (mysqli_query($conn, $sql)) {
    $statusCode = 200;
    $statusMessage = "User Details Was Updated Successfully.";
} else {
    $statusCode = 1042;
    $statusMessage = mysqli_error($conn);
}



$sql = ("DELETE FROM user_accessible_features WHERE unique_user_id = '$unique_user_id'");
if (mysqli_query($conn, $sql)) {
}

if ($_POST['menuItem']) {
    foreach ($_POST['menuItem'] as $key => $menuItemId) {
        $menuItemId = mysqli_real_escape_string($conn, $menuItemId);
        $query1 = mysqli_query($business_conn, "SELECT * FROM businesses_menu WHERE id = '$menuItemId'");
        while ($spec1 = mysqli_fetch_assoc($query1)) {
            $category = $spec1["Category"];
            $menuItemId = $spec1["MenuItem"];
            $sql = "INSERT INTO user_accessible_features (unique_user_id, Category, MenuItemId,UserId, LastActivity) VALUES ('$unique_user_id','$category','$menuItemId','$staffid', '$last_activity_new_entry')";
            if (mysqli_query($conn, $sql)) {
            } else {
                echo "Error: " . mysqli_error($conn);
            }
        }
    }
}



$resultData = array('code' => $statusCode, 'message' => $statusMessage);
echo json_encode($resultData);
