<?php 
session_start();

$db = new SQLite3("./api/db/studiolivecode_users.db");
$res = $db->query("SELECT * FROM users");

if (isset($_GET["delete"])) {
    $db->exec("DELETE FROM users WHERE id=" . $_GET["delete"]);
    header("Location: usuarios.php?r=deletado");
}
?>
<?php include './includes/header.php';?>
<div class="card radius-10">
                  <div class="card-body">
                    <h4 class="card-title">Editar Usuários</h4>
                    <div class="table-responsive">
                      <table class="table">
                        <thead>
                          <tr>
                            
                            <th> Nome</th>
                            <th> Usuario </th>
                            <th> Nivel </th>
                            <th> Editar </th>
                            <th> Excluir </th>
                          </tr>
                        </thead>
                        <?php
                        while ($row = $res->fetchArray()) {
                          $id = $row["id"];
                          $name = $row["name"];
                          $user = $row["username"];
                          $pass = $row["password"];
                          $logo = $row["logo"];
                          $nivel = $row["nivel"];
                          ?>
                        <tbody>
                          <tr>
                          
                          <td><?=$name;?></td>
                          <td><?=$user;?></td>
                          <td><?php echo $nivel == 1 ? "Admin" : "Usuário";?></td>
                            <td>
                          <div class="d-flex order-actions">
                          <a href="edit_usuario.php?update=<?=$id?>" class="text-primary bg-light-primary border-0"><i class='bx bxs-edit' ></i></a></div></td>
                          <td>
                           <div class="d-flex order-actions">
                          <a href="usuarios.php?delete=<?=$id?>" class="text-danger bg-light-danger border-0"><i class='bx bxs-trash' ></i></a></div>
                        </td>
                          </tr>
                    </tbody>
                            <?php } ?>
                      </table>
                    </div>
                    <a href="cad_usuarios.php" class="btn btn-primary mr-2"><i class='bx bx-plus mr-1'></i>Cadastrar Usuários</button></a>
                  </div>
                </div>
</div>

<?php 
 if (isset($_GET["r"])) {
          $result = $_GET["r"];
switch ($result) {
            case "atualizado":
            echo "<script>
            const Toast = Swal.mixin({
  toast: true,
  position: 'bottom',
  showConfirmButton: false,
  timer: 2000,
})
Toast.fire({
  icon: 'success',
  title: 'Usuário Atualizado com Sucesso!'
})</script>";
            break;
            case "add_sucesso":
            echo "<script>
            const Toast = Swal.mixin({
  toast: true,
  position: 'bottom',
  showConfirmButton: false,
  timer: 2000,
})
Toast.fire({
  icon: 'success',
  title: 'Usuário Adicionado com Sucesso!'
})</script>";
            break;
            case "deletado":
            echo "<script>
            const Toast = Swal.mixin({
  toast: true,
  position: 'bottom',
  showConfirmButton: false,
  timer: 2000,
})
Toast.fire({
  icon: 'success',
  title: 'Usuário Excluído com Sucesso!'
})</script>";
              }
            }
            ?>
				
<?php include './includes/footer.php';?>