{% extends "base.html" %}

{% block head %}
    <!-- Bootstrap Datepicker CSS -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.9.0/css/bootstrap-datepicker.min.css">
{% endblock %}

{% block title %}Militantes{% endblock %}

{% block content %}
<div class="container-fluid">
    <div class="row mb-4">
        <div class="col-12">
            <div class="d-flex justify-content-between align-items-center">
                <h1 class="h3 mb-0">
                    <i class="fas fa-users me-2"></i>Militantes
                </h1>
                <div>
                    <button type="button" class="btn btn-outline-primary me-2" id="btnExportar">
                        <i class="fas fa-file-export me-2"></i>Exportar
                    </button>
                    <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#modalNovoMilitante">
                        <i class="fas fa-user-plus me-2"></i>Novo Militante
                    </button>
                </div>
            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-body">
                    <div class="row mb-4">
                        <div class="col-md-6">
                            <div class="input-group">
                                <span class="input-group-text">
                                    <i class="fas fa-search"></i>
                                </span>
                                <input type="text" class="form-control" id="searchInput" placeholder="Pesquisar militantes...">
                            </div>
                        </div>
                        <div class="col-md-6 text-end">
                            <div class="btn-group me-2">
                                <button type="button" class="btn btn-outline-secondary btn-fixed-width dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
                                    <i class="fas fa-filter me-2"></i>Filtrar
                                </button>
                                <ul class="dropdown-menu">
                                    <li><h6 class="dropdown-header">Status</h6></li>
                                    <li><a class="dropdown-item" href="#" data-filter="todos">Todos</a></li>
                                    <li><hr class="dropdown-divider"></li>
                                    <li><h6 class="dropdown-header">Responsabilidades</h6></li>
                                    <li><a class="dropdown-item" href="#" data-filter="responsavel-financas">Responsável de Finanças</a></li>
                                    <li><a class="dropdown-item" href="#" data-filter="responsavel-imprensa">Responsável de Imprensa</a></li>
                                    <li><a class="dropdown-item" href="#" data-filter="quadro-orientador">Quadro-Orientador</a></li>
                                    <li><a class="dropdown-item" href="#" data-filter="secretario">Secretário</a></li>
                                    <li><a class="dropdown-item" href="#" data-filter="tesoureiro">Tesoureiro</a></li>
                                    <li><a class="dropdown-item" href="#" data-filter="imprensa">Imprensa</a></li>
                                    <li><a class="dropdown-item" href="#" data-filter="mns">MNS</a></li>
                                    <li><a class="dropdown-item" href="#" data-filter="mps">MPS</a></li>
                                    <li><a class="dropdown-item" href="#" data-filter="juventude">Juventude</a></li>
                                    <li><a class="dropdown-item" href="#" data-filter="aspirante">Aspirante</a></li>
                                    <li><hr class="dropdown-divider"></li>
                                    <li><h6 class="dropdown-header">Célula</h6></li>
                                    {% for celula in celulas %}
                                    <li><a class="dropdown-item" href="#" data-filter="celula" data-celula="{{ celula.id }}">{{ celula.nome }}</a></li>
                                    {% endfor %}
                                </ul>
                            </div>
                        </div>
                    </div>

                    <div class="table-responsive">
                        <table class="table table-hover" id="militantesTable">
                            <thead>
                                <tr>
                                    <th data-sort="nome">Nome <i class="fas fa-sort"></i></th>
                                    <th data-sort="cpf">CPF <i class="fas fa-sort"></i></th>
                                    <th data-sort="email">Email <i class="fas fa-sort"></i></th>
                                    <th data-sort="telefone">Telefone <i class="fas fa-sort"></i></th>
                                    <th>Responsabilidades</th>
                                    <th class="text-end">Ações</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for militante in militantes %}
                                <tr data-militante="{{ militante.id }}" 
                                    data-celula-id="{{ militante.celula_id }}"
                                    data-responsabilidades="{{ militante.responsabilidades }}">
                                    <td data-nome="{{ militante.nome }}">{{ militante.nome }}</td>
                                    <td data-cpf="{{ militante.cpf }}">{{ militante.cpf }}</td>
                                    <td data-email="{{ militante.emails[0].endereco_email if militante.emails else '' }}">{{ militante.emails[0].endereco_email if militante.emails else '' }}</td>
                                    <td data-telefone="{{ militante.telefone1 }}">{{ militante.telefone1 }}</td>
                                    <td>
                                        {% if militante.responsabilidades is defined and militante.responsabilidades %}
                                            {% if militante.responsabilidades|bitwise_and(Militante.RESPONSAVEL_FINANCAS) %}
                                                <span class="badge bg-primary" title="Responsável de Finanças">RFI</span>
                                            {% endif %}
                                            {% if militante.responsabilidades|bitwise_and(Militante.RESPONSAVEL_IMPRENSA) %}
                                                <span class="badge bg-info" title="Responsável de Imprensa">RIM</span>
                                            {% endif %}
                                            {% if militante.responsabilidades|bitwise_and(Militante.QUADRO_ORIENTADOR) %}
                                                <span class="badge bg-success" title="Quadro-Orientador">QOR</span>
                                            {% endif %}
                                            {% if militante.responsabilidades|bitwise_and(Militante.SECRETARIO) %}
                                                <span class="badge bg-secondary" title="Secretário">SEC</span>
                                            {% endif %}
                                            {% if militante.responsabilidades|bitwise_and(Militante.TESOUREIRO) %}
                                                <span class="badge bg-warning" title="Tesoureiro">TES</span>
                                            {% endif %}
                                            {% if militante.responsabilidades|bitwise_and(Militante.IMPRENSA) %}
                                                <span class="badge bg-danger" title="Imprensa">IMP</span>
                                            {% endif %}
                                            {% if militante.responsabilidades|bitwise_and(Militante.MNS) %}
                                                <span class="badge bg-purple" title="MNS">MNS</span>
                                            {% endif %}
                                            {% if militante.responsabilidades|bitwise_and(Militante.MPS) %}
                                                <span class="badge bg-teal" title="MPS">MPS</span>
                                            {% endif %}
                                            {% if militante.responsabilidades|bitwise_and(Militante.JUVENTUDE) %}
                                                <span class="badge bg-orange" title="Juventude">JUV</span>
                                            {% endif %}
                                            {% if militante.responsabilidades|bitwise_and(Militante.ASPIRANTE) %}
                                                <span class="badge bg-dark" title="Aspirante">ASP</span>
                                            {% endif %}
                                        {% endif %}
                                    </td>
                                    <td class="text-end">
                                        <div class="btn-group">
                                            <button type="button" 
                                                class="btn btn-sm btn-outline-primary"
                                                data-bs-toggle="modal"
                                                data-bs-target="#modalEditarMilitante"
                                                data-militante-id="{{ militante.id }}"
                                                data-militante-nome="{{ militante.nome }}"
                                                title="Editar">
                                                <i class="fas fa-edit"></i>
                                            </button>
                                            <button type="button" 
                                                class="btn btn-sm btn-outline-danger"
                                                data-bs-toggle="modal"
                                                data-bs-target="#deleteModal"
                                                data-militante-id="{{ militante.id }}"
                                                data-militante-nome="{{ militante.nome }}"
                                                title="Excluir">
                                                <i class="fas fa-trash"></i>
                                            </button>
                                        </div>
                                    </td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>

                    <div class="pagination-container d-flex justify-content-between align-items-center">
                        <div class="text-muted">
                            Mostrando <span id="countMilitantes">{{ militantes|length }}</span> militantes
                        </div>
                        <div class="d-flex align-items-center gap-3">
                            <div class="d-flex align-items-center">
                                <span class="me-2">Mostrar</span>
                                <select class="form-select form-select-sm me-2" id="rowsPerPage" style="width: auto;">
                                    <option value="10">10</option>
                                    <option value="20" selected>20</option>
                                    <option value="50">50</option>
                                    <option value="100">100</option>
                                </select>
                                <span>linhas</span>
                            </div>
                            <nav aria-label="Navegação de páginas">
                                <ul class="pagination mb-0">
                                    <li class="page-item disabled" id="prevPage">
                                        <a class="page-link" href="#"><i class="fas fa-chevron-left"></i></a>
                                    </li>
                                    <li class="page-item active"><a class="page-link" href="#">1</a></li>
                                    <li class="page-item"><a class="page-link" href="#">2</a></li>
                                    <li class="page-item"><a class="page-link" href="#">3</a></li>
                                    <li class="page-item" id="nextPage">
                                        <a class="page-link" href="#"><i class="fas fa-chevron-right"></i></a>
                                    </li>
                                </ul>
                            </nav>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Modais -->
{% include 'modals/militante_novo.html' %}
{% include 'modals/militante_editar.html' %}
{% include 'modals/militante_excluir.html' %}
{% endblock %}

{% block scripts %}
    <!-- jQuery -->
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    
    <!-- jQuery Mask Plugin -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.16/jquery.mask.min.js"></script>
    
    <!-- Nosso script -->
    <script src="{{ url_for('static', filename='js/militantes.js') }}"></script>
{% endblock %}

{% block extra_css %}
<style>
/* Estilo para o botão Novo Militante */
.btn-primary {
    background-color: var(--bs-danger);
    border-color: var(--bs-danger);
}

.btn-primary:hover,
.btn-primary:focus,
.btn-primary:active {
    background-color: var(--bs-danger-dark, #b02a37) !important;
    border-color: var(--bs-danger-dark, #b02a37) !important;
}

/* Estilo para os switches */
.form-check-input {
    background-color: #fff;
    border-color: rgba(220, 53, 69, 0.5);
}

.form-check-input:checked {
    background-color: var(--bs-danger);
    border-color: var(--bs-danger);
}

.form-check-input:focus {
    border-color: var(--bs-danger);
    box-shadow: 0 0 0 0.25rem rgba(220, 53, 69, 0.25);
}

.form-switch .form-check-input {
    background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='-4 -4 8 8'%3e%3ccircle r='3' fill='rgba%28220, 53, 69, 0.85%29'/%3e%3c/svg%3e");
    background-position: left center;
    border-radius: 2em;
    transition: background-position .15s ease-in-out;
}

.form-switch .form-check-input:checked {
    background-position: right center;
    background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='-4 -4 8 8'%3e%3ccircle r='3' fill='%23fff'/%3e%3c/svg%3e");
}

.form-check {
    min-height: 1.5rem;
    padding-left: 2.8em;
    margin-bottom: 0.5rem;
}

.form-check-label {
    cursor: pointer;
    user-select: none;
}

/* Estilo para o backdrop com blur em todos os modais */
.modal-backdrop.show {
    backdrop-filter: blur(8px);
    background-color: rgba(0, 0, 0, 0.7);
}

/* Estilo para botões com largura fixa */
.btn-fixed-width {
    min-width: 120px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0.375rem 0.75rem;
    text-align: center;
    height: 38px;  /* Altura padrão do Bootstrap para btn */
    line-height: 1.5;
    vertical-align: middle;
}

.btn-fixed-width i {
    margin-right: 8px;
    font-size: 0.875rem;  /* 14px - tamanho padrão de ícone */
}

.dropdown-toggle::after {
    margin-left: 8px;
}

/* Estilo para o botão de fechar dos modais */
.btn-close {
    background-color: transparent;
    padding: 0.5rem;
    opacity: 0.8;
    transition: opacity 0.2s;
    filter: invert(1) grayscale(100%) brightness(200%);
}

.btn-close:hover {
    opacity: 1;
    background-color: transparent;
}

/* Estilo para colunas ordenáveis */
th[data-sort] {
    cursor: pointer;
    user-select: none;
}

th[data-sort] i {
    margin-left: 5px;
    color: #ccc;
}

th[data-sort].sort-asc i,
th[data-sort].sort-desc i {
    color: var(--primary-color);
}

/* Animação para linhas da tabela */
#militantesTable tbody tr {
    transition: all 0.3s ease;
}

#militantesTable tbody tr:hover {
    background-color: rgba(0,0,0,0.02);
    transform: translateX(5px);
}

/* Estilo para badges */
.badge {
    font-weight: 500;
    padding: 0.4em 0.6em;
    font-size: 0.75rem;
    margin-right: 0.3rem;
    min-width: 2em;
    text-align: center;
    border-radius: 4px;
    cursor: help;
}

.badge:last-child {
    margin-right: 0;
}

/* Cores personalizadas para badges */
.bg-purple { background-color: #6f42c1 !important; color: white !important; }
.bg-teal { background-color: #20c997 !important; color: white !important; }
.bg-orange { background-color: #fd7e14 !important; color: white !important; }
.bg-indigo { background-color: #6610f2 !important; color: white !important; }
.bg-pink { background-color: #d63384 !important; color: white !important; }

/* Cores do Bootstrap que vamos usar */
.badge.bg-primary { background-color: #0d6efd !important; }
.badge.bg-info { background-color: #0dcaf0 !important; }
.badge.bg-success { background-color: #198754 !important; }
.badge.bg-danger { background-color: #dc3545 !important; }
.badge.bg-dark { background-color: #212529 !important; }

/* Tooltip personalizado */
.tooltip {
    font-size: 0.875rem;
}

.tooltip .tooltip-inner {
    padding: 0.5rem 0.75rem;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

/* Estilo para botões de ação */
.btn-group .btn {
    padding: 0.25rem 0.5rem;
}

.btn-group .btn i {
    width: 16px;
    text-align: center;
}

/* Estilo para modais */
.modal-content {
    border: none;
    border-radius: 12px;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
}

.modal-header {
    background: linear-gradient(to right, var(--bs-gray-dark), var(--bs-gray));
    color: white;
    border-radius: 12px 12px 0 0;
    border-bottom: none;
    padding: 1rem;
}

.modal-body {
    padding: 1.5rem;
}

.modal-footer {
    border-top: 1px solid #eee;
    padding: 1rem;
}

/* Responsividade */
@media (max-width: 768px) {
    .btn-group {
        display: flex;
        margin-top: 1rem;
    }
    
    .btn-group .btn {
        flex: 1;
    }
    
    .modal-footer {
        justify-content: center;
    }
    
    .modal-footer .btn {
        min-width: 120px;
    }
}

/* Estilos personalizados para o Bootstrap Datepicker */
.datepicker {
    padding: 4px;
    border-radius: 4px;
    border: 1px solid #dee2e6;
    box-shadow: 0 2px 4px rgba(0,0,0,0.05);
    font-size: 0.875rem;
    background-color: white !important;
    color: #212529 !important;
}

.datepicker table {
    background-color: white !important;
}

.datepicker table tr td,
.datepicker table tr th {
    text-align: center;
    width: 30px;
    height: 30px;
    border-radius: 2px;
    color: #212529 !important;
    background-color: white !important;
}

.datepicker table tr td.day:hover,
.datepicker table tr td.focused {
    background: #f8f9fa !important;
    color: #212529 !important;
}

.datepicker table tr td.active,
.datepicker table tr td.active:hover {
    background-color: var(--bs-primary) !important;
    color: white !important;
}

.datepicker table tr td.today {
    background-color: #e9ecef !important;
    color: #212529 !important;
}

.datepicker .datepicker-switch,
.datepicker .prev,
.datepicker .next {
    font-weight: normal;
    padding: 4px;
    color: #212529 !important;
    background-color: white !important;
}

.datepicker .dow {
    font-weight: normal;
    padding: 4px;
    color: #212529 !important;
    background-color: white !important;
}

.datepicker-dropdown:after {
    border-bottom-color: white !important;
}

.datepicker-dropdown.datepicker-orient-top:after {
    border-top-color: white !important;
}

/* Estilo para os campos de data */
.datepicker-input {
    background-color: white !important;
    color: #212529 !important;
    cursor: pointer;
}

.datepicker-clear-btn {
    color: #6c757d !important;
    background-color: white !important;
    padding: 5px 10px;
    font-size: 12px;
    border-radius: 4px;
}

.datepicker-clear-btn:hover {
    background-color: #f8f9fa !important;
    color: #495057 !important;
}
</style>
{% endblock %}