<?php

/**
 * @var array $values
 * @var array $settings
 * @var bool  $canUpdateSettings
 */

use MintyPHP\Session;

$values = $values ?? [];
$settings = $settings ?? [];
$systemAuditEnabled = !empty($values['system_audit_enabled']);
$systemAuditRetentionDays = (int) ($values['system_audit_retention_days'] ?? 365);
$systemAuditRetentionDaysDesc = $settings['system_audit_retention_days']['description'] ?? 'setting.system_audit_retention_days';
$canUpdateSettings = (bool) ($canUpdateSettings ?? false);
$isReadOnly = !$canUpdateSettings;
$readonlyAttr = $isReadOnly ? 'readonly' : '';
$disabledAttr = $isReadOnly ? 'disabled' : '';
?>

<div class="app-details-container">
  <section>
    <?php
    $titlebar = [
      'title' => t('Audit log settings'),
      'backHref' => 'admin/settings',
      'backTitle' => t('Cancel'),
      'actions' => $canUpdateSettings ? [
        [
          'form' => 'settings-audit-form',
          'name' => 'action',
          'value' => 'save',
          'class' => 'secondary outline',
          'label' => t('Save'),
        ],
        [
          'form' => 'settings-audit-form',
          'name' => 'action',
          'value' => 'save_close',
          'class' => 'primary',
          'label' => t('Save & close'),
        ],
      ] : [],
    ];
    require templatePath('partials/app-details-titlebar.phtml');
    ?>

    <form id="settings-audit-form" method="post" data-standard-detail-form="1">
      <div data-settings-audit-root>
        <label class="app-field">
          <input type="checkbox" role="switch" name="system_audit_enabled" value="1"
            data-audit-enabled-toggle
            <?php e($systemAuditEnabled ? 'checked' : ''); ?> <?php e($disabledAttr); ?>>
          <span><?php e(t('Enable system audit log')); ?></span>
        </label>
        <div data-audit-when-enabled <?php e($systemAuditEnabled ? '' : 'hidden'); ?>>
          <hr>
          <label class="app-field">
            <span><?php e(t('Retention (days)')); ?></span>
            <input type="number" name="system_audit_retention_days"
              value="<?php e((string) $systemAuditRetentionDays); ?>"
              min="30" max="1095" step="1"
              <?php e($readonlyAttr); ?>>
            <small><?php e(t($systemAuditRetentionDaysDesc)); ?></small>
          </label>
        </div>
      </div>

      <?php Session::getCsrfInput(); ?>
    </form>
  </section>
  <aside id="app-details-aside-section">
    <div class="app-details-aside-section"></div>
  </aside>
</div>
