{% extends '@CpCreationVitiCore/admin/base.html.twig' %}

{% block content %}
    <div class="panel panel-default">
        <div class="panel-heading">
            {{ 'admin_client_import.title'|trans }}
            <div class="pull-right">
                <a href="{{ path('admin_imported_client') }}" class="btn btn-default btn-xs">
                    {{ 'admin_client_import.back_to_base'|trans }}
                </a>
            </div>
        </div>
        <div class="panel-body">
            <div class="alert alert-info">
                <strong>{{ 'admin_client_import.prerequisite_title'|trans }}</strong>
                {{ 'admin_client_import.prerequisite_message'|trans }}
                <a href="{{ path('admin_product_price_group') }}" class="alert-link">
                    {{ 'admin_client_import.manage_groups'|trans }}
                </a>
            </div>

            {% if analysis is null %}
                <p>{{ 'admin_client_import.upload.help'|trans }}</p>
                <ul>
                    <li>{{ 'admin_client_import.upload.formats'|trans }}</li>
                    <li>{{ 'admin_client_import.upload.first_sheet'|trans }}</li>
                    <li>{{ 'admin_client_import.upload.expected_columns'|trans }}</li>
                    <li>{{ 'admin_client_import.upload.existing_clients'|trans }}</li>
                </ul>

                {{ form_start(upload_form) }}
                    {{ form_row(upload_form.file) }}
                    <button type="submit" class="btn btn-primary">
                        {{ 'admin_client_import.upload.analyze'|trans }}
                    </button>
                {{ form_end(upload_form) }}
            {% else %}
                <h4>{{ 'admin_client_import.analysis.title'|trans }}</h4>
                <div class="table-responsive">
                    <table class="table table-bordered">
                        <tbody>
                        <tr>
                            <th>{{ 'admin_client_import.analysis.email_column'|trans }}</th>
                            <td>{{ analysis.emailHeader }} ({{ analysis.emailColumn }})</td>
                        </tr>
                        <tr>
                            <th>{{ 'admin_client_import.analysis.group_column'|trans }}</th>
                            <td>{{ analysis.groupHeader }} ({{ analysis.groupColumn }})</td>
                        </tr>
                        <tr>
                            <th>{{ 'admin_client_import.analysis.total_rows'|trans }}</th>
                            <td>{{ analysis.totalRows }}</td>
                        </tr>
                        <tr>
                            <th>{{ 'admin_client_import.analysis.importable_rows'|trans }}</th>
                            <td>{{ analysis.rows|length }}</td>
                        </tr>
                        <tr>
                            <th>{{ 'admin_client_import.analysis.invalid_emails'|trans }}</th>
                            <td>{{ analysis.invalidEmailRows }}</td>
                        </tr>
                        <tr>
                            <th>{{ 'admin_client_import.analysis.missing_groups'|trans }}</th>
                            <td>{{ analysis.missingGroupRows }}</td>
                        </tr>
                        </tbody>
                    </table>
                </div>

                <h4>{{ 'admin_client_import.mapping.title'|trans }}</h4>
                <p>{{ 'admin_client_import.mapping.help'|trans }}</p>

                {% for error in mapping_errors %}
                    <div class="alert alert-danger">{{ error }}</div>
                {% endfor %}

                {% if available_groups is empty %}
                    <div class="alert alert-warning">
                        {{ 'admin_client_import.mapping.no_groups'|trans }}
                        <a href="{{ path('admin_product_price_group_new') }}" class="alert-link">
                            {{ 'admin_client_import.mapping.create_group'|trans }}
                        </a>
                    </div>
                {% endif %}
                    <form method="post" action="{{ path('admin_imported_client_import_confirm') }}">
                        <input type="hidden" name="_token" value="{{ csrf_token('client-import-' ~ import_token) }}">
                        <input type="hidden" name="import_token" value="{{ import_token }}">

                        <div class="table-responsive">
                            <table class="table table-bordered">
                                <thead>
                                <tr>
                                    <th>{{ 'admin_client_import.mapping.source_group'|trans }}</th>
                                    <th>{{ 'admin_client_import.mapping.target_group'|trans }}</th>
                                </tr>
                                </thead>
                                <tbody>
                                {% for row in mapping_rows %}
                                    <tr>
                                        <td>{{ row.source }}</td>
                                        <td>
                                            <label class="sr-only" for="group_mapping_{{ row.key }}">
                                                {{ 'admin_client_import.mapping.target_for'|trans({'%group%': row.source}) }}
                                            </label>
                                            <select
                                                id="group_mapping_{{ row.key }}"
                                                name="group_mapping[{{ row.key }}]"
                                                class="form-control"
                                                required
                                            >
                                                <option value="">{{ 'admin_client_import.mapping.select'|trans }}</option>
                                                <option
                                                    value="{{ ignore_group_value }}"
                                                    {% if selected_mappings[row.key]|default('') == ignore_group_value %}selected{% endif %}
                                                >
                                                    {{ 'admin_client_import.mapping.ignore'|trans }}
                                                </option>
                                                {% for group in available_groups %}
                                                    <option
                                                        value="{{ group.id }}"
                                                        {% if selected_mappings[row.key]|default('') == group.id.toString %}selected{% endif %}
                                                    >
                                                        {{ group.name }}
                                                    </option>
                                                {% endfor %}
                                            </select>
                                        </td>
                                    </tr>
                                {% endfor %}
                                </tbody>
                            </table>
                        </div>

                        <button type="submit" class="btn btn-primary">
                            {{ 'admin_client_import.mapping.import'|trans }}
                        </button>
                        <a href="{{ path('admin_imported_client_import') }}" class="btn btn-default">
                            {{ 'admin_client_import.mapping.cancel'|trans }}
                        </a>
                    </form>
            {% endif %}
        </div>
    </div>
{% endblock %}
