{% extends "base.html" %}

{% block title %}Gestión de Tarifas{% endblock %}

{% block content %}
<div id="admin-content" class="container-fluid">
        <div class="row">
            <div class="col-12 mb-4 admin-header">
                <h1>Gestión de Tarifas</h1>
            </div>
        </div>
        <div class="row mb-4">
            <div class="col-md-6">
                <p class="mb-0">Administre las mejores tarifas disponibles para comparar con las facturas de los clientes.</p>
            </div>
            <div class="col-md-6 text-md-end">
                {% if not is_comercial %}
                <a href="{{ url_for('admin.create_rate') }}" class="btn btn-primary">
                    <i class="fas fa-plus me-2"></i> Nueva Tarifa
                </a>
                {% endif %}
            </div>
        </div>
        
        <div class="row">
            <div class="col-12">
                <div class="card">
                    <div class="card-body p-0">
                        {% if rates %}
                        <div class="table-responsive">
                            <table class="table table-hover rates-table">
                                <thead>
                                    <tr>
                                        <th>ID</th>
                                        <th>Comercializadora</th>
                                        <th>Nombre</th>
                                        <th>Tipo de Tarifa</th>
                                        <th>Desde</th>
                                        <th>Hasta</th>
                                        <th>Energía P1</th>
                                        <th>Potencia P1</th>
                                        <th>Acciones</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {% for rate in rates %}
                                    <tr>
                                        <td>{{ rate.id }}</td>
                                        <td>{{ rate.supplier or 'No especificada' }}</td>
                                        <td>{{ rate.name }}</td>
                                        <td><span class="badge bg-secondary">{{ rate.tariff_type }}</span></td>
                                        <td>{{ rate.valid_from or 'Sin fecha' }}</td>
                                        <td>{{ rate.valid_until or 'Sin fecha' }}</td>
                                        <td>{{ rate.energy_prices.get('P1', '--') }} €/kWh</td>
                                        <td>{{ rate.power_prices.get('P1', '--') }} €/kW</td>
                                        <td>
                                            <div class="btn-group">
                                                {% if not is_comercial %}
                                                <a href="{{ url_for('admin.edit_rate', rate_id=rate.id) }}" 
                                                   class="btn btn-sm btn-outline-primary" title="Editar Tarifa">
                                                    <i class="fas fa-edit"></i>
                                                </a>
                                                <button type="button" class="btn btn-sm btn-outline-danger" 
                                                         data-rate-id="{{ rate.id }}" 
                                                         data-rate-name="{{ rate.name }}" 
                                                         onclick="confirmDelete(this.getAttribute('data-rate-id'), this.getAttribute('data-rate-name'))" title="Eliminar Tarifa">
                                                     <i class="fas fa-trash-alt"></i>
                                                 </button>
                                                {% else %}
                                                <span class="badge bg-info">Solo lectura</span>
                                                {% endif %}
                                            </div>
                                        </td>
                                    </tr>
                                    {% endfor %}
                                </tbody>
                            </table>
                        </div>
                        
                        {# Pagination controls #}
                        {% if pagination.total > 0 %}
                        <nav aria-label="Page navigation">
                            <ul class="pagination pagination-sm flex-wrap justify-content-center mt-4">{% if pagination.has_prev %}<li class="page-item">
                                    <a class="page-link" href="{{ url_for('admin.list_rates', page=pagination.prev_num, per_page=per_page) }}" aria-label="Previous">
                                        <span aria-hidden="true">&laquo;</span>
                                    </a>
                                </li>
                                {% endif %}
                                {% for p in pagination.iter_pages() %}
                                {% if p %}
                                    {% if p == pagination.page %}
                                    <li class="page-item active" aria-current="page"><a class="page-link" href="#">{{ p }}</a></li>
                                    {% else %}
                                    <li class="page-item"><a class="page-link" href="{{ url_for('admin.list_rates', page=p, per_page=per_page) }}">{{ p }}</a></li>
                                    {% endif %}
                                {% else %}
                                <li class="page-item disabled"><a class="page-link" href="#">...</a></li>
                                {% endif %}
                                {% endfor %}
                                {% if pagination.has_next %}
                                <li class="page-item">
                                    <a class="page-link" href="{{ url_for('admin.list_rates', page=pagination.next_num, per_page=per_page) }}" aria-label="Next">
                                        <span aria-hidden="true">&raquo;</span>
                                    </a>
                                </li>
                                {% endif %}
                            </ul>
                        </nav>
                        {% endif %}

                        {% else %}
                        <div class="empty-state">
                            <div class="empty-icon">
                                <i class="fas fa-bolt"></i>
                            </div>
                            <h3>No hay tarifas disponibles</h3>
                            <p>Añada una nueva tarifa para comenzar a comparar con las facturas de los clientes.</p>
                            {% if not is_comercial %}
                            <a href="{{ url_for('admin.create_rate') }}" class="btn btn-primary">
                                <i class="fas fa-plus"></i> Añadir primera tarifa
                            </a>
                            {% endif %}
                        </div>
                        {% endif %}
                    </div>
                </div>
            </div>
        </div>
</div>

<!-- Modal de confirmación para eliminar -->
<div class="modal fade" id="deleteModal" tabindex="-1" aria-labelledby="deleteModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="deleteModalLabel">Confirmar eliminación</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <p>¿Está seguro de que desea eliminar la tarifa <strong id="deleteTariffName"></strong>?</p>
                <p class="text-danger">Esta acción no se puede deshacer.</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancelar</button>
                <form id="deleteForm" method="POST" action="">
                    <button type="submit" class="btn btn-danger">Eliminar</button>
                </form>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block scripts %}
<script>
    function confirmDelete(rateId, rateName) {
        document.getElementById('deleteTariffName').textContent = rateName;
        document.getElementById('deleteForm').action = "{{ url_for('admin.delete_rate', rate_id=0) }}".replace('0', rateId);
        
        var deleteModal = new bootstrap.Modal(document.getElementById('deleteModal'));
        deleteModal.show();
    }
</script>
{% endblock %}

{% block extra_styles %}
<style>
    .rates-table th {
        background-color: #f8f9fa;
        font-weight: 600;
    }
    
    .empty-state {
        text-align: center;
        padding: 50px 20px;
    }
    
    .empty-icon {
        font-size: 3rem;
        color: #ccc;
        margin-bottom: 20px;
    }
    
    .table-responsive {
        border-radius: 4px;
    }
</style>
{% endblock %}
