{% extends request.ajax ? "layout_blank.phtml" : "layout_default.phtml" %}

{% import "macro_functions.phtml" as mf %}
{% block meta_title %}{{ 'Knowledge Base'|trans }}{% endblock %}
{% set active_menu = 'kb' %}

{% block content %}
<div class="widget simpleTabs">
    <ul class="tabs">
        <li><a href="#tab-index">{{ 'Articles'|trans }}</a></li>
        <li><a href="#tab-new-article">{{ 'New article'|trans }}</a></li>
        <li><a href="#tab-new-category">{{ 'New category'|trans }}</a></li>
        <li><a href="#tab-categories">{{ 'Manage categories'|trans }}</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>{{ 'Title'|trans }}</td>
                    <td>{{ 'Category'|trans }}</td>
                    <td>{{ 'Status'|trans }}</td>
                    <td>{{ 'Views'|trans }}</td>
                    <td>&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 %}
            <tr>
                <td>{{ post.title }}</td>
                <td><a href="{{ '/kb/category'|alink }}/{{post.category.id}}">{{post.category.title}}</a></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'|alink}}" href="{{ 'api/admin/kb/article_delete'|link({'id' : post.id}) }}"><img src="images/icons/dark/trash.png" alt=""></a>
                </td>
            </tr>
            {% else %}
                <tr>
                    <td colspan="4">
                        {{ 'The list is empty'|trans }}
                    </td>
                </tr>
            
            {% endfor %}
            </tbody>
        </table>
        
        {% include "partial_pagination.phtml" with {'list': posts, 'url':'kb'} %}
        </div>


        <div class="tab_content nopadding" id="tab-new-article">
            <form method="post" action="{{ 'api/admin/kb/article_create'|link }}" class="mainForm api-form" data-api-reload="1">
                <fieldset>
                    <div class="rowElem noborder">
                        <label>{{ 'Category'|trans }}:</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>{{ 'Title'|trans }}:</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>{{ 'Status'|trans }}:</label>
                        <div class="formRight">
                            <input type="radio" name="status" value="draft" checked="checked"/><label>{{ 'Draft'|trans }}</label>
                            <input type="radio" name="status" value="active"/><label>{{ 'Active'|trans }}</label>
                        </div>
                        <div class="fix"></div>
                    </div>

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

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

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

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

                    <input type="submit" value="{{ 'Create'|trans }}" 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="{{ 'api/admin/kb/category_delete'|link({'id' : cat_id}) }}" data-api-redirect="{{'kb/new'|alink}}"><img src="images/icons/dark/trash.png" alt=""></a>
                        </td>
                    </tr>
                    {% else %}
                    <tr>
                        <td colspan="3">{{ 'The list is empty'|trans }}</td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
    <div class="fix"></div>
</div>
{% endblock %}
