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

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

{% block meta_title %}{{ 'Product Promotions'|trans }}{% endblock %}

{% set active_menu = 'products' %}

{% 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" aria-current="page">{{ 'Product Promotions'|trans }}</li>
    </ol>
{% endblock %}

{% block content %}
    <ul class="nav nav-tabs" role="tablist">
        <li class="nav-item" role="presentation">
            <a class="nav-link active" href="#tab-promos" data-bs-toggle="tab">{{ 'Promotions'|trans }}</a>
        </li>
        <li class="nav-item" role="presentation">
            <a class="nav-link" href="#tab-new" data-bs-toggle="tab">
                <svg class="icon me-2">
                    <use xlink:href="#plus" />
                </svg>
                {{ 'New Promotion'|trans }}
            </a>
        </li>
    </ul>

    <div class="card">
        <div class="tab-content">
            <div class="tab-pane fade show active" id="tab-promos" role="tabpanel">
                <div class="card-header">
                    <h3 class="card-title">{{ 'Promo codes'|trans }}</h3>
                    <div class="card-actions">
                        {{ include('partial_search_action.html.twig') }}
                    </div>
                </div>
                <table class="table card-table table-vcenter table-striped text-nowrap">
                    <thead>
                        <tr>
                            <th>{{ 'Code'|trans }}</th>
                            <th>{{ 'Discount'|trans }}</th>
                            <th>{{ 'Applies To'|trans }}</th>
                            <th>{{ 'Client Groups'|trans }}</th>
                            <th>{{ 'Validity Period'|trans }}</th>
                            <th>{{ 'Enabled'|trans }}</th>
                            <th>{{ 'Usage'|trans }}</th>
                            <th class="w-1"></th>
                        </tr>
                    </thead>
                    <tbody>
                    {% set promos = admin.product_promo_get_list({ 'per_page': 30, 'page': request.page }|merge(request)) %}
                    {% for promo in promos.list %}
                    <tr>
                        <td>
                            <a href="{{ '/product/promo'|alink }}/{{ promo.id }}">{{ promo.code }}</a>
                        </td>
                        <td>{% if promo.type == 'percentage' %}{{ promo.value }}%{% endif %}{% if promo.type == 'absolute' %}{{ mf.currency_format(promo.value) }}{% endif %}</td>
                        <td>{% for pid,product in promo.applies_to %}
                            <a href="{{ 'product/manage'|alink }}/{{ pid }}">{{ product|truncate(15) }}</a><br/>
                            {% else %}
                            {{ 'All Products'|trans }}
                            {% endfor %}
                        </td>
                        <td>{% for cid,client_group in promo.cgroups %}
                            <a href="{{ 'client/group'|alink }}/{{ cid }}">{{ client_group|truncate(15) }}</a><br/>
                            {% else %}
                            {{ 'All Client groups'|trans }}
                            {% endfor %}
                        </td>
                        <td>
                            {# We need variable support to effectively localize these. Not every language is in "From *x* until *y*" format. #}
                            From {% if promo.start_at %}{{ promo.start_at|format_date }}{% else %}now{% endif %}
                            until {% if promo.end_at %}{{ promo.end_at|format_date }}{% else %}disabled{% endif %}
                        </td>
                        <td>
                            {% if promo.active == true %}
                                <span class="badge bg-success me-1"></span>
                            {% endif %}
                            {% if promo.active == false %}
                                <span class="badge bg-secondary me-1"></span>
                            {% endif %}
                            {{ mf.q(promo.active) }}
                        </td>
                        <td>{{ promo.used }} / {% if promo.maxuses %}{{ promo.maxuses }}{% else %}&#8734;{% endif %}</td>
                        <td>
                            <a class="btn btn-icon" href="{{ '/product/promo'|alink }}/{{ promo.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/promo_delete'|link({ 'id': promo.id }) }}" data-api-redirect="{{ 'product/promos'|alink }}">
                                <svg class="icon">
                                    <use xlink:href="#delete" />
                                </svg>
                            </a>
                        </td>
                    </tr>
                    {% else %}
                        <tr>
                            <td class="text-muted" colspan="7">{{ 'The list is empty'|trans }}</td>
                        </tr>
                    {% endfor %}
                    </tbody>
                </table>

                {{ include('partial_pagination.html.twig', { 'list': promos, 'url': 'product/promos' }) }}
            </div>

            <div class="tab-pane fade" id="tab-new" role="tabpanel">
                <div class="card-body">
                    <h3>{{ 'Create New Promotional Code'|trans }}</h3>
                    <p class="text-muted">{{ 'Create special offers for your clients by creating promo codes.'|trans }}</p>
                    <form method="post" action="{{ 'api/admin/product/promo_create'|link }}" class="api-form" data-api-redirect="{{ 'product/promos'|alink }}">
                        <input type="hidden" name="CSRFToken" value="{{ CSRFToken }}"/>
                        <div class="mb-3 row">
                            <label class="form-label col-3 col-form-label">{{ 'Promo Code'|trans }}</label>
                            <div class="col">
                                <input class="form-control" type="text" name="code" value="" required>
                            </div>
                        </div>
                        <div class="mb-3 row">
                            <label class="form-label col-3 col-form-label">{{ 'Discount Type'|trans }}</label>
                            <div class="col">
                                <div class="form-check form-check-inline">
                                    <input class="form-check-input" id="radioTypeAbsolute" type="radio" name="type" value="absolute" checked>
                                    <label class="form-check-label" for="radioTypeAbsolute">{{ 'Absolute'|trans }}</label>
                                </div>
                                <div class="form-check form-check-inline">
                                    <input class="form-check-input" id="radioTypePercentage" type="radio" name="type" value="percentage">
                                    <label class="form-check-label" for="radioTypePercentage">{{ 'Percentage'|trans }}</label>
                                </div>
                                <input class="form-control" type="text" name="value" required>
                            </div>
                        </div>
                        <div class="mb-3 row">
                            <label class="form-label col-3 col-form-label">{{ 'Recurring'|trans }}</label>
                            <div class="col">
                                <div class="form-check form-check-inline">
                                    <input class="form-check-input" id="radioApplyFirst" type="radio" name="recurring" value="0">
                                    <label class="form-check-label" for="radioApplyFirst">{{ 'Apply to first order only'|trans }}</label>
                                </div>
                                <div class="form-check form-check-inline">
                                    <input class="form-check-input" id="radioApplyAll" type="radio" name="recurring" value="1" checked>
                                    <label class="form-check-label" for="radioApplyAll">{{ 'Apply to first order and renewals'|trans }}</label>
                                </div>
                            </div>
                        </div>
                        <div class="mb-3 row">
                            <label class="form-label col-3 col-form-label">{{ 'Active'|trans }}</label>
                            <div class="col">
                                <div class="form-check form-check-inline">
                                    <input class="form-check-input" id="radioActiveYes" type="radio" name="active" value="1" checked>
                                    <label class="form-check-label" for="radioActiveYes">{{ 'Yes'|trans }}</label>
                                </div>
                                <div class="form-check form-check-inline">
                                    <input class="form-check-input" id="radioActiveNo" type="radio" name="active" value="0">
                                    <label class="form-check-label" for="radioActiveNo">{{ 'No'|trans }}</label>
                                </div>
                            </div>
                        </div>
                        <div class="mb-3 row">
                            <label class="form-label col-3 col-form-label">{{ 'Free Setup'|trans }}</label>
                            <div class="col">
                                <div class="form-check form-check-inline">
                                    <input class="form-check-input" id="radioFreeSetupYes" type="radio" name="freesetup" value="1">
                                    <label class="form-check-label" for="radioFreeSetupYes">{{ 'Yes'|trans }}</label>
                                </div>
                                <div class="form-check form-check-inline">
                                    <input class="form-check-input" id="radioFreeSetupNo" type="radio" name="freesetup" value="0" checked>
                                    <label class="form-check-label" for="radioFreeSetupNo">{{ 'No'|trans }}</label>
                                </div>
                            </div>
                        </div>

                        <h5>{{ 'Promotion Limitations'|trans }}</h5>
                        <div class="mb-3 row">
                            <label class="form-label col-3 col-form-label">{{ 'Once per Client'|trans }}</label>
                            <div class="col">
                                <div class="form-check form-check-inline">
                                    <input class="form-check-input" id="radioOncePerClientYes" type="radio" name="once_per_client" value="1" checked>
                                    <label class="form-check-label" for="radioOncePerClientYes">{{ 'Yes'|trans }}</label>
                                </div>
                                <div class="form-check form-check-inline">
                                    <input class="form-check-input" id="radioOncePerClientNo" type="radio" name="once_per_client" value="0">
                                    <label class="form-check-label" for="radioOncePerClientNo">{{ 'No'|trans }}</label>
                                </div>
                            </div>
                        </div>

                        <div class="mb-3 row">
                            <label class="form-label col-3 col-form-label">{{ 'Max Uses (Zero for Unlimited)'|trans }}</label>
                            <div class="col">
                                <input class="form-control" type="text" name="maxuses" value="" class="dirRight" title="{{ 'Leave blank for unlimited uses'|trans }}" placeholder="0">
                            </div>
                        </div>
                        <div class="mb-3 row">
                            <label class="form-label col-3 col-form-label" for="valid_time">{{ 'Validity Period (Blank for Indefinite)'|trans }}</label>
                            <div class="col">
                                <div class="input-group">
                                    <div class="input-icon w-100">
                                        <input class="form-control datepicker"
                                               id="valid_time"
                                               data-name-from="start_at"
                                               data-name-to="end_at">
                                        <span class="input-icon-addon">
                                            <svg xmlns="http://www.w3.org/2000/svg" class="icon" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
                                                <path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
                                                <path d="M4 5m0 2a2 2 0 0 1 2 -2h12a2 2 0 0 1 2 2v12a2 2 0 0 1 -2 2h-12a2 2 0 0 1 -2 -2z"></path>
                                                <path d="M16 3l0 4"></path>
                                                <path d="M8 3l0 4"></path>
                                                <path d="M4 11l16 0"></path>
                                                <path d="M11 15l1 0"></path>
                                                <path d="M12 15l0 3"></path>
                                            </svg>
                                        </span>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="mb-3 row">
                            <label class="form-label col-3 col-form-label">{{ 'Applicable Products (Select None to Apply to All)'|trans }}</label>
                            <div class="col">
                                <table class="table card-table table-vcenter table-striped text-nowrap">
                                    <tbody>
                                        {% set products = admin.product_get_pairs %}
                                        {% for id,product in products %}
                                        <tr><td class="w-1"><input type="checkbox" name="products[]" value="{{ id }}" /></td><td>{{ product }}</td></tr>
                                        {% endfor %}
                                    </tbody>
                                </table>
                            </div>
                        </div>
                        <div class="mb-3 row">
                            <label class="form-label col-3 col-form-label">{{ 'Applicable Periods (Select None to Apply to All)'|trans }}</label>
                            <div class="col">
                                <table class="table card-table table-vcenter table-striped text-nowrap">
                                    <tbody>
                                        {% set periods = guest.system_periods %}
                                        {% for id, period in periods %}
                                        <tr><td class="w-1"><input type="checkbox" name="periods[]" value="{{ id }}" /></td><td>{{ period }}</td></tr>
                                        {% endfor %}
                                    </tbody>
                                </table>
                            </div>
                        </div>
                        <div class="mb-3 row">
                            <label class="form-label col-3 col-form-label">{{ 'Applicable Client Groups (Select None to Apply to All)'|trans }}</label>
                            <div class="col">
                                <table class="table card-table table-vcenter table-striped text-nowrap">
                                    <tbody>
                                        {% set client_groups = admin.client_group_get_pairs %}
                                        {% for id, client_group in client_groups %}
                                        <tr><td class="w-1"><input type="checkbox" name="client_groups[]" value="{{ id }}" /></td><td>{{ client_group }}</td></tr>
                                        {% endfor %}
                                    </tbody>
                                </table>
                            </div>
                        </div>
                        <input type="submit" value="{{ 'Create'|trans }}" class="btn btn-primary w-100">
                    </form>
                </div>
            </div>
        </div>
    </div>
{% endblock %}
