<?php
defined('C5_EXECUTE') or die('Access Denied.');

/**
 * @var Concrete\Core\Form\Service\Form $form
 * @var Concrete\Core\Validation\CSRF\Token $token
 * @var Concrete\Controller\SinglePage\Dashboard\System\Registration\AutomatedLogout $controller
 * @var Concrete\Core\Url\UrlImmutable $trustedProxyUrl
 * @var bool $invalidateOnIPMismatch
 * @var bool $enableUserSpecificIgnoredIPMismatches
 * @var bool $invalidateOnUserAgentMismatch
 * @var bool $invalidateInactiveUsers
 * @var bool $displayLogoutMessage
 * @var string $logoutMessage
 * @var int|null $inactiveTime
 * @var string[] $ignoredIPMismatches
 * @var IPLib\Address\AddressInterface $myIPAddress
 * @var string $confirmInvalidateString
 */
?>
<form method="POST" action="<?= $controller->action('save') ?>">
    <?php $token->output('save_logout') ?>

    <fieldset>
        <legend><?=t('Logout Message')?></legend>
        <div class="mb-3">
            <div class="form-check">
                <?= $form->checkbox('displayLogoutMessage', '1', $displayLogoutMessage) ?>
                <label class="form-check-label" for="displayLogoutMessage"><?= t('Display a confirmation message when a successful logout occurs.') ?></label>
            </div>
        </div>
        <div class="mb-3">
            <?= $form->label('logoutMessage', t('Logout Message')) ?>
            <?= $form->textarea('logoutMessage', $logoutMessage) ?>
        </div>
    </fieldset>


    <fieldset>
        <legend><?=t('Session Security')?></legend>
        <div class="alert alert-info">
            <?= t('These settings help prevent a user from stealing other logged in user sessions. You may want to configure %s"Trusted Proxies"%s instead', '<a href="' . $trustedProxyUrl . '">', '</a>') ?>
        </div>
        <div class="form-check">
            <?= $form->checkbox('invalidateOnIPMismatch', '1', $invalidateOnIPMismatch) ?>
            <label class="form-check-label" for="invalidateOnIPMismatch"><?= t('Log users out if their IP changes') ?></label>
        </div>
        <div class="form-check">
            <?= $form->checkbox('enableUserSpecificIgnoredIPMismatches', '1', $enableUserSpecificIgnoredIPMismatches) ?>
            <label class="form-check-label" for="enableUserSpecificIgnoredIPMismatches"><?= t('Enable user-specific IP addresses to be ignored') ?></label>
        </div>
        <div class="form-check">
            <?= $form->checkbox('invalidateOnUserAgentMismatch', '1', $invalidateOnUserAgentMismatch) ?>
            <label class="form-check-label" for="invalidateOnUserAgentMismatch"><?= t("Log users out if their browser's user agent changes") ?></label>
        </div>
        <div class="form-check">
            <?= $form->checkbox('invalidateInactiveUsers', '1', $invalidateInactiveUsers) ?>
            <label class="form-check-label" for="invalidateInactiveUsers">
                <span class="row row-cols-auto g-0 align-items-center">
                    <?= t(
    'Automatically log out users who are inactive for %s seconds or more.',
    $form->number('inactiveTime', $inactiveTime, ['style' => 'width: 5rem', 'min' => '15', 'class' => 'form-control-sm ms-1 me-1'] + ($invalidateInactiveUsers ? [] : ['disabled' => 'disabled']))
) ?>
                </span>
            </label>
        </div>
    </fieldset>

    <div class="form-group">
        <?= $form->label('ignoredIPMismatches', t('Prevent logout if changed IP addresses are in the following ranges')) ?>
        <?= $form->textarea('ignoredIPMismatches', implode("\n", $ignoredIPMismatches)) ?>
        <div class="small text-muted">
            <?= t('Separate IP addresses with spaces or new lines.') ?><br />
            <?= t(
                'Accepted values are single addresses (IPv4 like %1$s, and IPv6 like %2$s) and ranges in subnet format (IPv4 like %3$s, and IPv6 like %4$s).',
                '<code>127.0.0.1</code>',
                '<code>::1</code>',
                '<code>127.0.0.1/24</code>',
                '<code>::1/8</code>'
            ) ?><br />
            <?= t('Your IP address:') ?> <code><?= h((string) $myIPAddress) ?></code>
        </div>
    </div>

    <div class="ccm-dashboard-form-actions-wrapper">
        <div class="ccm-dashboard-form-actions">
            <div class="d-flex">
                <a href="javascript:void(0)" class="btn btn-danger" id="invalidate-sessions-button"><?= t('Log out all active users') ?></a>
                <button class="ms-auto btn btn-primary" type="submit"><?= t('Save') ?></button>
            </div>
        </div>
    </div>
</form>

<fieldset>
    <legend><?=t('Invalidate Active Sessions') ?></legend>

    <form method="POST" id="invalidate-sessions-form" action="<?= $controller->action('invalidate_sessions') ?>">
        <?php $token->output('invalidate_sessions') ?>

        <div class="form-group">
            <?= $form->text('confirmation', '', ['autocomplete' => 'off']) ?>
            <small class="text-muted">
                <?= t('Type %s in the above box to proceed.', "<code>{$confirmInvalidateString}</code>") ?>
            </small>
        </div>

    </form>
</fieldset>


<script>
$(document).ready(function() {
    $('#invalidateOnIPMismatch')
        .on('change', function() {
            var enabled = $('#invalidateOnIPMismatch').is(':checked');
            $('#enableUserSpecificIgnoredIPMismatches,#ignoredIPMismatches').attr('disabled', !enabled);
        })
        .trigger('change')
    ;
    $('#invalidateInactiveUsers')
        .on('change', function() {
            $('#inactiveTime').attr('disabled', $(this).is(':checked') ? null : 'disabled');
        })
        .trigger('change')
    ;
    var $confirmation = $('#confirmation')
        $form = $('#invalidate-sessions-form');

    $form.on('submit', function(e) {
        e.preventDefault();
        return false;
    });
    function isConfirmed() {
        return $confirmation.val().toLowerCase() === <?= json_encode($confirmInvalidateString)?>.toLowerCase();
    }

    $confirmation.on('focus input', function() {
        var ok = isConfirmed();
        $confirmation.toggleClass('is-valid', ok).toggleClass('is-invalid', !ok);
        
    });

    $('#invalidate-sessions-button').on('click', function(e) {
        e.preventDefault();
        if (!isConfirmed()) {
            $confirmation.select().focus();
            return;
        }
        ConcreteAlert.confirm(
            <?= json_encode('<strong>' . t('Warning:') . '</strong> ' . t('This action will automatically log out all users (including yourself!)')) ?>,
            function() {
                $form.off('submit').submit();
            },
            'btn-danger',
            <?= json_encode(t('Log out')) ?>
        );
    });
});
</script>
