{% extends 'Admin/layout.html.twig' %}
{%- import 'Admin/Form/macros.html.twig' as m -%}

{% set mediaTypePdf = constant('AppBundle\\Enum\\MediaType::PDF') %}
{% set mediaTypePicture = constant('AppBundle\\Enum\\MediaType::PICTURE') %}
{% set mediaTypeVideo = constant('AppBundle\\Enum\\MediaType::VIDEO') %}

{% block javascripts %}
    <script>
        //<![CDATA[
        $(function() {
            $('.active-element input[type="checkbox"]').on('change', function () {
                $.ajax({
                    type: "POST",
                    url: "{{ path('element_actif') }}",
                    data: {
                        id: $(this).val(),
                        actif: $(this).is(':checked') ? 'true' : 'false',
                        type: 'Content'
                    },
                    cache: false
                });
            });

            $('.fancybox\\.iframe').fancybox({
                type: 'iframe',
                autoSize: false,
                width: 1000,
                height: 500,
                padding: 0,
                afterClose: function() {
                    window.location.reload();
                }
            });

            $('#medias ul').sortable({
                axis: "y",
                update: function (event, ui) {
                    var sortableElement = ui.item.closest('ul');

                    sortableElement.sortable('disable');

                    if (ui.position.top !== ui.originalPosition.top) {
                        $.ajax({
                            url: ui.item.attr('data-url-sort'),
                            type : 'POST',
                            data: {
                                positionDesired: ui.item.index()
                            },
                            complete: function () {
                                sortableElement.sortable('enable');
                            }
                        });
                    }
                }
            });
        });
        //]]>
    </script>
{% endblock %}

{% set constTTTitleFixed = constant('AppBundle\\Enum\\TypeTemplateTitle::FIXED') %}
{% set constTTTitleNews = constant('AppBundle\\Enum\\TypeTemplateTitle::NEWS') %}
{% set constTTTitleSimple = constant('AppBundle\\Enum\\TypeTemplateTitle::SIMPLE') %}
{% set constTTTitleContact = constant('AppBundle\\Enum\\TypeTemplateTitle::CONTACT') %}
{% set constTTTitlePartners = constant('AppBundle\\Enum\\TypeTemplateTitle::PARTNERS') %}
{% set constTTTitleTestimonials = constant('AppBundle\\Enum\\TypeTemplateTitle::TESTIMONIALS') %}
{% set constTTTitleListe = constant('AppBundle\\Enum\\TypeTemplateTitle::LIST') %}
{% set constTTTitlePresentation = constant('AppBundle\\Enum\\TypeTemplateTitle::PRESENTATION') %}
{% set constTTTitleListChild = constant('AppBundle\\Enum\\TypeTemplateTitle::LIST_CHILD') %}

{% block body %}
    <header>
        <ul class="btns">
            <li class="btns-back">
                {{ m.step_back() }}
            </li>
            <li>
                <button class="btn btn-save" form="form-editpage" name="save">
                    {{ 'action.save'|trans({}, 'action') }}
                </button>
            </li>
        </ul>

        {% set page_title %}
            {%- if menu.id is null -%}
                Ajouter
                {%- if constTTTitleSimple == menu.typeTemplate.title %} une page simple
                {%- elseif constTTTitleContact == menu.typeTemplate.title %} une page contact
                {%- elseif constTTTitlePartners == menu.typeTemplate.title %} une page liste partenaires
                {%- elseif constTTTitleTestimonials == menu.typeTemplate.title %} une page liste témoignages
                {%- elseif constTTTitleListe == menu.typeTemplate.title %} une page liste
                {%- elseif constTTTitlePresentation == menu.typeTemplate.title %} une page présentation
                {%- elseif constTTTitleListChild == menu.typeTemplate.title %} un parent catégorie
                {%- endif %}
            {%- else -%}
                {%- if constTTTitleSimple == menu.typeTemplate.title -%}Page simple
                {%- elseif constTTTitleContact == menu.typeTemplate.title -%}Page contact
                {%- elseif constTTTitlePartners == menu.typeTemplate.title -%}Page liste partenaires
                {%- elseif constTTTitleTestimonials == menu.typeTemplate.title -%}Page liste témoignages
                {%- elseif constTTTitleListe == menu.typeTemplate.title -%}Page liste
                {%- elseif constTTTitlePresentation == menu.typeTemplate.title -%}Page présentation
                {%- elseif constTTTitleListChild == menu.typeTemplate.title -%}Parent catégorie
                {%- endif %} <strong><em>{{ form.title.vars.value }}</em></strong>

                {% if menu.parent %}
                    {% if menu.parent.parent %}
                        {%- if constTTTitleSimple == menu.typeTemplate.title -%} {% set page_link = path('template_level3', {id: menu.id, title: menu.slug, idlevel1:menu.parent.id, titlelevel1:menu.parent.slug, idlevel2:menu.parent.parent.id, titlelevel2:menu.parent.parent.slug}) %}
                        {%- elseif constTTTitleContact == menu.typeTemplate.title -%} {% set page_link = path('template_level3', {id: menu.id, title: menu.slug, idlevel1:menu.parent.id, titlelevel1:menu.parent.slug, idlevel2:menu.parent.parent.id, titlelevel2:menu.parent.parent.slug}) %}
                        {%- elseif constTTTitleNews == menu.typeTemplate.title -%} {% set page_link = path('list_actu', {id: menu.parent.id, slug: menu.parent.slug, catid: menu.id, catslug: menu.slug}) %}
                        {%- elseif constTTTitlePartners == menu.typeTemplate.title -%} {% set page_link = path('list_partners', {id: menu.id, slug: menu.slug}) %}
                        {%- elseif constTTTitleTestimonials == menu.typeTemplate.title -%} {% set page_link = path('list_testimonials', {id: menu.id, slug: menu.slug}) %}
                        {%- elseif constTTTitleListe == menu.typeTemplate.title -%} {% set page_link = path('template_level3', {id: menu.id, title: menu.slug, idlevel1:menu.parent.id, titlelevel1:menu.parent.slug, idlevel2:menu.parent.parent.id, titlelevel2:menu.parent.parent.slug}) %}
                        {%- elseif constTTTitlePresentation == menu.typeTemplate.title -%} {% set page_link = path('template_level3', {id: menu.id, title: menu.slug, idlevel1:menu.parent.id, titlelevel1:menu.parent.slug, idlevel2:menu.parent.parent.id, titlelevel2:menu.parent.parent.slug}) %}
                        {%- elseif constTTTitleListChild == menu.typeTemplate.title -%} {% set page_link = path('template_level3', {id: menu.id, title: menu.slug, idlevel1:menu.parent.id, titlelevel1:menu.parent.slug, idlevel2:menu.parent.parent.id, titlelevel2:menu.parent.parent.slug}) %}
                        {%- endif %}
                    {% else %}
                        {%- if constTTTitleSimple == menu.typeTemplate.title -%} {% set page_link = path('template_level2', {id: menu.id, title: menu.slug, idlevel1:menu.parent.id, titlelevel1:menu.parent.slug}) %}
                        {%- elseif constTTTitleContact == menu.typeTemplate.title -%} {% set page_link = path('template_level2', {id: menu.id, title: menu.slug, idlevel1:menu.parent.id, titlelevel1:menu.parent.slug}) %}
                        {%- elseif constTTTitleNews == menu.typeTemplate.title -%} {% set page_link = path('list_actu', {id: menu.parent.id, slug: menu.parent.slug, catid: menu.id, catslug: menu.slug}) %}
                        {%- elseif constTTTitlePartners == menu.typeTemplate.title -%} {% set page_link = path('list_partners', {id: menu.id, slug: menu.slug}) %}
                        {%- elseif constTTTitleTestimonials == menu.typeTemplate.title -%} {% set page_link = path('list_testimonials', {id: menu.id, slug: menu.slug}) %}
                        {%- elseif constTTTitleListe == menu.typeTemplate.title -%} {% set page_link = path('template_level2', {id: menu.id, title: menu.slug, idlevel1:menu.parent.id, titlelevel1:menu.parent.slug}) %}
                        {%- elseif constTTTitlePresentation == menu.typeTemplate.title -%} {% set page_link = path('template_level2', {id: menu.id, title: menu.slug, idlevel1:menu.parent.id, titlelevel1:menu.parent.slug}) %}
                        {%- elseif constTTTitleListChild == menu.typeTemplate.title -%} {% set page_link = path('template_level2', {id: menu.id, title: menu.slug, idlevel1:menu.parent.id, titlelevel1:menu.parent.slug}) %}
                        {%- endif %}
                    {% endif %}
                {% else %}
                    {%- if constTTTitleSimple == menu.typeTemplate.title -%} {% set page_link = path('template', {id: menu.id, title: menu.slug }) %}
                    {%- elseif constTTTitleContact == menu.typeTemplate.title -%} {% set page_link = path('template', {id: menu.id, title: menu.slug}) %}
                    {%- elseif constTTTitleNews == menu.typeTemplate.title -%} {% set page_link = path('list_actu', {id: menu.id, title: menu.slug, catid: 0, catslug: 'All'}) %}
                    {%- elseif constTTTitlePartners == menu.typeTemplate.title -%} {% set page_link = path('list_partners', {id: menu.id, slug: menu.slug}) %}
                    {%- elseif constTTTitleTestimonials == menu.typeTemplate.title -%} {% set page_link = path('list_testimonials', {id: menu.id, slug: menu.slug}) %}
                    {%- elseif constTTTitleListe == menu.typeTemplate.title -%} {% set page_link = path('template', {id: menu.id, title: menu.slug}) %}
                    {%- elseif constTTTitlePresentation == menu.typeTemplate.title -%} {% set page_link = path('template', {id: menu.id, title: menu.slug}) %}
                    {%- elseif constTTTitleListChild == menu.typeTemplate.title -%} {% set page_link = path('template', {id: menu.id, title: menu.slug}) %}
                    {%- endif %}
                {% endif %}
                (<em><a href="{{ page_link }}" target="_blank">{{ page_link }}</a></em>)
            {%- endif -%}
        {% endset %}
        <h1>{{ page_title|raw }}</h1>
    </header>
    {{ form_start(form, {'attr': {'id': 'form-editpage', 'enctype' : 'multipart/form-data'}}) }}
        {{ form_row(form_menu._token) }}
        <input name="template" type="hidden" value="{{ menu.typeTemplate.title }}"/>

        <fieldset>
            <legend>{{ 'form.admin.editPage.fieldset.menu.legend'|trans({}, 'form') }}</legend>
            
            {{ form_row(form_menu.title) }}
            {{ form_row(form_menu.menu_type) }}
            {{ form_row(form_menu.parent) }}
            {{ form_row(form_menu.position) }}
            {#{ form_row(form_menu.sentence) }#}
            {{ form_row(form_menu.is_active) }}
            {{ form_row(form_menu.is_visible) }}
            {% if form_menu_params is defined %}
                {{ form_rest(form_menu_params) }}
            {% endif %}
        </fieldset>

        <fieldset>
            <legend>{{ 'form.admin.editPage.fieldset.header.legend'|trans({}, 'form') }}</legend>
            {{ form_row(form_menu.class, {attr:{class:'form-row-childs-inline'}}) }}
            {{ form_row(form.title) }}
            {{ form_row(form.file) }}
            {{ form_row(form.introduction) }}
        </fieldset>

        <fieldset>
            <legend>{{ 'form.admin.editPage.fieldset.content.legend'|trans({}, 'form') }}</legend>
            {{ form_rest(form_menu) }}
            {{ form_rest(form) }}
        </fieldset>
     {{ form_end(form) }}

        {% if menu.id is not null %}
            {% if menu.typeTemplate.title in [constTTTitleListe, constTTTitlePresentation] %}
                <div class="page-content" id="items">
                    <h4 class="page-content-title">Items</h4>
                    <ul class="page-content-actions">
                        <li>
                            <a
                                class="btn btn-add fancybox.iframe"
                                href="{{ path('admin_content_add_content', {
                                    'menu':menu.id,
                                    'content':content.id,
                                    'iframe':true
                                }) }}"
                            >Ajouter un item</a>
                        </li>
                    </ul>
                    <div class="page-content-list">
                        {% for contenu in contenus %}
                            <div class="page-content-single delete-element-container">
                                <ul class="page-content-single-actions">
                                    <li class="active-element">
                                        <input
                                            {% if contenu.isactive %} checked="checked"{% endif %}
                                            id="item-{{ contenu.id }}-checkbox"
                                            type="checkbox"
                                            value="{{ contenu.id }}"
                                        />
                                        <span class="checkbox"></span>
                                        <label for="item-{{ contenu.id }}-checkbox">Actif</label>
                                    </li>
                                    <li>
                                        <a class="delete-element" href="{{ path('admin_menu_delete_ajax', {id:contenu.id}) }}">
                                            <span>{{ 'action.delete.label'|trans({}, 'action') }}</span>
                                        </a>
                                    </li>
                                </ul>
                                <div class="page-content-single-details">
                                    <a
                                        class="fancybox.iframe"
                                        href="{{ path('admin_content_edit_content', {
                                            'menu':menu.id,
                                            'content':contenu.id,
                                            'contentbase':content,
                                            'iframe':true
                                        }) }}"
                                    >
                                        <strong>{{ contenu.title }}</strong>
                                    </a><br/>
                                    {% if contenu.picture is not null %}<img alt="" src="{{ asset(contenu.getWebPath()) }}"/>{% endif %}
                                    {{ contenu.article|raw }}
                                </div>
                            </div>
                        {% endfor %}
                    </div>
                </div>
            {% endif %}

    {#
        <div class="page-content" id="modules">
            <h4 class="page-content-title">Modules</h4>
            <ul class="page-content-actions">
                <li>
                    <a
                        class="btn btn-add fancybox.iframe"
                        href="{{ path('admin_content_add_module', {
                            'menu':menu.id,
                            'content':content.id,
                            'iframe':true
                        }) }}"
                    >Ajouter un module</a>
                </li>
            </ul>
            <div class="page-content-list">
                {% for module in modules %}
                    <div class="page-content-single delete-element-container">
                        <ul class="page-content-single-actions">
                            <li>
                                <a class="delete-element" href="{{ path('admin_menu_delete_ajax', {id:module.id}) }}">
                                    <span>{{ 'action.delete.label'|trans({}, 'action') }}</span>
                                </a>
                            </li>
                        </ul>
                        <div class="page-content-single-details">
                            <a
                                class="fancybox.iframe"
                                href="{{ path('admin_content_edit_module', {
                                    'menu':menu.id,
                                    'content':module.id,
                                    'contentbase':content.id,
                                    'iframe':true
                                }) }}"
                            >
                                <strong>{{ module.title }}</strong>
                            </a><br/>
                            {{ module.article|raw }}
                        </div>
                    </div>
                {% endfor %}
            </div>
        </div>
    #}
        {% include ('Admin/Module/content.html.twig') %}

        {% if medias is defined and medias|length > 0 %}
            <div class="page-content" id="medias">
                <h4 class="page-content-title">Médias</h4>
                <p>Les médias de la page <em>{{ page_title|raw }}</em> peuvent être réorganisés pour chacun des types. Leur affichage sur le site sʼeffectuera dans lʼordre dans lequel ils apparaissent ici&nbsp;: le premier dʼabord.</p>
                {% set typeCurrent = null %}

                {% for media in medias %}
                    {% if media.type != typeCurrent %}
                        {% if typeCurrent is not null %}</ul></div>{% endif %}

                        {% set typeCurrent = media.type %}

                        <div class="medias-type">
                            <h5>
                                {% if mediaTypePicture == typeCurrent %}
                                    Images
                                {% elseif mediaTypePdf == typeCurrent %}
                                    PDF
                                {% else %}
                                    Vidéos
                                {% endif %}
                            </h5>
                            <ul>
                    {% endif %}

                    <li
                        class="element {{ media.type }}{% if not media.getIsActive() %} disabled{% endif %}"
                        {% if media.menumedias is not empty %}
                            data-url-sort="{{ path('element_position_inside', {mediaType:media.type, menuMediaId:media.menumedias|first.id}) }}"
                        {% endif %}
                        {% if not media.getIsActive() %} title="Ce média est désactivé : il nʼest pas visible aux visiteurs."{% endif %}
                    >
                        {% if mediaTypePicture == media.type %}
                            {# <img> and <figcaption> @see http://html5doctor.com/html5-simplequiz-4-figures-captions-and-alt-text/#comment-12023 #}
                            <figure>
                                <a class="fancybox" href="{{ asset(media.getWebPathPicture()) }}" rel="gallery" target="_blank" title="{{ media.title }}">
                                    <img alt="" src="{{ asset(media.getWebPathPicture()|imagine_filter('50H')) }}">
                                </a>
                                <figcaption>{{ media.title }}</figcaption>
                            </figure>
                        {% elseif mediaTypePdf == media.type %}
                            <div><a href="{{ asset(media.getWebPathPdf()) }}" target="_blank">{{ media.title }}</a></div>
                        {% elseif mediaTypeVideo == media.type %}
                            <figure>
                                <a class="fancybox fancybox.iframe" href="{{ path('video', {ref:media.path}) }}" target="_blank">
                                    <img alt="" height="90" src="//i1.ytimg.com/vi/{{ media.path }}/default.jpg" width="120">
                                </a>
                                <figcaption>{{ media.title }}</figcaption>
                            </figure>
                        {% else %}
                            <div><a href="{{ media.path }}" target="_blank">{{ media.title }}</a></div>
                        {% endif %}
                    </li>

                    {% if loop.last %}
                        </ul></div>
                    {% endif %}
                {% endfor %}
            </div>
        {% endif %}
    {% endif %}

    <ul class="btns">
        <li>
            <button class="btn btn-save" name="save" form="form-editpage">{{ 'action.save'|trans({}, 'action') }}</button>
        </li>
    </ul>
{% endblock %}
