{% extends "layouts/base.html" %}
{% load i18n static %}

{% block extrahead %}
    <!-- jQuery do Django Admin -->
    <script src="{% static 'admin/js/vendor/jquery/jquery.js' %}"></script>
    <script>var django = { jQuery: window.jQuery };</script>

    {{ form.media }}
    <script type="text/javascript" src="{% url 'jsi18n' %}"></script>
{% endblock %}

{% block extrastyle %}
<link rel="stylesheet" href="{% static 'css/dashboard-custom.css' %}">
<style>
  @import url('https://fonts.googleapis.com/css2?family=Orbitron:wght@600&display=swap');

  .box-manager-bg {
    background: radial-gradient(ellipse at center, #1a1a2e 0%, #121220 100%);
    color: #f8f9fa;
    padding: 50px 0;
    min-height: 100vh;
  }

  .box-manager-title {
    font-family: 'Orbitron', sans-serif;
    font-size: 2.5rem;
    font-weight: 700;
    color: #00d9ff;
    margin-bottom: 2rem;
    text-shadow: 0 0 20px rgba(13, 217, 255, 0.5);
  }

  .box-manager-card {
    background: linear-gradient(135deg, #1a1a2e, #16213e);
    border: 2px solid #6f42c1;
    border-radius: 15px;
    box-shadow: 0 8px 32px rgba(111, 66, 193, 0.3);
    transition: all 0.3s ease;
    color: #f8f9fa;
    overflow: hidden;
  }

  .box-manager-header {
    background: #6f42c1;
    border-radius: 15px 15px 0 0;
    padding: 2rem;
    margin: -1px -1px 0 -1px;
  }

  .box-manager-header-title {
    font-family: 'Orbitron', sans-serif;
    font-size: 1.8rem;
    font-weight: 700;
    color: white;
    margin: 0;
  }

  .form-section {
    padding: 2rem;
  }

  .form-label {
    color: #f8f9fa;
    font-weight: 600;
    margin-bottom: 0.5rem;
  }

  .form-control, .form-select {
    background: rgba(255, 255, 255, 0.1);
    border: 1px solid rgba(111, 66, 193, 0.3);
    border-radius: 10px;
    color: #f8f9fa;
    padding: 0.75rem 1rem;
    transition: all 0.3s ease;
  }

  .form-control:focus, .form-select:focus {
    background: rgba(255, 255, 255, 0.15);
    border-color: #00d9ff;
    box-shadow: 0 0 0 0.2rem rgba(13, 217, 255, 0.25);
    color: #f8f9fa;
  }

  .form-control::placeholder {
    color: rgba(255, 255, 255, 0.6);
  }

  .action-btn {
    display: inline-block;
    background: linear-gradient(45deg, #28a745, #20c997);
    border: none;
    border-radius: 50px;
    padding: 12px 30px;
    color: white;
    font-weight: 600;
    text-decoration: none;
    transition: all 0.3s ease;
    box-shadow: 0 4px 15px rgba(40, 167, 69, 0.4);
  }

  .action-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(40, 167, 69, 0.6);
    color: white;
    text-decoration: none;
  }

  .cancel-btn {
    background: linear-gradient(45deg, #6c757d, #495057);
    box-shadow: 0 4px 15px rgba(108, 117, 125, 0.4);
  }

  .cancel-btn:hover {
    box-shadow: 0 6px 20px rgba(108, 117, 125, 0.6);
  }

  .error-message {
    color: #fd7e14;
    background: rgba(253, 126, 20, 0.1);
    border: 1px solid rgba(253, 126, 20, 0.3);
    border-radius: 8px;
    padding: 0.75rem;
    margin-bottom: 1rem;
  }

  .field-error {
    color: #fd7e14;
    font-size: 0.9rem;
    margin-top: 0.5rem;
  }

  @keyframes fadeInUp {
    from {
      opacity: 0;
      transform: translateY(30px);
    }
    to {
      opacity: 1;
      transform: translateY(0);
    }
  }

  .fade-in-up {
    animation: fadeInUp 0.6s ease-out;
  }

  @media (max-width: 768px) {
    .box-manager-title {
      font-size: 2rem;
    }
    
    .form-section {
      padding: 1.5rem;
    }
  }

  /* Seções do Formulário */
  .form-group-section {
    background: rgba(255, 255, 255, 0.03);
    border-radius: 10px;
    padding: 1.5rem;
    border: 1px solid rgba(111, 66, 193, 0.2);
  }

  .section-title {
    color: #00d9ff;
    font-family: 'Orbitron', sans-serif;
    font-size: 1.2rem;
    font-weight: 600;
    margin-bottom: 1.5rem;
    padding-bottom: 0.75rem;
    border-bottom: 2px solid rgba(0, 217, 255, 0.3);
  }

  .form-text {
    color: rgba(255, 255, 255, 0.7);
    font-size: 0.85rem;
    display: block;
    margin-top: 0.25rem;
  }

  /* Checkbox Grid para Itens */
  .items-checkbox-container {
    max-height: 400px;
    overflow-y: auto;
    padding: 1rem;
    background: rgba(0, 0, 0, 0.2);
    border-radius: 8px;
    border: 1px solid rgba(111, 66, 193, 0.2);
  }

  .items-checkbox-container ul {
    list-style: none;
    padding: 0;
    margin: 0;
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
    gap: 0.5rem;
  }

  .items-checkbox-container li {
    background: rgba(255, 255, 255, 0.05);
    padding: 0.5rem 1rem;
    border-radius: 6px;
    border: 1px solid rgba(111, 66, 193, 0.2);
    transition: all 0.3s ease;
  }

  .items-checkbox-container li:hover {
    background: rgba(255, 255, 255, 0.1);
    border-color: #00d9ff;
  }

  .items-checkbox-container input[type="checkbox"] {
    margin-right: 0.5rem;
    cursor: pointer;
  }

  .items-checkbox-container label {
    color: #f8f9fa;
    cursor: pointer;
    margin: 0;
    display: flex;
    align-items: center;
  }

  /* Badge de Total */
  .badge {
    font-family: 'Orbitron', sans-serif;
    font-size: 0.9rem;
    padding: 0.4rem 0.8rem;
  }

  .badge.bg-success {
    background: linear-gradient(45deg, #28a745, #20c997) !important;
  }

  .badge.bg-warning {
    background: linear-gradient(45deg, #ffc107, #fd7e14) !important;
  }

  .badge.bg-danger {
    background: linear-gradient(45deg, #dc3545, #c82333) !important;
  }

  /* Scrollbar personalizada */
  .items-checkbox-container::-webkit-scrollbar {
    width: 8px;
  }

  .items-checkbox-container::-webkit-scrollbar-track {
    background: rgba(0, 0, 0, 0.2);
    border-radius: 4px;
  }

  .items-checkbox-container::-webkit-scrollbar-thumb {
    background: rgba(111, 66, 193, 0.5);
    border-radius: 4px;
  }

  .items-checkbox-container::-webkit-scrollbar-thumb:hover {
    background: rgba(111, 66, 193, 0.8);
  }
</style>
{% endblock extrastyle %}

{% block content %}
<div class="box-manager-bg">
  <div class="container">
    <!-- Header -->
    <div class="d-flex justify-content-between align-items-center mb-4 fade-in-up">
      <div>
        <h1 class="box-manager-title">
          <i class="fas fa-tag me-3"></i>
          {% if box_type %}
            {% trans "Editar Tipo de Caixa" %}
          {% else %}
            {% trans "Criar Novo Tipo de Caixa" %}
          {% endif %}
        </h1>
      </div>
      <a href="{% url 'games:box_type_list' %}" class="action-btn cancel-btn">
        <i class="fas fa-arrow-left me-2"></i>
        {% trans "Voltar" %}
      </a>
    </div>

    <!-- Formulário -->
    <div class="box-manager-card fade-in-up">
      <div class="box-manager-header">
        <h4 class="box-manager-header-title">
          <i class="fas fa-edit me-2"></i>
          {% if box_type %}
            {% trans "Editar Informações" %}
          {% else %}
            {% trans "Informações do Tipo de Caixa" %}
          {% endif %}
        </h4>
      </div>
      <div class="form-section">
        <form method="post">
          {% csrf_token %}
          
          {% if form.non_field_errors %}
            <div class="error-message">
              <i class="fas fa-exclamation-triangle me-2"></i>
              {{ form.non_field_errors }}
            </div>
          {% endif %}

          <!-- Seção: Informações Básicas -->
          <div class="form-group-section mb-4">
            <h5 class="section-title">
              <i class="fas fa-info-circle me-2"></i>
              {% trans "Informações Básicas" %}
            </h5>
            <div class="row">
              <div class="col-md-6 mb-3">
                <label for="{{ form.name.id_for_label }}" class="form-label">
                  <i class="fas fa-tag me-2"></i>
                  {{ form.name.label }}
                </label>
                {{ form.name }}
                {% if form.name.help_text %}
                  <small class="form-text">{{ form.name.help_text }}</small>
                {% endif %}
                {% if form.name.errors %}
                  <div class="field-error">
                    <i class="fas fa-exclamation-circle me-1"></i>
                    {{ form.name.errors }}
                  </div>
                {% endif %}
              </div>
              <div class="col-md-3 mb-3">
                <label for="{{ form.price.id_for_label }}" class="form-label">
                  <i class="fas fa-dollar-sign me-2"></i>
                  {{ form.price.label }}
                </label>
                {{ form.price }}
                {% if form.price.help_text %}
                  <small class="form-text">{{ form.price.help_text }}</small>
                {% endif %}
                {% if form.price.errors %}
                  <div class="field-error">
                    <i class="fas fa-exclamation-circle me-1"></i>
                    {{ form.price.errors }}
                  </div>
                {% endif %}
              </div>
              <div class="col-md-3 mb-3">
                <label for="{{ form.boosters_amount.id_for_label }}" class="form-label">
                  <i class="fas fa-boxes me-2"></i>
                  {{ form.boosters_amount.label }}
                </label>
                {{ form.boosters_amount }}
                {% if form.boosters_amount.help_text %}
                  <small class="form-text">{{ form.boosters_amount.help_text }}</small>
                {% endif %}
                {% if form.boosters_amount.errors %}
                  <div class="field-error">
                    <i class="fas fa-exclamation-circle me-1"></i>
                    {{ form.boosters_amount.errors }}
                  </div>
                {% endif %}
              </div>
            </div>
          </div>

          <!-- Seção: Chances de Raridade -->
          <div class="form-group-section mb-4">
            <h5 class="section-title">
              <i class="fas fa-dice me-2"></i>
              {% trans "Probabilidades de Raridade" %}
              <span class="badge bg-info ms-2" id="total-chance">Total: 0%</span>
            </h5>
            <div class="row">
              <div class="col-md-6 col-lg-3 mb-3">
                <label for="{{ form.chance_common.id_for_label }}" class="form-label">
                  <i class="fas fa-circle text-secondary me-2"></i>
                  {{ form.chance_common.label }}
                </label>
                {{ form.chance_common }}
                {% if form.chance_common.help_text %}
                  <small class="form-text">{{ form.chance_common.help_text }}</small>
                {% endif %}
                {% if form.chance_common.errors %}
                  <div class="field-error">
                    <i class="fas fa-exclamation-circle me-1"></i>
                    {{ form.chance_common.errors }}
                  </div>
                {% endif %}
              </div>
              <div class="col-md-6 col-lg-3 mb-3">
                <label for="{{ form.chance_rare.id_for_label }}" class="form-label">
                  <i class="fas fa-circle text-primary me-2"></i>
                  {{ form.chance_rare.label }}
                </label>
                {{ form.chance_rare }}
                {% if form.chance_rare.help_text %}
                  <small class="form-text">{{ form.chance_rare.help_text }}</small>
                {% endif %}
                {% if form.chance_rare.errors %}
                  <div class="field-error">
                    <i class="fas fa-exclamation-circle me-1"></i>
                    {{ form.chance_rare.errors }}
                  </div>
                {% endif %}
              </div>
              <div class="col-md-6 col-lg-3 mb-3">
                <label for="{{ form.chance_epic.id_for_label }}" class="form-label">
                  <i class="fas fa-circle text-warning me-2"></i>
                  {{ form.chance_epic.label }}
                </label>
                {{ form.chance_epic }}
                {% if form.chance_epic.help_text %}
                  <small class="form-text">{{ form.chance_epic.help_text }}</small>
                {% endif %}
                {% if form.chance_epic.errors %}
                  <div class="field-error">
                    <i class="fas fa-exclamation-circle me-1"></i>
                    {{ form.chance_epic.errors }}
                  </div>
                {% endif %}
              </div>
              <div class="col-md-6 col-lg-3 mb-3">
                <label for="{{ form.chance_legendary.id_for_label }}" class="form-label">
                  <i class="fas fa-circle text-danger me-2"></i>
                  {{ form.chance_legendary.label }}
                </label>
                {{ form.chance_legendary }}
                {% if form.chance_legendary.help_text %}
                  <small class="form-text">{{ form.chance_legendary.help_text }}</small>
                {% endif %}
                {% if form.chance_legendary.errors %}
                  <div class="field-error">
                    <i class="fas fa-exclamation-circle me-1"></i>
                    {{ form.chance_legendary.errors }}
                  </div>
                {% endif %}
              </div>
            </div>
          </div>

          <!-- Seção: Limites de Itens -->
          <div class="form-group-section mb-4">
            <h5 class="section-title">
              <i class="fas fa-shield-alt me-2"></i>
              {% trans "Limites de Itens Raros" %}
            </h5>
            <div class="row">
              <div class="col-md-6 mb-3">
                <label for="{{ form.max_epic_items.id_for_label }}" class="form-label">
                  <i class="fas fa-crown text-warning me-2"></i>
                  {{ form.max_epic_items.label }}
                </label>
                {{ form.max_epic_items }}
                {% if form.max_epic_items.help_text %}
                  <small class="form-text">{{ form.max_epic_items.help_text }}</small>
                {% endif %}
                {% if form.max_epic_items.errors %}
                  <div class="field-error">
                    <i class="fas fa-exclamation-circle me-1"></i>
                    {{ form.max_epic_items.errors }}
                  </div>
                {% endif %}
              </div>
              <div class="col-md-6 mb-3">
                <label for="{{ form.max_legendary_items.id_for_label }}" class="form-label">
                  <i class="fas fa-crown text-danger me-2"></i>
                  {{ form.max_legendary_items.label }}
                </label>
                {{ form.max_legendary_items }}
                {% if form.max_legendary_items.help_text %}
                  <small class="form-text">{{ form.max_legendary_items.help_text }}</small>
                {% endif %}
                {% if form.max_legendary_items.errors %}
                  <div class="field-error">
                    <i class="fas fa-exclamation-circle me-1"></i>
                    {{ form.max_legendary_items.errors }}
                  </div>
                {% endif %}
              </div>
            </div>
          </div>

          <!-- Seção: Itens Permitidos -->
          <div class="form-group-section mb-4">
            <h5 class="section-title">
              <i class="fas fa-list-check me-2"></i>
              {% trans "Itens Permitidos" %}
            </h5>
            <div class="items-checkbox-container">
              <label for="{{ form.allowed_items.id_for_label }}" class="form-label">
                <i class="fas fa-treasure-chest me-2"></i>
                {{ form.allowed_items.label }}
              </label>
              {{ form.allowed_items }}
              {% if form.allowed_items.help_text %}
                <small class="form-text">{{ form.allowed_items.help_text }}</small>
              {% endif %}
              {% if form.allowed_items.errors %}
                <div class="field-error">
                  <i class="fas fa-exclamation-circle me-1"></i>
                  {{ form.allowed_items.errors }}
                </div>
              {% endif %}
            </div>
          </div>

          <div class="d-flex gap-3 mt-4">
            <button type="submit" class="action-btn">
              <i class="fas fa-save me-2"></i>
              {% if box_type %}
                {% trans "Salvar Alterações" %}
              {% else %}
                {% trans "Criar Tipo de Caixa" %}
              {% endif %}
            </button>
            <a href="{% url 'games:box_type_list' %}" class="action-btn cancel-btn">
              <i class="fas fa-times me-2"></i>
              {% trans "Cancelar" %}
            </a>
          </div>
        </form>
      </div>
    </div>
  </div>
</div>

<script>
  document.addEventListener('DOMContentLoaded', function() {
    // Função para calcular e atualizar o total das chances
    function updateTotalChance() {
      const chanceCommon = parseFloat(document.getElementById('{{ form.chance_common.id_for_label }}').value) || 0;
      const chanceRare = parseFloat(document.getElementById('{{ form.chance_rare.id_for_label }}').value) || 0;
      const chanceEpic = parseFloat(document.getElementById('{{ form.chance_epic.id_for_label }}').value) || 0;
      const chanceLegendary = parseFloat(document.getElementById('{{ form.chance_legendary.id_for_label }}').value) || 0;
      
      const total = chanceCommon + chanceRare + chanceEpic + chanceLegendary;
      const totalBadge = document.getElementById('total-chance');
      
      totalBadge.textContent = `Total: ${total.toFixed(2)}%`;
      
      // Atualizar a cor do badge baseado no total
      totalBadge.classList.remove('bg-success', 'bg-warning', 'bg-danger', 'bg-info');
      if (Math.abs(total - 100) < 0.01) {
        totalBadge.classList.add('bg-success');
      } else if (total > 100) {
        totalBadge.classList.add('bg-danger');
      } else {
        totalBadge.classList.add('bg-warning');
      }
    }
    
    // Adicionar event listeners aos campos de chance
    const chanceFields = [
      document.getElementById('{{ form.chance_common.id_for_label }}'),
      document.getElementById('{{ form.chance_rare.id_for_label }}'),
      document.getElementById('{{ form.chance_epic.id_for_label }}'),
      document.getElementById('{{ form.chance_legendary.id_for_label }}')
    ];
    
    chanceFields.forEach(field => {
      if (field) {
        field.addEventListener('input', updateTotalChance);
      }
    });
    
    // Calcular o total inicial
    updateTotalChance();
  });
</script>
{% endblock %}