<?php

require_once __DIR__ . '/includes/settings_helpers.php';

settings_require_auth();
settings_ensure_tables($conn);

$org = settings_get_current_org($conn);
$orgId = (int) ($org['id'] ?? 0);
$roles = settings_get_roles($conn, $orgId);
$flash = settings_flash_message();
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
<base href="<?php echo billing_escape(billing_base_href()); ?>">
  <title>Roles | Settings</title>
  <link rel="stylesheet" href="assets/fontawesome/css/all.min.css">
  <link rel="stylesheet" href="assets/font-inter.css">
  <link rel="stylesheet" href="orgprofile.css">
  <style>
    .settings-table{width:100%;border-collapse:collapse;margin-top:12px;}
    .settings-table th,.settings-table td{padding:18px 0;border-top:1px solid var(--border);text-align:left;vertical-align:middle;}
    .settings-table th{font-size:13px;text-transform:uppercase;color:#111827;}
    .role-name{font-size:16px;font-weight:600;color:#2563eb;display:flex;align-items:center;gap:8px;}
    .role-lock{color:#9ca3af;font-size:14px;}
    .row-menu{width:26px;height:26px;border:1px solid var(--border);border-radius:50%;display:inline-flex;align-items:center;justify-content:center;color:#9ca3af;}
  </style>
</head>
<body class="orgprofile-page">
  <?php settings_render_header($_SESSION['org_name'] ?? 'demo', 'dashboard.php', 'fa-user-shield'); ?>

  <main class="profile-layout">
    <?php settings_render_sidebar('roles'); ?>

    <section class="profile-main">
      <div class="profile-head">
        <h1>Roles</h1>
        <div class="toolbar-actions">
          <a class="save-btn" href="role_new.php"><i class="fa-solid fa-plus" style="margin-right:8px;"></i>New Role</a>
        </div>
      </div>

      <div class="profile-card">
        <?php if ($flash['type'] === 'success'): ?>
          <div class="alert-ok"><?php echo htmlspecialchars($flash['message']); ?></div>
        <?php endif; ?>
        <table class="settings-table">
          <thead>
            <tr>
              <th style="width:38%;">Role Name</th>
              <th>Description</th>
              <th style="width:60px;"></th>
            </tr>
          </thead>
          <tbody>
            <?php foreach ($roles as $role): ?>
              <tr>
                <td>
                  <div class="role-name">
                    <?php echo htmlspecialchars((string) $role['role_name']); ?>
                    <?php if ((int) ($role['is_system'] ?? 0) === 1): ?>
                      <span class="role-lock"><i class="fa-solid fa-lock"></i></span>
                    <?php endif; ?>
                  </div>
                </td>
                <td style="font-size:15px;"><?php echo htmlspecialchars((string) ($role['description'] ?? '')); ?></td>
                <td><span class="row-menu"><i class="fa-solid fa-ellipsis"></i></span></td>
              </tr>
            <?php endforeach; ?>
          </tbody>
        </table>
      </div>
    </section>
  </main>
</body>
</html>

