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

{% block content %}
    <div class="grid-container">
        <div class="grid-x grid-margin-x align-middle">
            <div class="cell auto">
                <h1>{{ 'social_publishing.title'|trans }}</h1>
                <p>{{ 'social_publishing.intro'|trans }}</p>
            </div>
            {% if channels is not empty %}
                <div class="cell shrink">
                    <a class="button primary" href="{{ path('admin_social_publication_new') }}">
                        {{ 'social_publishing.new_publication'|trans }}
                    </a>
                </div>
            {% endif %}
        </div>

        {% if not hub_available %}
            <div class="callout warning">
                <h2>{{ 'social_publishing.hub_unavailable_title'|trans }}</h2>
                <p>{{ 'social_publishing.hub_unavailable'|trans }}</p>
            </div>
        {% else %}
            <section class="panel panel-default">
                <div class="panel-heading"><h2>{{ 'social_publishing.channels.title'|trans }}</h2></div>
                <div class="panel-body">
                    <form method="post" action="{{ path('admin_social_connect', {provider: 'meta'}) }}">
                        <input type="hidden" name="_token" value="{{ csrf_token('social-connect-meta') }}">
                        <button class="button" type="submit">{{ 'social_publishing.channels.connect_meta'|trans }}</button>
                    </form>

                    {% if channels is empty %}
                        <p>{{ 'social_publishing.channels.empty'|trans }}</p>
                    {% else %}
                        <div class="grid-x grid-margin-x">
                            {% for channel in channels %}
                                <article class="cell medium-6 panel">
                                    <h3>{{ channel.name }}</h3>
                                    <p>{{ ('social_publishing.channel_type.' ~ channel.type)|trans }} · {{ channel.provider|upper }}</p>
                                    <form method="post" action="{{ path('admin_social_disconnect', {channelId: channel.id}) }}">
                                        <input type="hidden" name="_token" value="{{ csrf_token('social-disconnect-' ~ channel.id) }}">
                                        <button class="button alert small" type="submit">{{ 'social_publishing.channels.disconnect'|trans }}</button>
                                    </form>
                                </article>
                            {% endfor %}
                        </div>
                    {% endif %}
                </div>
            </section>

            <section class="panel panel-default">
                <div class="panel-heading"><h2>{{ 'social_publishing.publications.title'|trans }}</h2></div>
                <div class="panel-body">
                    {% if publications is empty %}
                        <p>{{ 'social_publishing.publications.empty'|trans }}</p>
                    {% else %}
                        <table>
                            <thead>
                                <tr>
                                    <th>{{ 'social_publishing.publications.date'|trans }}</th>
                                    <th>{{ 'social_publishing.publications.content'|trans }}</th>
                                    <th>{{ 'social_publishing.publications.destinations'|trans }}</th>
                                    <th>{{ 'social_publishing.publications.status'|trans }}</th>
                                    <th></th>
                                </tr>
                            </thead>
                            <tbody>
                            {% for publication in publications %}
                                <tr>
                                    <td>{{ publication.scheduledAt|date('d/m/Y H:i') }}</td>
                                    <td>{{ publication.message|u.truncate(90, '…') }}</td>
                                    <td>
                                        {% for target in publication.targets %}
                                            {{ target.channel.name }}{% if not loop.last %}, {% endif %}
                                        {% endfor %}
                                    </td>
                                    <td>{{ ('social_publishing.status.' ~ publication.status)|trans }}</td>
                                    <td>
                                        {% if publication.status == 'scheduled' %}
                                            <form method="post" action="{{ path('admin_social_publication_cancel', {publicationId: publication.id}) }}">
                                                <input type="hidden" name="_token" value="{{ csrf_token('social-cancel-' ~ publication.id) }}">
                                                <button class="button secondary tiny" type="submit">{{ 'social_publishing.publications.cancel'|trans }}</button>
                                            </form>
                                        {% endif %}
                                    </td>
                                </tr>
                            {% endfor %}
                            </tbody>
                        </table>
                    {% endif %}
                </div>
            </section>
        {% endif %}
    </div>
{% endblock %}
