{% extends 'layout_default.html.twig' %}

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

{% set active_menu = 'products' %}

{% block meta_title %}{{ 'Products'|trans }}{% endblock %}

{% block content %}
    <ul class="nav nav-tabs" role="tablist">
        <li class="nav-item" role="presentation">
            <button class="nav-link active" id="index-tab" data-bs-toggle="tab" data-bs-target="#tab-index" type="button" role="tab" aria-controls="index" aria-selected="true">{{ 'Products'|trans }}</button>
        </li>
        <li class="nav-item" role="presentation">
            <button class="nav-link" id="new-tab" data-bs-toggle="tab" data-bs-target="#tab-new" type="button" role="tab" aria-controls="index" aria-selected="true">
                <svg class="icon me-2">
                    <use xlink:href="#plus" />
                </svg>
                {{ 'New Product'|trans }}
            </button>
        </li>
        <li class="nav-item" role="presentation">
            <button class="nav-link" id="categories-tab" data-bs-toggle="tab" data-bs-target="#tab-categories" type="button" role="tab" aria-controls="index" aria-selected="true">{{ 'Categories'|trans }}</button>
        </li>
        <li class="nav-item" role="presentation">
            <button class="nav-link" id="new-category-tab" data-bs-toggle="tab" data-bs-target="#tab-new-category" type="button" role="tab" aria-controls="index" aria-selected="true">
                <svg class="icon me-2">
                    <use xlink:href="#plus" />
                </svg>
                {{ 'New Category'|trans }}
            </button>
        </li>
    </ul>

<div class="card">
    <div class="tab-content">
        <div class="tab-pane fade show active" id="tab-index" role="tabpanel" aria-labelledby="index-tab">
            <div class="card-header">
                <h3 class="card-title">{{ 'Products'|trans }}</h3>
                <div class="card-actions">
                    {{ include('partial_search_action.html.twig') }}
                </div>
            </div>
            <div class="table-responsive">
                <form method="post" action="{{ 'api/admin/product/update_priority'|link }}" class="api-form" data-api-reload="1">
                    <input type="hidden" name="CSRFToken" value="{{ CSRFToken }}"/>
                    <table class="table card-table table-vcenter table-striped text-nowrap sortable">
                        <thead>
                            <tr>
                                <th>{{ '#'|trans }}</th>
                                <th>{{ 'Type'|trans }}</th>
                                <th>{{ 'Category'|trans }}</th>
                                <th>{{ 'Title'|trans }}</th>
                                <th>{{ 'Status'|trans }}</th>
                                <th class="w-1 text-center">{{ 'Priority'|trans }}</th>
                                <th class="w-1 no-sort"></th>
                            </tr>
                        </thead>
                        <tbody>
                            {% set products = admin.product_get_list({ 'per_page': 30, 'page': request.page }|merge(request)) %}
                            {% for product in products.list %}
                            <tr>
                                <td>{{ product.id }}</td>
                                <td>{{ mf.status_name(product.type) }}</td>
                                <td>
                                    <a href="{{ '/product/category'|alink }}/{{ product.category.id }}">{{ product.category.title }}</a>
                                </td>
                                <td><a href="{{ '/product/manage'|alink }}/{{ product.id }}">{{ product.title }}</a></td>
                                <td>
                                    {% if product.status == 'enabled' %}
                                        <span class="badge bg-success me-1"></span>
                                    {% endif %}
                                    {% if product.status == 'disabled' %}
                                        <span class="badge bg-secondary me-1"></span>
                                    {% endif %}
                                    {{ mf.status_name(product.status) }}</td>
                                <td class="text-center">
                                    <input type="text" class="form-control form-control-sm" name="priority[{{ product.id }}]" value="{{ product.priority }}" size="2">
                                </td>
                                <td>
                                    <a class="btn btn-icon" href="{{ '/product/manage'|alink }}/{{ product.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/product/delete'|link({'id' : product.id, 'CSRFToken': CSRFToken}) }}" data-api-redirect="{{ 'product'|alink }}">
                                        <svg class="icon">
                                            <use xlink:href="#delete" />
                                        </svg>
                                    </a>
                                </td>
                            </tr>
                            {% else %}
                            <tr>
                                <td class="text-muted" colspan="6">{{ 'The list is empty'|trans }}</td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>

                    {{ include('partial_pagination.html.twig', { 'list': products, 'url': 'product' }) }}

                    <div class="card-footer text-end">
                        <button type="submit" class="btn btn-primary">
                            <svg class="icon">
                                <use xlink:href="#refresh" />
                            </svg>
                            {{ 'Update priority'|trans }}
                        </button>
                    </div>
                </form>
            </div>
        </div>

        <div class="tab-pane fade" id="tab-new" role="tabpanel" aria-labelledby="new-tab">
            <div class="card-body">
                <form method="post" action="{{ 'api/admin/product/prepare'|link }}" class="api-form" data-api-reload="1">
                    <input type="hidden" name="CSRFToken" value="{{ CSRFToken }}"/>
                    <div class="form-group mb-3 row">
                        <label class="form-label col-3 col-form-label">{{ 'Type'|trans }}:</label>
                        <div class="col">
                            {{ mf.selectbox('type', admin.product_get_types, request.type, 1) }}
                        </div>
                    </div>
                    <div class="form-group mb-3 row">
                        <label class="form-label col-3 col-form-label">{{ 'Category'|trans }}:</label>
                        <div class="col">
                            {{ mf.selectbox('product_category_id', admin.product_category_get_pairs, request.product_category_id, 1) }}
                        </div>
                    </div>
                    <div class="form-group mb-3 row">
                        <label class="form-label col-3 col-form-label">{{ 'Title'|trans }}:</label>
                        <div class="col">
                            <input type="text" class="form-control" name="title" value="{{ request.title }}" required>
                        </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" aria-labelledby="categories-tab">
            <table class="table card-table table-vcenter table-striped text-nowrap">
                <thead>
                    <tr>
                        <th class="w-1">#</th>
                        <th>{{ 'Title'|trans }}</th>
                        <th class="w-1"></th>
                    </tr>
                </thead>
                <tbody>
                    {% for cat_id, cat_title in admin.product_category_get_pairs %}
                    <tr>
                        <td>{{ loop.index }}</td>
                        <td>
                            <a href="{{ '/product/category'|alink }}/{{ cat_id }}">{{ cat_title }}</a>
                        </td>
                        <td>
                            <a class="btn btn-icon" href="{{ '/product/category'|alink }}/{{ cat_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/product/category_delete'|link({ 'id': cat_id, 'CSRFToken': CSRFToken }) }}" data-api-redirect="{{ 'product'|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" aria-labelledby="new-category-tab">
            <div class="card-body">
                <form method="post" action="{{ 'api/admin/product/category_create'|link }}" class="save api-form" data-api-redirect="{{ 'product'|alink }}">
                    <input type="hidden" name="CSRFToken" value="{{ CSRFToken }}"/>
                    <div class="form-group mb-3 row">
                        <label class="form-label col-3 col-form-label">{{ 'Title'|trans }}:</label>
                        <div class="col">
                            <input type="text" class="form-control" name="title" value="{{ request.cat_title }}" required>
                        </div>
                    </div>

                    <div class="form-group mb-3 row">
                        <label class="form-label col-3 col-form-label">{{ 'Icon URL'|trans }}:</label>
                        <div class="col">
                            <input type="text" class="form-control" name="icon_url" value="">
                        </div>
                    </div>

                    <div class="form-group 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="5">{{ request.cat_description }}</textarea>
                        </div>
                    </div>

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