@extends('backend.layouts.app')

@section('content')
    <style>
        .sorting_disabled {
            text-align: center;
        }
    </style>
    <div class="row page-titles">
        <div class="col-md-5 align-self-center">
            <h4 class="text-themecolor">Reports Manager</h4>
        </div>
        <div class="col-md-7 align-self-center text-right">
            <div class="d-flex justify-content-end align-items-center">
                <ol class="breadcrumb">
                    <li class="breadcrumb-item"><a href="{{ route('dashboard') }}">Dashboard</a></li>
                    <li class="breadcrumb-item">Reports Manager</li>
                    <li class="breadcrumb-item active">Unutilized Lead Report</li>
                </ol>
            </div>
        </div>
    </div>
    <div class="container-fluid">
        <div class="content-wrapper">
            @if (session('status'))
                <div class="alert alert-success alert-dismissible fade show" role="alert"><i
                        class="far fa-check-circle"></i>
                    {{ session('status') }}
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
            @endif
            @if (session('error'))
                <div class="alert alert-danger alert-dismissible fade show" role="alert"><i
                        class="far fa-check-circle"></i>
                    {{ session('error') }}
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
            @endif

            <section class="content">
                <div class="row">
                    <div class="col-12">
                        <div class="card">
                            <div class="card-body">
                                <h4>Unutilized Lead Report<p style="font-size: 10px; color:gray">This report show leads which are undistributed</p>
                                </h4>
                                <br>
                                <div class="table-responsive">
                                    <table id="report-table" class="table table-bordered table-hover" style="width:100%;">
                                        <thead>
                                            <tr class="text-center">
                                                @foreach ($columns as $column)
                                                    <th>{{ $column['title'] ?? $column['data'] }}</th>
                                                @endforeach
                                            </tr>
                                        </thead>
                                        <tbody>
                                            @php $sno = 1; @endphp
                                            @foreach ($data as $dt => $row)
                                                @if ($row['name'] !== 'Total')
                                                    <tr class="text-center">
                                                        @foreach ($columns as $index => $column)
                                                            @php
                                                                $key = $column['data'] ?? '';
                                                                $value = $row[$key] ?? '';
                                                            @endphp

                                                            @if ($index === 0)
                                                                <td>{{ $sno++ }}</td> {{-- ✅ Correct S.No. --}}
                                                            @else
                                                                <td @if ($index == 3) class="fw-bold" @endif
                                                                    @if ($index == 1 || $index == 2) class="text-left" @endif>
                                                                    {{ $value }}</td>
                                                            @endif
                                                        @endforeach
                                                    </tr>
                                                @endif
                                            @endforeach
                                        </tbody>

                                        <tfoot>
                                            @php
                                                $totalRow = collect($data)->firstWhere('name', 'Total');
                                            @endphp
                                            <tr class="text-center">
                                                @foreach ($columns as $index => $column)
                                                    @php
                                                        $key = $column['data'] ?? '';
                                                        $value = $totalRow[$key] ?? '';
                                                    @endphp
                                                    @if ($index === 0)
                                                        <td colspan="3" class="text-left"><strong>Total</strong></td>
                                                    @elseif ($index > 2)
                                                        <td><strong>{{ $value }}</strong></td>
                                                    @endif
                                                @endforeach
                                            </tr>
                                        </tfoot>
                                    </table>

                                </div>

                            </div>
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </div>
@endsection

@push('custom-script')
    <script>
        $(document).ready(function() {
            $('#report-table').DataTable({
                paging: false,
                ordering: true,
                searching: true,
                dom: 'Bfrtip',
                buttons: [{
                    extend: 'csv',
                    title: 'Credits Paid Report',
                    footer: true
                }]
            });
        });
    </script>
@endpush
