<?php
use MintyPHP\Router;

$canCreatePermissions = can('permissions.create');
$canUpdatePermissions = can('permissions.update');
?>
<?php
$breadcrumbs = [
  ['label' => t('Home'), 'path' => 'admin'],
  ['label' => t('Permissions')],
];
require templatePath('partials/app-breadcrumb.phtml');
?>
<div class="app-list-titlebar">
  <h1><?php e(t('Permissions')); ?></h1>
  <div class="app-list-titlebar-actions">
    <button class="outline secondary" type="button" data-toolbar-toggle data-toolbar-target="#permissions-toolbar"
      data-toolbar-text-show="<?php e(t('Show filters')); ?>" data-toolbar-text-hide="<?php e(t('Hide filters')); ?>">
      <i class="bi bi-funnel-fill"></i> <span data-toolbar-label><?php e(t('Hide filters')); ?></span>
    </button>
    <?php if ($canCreatePermissions): ?>
      <a role="button" class="primary" href="admin/permissions/create">
        <i class="bi bi-plus"></i> <?php e(t('Create permission')); ?>
      </a>
    <?php endif; ?>
  </div>
</div>
<div class="app-list-toolbar" id="permissions-toolbar">
  <label class="app-field">
    <span><?php e(t('Search')); ?></span>
    <input type="search" id="permission-search" placeholder="<?php e(t('Search...')); ?>">
  </label>
  <label class="app-field">
    <span><?php e(t('Status')); ?></span>
    <select id="permission-status-filter">
      <option value="all"><?php e(t('All')); ?></option>
      <option value="active"><?php e(t('Active')); ?></option>
      <option value="inactive"><?php e(t('Inactive')); ?></option>
    </select>
  </label>
</div>
<div class="app-list-table">
  <div id="permissions-grid"></div>
</div>

<script src="<?php e(asset('vendor/gridjs/gridjs.umd.js')); ?>"></script>
<script type="module">
  import { createServerGrid } from "<?php e(assetVersion('js/core/app-grid-factory.js')); ?>";
  import { buildUrl, badgeHtml, getAppBase } from "<?php e(asset('js/pages/app-list-utils.js')); ?>";

  const appBase = getAppBase();
  const labels = {
    active: "<?php e(t('Active')); ?>",
    inactive: "<?php e(t('Inactive')); ?>",
    system: "<?php e(t('System')); ?>"
  };

  const initPermissionsGrid = () => {
    const gridConfig = createServerGrid({
      gridjs: window.gridjs,
      container: '#permissions-grid',
      dataUrl: 'admin/permissions/data',
      appBase,
      columns: [
        { name: "<?php e(t('Description')); ?>", sort: true },
        { name: "<?php e(t('Permission key')); ?>", sort: true },
        {
          name: "<?php e(t('Status')); ?>",
          sort: true,
          formatter: (cell) => cell
            ? gridjs.html(`<span class="badge" data-variant="success">${labels.active}</span>`)
            : gridjs.html(`<span class="badge" data-variant="danger">${labels.inactive}</span>`)
        },
        {
          name: "<?php e(t('System')); ?>",
          sort: false,
          formatter: (cell) => cell
            ? gridjs.html(`<span class="badge" data-variant="neutral">${labels.system}</span>`)
            : gridjs.html('')
        },
        { name: "<?php e(t('Created')); ?>", sort: true, formatter: (cell) => badgeHtml(gridjs, cell) },
        { name: 'ID', hidden: true }
      ],
      sortColumns: ['description', 'key', 'active', null, 'created', null],
      paginationLimit: 10,
      language: <?php MintyPHP\Buffer::get('grid_lang'); ?>,
      mapData: (data) => data.data.map((row) => [
        row.description,
        row.key,
        row.active,
        row.is_system,
        row.created,
        row.id
      ]),
      rowDblClick: {
        getUrl: (row) => {
          const id = row?.cells?.[5]?.data;
          return id ? new URL(`admin/permissions/edit/${id}`, appBase).toString() : '';
        }
      },
      filters: [
        {
          input: '#permission-status-filter',
          param: 'active',
          default: 'all',
          normalize: (value) => (value && value !== 'all' ? value : '')
        }
      ],
      search: {
        input: '#permission-search',
        param: 'search',
        debounce: 250
      }
    });

    return gridConfig;
  };

  initPermissionsGrid();
</script>
