<?php
$roles = isset($roles) ? $roles : array();
$page_title = isset($page_title) ? $page_title : 'Roles';
?>
<div class="content-wrapper">
  <section class="content-header">
    <div class="container-fluid">
      <div class="row mb-2 align-items-center">
        <div class="col-sm-6">
          <h1><?php echo htmlspecialchars($page_title); ?></h1>
          <p class="report-lead">Define roles, then set view / export access per report.</p>
        </div>
        <div class="col-sm-6 text-sm-right">
          <a href="<?php echo base_url('index.php/roles/create'); ?>" class="btn btn-primary btn-sm">
            <i class="fas fa-plus mr-1"></i> Add Role
          </a>
        </div>
      </div>
    </div>
  </section>
  <section class="content">
    <div class="container-fluid">
      <?php if (!empty($flash_success)) { ?>
        <div class="alert alert-success"><?php echo htmlspecialchars($flash_success); ?></div>
      <?php } ?>
      <?php if (!empty($flash_error)) { ?>
        <div class="alert alert-danger"><?php echo htmlspecialchars($flash_error); ?></div>
      <?php } ?>
      <div class="report-card card">
        <div class="card-header">
          <h3 class="card-title">All roles</h3>
        </div>
        <div class="card-body table-responsive p-0">
          <table class="table table-hover table-striped mb-0 admin-table">
            <thead>
              <tr>
                <th>#</th>
                <th>Name</th>
                <th>Slug</th>
                <th>Users</th>
                <th>Status</th>
                <th class="text-right">Actions</th>
              </tr>
            </thead>
            <tbody>
            <?php if (empty($roles)) { ?>
              <tr><td colspan="6" class="text-center text-muted py-4">No roles yet. Run RBAC install first.</td></tr>
            <?php } else {
              $i = 1;
              foreach ($roles as $role) {
            ?>
              <tr>
                <td><?php echo $i++; ?></td>
                <td>
                  <?php echo htmlspecialchars($role->name); ?>
                  <?php if (!empty($role->is_superadmin)) { ?>
                    <span class="badge badge-info ml-1">Super</span>
                  <?php } ?>
                  <?php if (!empty($role->description)) { ?>
                    <div class="text-muted small"><?php echo htmlspecialchars($role->description); ?></div>
                  <?php } ?>
                </td>
                <td><code><?php echo htmlspecialchars($role->slug); ?></code></td>
                <td><?php echo (int) $role->user_count; ?></td>
                <td>
                  <?php if ((int) $role->status) { ?>
                    <span class="badge badge-success">Active</span>
                  <?php } else { ?>
                    <span class="badge badge-secondary">Inactive</span>
                  <?php } ?>
                </td>
                <td class="text-right text-nowrap">
                  <a href="<?php echo base_url('index.php/permissions/edit/' . (int) $role->id); ?>" class="btn btn-outline-info btn-xs" title="Permissions"><i class="fas fa-key"></i></a>
                  <a href="<?php echo base_url('index.php/roles/edit/' . (int) $role->id); ?>" class="btn btn-outline-primary btn-xs" title="Edit"><i class="fas fa-edit"></i></a>
                  <?php if (empty($role->is_superadmin)) { ?>
                  <a href="<?php echo base_url('index.php/roles/delete/' . (int) $role->id); ?>" class="btn btn-outline-danger btn-xs" title="Delete" onclick="return confirm('Delete this role?');"><i class="fas fa-trash"></i></a>
                  <?php } ?>
                </td>
              </tr>
            <?php } } ?>
            </tbody>
          </table>
        </div>
      </div>
    </div>
  </section>
</div>
