{% extends "system_settings/base_settings.html" %}

{% block title %}{% if is_edit %}Редактирование промокода{% else %}Создание промокода{% endif %}{% endblock %}

{% block settings_content %}
<div class="row justify-content-center">
    <div class="col-md-6">
        <div class="card">
            <div class="card-header d-flex justify-content-between align-items-center">
                <h4 class="mb-0">
                    {% if is_edit %}Редактирование промокода{% else %}Создание промокода{% endif %}
                </h4>
                <a href="{% url 'system_settings:discounts:promo-list' %}" class="btn btn-outline-secondary btn-sm">
                    <i class="bi bi-arrow-left"></i> Назад
                </a>
            </div>
            <div class="card-body">
                {% if messages %}
                {% for message in messages %}
                <div class="alert alert-{{ message.tags }} alert-dismissible fade show" role="alert">
                    {{ message }}
                    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                </div>
                {% endfor %}
                {% endif %}

                <form method="post">
                    {% csrf_token %}

                    <div class="mb-3">
                        <label for="id_code" class="form-label">Код промокода * <span class="text-muted small">(макс. 50 символов)</span></label>
                        <input type="text" class="form-control font-monospace" id="id_code" name="code"
                               value="{% if form.code.value %}{{ form.code.value }}{% endif %}"
                               maxlength="50" required
                               placeholder="Например: ЦВЕТЫ2025">
                        <div class="form-text">Код, который будут вводить покупатели</div>
                    </div>

                    <div class="mb-3">
                        <label for="id_discount" class="form-label">Скидка *</label>
                        <select class="form-select" id="id_discount" name="discount" required>
                            <option value="">Выберите скидку...</option>
                            {% for discount in all_discounts %}
                            <option value="{{ discount.id }}"
                                    {% if form.discount.value == discount.id %}selected{% endif %}
                                    data-type="{{ discount.discount_type }}"
                                    data-value="{{ discount.value }}">
                                {{ discount.name }}
                                ({% if discount.discount_type == 'percentage' %}{{ discount.value }}%{% else %}{{ discount.value }} руб.{% endif %})
                            </option>
                            {% endfor %}
                        </select>
                        <div class="form-text">Информация о скидке отобразится при применении промокода</div>
                    </div>

                    <div class="row mb-3">
                        <div class="col-md-6">
                            <label for="id_max_uses_per_user" class="form-label">Макс. использований на пользователя</label>
                            <input type="number" class="form-control" id="id_max_uses_per_user" name="max_uses_per_user"
                                   value="{% if form.max_uses_per_user.value %}{{ form.max_uses_per_user.value }}{% endif %}"
                                   min="1">
                            <div class="form-text">Оставьте пустым для безлимитного использования</div>
                        </div>
                        <div class="col-md-6">
                            <label for="id_max_total_uses" class="form-label">Макс. всего использований</label>
                            <input type="number" class="form-control" id="id_max_total_uses" name="max_total_uses"
                                   value="{% if form.max_total_uses.value %}{{ form.max_total_uses.value }}{% endif %}"
                                   min="1">
                            <div class="form-text">Общий лимит для всех пользователей</div>
                        </div>
                    </div>

                    <div class="row mb-3">
                        <div class="col-md-6">
                            <label for="id_start_date" class="form-label">Дата начала</label>
                            <input type="datetime-local" class="form-control" id="id_start_date" name="start_date"
                                   value="{% if form.start_date.value %}{{ form.start_date.value|date:'Y-m-d\TH:i' }}{% endif %}">
                        </div>
                        <div class="col-md-6">
                            <label for="id_end_date" class="form-label">Дата окончания</label>
                            <input type="datetime-local" class="form-control" id="id_end_date" name="end_date"
                                   value="{% if form.end_date.value %}{{ form.end_date.value|date:'Y-m-d\TH:i' }}{% endif %}">
                        </div>
                    </div>

                    <div class="form-check form-switch mb-3">
                        <input class="form-check-input" type="checkbox" id="id_is_active" name="is_active"
                               {% if form.is_active.value is None or form.is_active.value %}checked{% endif %}>
                        <label class="form-check-label" for="id_is_active">
                            Активен
                        </label>
                        <div class="form-text">Неактивные промокоды не применяются</div>
                    </div>

                    <div class="d-flex justify-content-between">
                        <a href="{% url 'system_settings:discounts:promo-list' %}" class="btn btn-secondary">Отмена</a>
                        <button type="submit" class="btn btn-primary">
                            <i class="bi bi-check-circle"></i>
                            {% if is_edit %}Сохранить изменения{% else %}Создать промокод{% endif %}
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
{% endblock %}
