{#
 # ---------------------------------------------------------------------
 #
 # GLPI - Gestionnaire Libre de Parc Informatique
 #
 # http://glpi-project.org
 #
 # @copyright 2015-2025 Teclib' and contributors.
 # @licence   https://www.gnu.org/licenses/gpl-3.0.html
 #
 # ---------------------------------------------------------------------
 #
 # LICENSE
 #
 # This file is part of GLPI.
 #
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
 # the Free Software Foundation, either version 3 of the License, or
 # (at your option) any later version.
 #
 # This program is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 # GNU General Public License for more details.
 #
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
 #
 # ---------------------------------------------------------------------
 #}

{# @var \Glpi\Form\AccessControl\ControlType\FormAccessControl access_control #}
{# @var \Glpi\Form\AccessControl\ControlType\DirectAccessConfig config #}
{# @var string                                                  url #}

{# Readonly input used to display the link, can be copied by clicking on it #}
<div class="d-flex w-100 mb-3">
    <div
        class="input-icon cursor-pointer flex-grow-1"
        data-glpi-clipboard-text="{{ url }}"
        data-bs-toggle="tooltip"
        data-bs-placement="top"
        title="{{ __("Click to copy to clipboard") }}"
    >
        <input
            type="text"
            class="form-control cursor-pointer rounded-end-0 border-end-0"
            readonly=""
            value="{{ url }}"
            aria-label="{{ __("Direct access URL") }}"
            data-glpi-direct-access-refresh-url
        >
        <span class="input-icon-addon">
            <i class="ti ti-files"></i>
        </span>
    </div>
    <i
        class="ti ti-refresh btn h-auto rounded-start-0"
        data-bs-toggle="tooltip"
        data-bs-placement="top"
        title="{{ __("Click to change the token") }}"
        data-glpi-direct-access-refresh-token
    ></i>
</div>

{# Unauthenticated access configuration #}
<label class="form-check form-switch">
    <input
        class="form-check-input"
        name="{{ access_control.getNormalizedInputName("_allow_unauthenticated") }}"
        type="hidden"
        value="0"
    >
    <input
        class="form-check-input"
        name="{{ access_control.getNormalizedInputName("_allow_unauthenticated") }}"
        type="checkbox"
        value="1"
        {{ config.allowUnauthenticated() ? 'checked' : '' }}
    >
    <span class="form-check-label">
        <i class="ti ti-lock-open"></i>
        {{ __("Allow unauthenticated users") }}
        <span class="form-help"
            data-bs-toggle="popover"
            data-bs-placement="top"
            data-bs-html="true"
            data-bs-content="{{ __("Users without accounts will be able to answer anonymously to this form. Authenticated users will not see this form.") }}">
            ?
        </span>
    </span>
</label>

<input
    type="hidden"
    name="{{ access_control.getNormalizedInputName("_token") }}"
    value="{{ config.getToken() }}"
    data-glpi-direct-access-token-input
>

<script>
    (() => {
        const refresh_button = document.querySelector('[data-glpi-direct-access-refresh-token]');
        const input = document.querySelector('[data-glpi-direct-access-token-input]');
        const preview = document.querySelector('[data-glpi-direct-access-refresh-url]');

        refresh_button.addEventListener('click', () => {
            // Quick copy of Toolbox::getRandomString()
            const keyspace = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
            let token = '';
            for (let i = 0; i <= 40; i++) {
                token += keyspace[Math.floor(Math.random() * keyspace.length)];
            }

            input.value = token;

            let url = new URL(preview.value);
            url.searchParams.set("token", token);
            preview.value = url;
        });
    })();
</script>
