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

$unique_user_id = addslashes(isset($_POST['unique_user_id']) ? $_POST['unique_user_id'] : '');
$username = addslashes(isset($_POST['username']) ? $_POST['username'] : '');
$password = addslashes(isset($_POST['password']) ? $_POST['password'] : '');
if (empty($username)) {
    $username = "Empty";
}
$status = "Active";
$todays_date = DATE("Y-m-d");
$query = mysqli_query($conn, "SELECT * FROM user_accessible_features  WHERE unique_user_id = '$unique_user_id'");
$affected_row = mysqli_affected_rows($conn);
if ($affected_row > 0) {
    $query = "SELECT * FROM users WHERE unique_user_id = '$unique_user_id' AND username != '' AND password != ''";
    $result = mysqli_query($conn, $query);
    $rowCount = mysqli_num_rows($result);
    if ($rowCount > 0) {
        $statusCode = 1062;
        $statusMessage = "User login has already been activated!";
    } else {
        $sql = ("UPDATE users SET username = '$username', password = '$password', status = '$status' WHERE unique_user_id = '$unique_user_id'");
        if (mysqli_query($conn, $sql)) {
            $statusCode = 200;
            $statusMessage = "Saved Successfully";
        } else {
            $statusCode = 1042;
            $statusMessage = mysqli_error($conn);
        }
    }
} else {
    $statusCode = 1062;
    $statusMessage = "User permissions not set to enable signup, please contact admin!";
}

$resultData = array('code' => $statusCode, 'message' => $statusMessage, 'tempUsername' => $tempUsername, 'tempPassword' => $tempPassword);

echo json_encode($resultData);
