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

{% set active_menu = 'products' %}

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

{% block breadcrumbs %}
    <ol class="breadcrumb">
        <li class="breadcrumb-item">
            <a href="{{ '/'|alink }}">
                <svg class="icon">
                    <use xlink:href="#home" />
                </svg>
            </a>
        </li>
        <li class="breadcrumb-item">
            <a href="{{ 'product'|alink }}">{{ 'Products'|trans }}</a>
        </li>
        <li class="breadcrumb-item active">{{ 'Addons'|trans }}</li>
    </ol>
{% 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">{{ 'Addons'|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">
                    <use xlink:href="#plus" />
                </svg>
                <span class="ms-2">{{ 'New Addon'|trans }}</span>
            </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">
            <table class="table card-table table-vcenter table-striped text-nowrap">
                <thead>
                    <tr>
                        <th class="w-1">#</th>
                        <th>{{ 'Title'|trans }}</th>
                        <th>{{ 'Description'|trans }}</th>
                        <th>{{ 'Status'|trans }}</th>
                        <th></th>
                    </tr>
                </thead>
                <tbody>
                    {% for addon_id, addon_title in admin.product_addon_get_pairs %}
                    <tr>
                        <td>{{ loop.index }}</td>
                        <td>
                            <a href="{{ '/product/addon'|alink }}/{{ addon_id }}">{{ addon_title }}</a>
                        </td>
                        <td><!-- TODO: show addon description --></td>
                        <td><!-- TODO: show addon status --></td>
                        <td class="w-1">
                            <a class="btn btn-icon" href="{{ '/product/addon'|alink }}/{{ addon_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="{{ 'product/addons'|alink }}" href="{{ 'api/admin/product/delete'|link({ 'id': addon_id, 'CSRFToken': CSRFToken }) }}">
                                <svg class="icon">
                                    <use xlink:href="#delete" />
                                </svg>
                            </a>
                        </td>
                    </tr>
                    {% else %}
                    <tr>
                        <td class="text-muted" colspan="2">{{ 'The list is empty'|trans }}</td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </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/addon_create'|link }}" class="api-form" data-api-jsonp="onAfterAddonCreate">
                    <input type="hidden" name="CSRFToken" value="{{ CSRFToken }}"/>
                    <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" type="radio" id="statusEnabled" name="status" value="enabled">
                                <label class="form-check-label" for="statusEnabled">{{ 'Enabled'|trans }}</label>
                            </div>
                            <div class="form-check form-check-inline">
                                <input class="form-check-input" type="radio" id="statusDisabled" name="status" value="disabled" checked>
                                <label class="form-check-label" for="statusDisabled">{{ 'Disabled'|trans }}</label>
                            </div>
                        </div>
                    </div>
                    <div class="mb-3 row">
                        <label class="form-label col-3 col-form-label">{{ 'Activation'|trans }}:</label>
                        <div class="col">
                            <div class="form-check form-check-inline">
                                <input class="form-check-input" type="radio" id="setupAfterOrder" name="setup" value="after_order">
                                <label class="form-check-label" for="setupAfterOrder">{{ 'After order is placed'|trans }}</label>
                            </div>
                            <div class="form-check form-check-inline">
                                <input class="form-check-input" type="radio" id="setupAfterPayment" name="setup" value="after_payment" checked>
                                <label class="form-check-label" for="setupAfterPayment">{{ 'After payment is received'|trans }}</label>
                            </div>
                            <div class="form-check form-check-inline">
                                <input class="form-check-input" type="radio" id="setupManual" name="setup" value="manual">
                                <label class="form-check-label" for="setupManual">{{ 'Manual activation'|trans }}</label>
                            </div>
                        </div>
                    </div>
                    <div class="mb-3 row">
                        <label class="form-label col-3 col-form-label">{{ 'Icon'|trans }}:</label>
                        <div class="col">
                            <input class="form-control" type="text" name="icon_url" value="{{ product.icon_url }}">
                        </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="{{ product.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="5">{{ product.description }}</textarea>
                        </div>
                    </div>

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

{% block js %}
<script>
    function onAfterAddonCreate(result) {
        bb.redirect("{{ 'product/addon/'|alink }}/" + result);
    }
</script>
{% endblock %}
