<?php

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

use MintyPHP\Session;

$values = $values ?? [];
$settings = $settings ?? [];
$smtpHost = (string) ($values['smtp_host'] ?? '');
$smtpPort = (string) ($values['smtp_port'] ?? '');
$smtpUser = (string) ($values['smtp_user'] ?? '');
$smtpSecure = (string) ($values['smtp_secure'] ?? '');
$smtpFrom = (string) ($values['smtp_from'] ?? '');
$smtpFromName = (string) ($values['smtp_from_name'] ?? '');
$smtpHostDesc = $settings['smtp_host']['description'] ?? 'setting.smtp_host';
$smtpPortDesc = $settings['smtp_port']['description'] ?? 'setting.smtp_port';
$smtpUserDesc = $settings['smtp_user']['description'] ?? 'setting.smtp_user';
$smtpPasswordDesc = $settings['smtp_password']['description'] ?? 'setting.smtp_password';
$smtpSecureDesc = $settings['smtp_secure']['description'] ?? 'setting.smtp_secure';
$smtpFromDesc = $settings['smtp_from']['description'] ?? 'setting.smtp_from';
$smtpFromNameDesc = $settings['smtp_from_name']['description'] ?? 'setting.smtp_from_name';
$canUpdateSettings = (bool) ($canUpdateSettings ?? false);
$isReadOnly = !$canUpdateSettings;
$readonlyAttr = $isReadOnly ? 'readonly' : '';
$disabledAttr = $isReadOnly ? 'disabled' : '';
?>

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

    <form id="settings-email-form" method="post" data-details-storage="settings-email-details-v1" data-standard-detail-form="1">
      <div class="grid">
        <label class="app-field">
          <span><?php e(t('SMTP host')); ?></span>
          <input type="text" name="smtp_host" value="<?php e($smtpHost); ?>" <?php e($readonlyAttr); ?>>
          <small><?php e(t($smtpHostDesc)); ?></small>
        </label>
        <label class="app-field">
          <span><?php e(t('SMTP port')); ?></span>
          <input type="number" name="smtp_port" value="<?php e($smtpPort); ?>" min="1" max="65535" <?php e($readonlyAttr); ?>>
          <small><?php e(t($smtpPortDesc)); ?></small>
        </label>
      </div>
      <div class="grid">
        <label class="app-field">
          <span><?php e(t('SMTP user')); ?></span>
          <input type="text" name="smtp_user" value="<?php e($smtpUser); ?>" autocomplete="username" <?php e($readonlyAttr); ?>>
          <small><?php e(t($smtpUserDesc)); ?></small>
        </label>
        <label class="app-field">
          <span><?php e(t('SMTP password')); ?></span>
          <input type="password" name="smtp_password" value="" autocomplete="new-password" <?php e($readonlyAttr); ?>>
          <small><?php e(t($smtpPasswordDesc)); ?></small>
        </label>
      </div>
      <div class="grid">
        <label class="app-field">
          <span><?php e(t('SMTP security')); ?></span>
          <select name="smtp_secure" <?php e($disabledAttr); ?>>
            <option value=""><?php e(t('None')); ?></option>
            <option value="tls" <?php e($smtpSecure === 'tls' ? 'selected' : ''); ?>>TLS</option>
            <option value="ssl" <?php e($smtpSecure === 'ssl' ? 'selected' : ''); ?>>SSL</option>
          </select>
          <small><?php e(t($smtpSecureDesc)); ?></small>
        </label>
        <label class="app-field">
          <span><?php e(t('SMTP from address')); ?></span>
          <input type="email" name="smtp_from" value="<?php e($smtpFrom); ?>" <?php e($readonlyAttr); ?>>
          <small><?php e(t($smtpFromDesc)); ?></small>
        </label>
      </div>
      <label class="app-field">
        <span><?php e(t('SMTP from name')); ?></span>
        <input type="text" name="smtp_from_name" value="<?php e($smtpFromName); ?>" <?php e($readonlyAttr); ?>>
        <small><?php e(t($smtpFromNameDesc)); ?></small>
      </label>

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