<?php

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

use MintyPHP\Session;

$values = $values ?? [];
$formId = $formId ?? 'permission-form';
$detailsOpenAll = $detailsOpenAll ?? false;
$isReadOnly = $isReadOnly ?? false;
$roles = $roles ?? [];
$selectedRoleIds = $selectedRoleIds ?? [];
$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' : '';
$keyReadonlyAttr = ($isReadOnly || !empty($values['is_system'])) ? 'readonly' : '';

?>
<form id="<?php e($formId); ?>" method="post" data-standard-detail-form="1">
  <div class="app-tabs" data-tabs data-tabs-param="tab" data-tabs-storage-key="admin-permission-form">
    <div class="app-tabs-nav">
      <button type="button" data-tab="basic" data-tab-default><?php e(t('Master data')); ?></button>
      <?php if ($roles): ?>
        <button type="button" data-tab="roles"><?php e(t('Roles')); ?></button>
      <?php endif; ?>
      <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="permission-description">
          <span><?php e(t('Description')); ?></span>
          <input type="text" name="description" id="permission-description"
            value="<?php e($values['description'] ?? ''); ?>" <?php e($readonlyAttr); ?> />
        </label>
        <label for="permission-key">
          <span><?php e(t('Permission key')); ?></span>
          <input required type="text" name="key" id="permission-key" value="<?php e($values['key'] ?? ''); ?>" <?php e($keyReadonlyAttr); ?> />
        </label>
      </div>
    </div>

    <?php if ($roles): ?>
      <div data-tab-panel="roles">
        <?php multiSelectForm('permission-roles', 'role_ids', 'Assigned roles', 'Select roles', $roles, $selectedRoleIds, $isReadOnly, 'description'); ?>
      </div>
    <?php endif; ?>

    <div data-tab-panel="visibility">
      <?php
      $statusFieldName = 'active';
      $statusFieldId = 'permission-active';
      $statusFieldValue = (string) ($values['active'] ?? '1');
      require templatePath('partials/app-visibility-status-field.phtml');
      ?>
      <fieldset>
        <legend><small><?php e(t('System permission')); ?></small></legend>
        <label class="app-field app-checkbox">
          <?php $systemValue = (int) ($values['is_system'] ?? 0); ?>
          <input type="checkbox" name="is_system" value="1" <?php if ($systemValue === 1) { ?>checked<?php } ?>
          <?php e($disabledAttr); ?> />
          <span><?php e(t('System permission')); ?></span>
        </label>
        <small class="muted"><?php e(t('System permissions are core and should rarely be disabled.')); ?></small>
      </fieldset>
    </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>
