<?php
$page_title = isset($page_title) ? $page_title : 'Users';
$users = isset($users) ? $users : array();
?>
<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">Create accounts and assign roles for report access.</p>
        </div>
        <div class="col-sm-6 text-sm-right">
          <a href="<?php echo base_url('index.php/users/create'); ?>" class="btn btn-primary btn-sm">
            <i class="fas fa-user-plus mr-1"></i> Add User
          </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 users</h3>
          <span class="card-title-meta"><?php echo count($users); ?> total</span>
        </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>Username</th>
                <th>Role</th>
                <th>Status</th>
                <th class="text-right">Actions</th>
              </tr>
            </thead>
            <tbody>
            <?php if (empty($users)) { ?>
              <tr><td colspan="6" class="text-center text-muted py-4">No users found.</td></tr>
            <?php } else {
              $i = 1;
              foreach ($users as $u) {
                $status = isset($u->status) ? (int) $u->status : 1;
            ?>
              <tr>
                <td><?php echo $i++; ?></td>
                <td><?php echo htmlspecialchars($u->name); ?></td>
                <td><code><?php echo htmlspecialchars($u->username); ?></code></td>
                <td>
                  <?php if (!empty($u->role_name)) {
                    echo htmlspecialchars($u->role_name);
                  } else {
                    echo htmlspecialchars($u->usertype);
                  } ?>
                </td>
                <td>
                  <?php if ($status) { ?>
                    <span class="badge badge-success">Active</span>
                  <?php } else { ?>
                    <span class="badge badge-secondary">Disabled</span>
                  <?php } ?>
                </td>
                <td class="text-right text-nowrap">
                  <a href="<?php echo base_url('index.php/users/edit/' . (int) $u->id); ?>" class="btn btn-outline-primary btn-xs" title="Edit"><i class="fas fa-edit"></i></a>
                  <a href="<?php echo base_url('index.php/users/toggle_status/' . (int) $u->id); ?>" class="btn btn-outline-secondary btn-xs" title="Toggle status" onclick="return confirm('Toggle user status?');"><i class="fas fa-power-off"></i></a>
                  <a href="<?php echo base_url('index.php/users/delete/' . (int) $u->id); ?>" class="btn btn-outline-danger btn-xs" title="Delete" onclick="return confirm('Delete this user permanently?');"><i class="fas fa-trash"></i></a>
                </td>
              </tr>
            <?php } } ?>
            </tbody>
          </table>
        </div>
      </div>
    </div>
  </section>
</div>
