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

{% block title %}{% trans "Categorias de Itens - Inflação" %}{% endblock %}

{% block extrastyle %}
<link rel="stylesheet" href="{% static 'css/dashboard-custom.css' %}">
<style>
  .inflation-container {
    background: linear-gradient(135deg, #1a1a2e 0%, #16213e 50%, #0f3460 100%);
    min-height: 100vh;
    padding: 2rem 0;
  }

  .inflation-header {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    border-radius: 15px;
    padding: 2rem;
    margin-bottom: 2rem;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
    text-align: center;
  }

  .inflation-header h1 {
    color: white;
    margin: 0;
    font-weight: 600;
  }

  .action-buttons {
    display: flex;
    gap: 1rem;
    margin-bottom: 2rem;
    flex-wrap: wrap;
  }

  .btn {
    padding: 1rem 2rem;
    border: none;
    border-radius: 10px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s ease;
    text-decoration: none;
    display: inline-block;
  }

  .btn-primary {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
  }

  .btn-primary:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 20px rgba(102, 126, 234, 0.4);
  }

  .btn-back {
    background: linear-gradient(135deg, #4a5568 0%, #2d3748 100%);
    color: white;
  }

  .btn-back:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 20px rgba(74, 85, 104, 0.4);
  }

  .form-container {
    background: linear-gradient(135deg, #2d3748 0%, #4a5568 100%);
    border-radius: 15px;
    padding: 2rem;
    margin-bottom: 2rem;
    box-shadow: 0 8px 25px rgba(0, 0, 0, 0.2);
  }

  .form-container h2 {
    color: white;
    margin-bottom: 1.5rem;
  }

  .form-group {
    margin-bottom: 1.5rem;
  }

  .form-group label {
    display: block;
    color: #a0aec0;
    margin-bottom: 0.5rem;
    font-weight: 500;
  }

  .form-group input,
  .form-group textarea {
    width: 100%;
    padding: 0.75rem;
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 8px;
    background: rgba(0, 0, 0, 0.3);
    color: white;
    font-size: 1rem;
  }

  .form-group input:focus,
  .form-group textarea:focus {
    outline: none;
    border-color: #667eea;
    box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
  }

  .form-group textarea {
    resize: vertical;
    min-height: 100px;
  }

  .table-container {
    background: linear-gradient(135deg, #2d3748 0%, #4a5568 100%);
    border-radius: 15px;
    padding: 2rem;
    box-shadow: 0 8px 25px rgba(0, 0, 0, 0.2);
    overflow-x: auto;
  }

  .table {
    width: 100%;
    color: white;
    border-collapse: collapse;
  }

  .table th {
    background: rgba(0, 0, 0, 0.3);
    padding: 1rem;
    text-align: left;
    border-bottom: 2px solid rgba(255, 255, 255, 0.1);
  }

  .table td {
    padding: 1rem;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  }

  .table tr:hover {
    background: rgba(255, 255, 255, 0.05);
  }

  .color-preview {
    display: inline-block;
    width: 30px;
    height: 30px;
    border-radius: 5px;
    border: 2px solid rgba(255, 255, 255, 0.3);
    vertical-align: middle;
    margin-right: 0.5rem;
  }

  .btn-danger {
    background: linear-gradient(135deg, #f56565 0%, #e53e3e 100%);
    color: white;
    padding: 0.5rem 1rem;
    border: none;
    border-radius: 5px;
    cursor: pointer;
    font-size: 0.875rem;
  }

  .btn-danger:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(245, 101, 101, 0.4);
  }

  .empty-state {
    text-align: center;
    padding: 3rem;
    color: #a0aec0;
  }
</style>
{% endblock %}

{% block content %}
<div class="inflation-container">
  <div class="container">
    <div class="inflation-header">
      <h1>{% trans "Categorias de Itens" %}</h1>
      <p style="color: rgba(255, 255, 255, 0.8); margin-top: 0.5rem;">
        {% trans "Gerencie categorias para organizar a análise de inflação de itens" %}
      </p>
    </div>

    <div class="action-buttons">
      <a href="{% url 'server:inflation_dashboard' %}" class="btn btn-back">
        <i class="fas fa-arrow-left"></i> {% trans "Voltar ao Dashboard" %}
      </a>
    </div>

    <div class="form-container">
      <h2>{% trans "Criar Nova Categoria" %}</h2>
      <form method="POST" id="categoryForm">
        {% csrf_token %}
        <input type="hidden" name="action" value="create">
        
        <div class="form-group">
          <label for="name">{% trans "Nome da Categoria" %} *</label>
          <input type="text" id="name" name="name" required placeholder="{% trans 'Ex: Armas, Armaduras, Consumíveis' %}">
        </div>

        <div class="form-group">
          <label for="description">{% trans "Descrição" %}</label>
          <textarea id="description" name="description" placeholder="{% trans 'Descrição opcional da categoria' %}"></textarea>
        </div>

        <div class="form-group">
          <label for="item_ids">{% trans "IDs dos Itens" %} *</label>
          <input type="text" id="item_ids" name="item_ids" required placeholder="{% trans 'Ex: 1, 2, 3, 4, 5' %}">
          <small style="color: #a0aec0; display: block; margin-top: 0.5rem;">
            {% trans "Separe os IDs por vírgula. Exemplo: 1, 2, 3, 4, 5" %}
          </small>
        </div>

        <div class="form-group">
          <label for="color">{% trans "Cor" %}</label>
          <input type="color" id="color" name="color" value="#667eea">
        </div>

        <div class="form-group">
          <label for="order">{% trans "Ordem" %}</label>
          <input type="number" id="order" name="order" value="0" min="0">
        </div>

        <button type="submit" class="btn btn-primary">
          <i class="fas fa-plus"></i> {% trans "Criar Categoria" %}
        </button>
      </form>
    </div>

    <div class="table-container">
      <h2 style="color: white; margin-bottom: 1.5rem;">{% trans "Categorias Existentes" %}</h2>
      
      {% if categories %}
      <table class="table">
        <thead>
          <tr>
            <th>{% trans "Nome" %}</th>
            <th>{% trans "Cor" %}</th>
            <th>{% trans "Itens" %}</th>
            <th>{% trans "Ordem" %}</th>
            <th>{% trans "Descrição" %}</th>
            <th>{% trans "Ações" %}</th>
          </tr>
        </thead>
        <tbody>
          {% for category in categories %}
          <tr>
            <td><strong style="color: white;">{{ category.name }}</strong></td>
            <td>
              <span class="color-preview" style="background-color: {{ category.color }};"></span>
              {{ category.color }}
            </td>
            <td>{{ category.item_ids|length|default:0 }} {% trans "itens" %}</td>
            <td>{{ category.order }}</td>
            <td style="color: #a0aec0;">{{ category.description|truncatewords:10|default:"-" }}</td>
            <td>
              <form method="POST" style="display: inline;" onsubmit="return confirm('{% trans 'Tem certeza que deseja deletar esta categoria?' %}');">
                {% csrf_token %}
                <input type="hidden" name="action" value="delete">
                <input type="hidden" name="category_id" value="{{ category.id }}">
                <button type="submit" class="btn-danger">
                  <i class="fas fa-trash"></i> {% trans "Deletar" %}
                </button>
              </form>
            </td>
          </tr>
          {% endfor %}
        </tbody>
      </table>
      {% else %}
      <div class="empty-state">
        <i class="fas fa-folder-open" style="font-size: 3rem; margin-bottom: 1rem; opacity: 0.5;"></i>
        <p>{% trans "Nenhuma categoria criada ainda. Crie uma categoria acima para começar." %}</p>
      </div>
      {% endif %}
    </div>
  </div>
</div>
{% endblock %}

