{% extends "layouts/_base.twig" %}

{% block main %}
    <form class="absolute top-16 right-16" method="post" action="" accept-charset="UTF-8">
        <input
                type="hidden"
                name="action"
                value="api2pdf/pdf/generate-from-url"
        />
        <input type="hidden" name="url" value="{{ category.url }}" />
        <!-- Redirect to the PDF URL -->
        <input type="hidden" name="options[redirect]" value="1" />
        {{ csrfInput() }}
        <input type="submit" class="bg-red p-2 rounded text-white hidden xl:block hover:cursor-pointer hover:bg-opacity-90" value="Generate PDF from URL" />
    </form>

    <div class="max-w-7xl mx-auto py-16">

        <img class="w-40 mx-auto mb-8" src="/assets/img/logo-black.svg" alt="NWO Brand Audit">

        <h4 class="text-center">Brand Audit responses for {{ category.title }}</h4>

        {# Get an entry so that craft can loop through all of the fields #}
        {% set entry = craft.entries().section('audits').one() %}

        {% set users = craft.users().relatedTo(category).ids() %}

        {% set audits = craft.entries().section('audits').authorId(users).all() %}

        {# Get all the fields for the audit entries and loop through them #}
        {% for fieldLayoutField in entry.getFieldLayout().getFields() %}
            {% set field = craft.app.fields.getFieldById(fieldLayoutField.id) %}
            {% set fieldHandle = field.handle %}

            <div class="py-8">
                <h5 class="text-2xl">{{ field.name }}</h5>
                {% for audit in audits %}
                    {% if audit | length %}
                        <div class="py-4">
                            <div><strong>{{ audit.author.firstName }}</strong></div>
                            {# Get the audit and print out the response for the current field #}
                            {{ attribute(audit, fieldHandle) }}
                        </div>
                    {% endif %}
                {% endfor %}
            </div>
            <hr>

        {% endfor %}
    </div>

{% endblock %}