{% extends base_template %}

{% import _self as macros %}

{% block actions %}
{% endblock %}
{% block breadcrumb %}{% endblock %}
{%- block tab_menu -%}
    {{ knp_menu_render(admin.sidemenu(action), {
        'currentClass': 'active',
        'template': get_global_template('tab_menu_template')
    }, 'twig') }}
{%- endblock -%}

{% block title %}
    {{ 'Import de données'|trans }}
{% endblock %}

{% block navbar_title %}
    {{ block('title') }}
{% endblock %}

{% block content %}
    <div class="p-4">
    	<h4><u>{{ "Exemples d'importations possible (Fichiers .csv uniquement):"|trans }}</u></h4>
    	<div class='mb-5'>
    		{% for key, csvparam in csvparams %}
				<h5><strong>{{ key|capitalize }} : </strong><a class='ml-4' target='_blank' href='{{ csvparam.file }}'>Télécharger un exemple</a></h5>
				<p class='ml-4'><i>{{ csvparam.header }}</i></p>
				<p class='ml-4 mb-4'><i>{{ 'Exemple'|trans }} :</i> {{ csvparam.example }}</p>
	    	{% endfor %}
	    </div>
    	{{form_start(form)}}
			{{ form_row(form.media) }}
			{{ form_row(form.user) }}
			{{ form_row(form.save) }}
		{{form_end(form)}}

		{% if errors is defined and errors|length > 0 %}
			<div class='container bg-danger w-100 py-4 row'>
				{% if errors['error'] is defined %}
					{{ errors['error'] }}
				{% else %}
					{{  macros.show_error(errors) }}
				{% endif %}
			</div>
		{% endif %}
		{% if warnings is defined and warnings|length > 0 %}
			<div class='container bg-warning w-100 py-4 row'>
				{{  macros.show_error(warnings) }}
			</div>
		{% endif %}
		{% if success is defined and success|length > 0 %}
			<div class='container bg-success w-100 py-4 row'>
				{{  macros.show_error(success) }}
			</div>
		{% endif %}
		{% if linkcsverror is defined and linkcsverror != null %}
			<div class='py-4'>
				<a href='{{ linkcsverror }}' role="button" class="btn-primary btn">{{ 'Télécharger CSV avec lignes en erreurs'|trans }}</a>
			</div>
		{% endif %}
    </div>
{% endblock %}

{% macro show_error(errors) %}
	{% for line, errorA in errors %}
		<div class='col-12'>
			<span>Ligne {{line}}</span>
			{% for key, errorB in errorA %}
				{% for csvline, errorString in errorB %}
					<span class="pl-2">{% if (key is not empty) %}[{{key}}]{% endif %}{{errorString}}</span>
				{% endfor %}
			{% endfor %}
		</div>
	{% endfor %}
{% endmacro %}