
  @if (Auth::user()->role_id >= 5)
    <div class="container-fluid" style="height:100%">
    <div>
  @else
    <div class="container" style="margin:0 1em">
  @endif

    @php
        $currentField = request('field', 'total_amount');
        $currentSort  = request('sort', 'desc');
        $nextSort     = $currentSort === 'asc' ? 'desc' : 'asc';
        $sortUrl      = fn($f) => url('payments') . '?' . http_build_query(array_filter(['date_from' => $dateFrom, 'date_to' => $dateTo, 'field' => $f, 'sort' => $currentField === $f ? $nextSort : 'desc']));
    @endphp

    @if(session('warning'))
        <div class="alert alert-warning">{{ session('warning') }}</div>
    @endif

    <div class="row align-items-center mb-2">
        <div class="col-sm-4">
          <h3 class="mb-0">Pagos a Proveedores</h3>
        </div>
        <div class="col-sm-8">
          <div class="d-flex justify-content-end align-items-center flex-wrap" style="gap:.5rem">
            {!! Form::open(['method' => 'GET', 'url' => 'payments', 'class' => 'd-flex align-items-center', 'style' => 'gap:.4rem']) !!}
            <label class="mb-0 mr-1">Filtrar</label>
            <input type="date" class="form-control form-control-sm" name="date_from" value="{{ $dateFrom }}">
            <input type="date" class="form-control form-control-sm" name="date_to" value="{{ $dateTo }}">
            <button type="submit" class="btn btn-sm btn-primary">
                <i class="fa fa-filter"></i>
            </button>
            <a href="{{ url('payments') }}" class="btn btn-sm btn-default">
                <i class="fa fa-times"></i>
            </a>
            {!! Form::close() !!}

            <form method="POST" action="{{ url('payments/generate') }}" id="generateForm">
                @csrf
                <input type="hidden" name="date_from" value="{{ $dateFrom }}">
                <input type="hidden" name="date_to" value="{{ $dateTo }}">
                <button type="button" class="btn btn-sm btn-success" onclick="confirmGenerate()">
                    <i class="fa fa-file-excel-o"></i> Generar Pagos
                </button>
            </form>
          </div>
        </div>
    </div>

    <script>
    function confirmGenerate() {
        var total = {{ $payments->total() }};
        if (total === 0) {
            alert('No hay contratos pendientes de pago.');
            return;
        }
        if (confirm('Se generará un archivo Excel con ' + total + ' proveedor(es) y se marcarán sus contratos como pagados. ¿Continuar?')) {
            document.getElementById('generateForm').submit();
        }
    }
    </script>

    <table class="table">
        <thead>
        <tr>
            <th style="vertical-align: middle"><a href="{{ $sortUrl('company_name') }}">Proveedor</a></th>
            <th style="vertical-align: middle"><a href="{{ $sortUrl('email') }}">Email</a></th>
            <th style="vertical-align: middle"><a href="{{ $sortUrl('rfc_curp') }}">RFC / CURP</a></th>
            <th style="vertical-align: middle"><a href="{{ $sortUrl('clabe') }}">CLABE</a></th>
            <th style="vertical-align: middle"><a href="{{ $sortUrl('bank') }}">Banco</a></th>
            <th style="vertical-align: middle"><a href="{{ $sortUrl('contract_count') }}">Contratos</a></th>
            <th style="vertical-align: middle"><a href="{{ $sortUrl('total_amount') }}">Monto Total</a></th>
            <th style="vertical-align: middle"><a href="{{ $sortUrl('total_revenue') }}">Utilidad</a></th>
            <th style="vertical-align: middle"><a href="{{ $sortUrl('total_iva') }}">Ret. IVA</a></th>
            <th style="vertical-align: middle"><a href="{{ $sortUrl('total_isr') }}">Ret. ISR</a></th>
            <th style="vertical-align: middle"><a href="{{ $sortUrl('total_fee') }}">Ret. JobHero</a></th>
        </tr>
        </thead>
        <tbody>
        @foreach ($payments as $row)
        <tr>
            <th>{{ optional($row->suppliers)->company_name ?? '—' }}</th>
            <td>{{ optional(optional($row->suppliers)->user)->email ?? '—' }}</td>
            <td>{{ optional($row->suppliers)->RFC ?: optional($row->suppliers)->CURP ?? '—' }}</td>
            <td>{{ optional($row->suppliers)->clabe ?? '—' }}</td>
            <td>{{ optional(optional($row->suppliers)->banks)->name ?? '—' }}</td>
            <td>{{ $row->contract_count }}</td>
            <td>${{ number_format($row->total_amount, 2) }}</td>
            <td>${{ number_format($row->total_revenue, 2) }}</td>
            <td>${{ number_format($row->total_iva, 2) }}</td>
            <td>${{ number_format($row->total_isr, 2) }}</td>
            <td>${{ number_format($row->total_fee, 2) }}</td>
        </tr>
        @endforeach
        </tbody>
    </table>

    {{ $payments->links() }}
</div>
