<?php

declare(strict_types=1);

use Fisharebest\Webtrees\Http\RequestHandlers\ControlPanel;
use Fisharebest\Webtrees\Http\RequestHandlers\EmailPreferencesAction;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Webtrees;

/**
 * @var string        $DKIM_DOMAIN
 * @var string        $DKIM_KEY
 * @var string        $DKIM_SELECTOR
 * @var string        $SMTP_ACTIVE
 * @var string        $SMTP_AUTH
 * @var string        $SMTP_AUTH_USER
 * @var string        $SMTP_DISP_NAME
 * @var string        $SMTP_FROM_NAME
 * @var string        $SMTP_HELO
 * @var string        $SMTP_HOST
 * @var string        $SMTP_PORT
 * @var string        $SMTP_SSL
 * @var array<string> $mail_ssl_options
 * @var array<string> $mail_transport_options
 * @var bool          $smtp_helo_valid
 * @var bool          $smtp_from_name_valid
 * @var string        $title
 */
?>

<?= view('components/breadcrumbs', ['links' => [route(ControlPanel::class) => I18N::translate('Control panel'), $title]]) ?>

<h1><?= $title ?></h1>

<div class="alert alert-info">
    <?= I18N::translate('See %s for more information.', '<a class="alert-link" href="' . e(Webtrees::URL_FAQ_EMAIL) . '">' . e(Webtrees::URL_FAQ_EMAIL) . '</a>') ?>
</div>

<form method="post" action="<?= e(route(EmailPreferencesAction::class)) ?>" class="form-horizontal">
    <div class="row mb-3">
        <label for="SMTP_ACTIVE" class="col-sm-3 col-form-label">
            <?= /* I18N: A configuration setting */
            I18N::translate('Messages') ?>
        </label>
        <div class="col-sm-9">
            <?= view('components/select', ['name' => 'SMTP_ACTIVE', 'selected' => $SMTP_ACTIVE, 'options' => $mail_transport_options]) ?>
            <div class="form-text">
                <?= /* I18N: Help text for the “Messages” site configuration setting */
                I18N::translate('webtrees needs to send emails, such as password reminders and website notifications.') ?>
            </div>
        </div>
    </div>

    <div class="row mb-3">
        <label for="SMTP_DISP_NAME" class="col-sm-3 col-form-label">
            <?= /* I18N: A configuration setting */
            I18N::translate('Sender name') ?>
        </label>
        <div class="col-sm-9">
            <input type="text" class="form-control" id="SMTP_DISP_NAME" name="SMTP_DISP_NAME" value="<?= e($SMTP_DISP_NAME) ?>" maxlength="255" required="required">
            <div class="form-text">
                <?= /* I18N: Help text for the “Sender name” site configuration setting */
                I18N::translate('This name is used in the “From” field, when sending automatic emails from this server.') ?>
            </div>
        </div>
    </div>

    <div class="row mb-3">
        <label for="SMTP_FROM_NAME" class="col-sm-3 col-form-label">
            <?= /* I18N: A configuration setting */
            I18N::translate('Sender email') ?>
        </label>
        <div class="col-sm-9">
            <input type="email" class="form-control" id="SMTP_FROM_NAME" name="SMTP_FROM_NAME" value="<?= e($SMTP_FROM_NAME) ?>" maxlength="255" required="required">
            <div class="form-text">
                <?= /* I18N: Help text for the “Sender name” site configuration setting */
                I18N::translate('This name is used in the “From” field, when sending automatic emails from this server.') ?>
            </div>

            <?php if (!$smtp_from_name_valid) : ?>
                <p class="alert alert-warning">
                    <?= I18N::translate('Most mail servers require a valid email address.') ?>
                </p>
            <?php endif ?>
        </div>
    </div>

    <h2><?= I18N::translate('SMTP mail server') ?></h2>

    <div class="row mb-3">
        <label for="SMTP_HOST" class="col-sm-3 col-form-label">
            <?= /* I18N: A configuration setting */
            I18N::translate('Server name') ?>
        </label>
        <div class="col-sm-9">
            <input type="text" class="form-control" id="SMTP_HOST" name="SMTP_HOST" value="<?= e($SMTP_HOST) ?>" placeholder="smtp.example.com" maxlength="255" pattern="[a-z0-9\-]+(\.[a-z0-9\-]+)*">
            <div class="form-text">
                <?= /* I18N: Help text for the “Server name” site configuration setting */
                I18N::translate('This is the name of the SMTP server. “localhost” means that the mail service is running on the same computer as your web server.') ?>
            </div>
        </div>
    </div>

    <div class="row mb-3">
        <label for="SMTP_PORT" class="col-sm-3 col-form-label">
            <?= /* I18N: A configuration setting */
            I18N::translate('Port number') ?>
        </label>
        <div class="col-sm-9">
            <input type="text" class="form-control" id="SMTP_PORT" name="SMTP_PORT" value="<?= e($SMTP_PORT) ?>" pattern="[0-9]*" placeholder="25" maxlength="5">
            <div class="form-text">
                <?= /* I18N: Help text for the "Port number" site configuration setting */
                I18N::translate('By default, SMTP works on port 25.') ?>
            </div>
        </div>
    </div>

    <fieldset class="row mb-3">
        <legend class="col-form-label col-sm-3">
            <?= /* I18N: A configuration setting */
            I18N::translate('Use password') ?>
        </legend>
        <div class="col-sm-9">
            <?= view('components/radios-inline', ['name' => 'SMTP_AUTH', 'options' => [I18N::translate('no'), I18N::translate('yes')], 'selected' => (int) $SMTP_AUTH]) ?>
            <div class="form-text">
                <?= /* I18N: Help text for the “Use password” site configuration setting */
                I18N::translate('Most SMTP servers require a password.') ?>
            </div>
        </div>
    </fieldset>

    <div class="row mb-3">
        <label for="SMTP_AUTH_USER" class="col-sm-3 col-form-label">
            <?= /* I18N: A configuration setting */
            I18N::translate('Username') ?>
        </label>
        <div class="col-sm-9">
            <input type="text" class="form-control" id="SMTP_AUTH_USER" name="SMTP_AUTH_USER" value="<?= e($SMTP_AUTH_USER) ?>" maxlength="255">
            <div class="form-text">
                <?= /* I18N: Help text for the "Username" site configuration setting */
                I18N::translate('The username required for authentication with the SMTP server.') ?>
            </div>
        </div>
    </div>

    <div class="row mb-3">
        <label for="SMTP_AUTH_PASS" class="col-sm-3 col-form-label">
            <?= /* I18N: A configuration setting */
            I18N::translate('Password') ?>
        </label>
        <div class="col-sm-9">
            <?= view('edit/password', ['id' => 'SMTP_AUTH_PASS']) ?>
            <div class="form-text">
                <?= I18N::translate('The password required for authentication with the SMTP server.') ?>
            </div>
        </div>
    </div>

    <div class="row mb-3">
        <label for="SMTP_SSL" class="col-sm-3 col-form-label">
            <?= /* I18N: A configuration setting */
            I18N::translate('Secure connection') ?>
        </label>
        <div class="col-sm-9">
            <?= view('components/select', ['name' => 'SMTP_SSL', 'selected' => $SMTP_SSL, 'options' => $mail_ssl_options]) ?>
            <div class="form-text">
                <?= /* I18N: Help text for the “Secure connection” site configuration setting */
                I18N::translate('Most servers do not use secure connections.') ?>
            </div>
        </div>
    </div>

    <div class="row mb-3">
        <label for="SMTP_HELO" class="col-sm-3 col-form-label">
            <?= /* I18N: A configuration setting */
            I18N::translate('Sending server name') ?>
        </label>
        <div class="col-sm-9">
            <input type="text" class="form-control" id="SMTP_HELO" name="SMTP_HELO" value="<?= e($SMTP_HELO) ?>" maxlength="255" pattern="[a-z0-9\-]+(\.[a-z0-9\-]+)*">
            <div class="form-text">
                <?= /* I18N: Help text for the "Sending server name" site configuration setting */
                I18N::translate('Most mail servers require that the sending server identifies itself correctly, using a valid domain name.') ?>
            </div>

            <?php if (!$smtp_helo_valid) : ?>
                <p class="alert alert-warning">
                    <?= I18N::translate('Most mail servers require a valid domain name.') ?>
                </p>
            <?php endif ?>
        </div>
    </div>

    <h2>
        <?= /* I18N: https://en.wikipedia.org/wiki/DomainKeys_Identified_Mail */
        I18N::translate('DKIM digital signature') ?>
    </h2>

    <div class="row mb-3">
        <label for="DKIM_DOMAIN" class="col-sm-3 col-form-label">
            <?= I18N::translate('Domain name') ?>
        </label>
        <div class="col-sm-9">
            <input type="text" class="form-control" id="DKIM_DOMAIN" name="DKIM_DOMAIN" value="<?= e($DKIM_DOMAIN) ?>" maxlength="255">
        </div>
    </div>

    <div class="row mb-3">
        <label for="DKIM_SELECTOR" class="col-sm-3 col-form-label">
            <?= I18N::translate('Selector') ?>
        </label>
        <div class="col-sm-9">
            <input type="text" class="form-control" id="DKIM_SELECTOR" name="DKIM_SELECTOR" value="<?= e($DKIM_SELECTOR) ?>" maxlength="255">
        </div>
    </div>

    <div class="row mb-3">
        <label for="DKIM_KEY" class="col-sm-3 col-form-label">
            <?= I18N::translate('Private key') ?>
        </label>
        <div class="col-sm-9">
            <textarea class="form-control" id="DKIM_KEY" name="DKIM_KEY"><?= e($DKIM_KEY) ?></textarea>
        </div>
    </div>

    <hr>

    <div class="row mb-3">
        <div class="offset-sm-3 col-sm-9">
            <p class="form-check">
                <input class="form-check-input" type="checkbox" id="test" name="test">
                <label class="form-check-label" for="test">
                    <?= I18N::translate('Send a test email using these settings') ?>
                </label>
            </p>

            <button type="submit" class="btn btn-primary">
                <?= view('icons/save') ?>
                <?= I18N::translate('save') ?>
            </button>

            <a href="<?= e(route(ControlPanel::class)) ?>" class="btn btn-secondary">
                <?= view('icons/cancel') ?>
                <?= I18N::translate('cancel') ?>
            </a>
        </div>
    </div>

    <?= csrf_field() ?>
</form>
