{% extends request.ajax ? "layout_blank.phtml" : "layout_default.phtml" %}
{% import "macro_functions.phtml" as mf %}
{% block meta_title %}Knowledge base{% endblock %}
{% set active_menu = 'support' %}
{% block content %}

<div class="widget simpleTabs nomargin">

    <ul class="tabs">
        <li><a href="#tab-index">{% trans 'Articles' %}</a></li>
        <li><a href="#tab-new-article">{% trans 'New article' %}</a></li>
        <li><a href="#tab-new-category">{% trans 'New category' %}</a></li>
        <li><a href="#tab-categories">{% trans 'Manage categories' %}</a></li>
    </ul>

    <div class="tabs_container">

        <div class="fix"></div>
        <div class="tab_content nopadding" id="tab-index">

        {{ mf.table_search }}
        <table class="tableStatic wide">
            <thead>
                <tr>
                    <td>{% trans 'Title' %}</td>
                    <td style="width: 10%">{% trans 'Status' %}</td>
                    <td style="width: 10%">{% trans 'Views' %}</td>
                    <td style="width: 13%">&nbsp;</td>
                </tr>
            </thead>

            <tbody>
            {% set posts = admin.kb_article_get_list({"per_page":30, "page":request.page}|merge(request)) %}
            {% for i, post in posts.list %}

            {% if post.category.id != group %}
            <tr class="group">
                <th colspan="4"><a href="{{ '/kb/category'|alink }}/{{post.category.id}}">{{post.category.title}}</a></th>
            </tr>
            {% set group = post.category.id %}
            {% endif %}

            <tr>
                <td>{{ post.title }}</td>
                <td>{{ mf.status_name(post.status) }}</td>
                <td>{{ post.views }}</td>
                <td class="actions">
                    <a class="bb-button btn14" href="{{ '/kb/article'|alink }}/{{post.id}}"><img src="images/icons/dark/pencil.png" alt=""></a>
                    <a class="bb-button btn14 bb-rm-tr api-link" data-api-confirm="Are you sure?" data-api-redirect="kb" href="admin/kb/article_delete?id={{post.id}}"><img src="images/icons/dark/trash.png" alt=""></a>
                </td>
            </tr>
            </tbody>

            {% else %}
            <tbody>
                <tr>
                    <td colspan="4">
                        {% trans 'The list is empty' %}
                    </td>
                </tr>
            </tbody>
            {% endfor %}

            {% if posts.pages > 1 %}
            <tfoot>
                <tr>
                    <td colspan="4">
                        {% include "partial_pagination.phtml" with {'list': posts, 'url':'kb'} %}
                    </td>
                </tr>
            </tfoot>
            {% endif %}

        </table>

        </div>


        <div class="tab_content nopadding" id="tab-new-article">
            <form method="post" action="admin/kb/article_create" class="mainForm api-form" data-api-reload="1">
                <fieldset>
                    <div class="rowElem noborder">
                        <label>{% trans 'Category' %}:</label>
                        <div class="formRight">
                            {{ mf.selectbox('kb_article_category_id', admin.kb_category_get_pairs, request.kb_article_category_id, 1) }}
                        </div>
                        <div class="fix"></div>
                    </div>

                    <div class="rowElem">
                        <label>{% trans 'Title' %}:</label>
                        <div class="formRight">
                            <input type="text" name="title" value="{{request.title}}" required="required" placeholder=""/>
                        </div>
                        <div class="fix"></div>
                    </div>

                    <div class="rowElem">
                        <label>{% trans 'Status' %}:</label>
                        <div class="formRight">
                            <input type="radio" name="status" value="draft" checked="checked"/><label>{% trans 'Draft' %}</label>
                            <input type="radio" name="status" value="active"/><label>{% trans 'Active' %}</label>
                        </div>
                        <div class="fix"></div>
                    </div>

                    <div class="rowElem">
                        <label>{% trans 'Content' %}</label>
                        <div class="formRight">
                            <textarea name="content" cols="5" rows="10">{{ request.content }}</textarea>
                        </div>
                        <div class="fix"></div>
                    </div>
                    <input type="submit" value="{% trans 'Create' %}" class="greyishBtn submitForm" />
                </fieldset>
            </form>
        </div>

        <div class="tab_content nopadding" id="tab-new-category">

            <form method="post" action="admin/kb/category_create" class="mainForm save api-form" data-api-reload="{% trans 'Category created' %}">
                <fieldset>
                    <div class="rowElem noborder">
                        <label>{% trans 'Title' %}:</label>
                        <div class="formRight">
                            <input type="text" name="title" value="" required="required"/>
                        </div>
                        <div class="fix"></div>
                    </div>

                    <div class="rowElem">
                        <label>{% trans 'Description' %}:</label>
                        <div class="formRight">
                            <textarea name="description" cols="5" rows="20"></textarea>
                        </div>
                        <div class="fix"></div>
                    </div>

                    <input type="submit" value="{% trans 'Create' %}" class="greyishBtn submitForm" />
                </fieldset>
            </form>
        </div>

        <div class="tab_content nopadding" id="tab-categories">
            <table class="tableStatic wide">
                <tbody>
                    {% for cat_id, cat_title in admin.kb_category_get_pairs %}
                    <tr {% if loop.first %}class="noborder"{% endif %}>
                        <td>{{cat_title}}</td>
                        <td class="actions" style="width:13%">
                            <a class="bb-button btn14" href="{{ '/kb/category'|alink }}/{{cat_id}}"><img src="images/icons/dark/pencil.png" alt=""></a>
                            <a class="bb-button btn14 bb-rm-tr api-link" data-api-confirm="Are you sure?" href="admin/kb/category_delete?id={{cat_id}}" data-api-redirect="kb/new"><img src="images/icons/dark/trash.png" alt=""></a>
                        </td>
                    </tr>
                    {% else %}
                    <tr>
                        <td colspan="3">{% trans 'The list is empty' %}</td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>

    </div>

    <div class="fix"></div>
</div>

{% endblock %}
