<?php

session_start();

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

if (!isset($_POST["admin"])) {
    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-admin-id\" value=\"" . $_POST["admin"] . "\">";
$root = "api/update_admin.php";
if (in_array($root, json_decode($admin_info->permissions))) {
    $selected_admin_info = $admins_function->get_by_id($_POST["admin"])[0];

    $output .= '
                <div class="col-12 " id="update-admin-perms-parent">
    <label>دسترسی ها</label>
        <select id="update-admin-permissions" class="form-control js-select3" multiple="multiple">
            <option value="0">دسترسی ها را انتخاب کنید</option>';

    $permissions = $admins_function->get_permissions_list();

    foreach ($permissions as $permission) {
        $selected = ((in_array($permission->url,json_decode($selected_admin_info->permissions))) ? "selected" : "");
        $output .= "<option value=\"{$permission->url}\" $selected >{$permission->type_fa}</option>";
    }
    $output .= '</select>
        </div>
    
        <div class="col-12" id="update-admin-userid-parent">
        <label>کاربر</label>
        <select id="update-admin-userid" class="form-control js-select2">
            <option value="0">یک کاربر را انتخاب کنید</option>';

    $users = $users_function->get_users();

    foreach ($users as $user) {
        $selected = (($selected_admin_info->userid == $user->userid) ? "selected" : "");
        $output .= "<option value=\"{$user->userid}\" $selected >{$user->id} - {$user->name} - {$user->userid}</option>";
    }
    $output .= '</select>
        </div>
        
    
        <div class="col-12">
        <label>نام</label>
            <input id="update-admin-name" class="form-control" type="text" 
                   placeholder="نام" value="' . $selected_admin_info->name . '" >
        </div>
        
    
    
        <div class="col-12">
        <label>نام کاربری</label>
            <input id="update-admin-username" class="form-control" type="text"  style="direction: ltr;" 
                   placeholder="نام کاربری" value="' . $selected_admin_info->username . '" >
        </div>
        
               <div class="col-12">
        <label>ایمیل</label>
            <input id="update-admin-email" class="form-control" type="text" style="direction: ltr;" 
                   placeholder="ایمیل" value="' . $selected_admin_info->email . '" >
        </div>
        

        
        <div class="col-12 mb-3">
            <label>امکان ورود</label>
            <select id="update-admin-status" class="form-control" style="direction: ltr;" >
                <option value="1" '.(($selected_admin_info->status == 1)?"selected":"").'>فعال</option>
                <option value="0" '.(($selected_admin_info->status == 0)?"selected":"").'>غیرفعال</option>
            </select>
        </div>
        
    
        <div class="col-12">
            <button onclick="update_admin();"
                    class="btn btn-primary btn-block waves-effect waves-light" type="submit">
                ثبت تغییرات
            </button>
    </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-admin-userid-parent")
  });
  
      $(".js-select3").select2({
    dropdownParent: $("#update-admin-perms-parent")
  });
});
</script>';
    echo json_encode(["ok" => true, "output" => $output, "name" => $selected_admin_info->name]);
    exit();
} else {
    echo json_encode(["ok" => false, "error_code" => 102]);
    exit();
}
?>