{% extends "base.html" %}

{% block title %}{{ title }}{% endblock %}

{% block content %}
<div class="container-fluid">
  <!-- Header -->
  <div class="row mb-4">
    <div class="col-12">
      <h1 class="mb-2">
        <i class="bi bi-flower1 text-primary me-2"></i>{{ form_title }}
      </h1>
      <nav aria-label="breadcrumb">
        <ol class="breadcrumb">
          <li class="breadcrumb-item"><a href="{% url 'inventory:inventory-home' %}">Склад</a></li>
          <li class="breadcrumb-item"><a href="{% url 'inventory:showcase-list' %}">Витрины</a></li>
          <li class="breadcrumb-item active">{{ form_title }}</li>
        </ol>
      </nav>
    </div>
  </div>

  <!-- Form -->
  <div class="row">
    <div class="col-lg-8">
      <div class="card">
        <div class="card-body">
          <form method="post" novalidate>
            {% csrf_token %}

            {% if form.non_field_errors %}
              <div class="alert alert-danger">
                {{ form.non_field_errors }}
              </div>
            {% endif %}

            <!-- Name -->
            <div class="mb-3">
              <label for="{{ form.name.id_for_label }}" class="form-label fw-semibold">
                {{ form.name.label }}
                <span class="text-danger">*</span>
              </label>
              {{ form.name }}
              {% if form.name.errors %}
                <div class="invalid-feedback d-block">
                  {{ form.name.errors }}
                </div>
              {% endif %}
              {% if form.name.help_text %}
                <small class="form-text text-muted">{{ form.name.help_text }}</small>
              {% endif %}
            </div>

            <!-- Warehouse -->
            <div class="mb-3">
              <label for="{{ form.warehouse.id_for_label }}" class="form-label fw-semibold">
                {{ form.warehouse.label }}
                <span class="text-danger">*</span>
              </label>
              {{ form.warehouse }}
              {% if form.warehouse.errors %}
                <div class="invalid-feedback d-block">
                  {{ form.warehouse.errors }}
                </div>
              {% endif %}
              {% if form.warehouse.help_text %}
                <small class="form-text text-muted">{{ form.warehouse.help_text }}</small>
              {% endif %}
            </div>

            <!-- Description -->
            <div class="mb-3">
              <label for="{{ form.description.id_for_label }}" class="form-label fw-semibold">
                {{ form.description.label }}
              </label>
              {{ form.description }}
              {% if form.description.errors %}
                <div class="invalid-feedback d-block">
                  {{ form.description.errors }}
                </div>
              {% endif %}
              {% if form.description.help_text %}
                <small class="form-text text-muted">{{ form.description.help_text }}</small>
              {% endif %}
            </div>

            <!-- Is Active -->
            <div class="mb-3">
              <div class="form-check">
                {{ form.is_active }}
                <label class="form-check-label" for="{{ form.is_active.id_for_label }}">
                  {{ form.is_active.label }}
                </label>
                {% if form.is_active.help_text %}
                  <div><small class="form-text text-muted">{{ form.is_active.help_text }}</small></div>
                {% endif %}
              </div>
              {% if form.is_active.errors %}
                <div class="invalid-feedback d-block">
                  {{ form.is_active.errors }}
                </div>
              {% endif %}
            </div>

            <!-- Is Default -->
            <div class="mb-4">
              <div class="form-check">
                {{ form.is_default }}
                <label class="form-check-label" for="{{ form.is_default.id_for_label }}">
                  {{ form.is_default.label }}
                </label>
                {% if form.is_default.help_text %}
                  <div><small class="form-text text-muted">{{ form.is_default.help_text }}</small></div>
                {% endif %}
              </div>
              {% if form.is_default.errors %}
                <div class="invalid-feedback d-block">
                  {{ form.is_default.errors }}
                </div>
              {% endif %}
            </div>

            <!-- Buttons -->
            <div class="d-flex gap-2">
              <button type="submit" class="btn btn-primary">
                <i class="bi bi-check-circle me-1"></i>{{ submit_text }}
              </button>
              <a href="{% url 'inventory:showcase-list' %}" class="btn btn-secondary">
                <i class="bi bi-x-circle me-1"></i>Отмена
              </a>
            </div>
          </form>
        </div>
      </div>
    </div>

    <!-- Info Card (for edit mode) -->
    {% if object %}
    <div class="col-lg-4">
      <div class="card">
        <div class="card-header bg-light">
          <h6 class="mb-0">Информация о витрине</h6>
        </div>
        <div class="card-body">
          <dl class="row mb-0">
            <dt class="col-sm-6">Создана:</dt>
            <dd class="col-sm-6 text-end">{{ object.created_at|date:"d.m.Y H:i" }}</dd>

            <dt class="col-sm-6">Обновлена:</dt>
            <dd class="col-sm-6 text-end">{{ object.updated_at|date:"d.m.Y H:i" }}</dd>

            {% if active_reservations_count is not None %}
            <dt class="col-sm-6">Активные резервы:</dt>
            <dd class="col-sm-6 text-end">
              {% if active_reservations_count > 0 %}
                <span class="badge bg-info">{{ active_reservations_count }}</span>
              {% else %}
                <span class="text-muted">—</span>
              {% endif %}
            </dd>
            {% endif %}
          </dl>
        </div>
      </div>
    </div>
    {% endif %}
  </div>
</div>
{% endblock %}
