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

{% block meta_title %}{{ 'Product Promotions'|trans }} - {{ promo.code }}{% endblock %}

{% set active_menu = 'products' %}

{% block breadcrumbs %}
    <ol class="breadcrumb">
        <li class="breadcrumb-item">
            <a href="{{ '/'|url }}">
                <svg class="icon">
                    <use xlink:href="#home" />
                </svg>
            </a>
        </li>
        <li class="breadcrumb-item">
            <a href="{{ 'product'|url }}">{{ 'Products'|trans }}</a>
        </li>
        <li class="breadcrumb-item">
            <a href="{{ 'product/promos'|url }}">{{ 'Product Promotions'|trans }}</a>
        </li>
        <li class="breadcrumb-item active" aria-current="page">{{ promo.code }}</li>
    </ol>
{% endblock %}

{% block content %}
<div class="card">
    <form method="post" action="{{ 'product/promo_update'|api_url }}" {{ fb_api_form({redirect: 'product/promos'|url}) }}>
        {% embed 'partial_admin_form_card.html.twig' with {
            title: 'Manage Promotion'|trans,
            subtitle: 'Update the offer, recurring behavior, eligibility rules, and applicability of this promotion.'|trans,
            body_class: 'pt-4'
        } %}
            {% block form_content %}
                <div class="row g-3">
                    <div class="col-lg-6">
                        <label class="form-label">{{ 'Promo Code'|trans }}</label>
                        <input class="form-control" type="text" name="code" value="{{ promo.code }}" required>
                    </div>
                    <div class="col-lg-3">
                        <label class="form-label d-block">{{ 'Discount Type'|trans }}</label>
                        <div class="d-flex flex-wrap gap-3">
                            <label class="form-check"><input class="form-check-input" id="radioTypeAbsolute" type="radio" name="type" value="absolute"{% if promo.type == 'absolute' %} checked{% endif %}><span class="form-check-label">{{ 'Absolute'|trans }}</span></label>
                            <label class="form-check"><input class="form-check-input" id="radioTypePercentage" type="radio" name="type" value="percentage"{% if promo.type == 'percentage' %} checked{% endif %}><span class="form-check-label">{{ 'Percentage'|trans }}</span></label>
                        </div>
                    </div>
                    <div class="col-lg-3">
                        <label class="form-label">{{ 'Discount Value'|trans }}</label>
                        <input class="form-control" type="text" name="value" value="{{ promo.value }}" required>
                    </div>
                    <div class="col-lg-6">
                        <label class="form-label d-block">{{ 'Recurring'|trans }}</label>
                        <div class="d-flex flex-wrap gap-3">
                            <label class="form-check"><input class="form-check-input" id="radioApplyAll" type="radio" name="recurring" value="0"{% if not promo.recurring %} checked{% endif %}><span class="form-check-label">{{ 'Apply to First Order Only'|trans }}</span></label>
                            <label class="form-check"><input class="form-check-input" id="radioApplyFirst" type="radio" name="recurring" value="1"{% if promo.recurring %} checked{% endif %}><span class="form-check-label">{{ 'Apply to First Order and Renewals'|trans }}</span></label>
                        </div>
                    </div>
                    <div class="col-lg-3">
                        <label class="form-label d-block">{{ 'Active'|trans }}</label>
                        <div class="d-flex flex-wrap gap-3">
                            <label class="form-check"><input class="form-check-input" id="radioActiveYes" type="radio" name="active" value="1"{% if promo.active %} checked{% endif %}><span class="form-check-label">{{ 'Yes'|trans }}</span></label>
                            <label class="form-check"><input class="form-check-input" id="radioActiveNo" type="radio" name="active" value="0"{% if not promo.active %} checked{% endif %}><span class="form-check-label">{{ 'No'|trans }}</span></label>
                        </div>
                    </div>
                    <div class="col-lg-3">
                        <label class="form-label d-block">{{ 'Free Setup'|trans }}</label>
                        <div class="d-flex flex-wrap gap-3">
                            <label class="form-check"><input class="form-check-input" id="radioFreeSetupYes" type="radio" name="freesetup" value="1"{% if promo.freesetup %} checked{% endif %}><span class="form-check-label">{{ 'Yes'|trans }}</span></label>
                            <label class="form-check"><input class="form-check-input" id="radioFreeSetupNo" type="radio" name="freesetup" value="0"{% if not promo.freesetup %} checked{% endif %}><span class="form-check-label">{{ 'No'|trans }}</span></label>
                        </div>
                    </div>
                </div>

                <hr class="my-4">
                <h6 class="card-title text-secondary mb-3">{{ 'Promotion Limitations'|trans }}</h6>
                <div class="row g-3">
                    <div class="col-lg-4">
                        <label class="form-label d-block">{{ 'Once per Client'|trans }}</label>
                        <div class="d-flex flex-wrap gap-3">
                            <label class="form-check"><input class="form-check-input" id="radioOncePerClientYes" type="radio" name="once_per_client" value="1"{% if promo.once_per_client %} checked{% endif %}><span class="form-check-label">{{ 'Yes'|trans }}</span></label>
                            <label class="form-check"><input class="form-check-input" id="radioOncePerClientNo" type="radio" name="once_per_client" value="0"{% if not promo.once_per_client %} checked{% endif %}><span class="form-check-label">{{ 'No'|trans }}</span></label>
                        </div>
                    </div>
                    <div class="col-lg-4">
                        <label class="form-label">{{ 'Max Uses (Zero for Unlimited)'|trans }}</label>
                        <input class="form-control" type="text" name="maxuses" value="{{ promo.maxuses }}" title="{{ 'Leave blank for unlimited uses'|trans }}" placeholder="0">
                    </div>
                    <div class="col-lg-4">
                        <label class="form-label" for="validity">{{ 'Validity Period (Blank for Indefinite)'|trans }}</label>
                        <div class="input-icon w-100">
                            <input class="form-control datepicker"
                                   id="validity"
                                   value="{% if promo.start_at %}{{ promo.start_at|date('Y-m-d') }}{% endif %}{% if promo.end_at %} to {{ promo.end_at|date('Y-m-d') }}{% endif %}"
                                   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>

                <hr class="my-4">
                <div class="row g-4">
                    <div class="col-lg-4">
                        <label class="form-label">{{ 'Applicable Products (Select None to Apply to All)'|trans }}</label>
                        <input type="hidden" name="products[]" value=""/>
                        <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 }}" {% if id in promo.products %} checked{% endif %} /></td><td>{{ product }}</td></tr>{% endfor %}</tbody>
                        </table>
                    </div>
                    <div class="col-lg-4">
                        <label class="form-label">{{ 'Applicable Periods (Select None to Apply to All)'|trans }}</label>
                        <input type="hidden" name="periods[]" value=""/>
                        <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 }}" {% if id in promo.periods %} checked{% endif %} /></td><td>{{ period }}</td></tr>{% endfor %}</tbody>
                        </table>
                    </div>
                    <div class="col-lg-4">
                        <label class="form-label">{{ 'Applicable Client Groups (Select None to Apply to All)'|trans }}</label>
                        <input type="hidden" name="client_groups[]" value=""/>
                        <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 }}" {% if id in promo.client_groups %} checked{% endif %} /></td><td>{{ client_group }}</td></tr>{% endfor %}</tbody>
                        </table>
                    </div>
                </div>
                <input type="hidden" name="id" value="{{ promo.id }}">
            {% endblock %}

            {% block form_actions %}
                <button class="btn btn-primary" type="submit">{{ 'Update'|trans }}</button>
            {% endblock %}
        {% endembed %}
    </form>
</div>
{% endblock %}
