{% extends "layout_default.phtml" %}
{% set active_menu = 'system' %}
{% block meta_title %}Products management{% endblock %}

{% block content %}

<div class="widget simpleTabs nomargin">

    <ul class="tabs">
        <li><a href="#tab-index">{% trans 'Products' %}</a></li>
        <li><a href="#tab-new">{% trans 'New product' %}</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 }}
            <form method="post" action="admin/product/update_priority" class="mainForm api-form" data-api-reload="1">
            <fieldset>
            <table class="tableStatic wide">
                <thead>
                    <tr>
                        <td width="50%">Title</td>
                        <td>Status</td>
                        <td width="10%">Type</td>
                        <td style="width: 5%">{% trans 'Priority' %}</td>
                        <td width="13%">&nbsp;</td>
                    </tr>
                </thead>
                <tbody>
                    {% set products = admin.product_get_list({"per_page":30, "page":request.page}|merge(request)) %}
                    {% for product in products.list %}

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

                    <tr>
                        <td><a href="{{ '/product/manage'|alink }}/{{product.id}}">{{ product.title }}</a></td>
                        <td>{{ mf.status_name(product.status) }}</td>
                        <td>{{ mf.status_name(product.type) }}</td>
                        <td><input type="text" name="priority[{{product.id}}]" value="{{ product.priority }}" style="width:30px;"></td>
                        <td class="actions">
                            <a class="bb-button btn14" href="{{ '/product/manage'|alink }}/{{product.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/product/delete?id={{product.id}}" data-api-redirect="product"><img src="images/icons/dark/trash.png" alt=""></a>
                        </td>
                    </tr>
                    {% else %}
                    <tr>
                        <td colspan="5">{% trans 'The list is empty' %}</td>
                    </tr>
                    {% endfor %}
                </tbody>
                
                {% if products.pages > 1 %}
                <tfoot>
                    <tr>
                        <td colspan="5">
                            {% include "partial_pagination.phtml" with {'list': products, 'url':'product'} %}
                        </td>
                    </tr>
                </tfoot>
                {% endif %}
            </table>
                    <input type="submit" value="{% trans 'Update priority' %}" class="greyishBtn submitForm" />
                </fieldset>
            </form>
        </div>

        <div class="tab_content nopadding" id="tab-new">
            <form method="post" action="admin/product/prepare" class="mainForm" id="prepare-product">
                <fieldset>
                    <div class="rowElem noborder">
                        <label>{% trans 'Type' %}:</label>
                        <div class="formRight">
                            {{ mf.selectbox('type', admin.product_get_types, request.type, 1) }}
                        </div>
                        <div class="fix"></div>
                    </div>
                    <div class="rowElem">
                        <label>{% trans 'Category' %}:</label>
                        <div class="formRight">
                            {{ mf.selectbox('product_category_id', admin.product_category_get_pairs, request.product_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>


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

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

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

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

                    <div class="rowElem">
                        <label>{% trans 'Icon URL' %}:</label>
                        <div class="formRight">
                            <input type="text" name="icon_url" value=""/>
                        </div>
                        <div class="fix"></div>
                    </div>

                    <div class="rowElem">
                        <label>{% trans 'Description' %}:</label>
                        <div class="formRight">
                            <textarea name="description" cols="5" rows="4">{{request.cat_description}}</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.product_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="{{ '/product/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/product/category_delete?id={{cat_id}}" data-api-redirect="product"><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>

{% endblock %}

{% block js %}
<script type="text/javascript">
$(function() {

    $('#prepare-product').bind('submit', function(){
        api.post(
            $(this).attr('action'),
            $(this).serialize(),
            function(result) {
                bb.redirect('{{"product/manage/"}}' + result);
            }
        );
        return false;
    });
});
</script>
{% endblock %}