<?php
  require_once("../../session_check.php");
  require_once("../../../classes/main_classes.php");

  if(!isset($_POST["user_id"])){
    die("<p class='alert alert-warning'>Invalid request</p>");
  }

  
  $another_user_id=addslashes($_POST["user_id"]);



    //OBJECTS
    $retrieveObj=new RETRIEVE();
    
    $get_user_info=$retrieveObj->retriever("users","*","user_id",$another_user_id,"fetch");

    //get roles
    $get_roles=$retrieveObj->retriever("user_role","*","fetchAll");

    if($get_user_info=="1020"){
      die("<p class='alert alert-warning'>Something went wrong</p>");
    }
?>  

<h4 class="page-title">
  <span class="la la-users"></span>
  Users
</h4>
<p><a href="#" class="bold" onclick="users_view()">Users</a> <span class="fas fa-angle-right"></span> Edit user : <i><?php echo $get_user_info->firstname." ".$get_user_info->lastname; ?></i></p>
<div class="content-wrapper ">
    <div class="row">
        <div class="col-md-12">
          <div class="mx-auto card p-2" style="max-width: 400px">
            <p class="text-center lead">Edit user</p>
            <form id="edit_user_form">
              <div class="row mb-2">
                <div class="col-6 col-sm-6 col-md-4 col-lg-3">
                  <label for="firstname">Firstname</label>
                </div>
                <div class="col-6 col-sm-6 col-md-8 col-lg-9">
                  <input class="form-control form-control-sm" type="text" name="firstname" placeholder="Firstname" value="<?php echo $get_user_info->firstname; ?>">
                </div>
              </div>
              <div class="row mb-2">
                <div class="col-6 col-sm-6 col-md-4 col-lg-3">
                  <label for="lastname">Lastname</label>
                </div>
                <div class="col-6 col-sm-6 col-md-8 col-lg-9">
                  <input class="form-control form-control-sm" type="text" name="lastname" placeholder="Lastname" value="<?php echo $get_user_info->lastname; ?>">
                </div>
              </div>
              <div class="row mb-2">
                <div class="col-6 col-sm-6 col-md-4 col-lg-3">
                  <label for="username">Username</label>
                </div>
                <div class="col-6 col-sm-6 col-md-8 col-lg-9">
                  <input class="form-control form-control-sm" type="text" name="username" placeholder="Username" value="<?php echo $get_user_info->username; ?>">
                </div>
              </div>
              <div class="row mb-2">
                <div class="col-6 col-sm-6 col-md-4 col-lg-3">
                  <label for="email">Email</label>
                </div>
                <div class="col-6 col-sm-6 col-md-8 col-lg-9">
                  <input class="form-control form-control-sm" type="text" name="email" placeholder="Email" value="<?php echo $get_user_info->email; ?>">
                </div>
              </div>
              <div class="row mb-2">
                <div class="col-6 col-sm-6 col-md-4 col-lg-3">
                  <label for="user_role">User role</label>
                </div>
                <div class="col-6 col-sm-6 col-md-8 col-lg-9">
                  <div class="">
                    <select class="form-control form-control-sm" name="user_role">
                      <?php
                        if($get_roles!="1020"){
                          $role_selected_status="";

                          foreach ($get_roles as $role) {
                            if($role->user_role==$get_user_info->user_role){
                              $role_selected_status="selected";
                            }else{
                              $role_selected_status="";
                            }
                            echo "<option $role_selected_status value='".$role->role_id."' >".$role->user_role."</option>";
                          }
                        }
                      ?>
                    </select>
                  </div>
                </div>
              </div>
              <div class="my-2" id="edit_user_response">
                
              </div>
              <div class="my-2">
                <input type="hidden" name="edit_user_note" value="<?php echo $get_user_info->user_id ?>">
                <button type="submit" class="btn btn-primary btn-block enter-btn" onclick="edit_user()">Update</button>
              </div>
            </form>
          </div>
        </div>
    </div>
</div>