<?php

/**
 * @var array $tenants
 * @var array $roles
 * @var array $departments
 * @var array $values
 * @var array $settings
 * @var bool $canUpdateSettings
 */

use MintyPHP\Session;

$values = $values ?? [];
$settings = $settings ?? [];
$defaultTenantId = (int) ($values['default_tenant_id'] ?? 0);
$defaultRoleId = (int) ($values['default_role_id'] ?? 0);
$defaultDepartmentId = (int) ($values['default_department_id'] ?? 0);
$appTitle = (string) ($values['app_title'] ?? '');
$appLocale = (string) ($values['app_locale'] ?? '');
$appRegistration = !empty($values['app_registration']);
$appTitleDesc = $settings['app_title']['description'] ?? 'setting.app_title';
$appLocaleDesc = $settings['app_locale']['description'] ?? 'setting.app_locale';
$appRegistrationDesc = $settings['app_registration']['description'] ?? 'setting.app_registration';
$locales = defined('APP_LOCALES') ? APP_LOCALES : [];
$canUpdateSettings = (bool) ($canUpdateSettings ?? false);
$isReadOnly = !$canUpdateSettings;
$readonlyAttr = $isReadOnly ? 'readonly' : '';
$disabledAttr = $isReadOnly ? 'disabled' : '';
?>

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

    <form id="settings-general-form" method="post" data-details-storage="settings-general-details-v1" data-standard-detail-form="1">
      <details class="app-details-card" name="settings-general-app-identity" data-details-key="settings-general-app-identity" open>
        <summary>
          <span class="app-details-card-summary-title"><?php e(t('App identity')); ?></span>
          <span class="app-details-card-summary-meta">
            <span class="badge" data-variant="neutral"><?php e($appTitle !== '' ? t('Configured') : t('Default')); ?></span>
            <span class="badge" data-variant="neutral"><?php e($appLocale !== '' ? strtoupper($appLocale) : t('Default')); ?></span>
          </span>
        </summary>
        <div class="app-details-card-container">
          <div class="grid">
            <label class="app-field">
              <span><?php e(t('App title')); ?> <span class="badge" data-variant="info"><?php e(t('Cached')); ?></span></span>
              <input type="text" name="app_title" value="<?php e($appTitle); ?>" placeholder="<?php e(appTitle()); ?>" <?php e($readonlyAttr); ?>>
              <small><?php e(t($appTitleDesc)); ?></small>
            </label>
            <label class="app-field">
              <span><?php e(t('Default language')); ?> <span class="badge" data-variant="info"><?php e(t('Cached')); ?></span></span>
              <select name="app_locale" <?php e($disabledAttr); ?>>
                <option value=""><?php e(t('None')); ?></option>
                <?php foreach ($locales as $locale): ?>
                  <?php
                  $label = strtoupper($locale);
                  if ($locale === 'de') {
                    $label = t('German');
                  } elseif ($locale === 'en') {
                    $label = t('English');
                  }
                  ?>
                  <option value="<?php e($locale); ?>" <?php e($appLocale === $locale ? 'selected' : ''); ?>>
                    <?php e($label); ?>
                  </option>
                <?php endforeach; ?>
              </select>
              <small><?php e(t($appLocaleDesc)); ?></small>
            </label>
          </div>
        </div>
      </details>

      <?php $configuredDefaultsCount = (int) ($defaultTenantId > 0) + (int) ($defaultDepartmentId > 0) + (int) ($defaultRoleId > 0); ?>
      <details class="app-details-card" name="user-creation" data-details-key="user-creation">
        <summary>
          <span class="app-details-card-summary-title"><?php e(t('User onboarding')); ?></span>
          <span class="app-details-card-summary-meta">
            <span class="badge" data-variant="neutral"><?php e($appRegistration ? t('Registration on') : t('Registration off')); ?></span>
            <span class="badge" data-variant="neutral"><?php e(sprintf(t('%d defaults configured'), $configuredDefaultsCount)); ?></span>
          </span>
        </summary>
        <div class="app-details-card-container">
          <label class="app-field">
            <input type="checkbox" role="switch" name="app_registration" value="1" <?php e($appRegistration ? 'checked' : ''); ?>
              <?php e($disabledAttr); ?>>
            <span><?php e(t('Allow registration')); ?> <span class="badge" data-variant="info"><?php e(t('Cached')); ?></span></span>
          </label>
          <small class="muted"><?php e(t($appRegistrationDesc)); ?></small>
          <hr>
          <blockquote data-variant="info">
            <small><?php e(t('These defaults prefill tenant, department and role when new users are created.')); ?></small>
          </blockquote>
          <div class="grid">
            <label class="app-field">
              <span><?php e(t('Default tenant')); ?></span>
              <select name="default_tenant_id" <?php e($disabledAttr); ?>>
                <option value=""><?php e(t('None')); ?></option>
                <?php foreach ($tenants ?? [] as $tenant): ?>
                  <?php $tenantId = (int) ($tenant['id'] ?? 0); ?>
                  <?php if ($tenantId > 0): ?>
                    <option value="<?php e((string) $tenantId); ?>" <?php e($tenantId === $defaultTenantId ? 'selected' : ''); ?>>
                      <?php e($tenant['description'] ?? ''); ?>
                    </option>
                  <?php endif; ?>
                <?php endforeach; ?>
              </select>
            </label>
            <label class="app-field">
              <span><?php e(t('Default department')); ?></span>
              <select name="default_department_id" <?php e($disabledAttr); ?>>
                <option value=""><?php e(t('None')); ?></option>
                <?php foreach ($departments ?? [] as $department): ?>
                  <?php $departmentId = (int) ($department['id'] ?? 0); ?>
                  <?php if ($departmentId > 0): ?>
                    <option value="<?php e((string) $departmentId); ?>" <?php e($departmentId === $defaultDepartmentId ? 'selected' : ''); ?>>
                      <?php e($department['description'] ?? ''); ?>
                    </option>
                  <?php endif; ?>
                <?php endforeach; ?>
              </select>
            </label>
          </div>
          <label class="app-field">
            <span><?php e(t('Default role')); ?></span>
            <select name="default_role_id" <?php e($disabledAttr); ?>>
              <option value=""><?php e(t('None')); ?></option>
              <?php foreach ($roles ?? [] as $role): ?>
                <?php $roleId = (int) ($role['id'] ?? 0); ?>
                <?php if ($roleId > 0): ?>
                  <option value="<?php e((string) $roleId); ?>" <?php e($roleId === $defaultRoleId ? 'selected' : ''); ?>>
                    <?php e($role['description'] ?? ''); ?>
                  </option>
                <?php endif; ?>
              <?php endforeach; ?>
            </select>
          </label>
        </div>
      </details>

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