{% extends request.ajax ? 'layout_blank.html.twig' : 'layout_default.html.twig' %}

{% import 'macro_functions.html.twig' as mf %}

{% set active_menu = 'support' %}

{% block breadcrumbs %}
    <ol class="breadcrumb">
        <li class="breadcrumb-item">
            <a href="{{ '/'|alink }}">
                <svg class="icon" width="24">
                    <use xlink:href="#home" />
                </svg>
            </a>
        </li>
        <li class="breadcrumb-item">
            <a href="{{ 'support'|alink }}">{{ 'Support'|trans }}</a>
        </li>
        <li class="breadcrumb-item active" aria-current="page">{{ 'Canned Responses'|trans }}</li>
    </ol>
{% endblock %}

{% block meta_title %}Canned Responses{% endblock %}

{% block content %}
    <ul class="nav nav-tabs" role="tablist">
        <li class="nav-item" role="presentation">
            <a class="nav-link active" href="#tab-index" data-bs-toggle="tab">{{ 'Canned Responses'|trans }}</a>
        </li>
        <li class="nav-item" role="presentation">
            <a class="nav-link" href="#tab-new" data-bs-toggle="tab">
                <svg class="icon me-2">
                    <use xlink:href="#plus" />
                </svg>
                {{ 'New Response'|trans }}
            </a>
        </li>
        <li class="nav-item" role="presentation">
            <a class="nav-link" href="#tab-categories" data-bs-toggle="tab">{{ 'Manage Categories'|trans }}</a>
        </li>
        <li class="nav-item" role="presentation">
            <a class="nav-link" href="#tab-new-category" data-bs-toggle="tab">
                <svg class="icon me-2">
                    <use xlink:href="#plus" />
                </svg>
                {{ 'New Category'|trans }}
            </a>
        </li>
    </ul>

<div class="card">
    <div class="tab-content">
        <div class="tab-pane fade show active" id="tab-index" role="tabpanel">
            {% include 'partial_search.html.twig' %}
            <table class="table card-table table-vcenter table-striped text-nowrap sortable">
                <thead>
                    <tr>
                        <th>{{ 'Title'|trans }}</th>
                        <th class="text-center">{{ 'Category'|trans }}</th>
                        <th class="w-1 no-sort"></th>
                    </tr>
                </thead>
                <tbody>
                {% set responses = admin.support_canned_get_list({ 'per_page': 90, 'page': request.page }|merge(request)) %}
                {% for i, response in responses.list %}
                <tr>
                    <td>
                        <a href="{{ '/support/canned'|alink }}/{{ response.id }}">{{ response.title }}</a>
                    </td>
                    <td class="text-center">
                        <a href="{{ '/support/canned-category'|alink }}/{{ response.category.id }}">{{ response.category.title }}</a>
                    </td>
                    <td>
                        <a class="btn btn-icon" href="{{ '/support/canned'|alink }}/{{ response.id }}">
                            <svg class="icon">
                                <use xlink:href="#edit" />
                            </svg>
                        </a>
                        <a class="btn btn-icon api-link" data-api-confirm="{{ 'Are you sure?'|trans }}" href="{{ 'api/admin/support/canned_delete'|link({ 'id': response.id, 'CSRFToken': CSRFToken }) }}" data-api-redirect="{{ 'support/canned-responses'|alink }}">
                            <svg class="icon">
                                <use xlink:href="#delete" />
                            </svg>
                        </a>
                    </td>
                </tr>
                {% else %}
                <tr>
                    <td class="text-muted" colspan="3">{{ 'The list is empty'|trans }}</td>
                </tr>
                {% endfor %}
                </tbody>
            </table>
            {% include 'partial_pagination.html.twig' with { 'list': responses, 'url': 'support/canned-responses' } %}
        </div>

        <div class="tab-pane fade" id="tab-new" role="tabpanel">
            <div class="card-body">
                <form method="post" action="{{ 'api/admin/support/canned_create'|link }}" class="save api-form" data-api-redirect="{{ 'support/canned-responses'|alink }}">
                    <input type="hidden" name="CSRFToken" value="{{ CSRFToken }}"/>
                    <div class="mb-3 row">
                        <label class="form-label col-3 col-form-label">{{ 'Category'|trans }}:</label>
                        <div class="col">
                            {{ mf.selectbox('category_id', admin.support_canned_category_pairs, '', 1) }}
                        </div>
                    </div>
                    <div class="mb-3 row">
                        <label class="form-label col-3 col-form-label">{{ 'Title'|trans }}:</label>
                        <div class="col">
                            <input class="form-control" type="text" name="title" value="{{ request.title }}" required>
                        </div>
                    </div>

                    <div class="mb-3 row">
                        <label class="form-label col-3 col-form-label">{{ 'Content'|trans }}</label>
                        <div class="col">
                            <textarea class="form-control" name="content" rows="10">{{ request.content }}</textarea>
                        </div>
                    </div>

                    <button class="btn btn-primary w-100" type="submit">{{ 'Create'|trans }}</button>
                </form>
            </div>
        </div>

        <div class="tab-pane fade" id="tab-categories" role="tabpanel">
            <table class="table card-table table-vcenter table-striped text-nowrap">
                <thead>
                    <tr>
                        <th>{{ 'Title'|trans }}</th>
                        <th class="w-1"></th>
                    </tr>
                </thead>
                <tbody>
                    {% for cat_id, cat_title in admin.support_canned_category_pairs %}
                    <tr>
                        <td>
                            <a href="{{ '/support/canned-category'|alink }}/{{ cat_id }}">{{ cat_title }}</a>
                        </td>
                        <td>
                            <a class="btn btn-icon" href="{{ '/support/canned-category'|alink }}/{{ cat_id }}">
                                <svg class="icon">
                                    <use xlink:href="#edit" />
                                </svg>
                            </a>
                            <a class="btn btn-icon api-link"
                                href="{{ 'api/admin/support/canned_category_delete'|link({ 'id': cat_id }) }}"
                                data-api-confirm="{{ 'Are you sure?'|trans }}"
                                data-api-redirect="{{ 'support/canned-responses'|alink }}">
                                <svg class="icon">
                                    <use xlink:href="#delete" />
                                </svg>
                            </a>
                        </td>
                    </tr>
                    {% else %}
                    <tr>
                        <td class="text-muted" colspan="3">{{ 'The list is empty'|trans }}</td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>

        <div class="tab-pane fade" id="tab-new-category" role="tabpanel">
            <div class="card-body">
                <form method="post" action="{{ 'api/admin/support/canned_category_create'|link }}" class="api-form" data-api-redirect="{{ 'support/canned-responses'|alink }}">
                    <input type="hidden" name="CSRFToken" value="{{ CSRFToken }}"/>
                    <div class="mb-3 row">
                        <label class="form-label col-3 col-form-label">{{ 'Title'|trans }}:</label>
                        <div class="col">
                            <input class="form-control" type="text" name="title" value="{{ request.title }}" required>
                        </div>
                    </div>

                    <input type="submit" value="{{ 'Create'|trans }}" class="btn btn-primary w-100">
                </form>
            </div>
        </div>
    </div>
</div>
{% endblock %}
