<?php

session_start();

if (!isset($_SESSION["id"])) {
    echo json_encode(["ok" => false, "error_code" => 100]);
    exit();
}

if (!isset($_POST["user_id"])) {
    echo json_encode(["ok" => false, "error_code" => 101]);
    exit();
}

include("../../config.php");
$users_function = new \Functions\Bot_Users();
$admins_function = new \Functions\Admins();

$admin_info = $admins_function->get_by_id($_SESSION["id"])[0];
$output = "<input type=\"hidden\" id=\"update-user-user_id\" value=\"" . $_POST["user_id"] . "\">";
$root = "api/update_user.php";
if (in_array($root, json_decode($admin_info->permissions))) {

    $user_info = $users_function->get_by_id($_POST["user_id"])[0];
    (($user_info->status == 0) ? "selected" : "");
    $output .= '
<div class="form-group row">
        <div class="col-12">
        <label>نام</label>
            <input id="update-user-name" class="form-control" type="text" 
                   placeholder="نام" value="' . $user_info->name . '" disabled>
        </div>
    </div>
    
    <div class="form-group row">
        <div class="col-12">
        <label>ایدی عددی</label>
            <input id="update-user-name" class="form-control" type="text" 
                   placeholder="ایدی عددی" value="' . $user_info->userid . '" disabled>
        </div>
    </div>
    
    <div class="form-group row">
        <div class="col-12">
        <label>تلاش ناموفق ورود</label>
            <input id="update-user-login_try" class="form-control" type="text" required=""
                   placeholder="تلاش نا موفق ورود" value="' . $user_info->login_try . '">
        </div>
    </div>
    
    <div class="form-group row">
        <div class="col-12">
        <label>وضعیت</label>
            <select id="update-user-status" class="form-control">
            <option value="block" ' . (($user_info->use_bot == "block") ? "selected" : "") . '>مسدود شده</option>
            <option value="open" ' . (($user_info->use_bot == "open") ? "selected" : "") . '>تایید شده</option>
            </select>
        </div>
    </div>

    <div class="form-group text-center row m-t-20">
        <div class="col-12">
            <button onclick="update_user();"
                    class="btn btn-primary btn-block waves-effect waves-light" type="submit">
                ثبت تغییرات
            </button>
        </div>
    </div>';
    $output .= '<!-- Magnific popup -->
<script src="assets/plugins/magnific-popup/jquery.magnific-popup.min.js"></script>
<script src="assets/js/lightbox.js"></script>
<script>
$(document).ready(function() {

    $(".js-select2").select2({
    dropdownParent: $("#update-user-customer_parent")
  });
});
</script>';
    echo json_encode(["ok" => true, "output" => $output, "name" => $user_info->name]);
    exit();
} else {
    echo json_encode(["ok" => false, "error_code" => 102]);
    exit();
}
?>