<?php

/**
 * @var array $values
 * @var string|null $formId
 * @var bool $detailsOpenAll
 * @var bool $isReadOnly
 * @var bool $showDangerZone
 * @var string|null $dangerZoneDeleteFormId
 * @var string|null $dangerZoneWarning
 * @var string|null $dangerZoneActionLabel
 */

use MintyPHP\Session;

$values = $values ?? [];
$formId = $formId ?? 'role-form';
$detailsOpenAll = $detailsOpenAll ?? false;
$isReadOnly = $isReadOnly ?? false;
$showDangerZone = (bool) ($showDangerZone ?? false);
$dangerZoneDeleteFormId = (string) ($dangerZoneDeleteFormId ?? '');
$dangerZoneWarning = (string) ($dangerZoneWarning ?? t('This action cannot be undone.'));
$dangerZoneActionLabel = (string) ($dangerZoneActionLabel ?? t('Delete'));
$detailsOpenAttr = $detailsOpenAll ? 'open' : '';
$readonlyAttr = $isReadOnly ? 'readonly' : '';
$disabledAttr = $isReadOnly ? 'disabled' : '';
$permissions = is_array($permissions ?? null) ? $permissions : [];
$selectedPermissionIds = is_array($selectedPermissionIds ?? null) ? $selectedPermissionIds : [];
$allRolesForAssignable = is_array($allRolesForAssignable ?? null) ? $allRolesForAssignable : [];
$selectedAssignableRoleIds = is_array($selectedAssignableRoleIds ?? null) ? $selectedAssignableRoleIds : [];

?>
<form id="<?php e($formId); ?>" method="post" data-standard-detail-form="1">
  <div class="app-tabs" data-tabs data-app-component="tabs" data-tabs-param="tab" data-tabs-storage-key="admin-role-form"<?php if (!empty($ignoreTabStorage)) { echo ' data-tabs-ignore-storage'; } ?>>
    <div class="app-tabs-nav">
      <button type="button" data-tab="basic" data-tab-default><?php e(t('Master data')); ?></button>
      <button type="button" data-tab="permissions"><?php e(t('Permissions')); ?></button>
      <button type="button" data-tab="assignable-roles"><?php e(t('Assignable roles')); ?></button>
      <button type="button" data-tab="visibility"><?php e(t('Visibility')); ?></button>
      <?php if ($showDangerZone && $dangerZoneDeleteFormId !== ''): ?>
        <button type="button" data-tab="danger"><?php e(t('Danger zone')); ?></button>
      <?php endif; ?>
    </div>

    <div data-tab-panel="basic">
      <div class="grid">
        <label for="description">
          <span><?php e(t('Description')); ?></span>
          <input required type="text" name="description" id="description" value="<?php e($values['description'] ?? ''); ?>" <?php e($readonlyAttr); ?> />
        </label>
        <div class="grid">
          <label for="code">
            <span><?php e(t('Code')); ?></span>
            <input type="text" name="code" id="code" value="<?php e($values['code'] ?? ''); ?>" <?php e($readonlyAttr); ?> />
          </label>
        </div>
      </div>
    </div>

    <div data-tab-panel="permissions">
      <?php if ($permissions): ?>
        <?php tokenSelectForm(
          'role-permissions',
          'permission_ids',
          'Assigned permissions',
          'Search permissions…',
          $permissions,
          $selectedPermissionIds,
          $isReadOnly,
          ['description', 'key'],
          null,
          [
            'emptyState'     => t('No permissions selected'),
            'removeTooltip'  => t('Remove permission'),
          ]
        ); ?>
      <?php else: ?>
        <?php
        $emptyState = [
          'message' => t('No permissions available'),
          'hint' => t('No active permissions are configured yet.'),
          'size' => 'compact',
        ];
        require templatePath('partials/app-empty-state.phtml');
        ?>
      <?php endif; ?>
    </div>

    <div data-tab-panel="assignable-roles">
      <?php if ($allRolesForAssignable): ?>
        <?php tokenSelectForm(
          'role-assignable-roles',
          'assignable_role_ids',
          'Assignable roles',
          'Search roles…',
          $allRolesForAssignable,
          $selectedAssignableRoleIds,
          $isReadOnly,
          'description',
          null,
          [
            'emptyState'    => t('No roles selected'),
            'removeTooltip' => t('Remove role'),
          ]
        ); ?>
      <?php else: ?>
        <?php
        $emptyState = [
          'message' => t('No roles available'),
          'hint' => t('No active roles are configured yet.'),
          'size' => 'compact',
        ];
        require templatePath('partials/app-empty-state.phtml');
        ?>
      <?php endif; ?>
    </div>

    <div data-tab-panel="visibility">
      <?php
      $statusFieldName = 'active';
      $statusFieldId = 'role-active';
      $statusFieldValue = (string) ($values['active'] ?? '1');
      require templatePath('partials/app-visibility-status-field.phtml');
      ?>
    </div>

    <?php if ($showDangerZone && $dangerZoneDeleteFormId !== ''): ?>
      <div data-tab-panel="danger">
        <?php
        $dangerFormId = $dangerZoneDeleteFormId;
        $dangerLegend = t('Delete');
        $dangerWarning = $dangerZoneWarning;
        $dangerButtonLabel = $dangerZoneActionLabel;
        require templatePath('partials/app-danger-zone-delete-field.phtml');
        ?>
      </div>
    <?php endif; ?>
  </div>
  <?php Session::getCsrfInput(); ?>
</form>
