{% extends "demo/_layout" %}

{% set form = freeform.form(craft.app.request.segment(4)) %}
{% set pageTitle = form.name ~ " form with " ~ craft.app.request.segment(3)|title|replace({'-':' '}) ~ " template" %}
{% if craft.app.request.segment(5) == "edit" %}
    {% set submissionToken = craft.app.request.segment(6) %}
{% endif %}

{% block content %}

    {% if form %}
    
        <div class="freeform-page-heading">
            <h2{{ craft.app.request.segment(5) == "edit" ? ' class="edit-mode-badge"' }}>{{ form.name }}</h2>
        </div>

        {# Check which if a duplicate check is applied #}
        {% set duplicateCheck = form.settings.duplicateCheck %}
        {% if duplicateCheck == "no_limit" %}
            {% set duplicateCheckType = false %}
        {% elseif duplicateCheck == "no_limit_logged_in_users_only" %}
            {% set duplicateCheckType = "Logged in Users Only - No Limit" %}
        {% elseif duplicateCheck == "limit_once_per_logged_in_user_only" %}
            {% set duplicateCheckType = "Logged in Users Only - Once per Form" %}
        {% elseif duplicateCheck == "limit_once_per_email" %}
            {% set duplicateCheckType = "Anyone - Once per Email Address" %}
        {% elseif duplicateCheck == "limit_once_per_email" %}
            {% set duplicateCheckType = "Anyone - Once per Email Address" %}
        {% elseif duplicateCheck == "limit_once_per_user_or_cookie" %}
            {% set duplicateCheckType = "Anyone - Once per Logged in User or Guest Cookie" %}
        {% elseif duplicateCheck == "limit_once_per_user_or_ip_or_cookie" %}
            {% set duplicateCheckType = "Anyone - Once per Logged in User or Guest IP or Cookie" %}
        {% endif %}
        
        {# Handle message content for Guests viewing a Logged-in User only form #}
        {% if duplicateCheck in ["no_limit_logged_in_users_only", "limit_once_per_logged_in_user_only"] and currentUser == false %}
            {% set guestUserForLoggedInOnly = true %}
        {% else %}
            {% set guestUserForLoggedInOnly = false %}
        {% endif %}

        {# Display a tip for forms with a duplicate check enabled #}
        {% if duplicateCheckType %}
            <div class="freeform-notice{{ colorMode == "dark" ? " freeform-notice-dark" }}">
                <h4>Please note...</h4>
                <p>You currently have a duplicate check enabled (<i>{{ duplicateCheckType }}</i>) for this form.</p>
            </div>
        {% endif %}

        {# Display a message if the form has already been submitted by the user. #}
        {% if form.duplicate %}

            <div class="freeform-error{{ colorMode == "dark" ? " freeform-notice-dark" }}">
                {# Display special message content for Guests viewing a Logged-in User only form #}
                <h4>{{ guestUserForLoggedInOnly ? "Not Allowed!" : "Already Submitted!" }}</h4>
                <p>
                    {{ guestUserForLoggedInOnly ? "You must be logged in to submit this form." : "You have already submitted this form! No duplicates are allowed for this form." }}
                </p>
            </div>

        {% else %}

            {{ form.render({
                submissionToken: submissionToken|default(null)
            }) }}

        {% endif %}

    {% else %}

        <div class="freeform-warning-notice{{ colorMode == "dark" ? " freeform-notice-dark" }}">
            Sorry, a form with the handle of <code>{{ formHandle }}</code> does not exist.
        </div>

    {% endif %}

{% endblock %}

{% block footerJs %}
<script>
    document.addEventListener('freeform-ready', (event) => {
        event.options.scrollOffset = -200;
    });
</script>
{% endblock %}