{#
 # ---------------------------------------------------------------------
 #
 # GLPI - Gestionnaire Libre de Parc Informatique
 #
 # http://glpi-project.org
 #
 # @copyright 2015-2026 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/>.
 #
 # ---------------------------------------------------------------------
 #}

{% extends "layout/page_without_tabs.html.twig" %}

{% set container_size = "narrow" %}

{% block content_title %}
    {{ __('Import forms') }}
{% endblock content_title %}

{% block content_body %}
    <form
        method="POST"
        action="{{ path('Form/Import/Execute') }}"
        data-submit-once
    >
        <div class="card">
            <div class="card-header py-3 px-4">
                <h3 class="card-title">{{ __("Import preview") }}</h3>
            </div>
            <table class="table table-card mb-0">
                <thead>
                    <tr>
                        <th class="w-50 px-4">{{ __("Form name") }}</th>
                        <th class="w-25 px-4">{{ __("Status") }}</th>
                        <th class="w-25 px-4">{{ _n('Action', 'Actions', get_plural_number()) }}</th>
                    </tr>
                </thead>
                <tbody>
                    {% macro render_row(form_id, form_name, status_icon, messages, show_resolve_issues_action, show_remove_action) %}
                        <tr>
                            <td class="w-50 px-4 align-middle">{{ form_name }}</td>
                            <td class="w-50 px-4 align-middle">
                                <div class="d-flex align-items-center">
                                    <i class="ti {{ status_icon }} me-2"></i>
                                    <div>
                                        {% for message in messages %}
                                            <span>{{ message }}</span>
                                        {% endfor %}
                                    </div>
                                </div>
                            </td>
                            <td class="w-25 px-4 align-middle">
                                {% if show_resolve_issues_action or show_remove_action %}
                                    <div class="d-flex flex-row-reverse align-items-center gap-2">
                                        {% if show_remove_action %}
                                            <button
                                                type="submit"
                                                class="btn btn-link p-0 text-danger"
                                                name="skipped_forms[]"
                                                value="{{ form_id }}"
                                                formaction="{{ path('Form/Import/Preview') }}"
                                                title="{{ __('Remove this form from the import list') }}"
                                                aria-label="{{ __('Remove form') }}"
                                            >
                                                <i class="ti ti-trash"></i>
                                            </button>
                                        {% endif %}

                                        {% if show_resolve_issues_action and show_remove_action %}
                                        <span class="vr"></span>
                                        {% endif %}

                                        {% if show_resolve_issues_action %}
                                            <button
                                                type="submit"
                                                class="btn btn-link p-0"
                                                name="form_id"
                                                value="{{ form_id }}"
                                                formaction="{{ path('Form/Import/ResolveIssues') }}"
                                            >
                                                {{ __("Resolve issues") }}
                                            </button>
                                        {% endif %}
                                    </div>
                                {% endif %}
                            </td>
                        </tr>
                    {% endmacro %}

                    {% for form_id, form_name in preview.getValidForms() %}
                        {{ _self.render_row(
                            form_id,
                            form_name,
                            'ti-check text-success',
                            [__("Ready to be imported")],
                            false,
                            true
                        ) }}
                    {% endfor %}
                    {% for form_id, form_name in preview.getInvalidForms() %}
                        {{ _self.render_row(
                            form_id,
                            form_name,
                            'ti-x text-danger',
                            [__("Can't be imported")],
                            true,
                            true
                        ) }}
                    {% endfor %}
                    {% for form_id, details in preview.getFormsWithFatalErrors() %}
                        {{ _self.render_row(
                            form_id,
                            details.name,
                            'ti-x text-danger',
                            details.errors,
                            false,
                            true
                        ) }}
                    {% endfor %}
                </tbody>
            </table>
        </div>

        <div class="row mt-3">
            <div class="col">
                <div class="justify-content-end d-flex">
                    <button type="submit" href="#" class="btn btn-primary">
                        {{ __("Import") }}
                    </button>
                </div>
            </div>
        </div>

        <input type="hidden" name="json" value="{{ json }}" />
        <input type="hidden" name="_glpi_csrf_token" value="{{ csrf_token() }}" />

        {% for skipped_form_id in preview.getSkippedForms()|keys %}
            <input type="hidden" name="skipped_forms[]" value="{{ skipped_form_id }}" />
        {% endfor %}

        {% for replacement_data in replacements %}
            <input type="hidden" name="replacements[{{ loop.index0 }}][itemtype]" value="{{ replacement_data.itemtype }}"/>
            <input type="hidden" name="replacements[{{ loop.index0 }}][original_name]" value="{{ replacement_data.original_name }}"/>
            <input type="hidden" name="replacements[{{ loop.index0 }}][replacement_id]" value="{{ replacement_data.replacement_id }}"/>
        {% endfor %}
    </form>
{% endblock content_body %}

