<?php
$perm_pages = crmPagesConfig();
$perm_users = $conn->query("SELECT id,name,email FROM users WHERE account_id=$account_id ORDER BY name");
$selected_perm_user = isset($_GET['perm_user_id']) ? (int)$_GET['perm_user_id'] : 0;
$selected_permissions = [];
if ($selected_perm_user && tableExists($conn,'user_permissions')) {
  $pq = $conn->query("SELECT page_key, action_key, allowed FROM user_permissions WHERE account_id=$account_id AND user_id=$selected_perm_user");
  while($pr=$pq->fetch_assoc()) $selected_permissions[$pr['page_key'].'|'.$pr['action_key']] = (int)$pr['allowed'];
}
?>
<div class="tab-pane <?= ($requested_tab === 'permissions') ? 'active' : '' ?>" id="tab-permissions">
  <div class="toolbar" style="justify-content:space-between;gap:12px;flex-wrap:wrap">
    <span class="card-title">User Permissions</span>
    <form method="get" style="display:flex;gap:8px;align-items:center">
      <input type="hidden" name="page" value="settings"><input type="hidden" name="tab" value="permissions">
      <select class="form-control" name="perm_user_id" onchange="this.form.submit()" style="min-width:260px">
        <option value="0">Select User</option>
        <?php while($pu=$perm_users->fetch_assoc()): if($pu['id']==$user_id) continue; ?>
          <option value="<?= $pu['id'] ?>" <?= $selected_perm_user==$pu['id']?'selected':'' ?>><?= htmlspecialchars($pu['name'].' - '.$pu['email']) ?></option>
        <?php endwhile; ?>
      </select>
    </form>
  </div>
  <div class="card" style="padding:18px">
    <?php if(!$selected_perm_user): ?>
      <div class="empty-state"><div class="empty-state-icon">🔐</div><p>Select a user to set page/action permissions.</p></div>
    <?php else: ?>
      <input type="hidden" id="permUserId" value="<?= $selected_perm_user ?>">
      <div style="display:flex;justify-content:flex-end;margin-bottom:10px"><label style="display:inline-flex;gap:7px;align-items:center"><input type="checkbox" onchange="toggleAllExistingPermissions(this.checked)"> Give all access</label></div><div class="table-wrapper"><table class="crm-table"><thead><tr><th>Page</th><th>Actions</th></tr></thead><tbody>
      <?php foreach($perm_pages as $key=>$cfg): if($key==='settings'){} ?>
        <tr><td><label style="display:flex;gap:8px;align-items:center"><input type="checkbox" class="perm-page-all" data-page="<?= htmlspecialchars($key) ?>" onchange="toggleExistingPagePermissions('<?= htmlspecialchars($key) ?>', this.checked)"><strong><?= htmlspecialchars($cfg['label']) ?></strong></label><div class="text-xs text-muted"><?= htmlspecialchars($key) ?></div></td><td style="display:flex;gap:12px;flex-wrap:wrap">
          <?php foreach($cfg['actions'] as $act): $checked = !empty($selected_permissions[$key.'|'.$act]); ?>
          <label style="display:inline-flex;align-items:center;gap:6px;background:var(--bg-base);padding:6px 10px;border-radius:8px;border:1px solid var(--border)">
            <input type="checkbox" class="perm-check perm-page-<?= htmlspecialchars($key) ?>" data-page="<?= htmlspecialchars($key) ?>" data-action="<?= htmlspecialchars($act) ?>" <?= $checked?'checked':'' ?>> <?= htmlspecialchars(str_starts_with($act, 'tab_') ? ucwords(str_replace('_',' ',substr($act,4))) . ' Tab' : ucwords(str_replace('_',' ',$act))) ?>
          </label>
          <?php endforeach; ?>
        </td></tr>
      <?php endforeach; ?>
      </tbody></table></div>
      <div style="margin-top:16px;text-align:right"><button class="btn btn-primary" onclick="saveUserPermissions()">Save Permissions</button></div>
    <?php endif; ?>
  </div>
</div>
