{% extends 'base.html' %}
{% from 'macros.html' import render_messages, render_pagination %}

{% block content %}
<div class="container">
    {{ render_messages() }}
    
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2>{{ _('Reglas de Cálculo') }}</h2>
        <a href="{{ url_for('calculation_rule.new') }}" class="btn btn-primary">
            <i class="bi bi-plus-lg me-1"></i>{{ _('Nueva Regla') }}
        </a>
    </div>

    <div class="card">
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-hover">
                    <thead>
                        <tr>
                            <th>{{ _('Código') }}</th>
                            <th>{{ _('Nombre') }}</th>
                            <th>{{ _('Tipo') }}</th>
                            <th>{{ _('Jurisdicción') }}</th>
                            <th>{{ _('Versión') }}</th>
                            <th>{{ _('Vigencia') }}</th>
                            <th>{{ _('Estado') }}</th>
                            <th>{{ _('Acciones') }}</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for rule in rules %}
                        <tr>
                            <td><strong>{{ rule.codigo }}</strong></td>
                            <td>{{ rule.nombre }}</td>
                            <td>
                                {% if rule.tipo_regla == 'impuesto' %}
                                    <span class="badge bg-danger">{{ _('Impuesto') }}</span>
                                {% elif rule.tipo_regla == 'deduccion' %}
                                    <span class="badge bg-warning text-dark">{{ _('Deducción') }}</span>
                                {% elif rule.tipo_regla == 'percepcion' %}
                                    <span class="badge bg-info">{{ _('Percepción') }}</span>
                                {% else %}
                                    <span class="badge bg-primary">{{ _('Prestación') }}</span>
                                {% endif %}
                            </td>
                            <td>{{ rule.jurisdiccion or '-' }}</td>
                            <td><code>{{ rule.version }}</code></td>
                            <td>
                                {{ rule.vigente_desde.strftime('%d/%m/%Y') if rule.vigente_desde else '-' }}
                                {% if rule.vigente_hasta %}
                                    - {{ rule.vigente_hasta.strftime('%d/%m/%Y') }}
                                {% endif %}
                            </td>
                            <td>
                                {% if rule.activo %}
                                    <span class="badge bg-success">{{ _('Activo') }}</span>
                                {% else %}
                                    <span class="badge bg-secondary">{{ _('Inactivo') }}</span>
                                {% endif %}
                            </td>
                            <td>
                                <div class="btn-group btn-group-sm">
                                    <a href="{{ url_for('calculation_rule.edit_schema', id=rule.id) }}" 
                                       class="btn btn-outline-primary" title="{{ _('Editar Esquema') }}">
                                        <i class="bi bi-code-square"></i>
                                    </a>
                                    <a href="{{ url_for('calculation_rule.edit', id=rule.id) }}" 
                                       class="btn btn-outline-secondary" title="{{ _('Editar Metadatos') }}">
                                        <i class="bi bi-pencil"></i>
                                    </a>
                                    <form action="{{ url_for('calculation_rule.duplicate', id=rule.id) }}" 
                                          method="POST" class="d-inline">
                                        <button type="submit" class="btn btn-outline-info" title="{{ _('Duplicar') }}">
                                            <i class="bi bi-copy"></i>
                                        </button>
                                    </form>
                                    <form action="{{ url_for('calculation_rule.delete', id=rule.id) }}" 
                                          method="POST" class="d-inline" 
                                          onsubmit="return confirm('{{ _('¿Está seguro de eliminar esta regla?') }}');">
                                        <button type="submit" class="btn btn-outline-danger" title="{{ _('Eliminar') }}">
                                            <i class="bi bi-trash"></i>
                                        </button>
                                    </form>
                                </div>
                            </td>
                        </tr>
                        {% else %}
                        <tr>
                            <td colspan="8" class="text-center text-muted">
                                {{ _('No hay reglas de cálculo registradas.') }}
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
            {{ render_pagination(pagination, 'calculation_rule.index') }}
        </div>
    </div>
</div>
{% endblock %}
