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

{% block content %}
    <h1 class="page-header">{{ 'marketing.review.admin.title'|trans }}</h1>

    <div class="panel panel-default">
        <div class="panel-heading">
            <a class="btn btn-danger btn-rounded" href="{{ path('admin_marketing') }}">
                {{ 'marketing.common.back'|trans }}
            </a>
        </div>
    </div>

    <div class="alert alert-warning">
        <strong>{{ 'marketing.review.moderation.neutrality_title'|trans }}</strong>
        {{ 'marketing.review.moderation.neutrality_notice'|trans }}
    </div>

    <div class="panel panel-default">
        <div class="panel-heading">
            <strong>{{ 'marketing.review.admin.filters.title'|trans }}</strong>
        </div>
        <div class="panel-body">
            <form method="get" action="{{ path('admin_marketing_product_reviews') }}">
                <div class="row">
                    <div class="col-md-4">
                        <label for="review-filter-search">{{ 'marketing.review.admin.filters.search'|trans }}</label>
                        <input
                            id="review-filter-search"
                            class="form-control"
                            type="search"
                            name="search"
                            value="{{ filters.search }}"
                            maxlength="200"
                            placeholder="{{ 'marketing.review.admin.filters.search_placeholder'|trans }}"
                        >
                    </div>
                    <div class="col-md-3">
                        <label for="review-filter-product">{{ 'marketing.review.admin.filters.product'|trans }}</label>
                        <select id="review-filter-product" class="form-control" name="product">
                            <option value="">{{ 'marketing.review.admin.filters.all_products'|trans }}</option>
                            {% for product in filter_products %}
                                <option value="{{ product.id }}" {{ filters.product == product.id.toString ? 'selected' }}>
                                    {{ product.fullName }}
                                </option>
                            {% endfor %}
                        </select>
                    </div>
                    <div class="col-md-2">
                        <label for="review-filter-rating">{{ 'marketing.review.admin.filters.rating'|trans }}</label>
                        <select id="review-filter-rating" class="form-control" name="rating">
                            <option value="">{{ 'marketing.review.admin.filters.all_ratings'|trans }}</option>
                            {% for rating in 5..1 %}
                                <option value="{{ rating }}" {{ filters.rating == rating ? 'selected' }}>
                                    {{ 'marketing.review.admin.filters.rating_value'|trans({'%rating%': rating}) }}
                                </option>
                            {% endfor %}
                        </select>
                    </div>
                    <div class="col-md-3">
                        <label for="review-filter-status">{{ 'marketing.review.admin.filters.status'|trans }}</label>
                        <select id="review-filter-status" class="form-control" name="status">
                            <option value="all" {{ filters.status == 'all' ? 'selected' }}>
                                {{ 'marketing.review.admin.filters.all_statuses'|trans }}
                            </option>
                            {% for review_status in ['pending', 'published', 'rejected', 'withdrawn'] %}
                                <option value="{{ review_status }}" {{ filters.status == review_status ? 'selected' }}>
                                    {{ ('marketing.review.status.' ~ review_status)|trans }}
                                </option>
                            {% endfor %}
                        </select>
                    </div>
                </div>
                <div style="margin-top: 15px;">
                    <button class="btn btn-primary" type="submit">
                        {{ 'marketing.review.admin.filters.apply'|trans }}
                    </button>
                    {% if has_filters %}
                        <a class="btn btn-default" href="{{ path('admin_marketing_product_reviews') }}">
                            {{ 'marketing.review.admin.filters.reset'|trans }}
                        </a>
                    {% endif %}
                </div>
            </form>
        </div>
    </div>

    <div class="panel panel-info">
        <div class="panel-heading">{{ 'marketing.review.admin.latest'|trans }}</div>
        <div class="panel-body">
            {% if reviews is empty %}
                <p class="text-muted">
                    {{ (has_filters ? 'marketing.review.admin.filters.no_results' : 'marketing.review.admin.empty')|trans }}
                </p>
            {% else %}
                <div class="table-responsive">
                    <table class="table table-striped">
                        <thead>
                            <tr>
                                <th>{{ 'marketing.review.field.product'|trans }}</th>
                                <th>{{ 'marketing.review.field.author'|trans }}</th>
                                <th>{{ 'marketing.review.field.rating'|trans }}</th>
                                <th>{{ 'marketing.review.field.comment'|trans }}</th>
                                <th>{{ 'marketing.review.field.status'|trans }}</th>
                                <th>{{ 'marketing.review.field.date'|trans }}</th>
                                <th>{{ 'marketing.review.field.actions'|trans }}</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for review in reviews %}
                                <tr>
                                    <td>{{ review.product.fullName }}</td>
                                    <td>{{ review.authorDisplayName }}</td>
                                    <td>{{ review.rating }}/5</td>
                                    <td>
                                        {% if review.title %}<strong>{{ review.title }}</strong><br>{% endif %}
                                        {{ review.comment }}
                                        {% if review.rejectionReasonCode %}
                                            <div class="text-danger">
                                                {{ 'marketing.review.moderation.rejection_reason'|trans }} :
                                                {{ ('marketing.review.moderation.reason.' ~ review.rejectionReasonCode)|trans }}
                                                {% if review.rejectionReason %}— {{ review.rejectionReason }}{% endif %}
                                            </div>
                                        {% endif %}
                                        {% if review.withdrawalReasonCode %}
                                            <div class="text-danger">
                                                {{ 'marketing.review.moderation.withdrawal_reason'|trans }} :
                                                {{ ('marketing.review.moderation.reason.' ~ review.withdrawalReasonCode)|trans }}
                                                {% if review.withdrawalReason %}— {{ review.withdrawalReason }}{% endif %}
                                            </div>
                                        {% endif %}
                                        {% if review.moderationActions is not empty %}
                                            <details style="margin-top: 10px;">
                                                <summary>{{ 'marketing.review.history.title'|trans }}</summary>
                                                <ul class="list-unstyled" style="margin-top: 8px;">
                                                    {% for action in review.moderationActions %}
                                                        <li style="margin-bottom: 8px;">
                                                            <strong>{{ ('marketing.review.history.action.' ~ action.action)|trans }}</strong>
                                                            — {{ action.occurredAt|date('d/m/Y H:i') }}
                                                            — {{ action.actorIdentifier }}
                                                            {% if action.reasonCode %}
                                                                <br>{{ ('marketing.review.history.reason.' ~ action.reasonCode)|trans }}
                                                            {% endif %}
                                                            {% if action.reasonDetail %}
                                                                — {{ action.reasonDetail }}
                                                            {% endif %}
                                                            {% if action.action == constant('CpCreation\\VitiCore\\Marketing\\Model\\ProductReviewModerationAction::ACTION_RESUBMITTED') %}
                                                                <blockquote style="margin-top: 5px;">
                                                                    {{ action.snapshot.rating }}/5 —
                                                                    {% if action.snapshot.title %}<strong>{{ action.snapshot.title }}</strong><br>{% endif %}
                                                                    {{ action.snapshot.comment }}
                                                                </blockquote>
                                                            {% endif %}
                                                        </li>
                                                    {% endfor %}
                                                </ul>
                                            </details>
                                        {% endif %}
                                    </td>
                                    <td>{{ ('marketing.review.status.' ~ review.status)|trans }}</td>
                                    <td>{{ review.submittedAt|date('d/m/Y H:i') }}</td>
                                    <td style="min-width: 220px;">
                                        {% if is_granted('ROLE_SUPER_ADMIN') and review.status in [
                                            constant('CpCreation\\VitiCore\\Marketing\\Model\\ProductReview::STATUS_PENDING'),
                                            constant('CpCreation\\VitiCore\\Marketing\\Model\\ProductReview::STATUS_REJECTED')
                                        ] %}
                                            <form method="post" action="{{ path('admin_marketing_product_review_publish', filter_query|merge({id: review.id})) }}" style="margin-bottom: 8px;">
                                                <input type="hidden" name="_token" value="{{ csrf_token('publish-review-' ~ review.id) }}">
                                                <button class="btn btn-success btn-sm" type="submit">
                                                    {{ 'marketing.review.moderation.publish'|trans }}
                                                </button>
                                            </form>
                                        {% endif %}
                                        {% if is_granted('ROLE_SUPER_ADMIN') and review.status == constant('CpCreation\\VitiCore\\Marketing\\Model\\ProductReview::STATUS_PENDING') %}
                                            <form method="post" action="{{ path('admin_marketing_product_review_reject', filter_query|merge({id: review.id})) }}">
                                                <input type="hidden" name="_token" value="{{ csrf_token('reject-review-' ~ review.id) }}">
                                                <select class="form-control input-sm" name="reason_code" required>
                                                    <option value="">{{ 'marketing.review.moderation.reason_placeholder'|trans }}</option>
                                                    {% for reason_code in moderation_reason_codes %}
                                                        <option value="{{ reason_code }}">{{ ('marketing.review.moderation.reason.' ~ reason_code)|trans }}</option>
                                                    {% endfor %}
                                                </select>
                                                <textarea class="form-control input-sm" name="reason_detail" maxlength="2000" placeholder="{{ 'marketing.review.moderation.reason_detail_placeholder'|trans }}" style="margin-top: 5px;"></textarea>
                                                <button class="btn btn-danger btn-sm" type="submit" style="margin-top: 5px;">
                                                    {{ 'marketing.review.moderation.reject'|trans }}
                                                </button>
                                            </form>
                                        {% endif %}
                                        {% if is_granted('ROLE_SUPER_ADMIN') and review.status == constant('CpCreation\\VitiCore\\Marketing\\Model\\ProductReview::STATUS_PUBLISHED') %}
                                            <form method="post" action="{{ path('admin_marketing_product_review_withdraw', filter_query|merge({id: review.id})) }}">
                                                <input type="hidden" name="_token" value="{{ csrf_token('withdraw-review-' ~ review.id) }}">
                                                <select class="form-control input-sm" name="reason_code" required>
                                                    <option value="">{{ 'marketing.review.moderation.withdrawal_reason_placeholder'|trans }}</option>
                                                    {% for reason_code in moderation_reason_codes %}
                                                        <option value="{{ reason_code }}">{{ ('marketing.review.moderation.reason.' ~ reason_code)|trans }}</option>
                                                    {% endfor %}
                                                </select>
                                                <textarea class="form-control input-sm" name="reason_detail" maxlength="2000" placeholder="{{ 'marketing.review.moderation.reason_detail_placeholder'|trans }}" style="margin-top: 5px;"></textarea>
                                                <button class="btn btn-danger btn-sm" type="submit" style="margin-top: 5px;">
                                                    {{ 'marketing.review.moderation.withdraw'|trans }}
                                                </button>
                                            </form>
                                        {% endif %}
                                        {% if review.status == constant('CpCreation\\VitiCore\\Marketing\\Model\\ProductReview::STATUS_PUBLISHED') %}
                                            <form method="post" action="{{ path('admin_marketing_product_review_reply', filter_query|merge({id: review.id})) }}" style="margin-top: 8px;">
                                                <input type="hidden" name="_token" value="{{ csrf_token('reply-review-' ~ review.id) }}">
                                                <textarea class="form-control input-sm" name="reply" maxlength="5000" placeholder="{{ 'marketing.review.moderation.reply_placeholder'|trans }}">{{ review.merchantReply }}</textarea>
                                                <button class="btn btn-primary btn-sm" type="submit" style="margin-top: 5px;">
                                                    {{ 'marketing.review.moderation.save_reply'|trans }}
                                                </button>
                                            </form>
                                        {% endif %}
                                    </td>
                                </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
                <div class="text-center">
                    {{ knp_pagination_render(reviews) }}
                </div>
            {% endif %}
        </div>
    </div>
{% endblock %}
