@extends('header_panel_generico')

@section('titulo_panel', 'OPERACIONES MASIVAS')
@section('styles_panel')
    <link rel="stylesheet" href="{{ asset('css/fidelitas/operaciones_masivas.css') }}">
@endsection

@section('contenido_panel')
 
<div class="mb-2">
    <a href="{{ route('operaciones-masivas.generacion.paso1') }}" class="btn btn-small btn-prod-text p-4">
        <i class="bi bi-arrow-left"></i> Atrás
    </a>
</div>
<div class="contenedor-blanco w-100">

  

    <h3 class="fw-700 text-gris-500 text-uppercase mb-3" style="font-size:0.92rem; letter-spacing:.03em;">
        GENERACIÓN MASIVA DE INFORMES
    </h3>

    <div class="om-paso-header mb-4">
        <span class="om-paso-label">ASIGNACIÓN DE INFORMES</span>
        <p class="text-gris-400 mb-0" style="font-size:0.83rem;">
            Paso 2 de 2: Seleccioná las contrapartes y definí el tipo de informe que deseás generar para cada una de ellas.
        </p>
    </div>

    {{-- Toolbar --}}
    <div class="d-flex justify-content-between align-items-center gap-2 flex-wrap mb-3">
        <div class="d-flex gap-2 align-items-center flex-wrap">
            <div class="om-search-input">
                <input type="text" id="input-buscar-contraparte" class="input-prod" placeholder="Buscar">
                <span class="om-search-icon"><i class="bi bi-search"></i></span>
            </div>
            <select id="filtro-pais" class="select-prod">
                <option value="">Seleccioná el país</option>
            </select>
        </div>
        <div class="d-flex gap-2 align-items-center flex-wrap">
            <div class="om-dropdown" id="dropdown-seleccionar">
                <button type="button" class="btn btn-small btn-prod-outline om-dropdown__toggle">
                    Seleccionar <i class="bi bi-chevron-down"></i>
                </button>
                <div class="om-dropdown__menu" style="display:none;">
                    <button type="button" data-cantidad="100">Primeros 100</button>
                    <button type="button" data-cantidad="200">Primeros 200</button>
                    <button type="button" data-cantidad="300">Primeros 300</button>
                </div>
            </div>
            <div class="om-dropdown" id="dropdown-tipo-informe">
                <button type="button" class="btn btn-small btn-prod-outline om-dropdown__toggle">
                    Tipo de informe <i class="bi bi-chevron-down"></i>
                </button>
                <div class="om-dropdown__menu" style="display:none;">
                    @foreach ($tiposInforme as $tipo)
                        <button type="button" data-tipo-id="{{ $tipo->id }}" data-tipo-nombre="{{ $tipo->nombre }}">
                            {{ $tipo->nombre }}
                        </button>
                    @endforeach
                    @if ($tiposInforme->isEmpty())
                        <button type="button" disabled style="color:var(--gris-350); cursor:default;">
                            Sin tipos disponibles
                        </button>
                    @endif
                </div>
            </div>
        </div>
    </div>

    {{-- Tabla --}}
    <div class="om-table-wrapper">
        <table class="om-table" id="tabla-asignacion">
            <thead>
                <tr>
                    <th><input type="checkbox" id="check-all-asignacion" class="form-check-input checkbox-prod"></th>
                    <th>Nombre / Razón social</th>
                    <th>CUIT</th>
                    <th>País</th>
                    <th>Tipo de informe asignado</th>
                </tr>
            </thead>
            <tbody id="tbody-asignacion">
                @foreach ($contrapartes as $cp)
                <tr data-cuit="{{ $cp->cuit ?? '' }}" data-pais="{{ $cp->pais ?? '' }}" data-contraparte="{{ $cp->contraparte ?? '' }}" data-gestion="{{ $cp->gestion ?? '' }}" data-id-padron="{{ $cp->id_padron ?? '' }}" data-id-team-default="{{ $cp->id_team_default ?? '' }}">
                    <td><input type="checkbox" class="form-check-input checkbox-prod check-contraparte" value="{{ $cp->id ?? '' }}"></td>
                    <td class="fw-600 text-gris-500">{{ $cp->nombre ?? '—' }}</td>
                    <td class="text-gris-400">{{ $cp->cuit ?? '—' }}</td>
                    <td class="text-gris-400">{{ $cp->pais ?? '—' }}</td>
                    <td class="td-tipo-informe text-gris-350" data-id="{{ $cp->id ?? '' }}">—</td>
                </tr>
                @endforeach
                @if ($contrapartes->isEmpty())
                <tr>
                    <td colspan="5" class="text-center text-gris-350 py-4" style="font-size:0.83rem;">
                        Sin contrapartes en este lote
                    </td>
                </tr>
                @endif
            </tbody>
        </table>
    </div>

    <div class="d-flex justify-content-between align-items-center mt-2 mb-4" style="font-size:0.8rem; color:var(--gris-400);">
        <div class="d-flex align-items-center gap-1">
            Mostrando
            <select id="om-per-page" class="select-prod mx-1" style="padding:.2rem .5rem; font-size:.8rem;">
                <option value="20">20</option>
                <option value="50">50</option>
                <option value="100">100</option>
            </select>
            <span class="om-per-page-info">— de {{ $contrapartes->count() }}</span> registros
        </div>
        <div id="om-paginacion-asignacion" class="om-pagination"></div>
    </div>

    <div class="d-flex justify-content-end gap-3 pt-3 border-top">
        <a href="{{ route('operaciones-masivas.generacion.paso1') }}" class="btn btn-small btn-neg-outline">
            <i class="bi bi-x-lg"></i> Cancelar
        </a>
        <button type="button" class="btn btn-small btn-prod-fill" id="btn-confirmar-asignacion">
            <i class="bi bi-check2"></i> Confirmar asignación
        </button>
    </div>

</div>
@endsection

@section('extras_panel')
{{-- Modal: Resumen informes --}}
<div class="om-modal" id="modal-generar-informes" style="display:none;">
    <div class="om-modal-content">
        <div class="d-flex justify-content-between align-items-center mb-3 pb-2 border-bottom">
            <h5 class="fw-700 text-gris-500 text-uppercase mb-0" style="font-size:0.88rem; letter-spacing:.04em;">
                GENERAR INFORMES
            </h5>
            <button type="button" class="btn btn-small btn-prod-text px-1" id="btn-cerrar-modal-generar">
                <i class="bi bi-x-lg"></i>
            </button>
        </div>
        <p class="fw-600 text-gris-500 mb-2" style="font-size:0.84rem;">Se generarán los siguientes informes:</p>
        <ul id="om-resumen-informes" class="text-gris-400 mb-3" style="font-size:0.83rem; padding-left:1.25rem; line-height:1.7;"></ul>
        <p class="fw-700 text-gris-500" style="font-size:0.84rem;">
            Total de contrapartes: <span id="om-total-contrapartes">0</span>
        </p>
        <div class="d-flex justify-content-end gap-3 mt-3 pt-3 border-top">
            <button type="button" class="btn btn-small btn-prod-outline" id="btn-cancelar-generar">Cancelar</button>
            <button type="button" class="btn btn-small btn-prod-fill" id="btn-ejecutar-generar">
                <i class="bi bi-check2"></i> Generar informes
            </button>
        </div>
    </div>
</div>

{{-- Modal: Confirmación final --}}
<div class="om-modal" id="modal-informes-creados" style="display:none;">
    <div class="om-modal-content" style="max-width:400px;">
        <div class="d-flex justify-content-between align-items-center mb-3 pb-2 border-bottom">
            <h5 class="fw-700 text-gris-500 text-uppercase mb-0" style="font-size:0.88rem; letter-spacing:.04em;">
                GENERAR INFORMES
            </h5>
            <button type="button" class="btn btn-small btn-prod-text px-1" id="btn-cerrar-modal-creados">
                <i class="bi bi-x-lg"></i>
            </button>
        </div>
        <p class="text-gris-400 my-3" style="font-size:0.84rem;">Los informes fueron creados correctamente.</p>
        <a href="{{ route('operaciones-masivas.index') }}" class="btn btn-small btn-prod-fill w-100 text-center">
            Aceptar
        </a>
    </div>
</div>
@endsection

@section('funcionesJS_panel')
<script>
    window.OM_ROUTES = {
        confirmarAsignacion: "{{ route('operaciones-masivas.generacion.confirmar-asignacion') }}",
        generarInformes:     "{{ route('operaciones-masivas.generacion.generar-informes') }}",
        descargarExcel:      "{{ route('operaciones-masivas.generacion.descargar-excel', ['lote' => $lote]) }}",
    };
    window.OM_LOTE = {{ $lote }};
</script>
<script src="{{ asset('js/operaciones_masivas/generacion.js') }}"></script>
@endsection
