{% 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">{{ 'Knowledge Base'|trans }}</li>
    </ol>
{% endblock %}

{% block meta_title %}{{ 'Knowledge Base'|trans }}{% 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">{{ 'Articles'|trans }}</a>
        </li>
        <li class="nav-item" role="presentation">
            <a class="nav-link" href="#tab-new-article" data-bs-toggle="tab">
                <svg class="icon me-2">
                    <use xlink:href="#plus" />
                </svg>
                {{ 'New Article'|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>{{ 'Category'|trans }}</th>
                        <th>{{ 'Status'|trans }}</th>
                        <th class="text-center">{{ 'Views'|trans }}</th>
                        <th class="w-1"></th>
                    </tr>
                </thead>
                <tbody>
                    {% set posts = admin.support_kb_article_get_list({ 'per_page': 30, 'page': request.page }|merge(request)) %}
                    {% for i, post in posts.list %}
                    <tr>
                        <td>
                            <a href="{{ 'support/kb/article'|alink }}/{{ post.id }}">{{ post.title }}</a>
                        </td>
                        <td>
                            <a href="{{ 'support/kb/category'|alink }}/{{ post.category.id }}">{{ post.category.title }}</a>
                        </td>
                        <td>
                            {% if post.status == 'active' %}
                                <span class="badge bg-success me-1"></span>
                            {% endif %}
                            {% if post.status == 'draft' %}
                                <span class="badge bg-secondary me-1"></span>
                            {% endif %}
                            {{ mf.status_name(post.status) }}</td>
                        <td class="text-center">{{ post.views }}</td>
                        <td>
                            <a class="btn btn-icon" href="{{ 'support/kb/article'|alink }}/{{ post.id }}">
                                <svg class="icon">
                                    <use xlink:href="#edit" />
                                </svg>
                            </a>
                            <a class="btn btn-icon api-link" data-api-confirm="{{ 'Are you sure?'|trans }}" data-api-redirect="{{'support/kb'|alink}}" href="{{ 'api/admin/support/kb_article_delete'|link({ 'id': post.id, 'CSRFToken': CSRFToken }) }}">
                                <svg class="icon">
                                    <use xlink:href="#delete" />
                                </svg>
                            </a>
                        </td>
                    </tr>
                {% else %}
                    <tr>
                        <td class="text-muted" colspan="4">{{ 'The list is empty'|trans }}</td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>

            {{ include('partial_pagination.html.twig', { 'list': posts, 'url': 'support/kb' }) }}
        </div>

        <div class="tab-pane fade" id="tab-new-article" role="tabpanel">
            <div class="card-body">
                <form method="post" action="{{ 'api/admin/support/kb_article_create'|link }}" class="api-form" data-api-reload="1">
                    <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('kb_article_category_id', admin.support_kb_category_get_pairs, request.kb_article_category_id, 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="required" placeholder=""/>
                        </div>
                    </div>
                    <div class="mb-3 row">
                        <label class="form-label col-3 col-form-label">{{ 'Status'|trans }}:</label>
                        <div class="col">
                            <div class="form-check form-check-inline">
                                <input class="form-check-input" id="radioStatusDraft" type="radio" name="status" value="draft" checked>
                                <label class="form-check-label" for="radioStatusDraft">{{ 'Draft'|trans }}</label>
                            </div>
                            <div class="form-check form-check-inline">
                                <input class="form-check-input" id="radioStatusActive" type="radio" name="status" value="active">
                                <label class="form-check-label" for="radioStatusActive">{{ 'Active'|trans }}</label>
                            </div>
                        </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 bb-textarea" 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">
                <tbody>
                    {% for cat_id, cat_title in admin.support_kb_category_get_pairs %}
                    <tr>
                        <td>
                            <a href="{{ 'support/kb/category'|alink }}/{{ cat_id }}">{{ cat_title }}</a>
                        </td>
                        <td class="w-1">
                            <a class="btn btn-icon" href="{{ 'support/kb/category'|alink }}/{{ cat_id }}">
                                <svg class="icon">
                                    <use xlink:href="#edit" />
                                </svg>
                            </a>
                            <a class="btn btn-icon api-link"
                                href="{{ 'api/admin/support/kb_category_delete'|link({ 'id': cat_id, 'CSRFToken': CSRFToken }) }}"
                                data-api-confirm="{{ 'Are you sure?'|trans }}"
                                data-api-redirect="{{ 'support/kb'|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/kb_category_create'|link }}" class="api-form" data-api-reload="{{ 'Category created'|trans }}">
                    <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" required>
                        </div>
                    </div>
                    <div class="mb-3 row">
                        <label class="form-label col-3 col-form-label">{{ 'Description'|trans }}:</label>
                        <div class="col">
                            <textarea class="form-control" name="description" rows="20"></textarea>
                        </div>
                    </div>

                    <button class="btn btn-primary w-100" type="submit">{{ 'Create'|trans }}</button>
                </form>
            </div>
        </div>
    </div>
</div>
{{ mf.bb_editor('bb-textarea') }}
{% endblock %}
