{% extends "demo/_layout" %}

{% set pageTitle = "Checking Submission Duplicates | Extras" %}

{# Replace 'freeformSubmissionDuplicate' with your form handle #}
{% set demoFormHandle = "freeformSubmissionDuplicate" %}

{% block content %}

    {% set form = freeform.form(demoFormHandle, { formattingTemplate: pageTemplateFile }) %}

    <div class="freeform-page-heading">
        <h2>Checking Submission Duplicates</h2>
        <p>This example shows how to check if the user has already submitted the form when using the <i>Duplicate Check</i> setting.</p>
    </div>

    {% 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-error{{ colorMode == "dark" ? " freeform-notice-dark" }}">
            <p>You must rename your form handle to <code>{{ demoFormHandle }}</code> and select an option for the <i>Duplicate Check</i> setting (inside the form builder Settings tab) for this part of the demo to work.</p>
        </div>

    {% endif %}

    {# Instructions to get this page working correctly #}
    <div class="extras-instructions">
        <h4>This page will not display correctly until...</h4>
        <p>
            In order to view this page live, you'll need to make some adjustments to this template or your test form:
            <ol>
                <li>Rename your test form handle to <code>{{ demoFormHandle }}</code> or adjust the form handle name inside this template to match your test form.</li>
                <li>
                    Make sure you have the <b>Duplicate Check</b> setting enabled (inside the form builder Settings tab).
                </li>
                <li>This page template contains a conditional that will hide the form once it detects that the user has already submitted the form.</li>
            </ol>
        </p>
    </div>

{% endblock %}