<?php
include 'db.php';
include 'layout.php';
include 'auth.php';  

require_right('Users List');

?>

<div class="page-wrapper">
  <div class="content container-fluid">
    <div class="page-header">
      <div class="row align-items-center">
        <div class="col">
          <h4 class="page-title">User Management</h4>
        </div>
        <div class="col-auto float-end ms-auto">
          <button class="btn btn-primary me-1" data-bs-toggle="modal" data-bs-target="#addUserModal">
            <i class="fa fa-user-plus"></i> Add User
          </button>
          <button class="btn btn-success me-1" data-bs-toggle="modal" data-bs-target="#addRoleModal">
            <i class="fa fa-plus-circle"></i> Create Role
          </button>
          <button class="btn btn-warning" data-bs-toggle="modal" data-bs-target="#assignRightsModal">
            <i class="fa fa-key"></i> Assign Rights
          </button>
        </div>
      </div>
    </div>

    <!-- User Table -->
    <div class="row">
      <div class="col-md-12">
        <div class="card card-table">
          <div class="card-body">
            <div class="table-responsive">
              <table class="table table-hover table-center mb-0 datatable">
                <thead>
                  <tr>
                    <th>#</th>
                    <th>Username</th>
                    <th>Email</th>
                    <th>Role</th>
                    <th>Rights</th>
                  </tr>
                </thead>
                <tbody>
                  <?php
                  $sql = "
                    SELECT u.id, u.username, u.email, u.role_id, r.name AS role_name
                    FROM users u
                    LEFT JOIN roles r ON u.role_id = r.id
                  ";
                  $result = $conn->query($sql);
                  $i = 1;
                  while ($row = $result->fetch_assoc()):
                    $role_id = $row['role_id'];
                    $rights = [];

                    if ($role_id) {
                      $rights_q = $conn->query("
                        SELECT rights.name FROM role_rights
                        JOIN rights ON role_rights.right_id = rights.id
                        WHERE role_rights.role_id = $role_id
                      ");
                      while ($right = $rights_q->fetch_assoc()) {
                        $rights[] = $right['name'];
                      }
                    }

                    $rightsList = $rights ? implode(', ', $rights) : 'No rights';
                  ?>
                  <tr>
                    <td><?= $i++ ?></td>
                    <td><?= htmlspecialchars($row['username']) ?></td>
                    <td><?= htmlspecialchars($row['email']) ?></td>
                    <td><?= htmlspecialchars($row['role_name'] ?? 'None') ?></td>
                    <td><?= $rightsList ?></td>
                  </tr>
                  <?php endwhile; ?>
                </tbody>
              </table>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>

  <!-- Include Modals -->
  <?php
    include 'modals/add_user_modal.php';
    include 'modals/add_role_modal.php';
    include 'modals/assign_rights_modal.php';
        include 'modals/add_right_modal.php';
   
  ?>
</div>
