{% extends "base.html" %}

{% block content %}
<div class="container-fluid">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2>
            <i class="bi bi-calculator me-2"></i>
            {% if is_edit %}{{ _('Editar Planilla') }}{% else %}{{ _('Nueva Planilla') }}{% endif %}
        </h2>
        <div>
            {% if is_edit %}
            <a href="{{ url_for('planilla.config', planilla_id=planilla.id) }}" class="btn btn-outline-primary me-2">
                <i class="bi bi-gear me-1"></i>{{ _('Ver Configuración') }}
            </a>
            {% endif %}
            <a href="{{ url_for('planilla.index') }}" class="btn btn-outline-secondary">
                <i class="bi bi-arrow-left me-1"></i>{{ _('Volver') }}
            </a>
        </div>
    </div>

    {% with messages = get_flashed_messages(with_categories=true) %}
        {% if messages %}
            {% for category, message in messages %}
                <div class="alert alert-{{ 'danger' if category == 'error' else category }} alert-dismissible fade show" role="alert">
                    {{ message }}
                    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                </div>
            {% endfor %}
        {% endif %}
    {% endwith %}

    <div class="row">
        <!-- Main Form Column -->
        <div class="col-lg-8">
            <div class="card shadow-sm mb-4">
                <div class="card-header bg-coaty-light">
                    <h5 class="mb-0"><i class="bi bi-gear me-2"></i>{{ _('Configuración General') }}</h5>
                </div>
                <div class="card-body">
                    <form method="POST">
                        {{ form.hidden_tag() }}
                        
                        <div class="row">
                            <div class="col-md-8 mb-3">
                                {{ form.nombre.label(class="form-label") }}
                                {{ form.nombre(class="form-control" + (" is-invalid" if form.nombre.errors else "")) }}
                                {% if form.nombre.description %}
                                <div class="form-text">{{ form.nombre.description }}</div>
                                {% endif %}
                                {% for error in form.nombre.errors %}
                                <div class="invalid-feedback">{{ error }}</div>
                                {% endfor %}
                            </div>
                            <div class="col-md-4 mb-3">
                                <label class="form-label">&nbsp;</label>
                                <div class="form-check mt-2">
                                    {{ form.activo(class="form-check-input") }}
                                    {{ form.activo.label(class="form-check-label") }}
                                </div>
                            </div>
                        </div>

                        <div class="mb-3">
                            {{ form.descripcion.label(class="form-label") }}
                            {{ form.descripcion(class="form-control") }}
                        </div>

                        <div class="row">
                            <div class="col-md-4 mb-3">
                                {{ form.tipo_planilla_id.label(class="form-label") }}
                                {{ form.tipo_planilla_id(class="form-select" + (" is-invalid" if form.tipo_planilla_id.errors else "")) }}
                                {% if form.tipo_planilla_id.description %}
                                <div class="form-text">{{ form.tipo_planilla_id.description }}</div>
                                {% endif %}
                                {% for error in form.tipo_planilla_id.errors %}
                                <div class="invalid-feedback">{{ error }}</div>
                                {% endfor %}
                            </div>
                            <div class="col-md-4 mb-3">
                                {{ form.moneda_id.label(class="form-label") }}
                                {{ form.moneda_id(class="form-select" + (" is-invalid" if form.moneda_id.errors else "")) }}
                                {% if form.moneda_id.description %}
                                <div class="form-text">{{ form.moneda_id.description }}</div>
                                {% endif %}
                                {% for error in form.moneda_id.errors %}
                                <div class="invalid-feedback">{{ error }}</div>
                                {% endfor %}
                            </div>
                            <div class="col-md-4 mb-3">
                                {{ form.empresa_id.label(class="form-label") }}
                                {{ form.empresa_id(class="form-select" + (" is-invalid" if form.empresa_id.errors else "")) }}
                                {% if form.empresa_id.description %}
                                <div class="form-text">{{ form.empresa_id.description }}</div>
                                {% endif %}
                                {% for error in form.empresa_id.errors %}
                                <div class="invalid-feedback">{{ error }}</div>
                                {% endfor %}
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-md-6 mb-3">
                                {{ form.periodo_fiscal_inicio.label(class="form-label") }}
                                {{ form.periodo_fiscal_inicio(class="form-control", type="date") }}
                                {% if form.periodo_fiscal_inicio.description %}
                                <div class="form-text">{{ form.periodo_fiscal_inicio.description }}</div>
                                {% endif %}
                            </div>
                            <div class="col-md-6 mb-3">
                                {{ form.periodo_fiscal_fin.label(class="form-label") }}
                                {{ form.periodo_fiscal_fin(class="form-control", type="date") }}
                                {% if form.periodo_fiscal_fin.description %}
                                <div class="form-text">{{ form.periodo_fiscal_fin.description }}</div>
                                {% endif %}
                            </div>
                        </div>

                        <hr class="my-4">
                        <h6 class="text-muted mb-3"><i class="bi bi-lightning me-2"></i>{{ _('Deducciones Automáticas') }}</h6>
                        
                        <div class="row">
                            <div class="col-md-6 mb-3">
                                <div class="form-check mb-2">
                                    {{ form.aplicar_prestamos_automatico(class="form-check-input") }}
                                    {{ form.aplicar_prestamos_automatico.label(class="form-check-label") }}
                                </div>
                                {{ form.prioridad_prestamos.label(class="form-label") }}
                                {{ form.prioridad_prestamos(class="form-control", placeholder="250") }}
                                {% if form.prioridad_prestamos.description %}
                                <div class="form-text">{{ form.prioridad_prestamos.description }}</div>
                                {% endif %}
                            </div>
                            <div class="col-md-6 mb-3">
                                <div class="form-check mb-2">
                                    {{ form.aplicar_adelantos_automatico(class="form-check-input") }}
                                    {{ form.aplicar_adelantos_automatico.label(class="form-check-label") }}
                                </div>
                                {{ form.prioridad_adelantos.label(class="form-label") }}
                                {{ form.prioridad_adelantos(class="form-control", placeholder="251") }}
                                {% if form.prioridad_adelantos.description %}
                                <div class="form-text">{{ form.prioridad_adelantos.description }}</div>
                                {% endif %}
                            </div>
                        </div>

                        <div class="d-grid gap-2 d-md-flex justify-content-md-end mt-4">
                            <a href="{{ url_for('planilla.index') }}" class="btn btn-outline-secondary">{{ _('Cancelar') }}</a>
                            {{ form.submit(class="btn btn-coaty") }}
                        </div>
                    </form>
                </div>
            </div>
        </div>

        {% if is_edit %}
        <!-- Associations Summary Column -->
        <div class="col-lg-4">
            <div class="card shadow-sm">
                <div class="card-header bg-coaty-light">
                    <h5 class="mb-0"><i class="bi bi-diagram-3 me-2"></i>{{ _('Componentes Asignados') }}</h5>
                </div>
                <div class="card-body">
                    <ul class="list-group list-group-flush">
                        <li class="list-group-item d-flex justify-content-between align-items-center">
                            <span><i class="bi bi-people me-2 text-primary"></i>{{ _('Empleados') }}</span>
                            <span class="badge bg-primary rounded-pill">{{ empleados_count }}</span>
                        </li>
                        <li class="list-group-item d-flex justify-content-between align-items-center">
                            <span><i class="bi bi-plus-circle me-2 text-success"></i>{{ _('Percepciones') }}</span>
                            <span class="badge bg-success rounded-pill">{{ percepciones_count }}</span>
                        </li>
                        <li class="list-group-item d-flex justify-content-between align-items-center">
                            <span><i class="bi bi-dash-circle me-2 text-danger"></i>{{ _('Deducciones') }}</span>
                            <span class="badge bg-danger rounded-pill">{{ deducciones_count }}</span>
                        </li>
                        <li class="list-group-item d-flex justify-content-between align-items-center">
                            <span><i class="bi bi-gift me-2 text-info"></i>{{ _('Prestaciones') }}</span>
                            <span class="badge bg-info rounded-pill">{{ prestaciones_count }}</span>
                        </li>
                        <li class="list-group-item d-flex justify-content-between align-items-center">
                            <span><i class="bi bi-code-square me-2 text-warning"></i>{{ _('Reglas') }}</span>
                            <span class="badge bg-warning text-dark rounded-pill">{{ reglas_count }}</span>
                        </li>
                    </ul>
                    <div class="d-grid mt-3">
                        <a href="{{ url_for('planilla.config', planilla_id=planilla.id) }}" class="btn btn-outline-primary">
                            <i class="bi bi-gear me-1"></i>{{ _('Gestionar Componentes') }}
                        </a>
                    </div>
                </div>
            </div>
        </div>
        {% endif %}
    </div>
</div>
{% endblock %}
