@extends('template.app')

@section('meta')
@endsection

@section('title')
Dashboard
@endsection
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
@section('styles')
<style>
     body {
            background-color: #f8f9fa;
            font-family: Arial, sans-serif;
            height: 250%;
        }

        .card-header {
            font-weight: bold;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            table-layout: fixed;
        }

        th, td {
            padding: 10px;
            text-align: center;
            font-size: 14px;
            color: white;
            word-wrap: break-word;
        }

        thead {
            background-color: #2e367c;
        }

        .model-header {
            background-color: #6c757d;
            /*color: white;*/
            font-weight: bold;
        }
        
        .model-header.black-bg {
            background-color: #43568d;
            /*color: white;*/
        }
        
        .booking-header {
            background-color: #6c757d;
            /*color: white;*/
            font-weight: bold;
        }
        
        .booking-header.black-bg {
            background-color: #43568d;
            color: white !important;
        }

        .fuel-row {
            background-color: #6c757d;
            color: white !important;
        }
        
        .custom-total-color {
            background-color:#463862!important;
            color: white !important;
        }
        
        .custom-total-data-color{
            background-color:#5c5283!important;
            color: white !important;
        }
        
        .custom-total-data-color>.black_anchor_tag, .custom-total-fuel-color>.white_anchor_tag, .custom-total-color>.black_anchor_tag{
            color: white !important;
        }
        .custom-total-fuel-color{
            background-color:#9186bb!important;
            color: white !important;
        }

        .table-footer {
            background-color: #2e367c;
            font-weight: bold;
        }
        
        tbody tr:not(.model-header):not(.booking-header):not(.fuel-row):not(.table-footer) td {
            background-color: #ebebeb;
            color: black;
        }
        
        .table-responsive {
            overflow-x: auto;
        }
        
        .black_anchor_tag{
            /*color:white!important;*/
        }
        
        .white_anchor_tag{
            color:black!important;
        }
        
        .text-right {
            text-align: right;
        }
        
        .modal-xl {
            max-width: 80%!important;
        }
        
        .custom_height {
            height: 120px!important;
        }
        
</style>
@endsection

@section('content')

<div class="page-breadcrumb">
    <div class="row">
        <h4 class="page-title text-truncate text-dark font-weight-medium mb-1">Report Dashboard</h4>
        <div class="col-7 align-self-center">
            <div class="d-flex align-items-center">
                <nav aria-label="breadcrumb">
                    <ol class="breadcrumb m-0 p-0">
                        <li class="breadcrumb-item"><a href="{{ route('dashboard') }}" class="text-muted">Dashboard</a></li>
                        <li class="breadcrumb-item text-muted active" aria-current="page">MIS Report</li>
                    </ol>
                </nav>
            </div>
        </div>
        <div class="col-sm-8"></div>
    </div>
</div>

<div class="container-fluid">
    <div class="row">
        <!-- MIS Report Card -->
        @if(auth()->user()->can('physical-stock-report'))
        <div class="card shadow mb-4">
            <!--<div class="card-header bg-success text-white">
                <h6 class="mb-0">Location Wise Physical Stock Report</h6>
            </div>-->
            <div class="card-header bg-success text-white d-flex justify-content-between align-items-center">
    
                <h6 class="mb-0">Location Wise Physical Stock Report</h6>
            
                <form method="GET" action="" class="d-flex align-items-center" style="gap:8px;">
                    <input type="date" name="from_date" value="{{ request('from_date') }}" class="form-control form-control-sm">
                    
                    <span>to</span>
                    
                    <input type="date" name="to_date" value="{{ request('to_date') }}" class="form-control form-control-sm">
                    
                    <button type="submit" class="btn btn-success btn-sm">Filter</button>
            
                    <a href="{{ url()->current() }}" class="btn btn-dark btn-sm">Reset</a>
                </form>
            
            </div>
            <div class="card-body p-2">
                <div class="table-responsive">
                
                    <table class="table table-bordered w-100 mis-report-table"> 
                        <thead class="table-header">
                            <tr>
                                <th>Model</th>
                                @foreach ($branches as $branch)
                                    @php
                                        $branch_full = $branch->name ?? '';
                                    
                                        if (str_contains($branch_full, 'Rajkot')) {
                                            $b_name = 'Rajkot';
                                        } elseif (str_contains($branch_full, 'Rjt_1s')) {
                                            $b_name = 'Rajkot-1s';
                                        } elseif (str_contains($branch_full, 'Jamnagar')) {
                                            $b_name = 'Jamnagar';
                                        } elseif (str_contains($branch_full, 'Junagadh')) {
                                            $b_name = 'Junagadh';
                                        } elseif (str_contains($branch_full, 'Morbi')) {
                                            $b_name = 'Morbi';
                                        } elseif (str_contains($branch_full, '_M')) {
                                            $b_name = 'Mehsana';
                                        } elseif (str_contains($branch_full, '_K')) {
                                            $b_name = 'Kadi';
                                        } elseif (str_contains($branch_full, 'Porbandar')) {
                                            $b_name = 'Porbandar';
                                        } else {
                                            $b_name = $branch_full;
                                        }
                                    @endphp
                                    <th>{{ $b_name }}</th>
                                @endforeach
                                <th class="custom-total-color">Total</th>
                                <th>Transit</th>        {{-- moved Transit right after Total --}}
                                <th>Grand Total</th>    {{-- NEW Grand Total column --}}
                                <th>Allocated</th>
                                <th>Free Stock</th>
                                <th>PDI Hold</th>
                            </tr>
                        </thead>
                        <tbody>
                            @php
                                $grandInventoryIds = [
                                    'total' => [],
                                    'allocated' => [],
                                    'free' => [],
                                    'pdi_hold' => []
                                ];
                                $grandTransitIdsAll = [];
                            @endphp
                            
                            {{-- ================= MODEL ROWS ================= --}}
                            @foreach ($models as $model)
                            @php
                                $modelInventoryIds = ['total'=>[], 'allocated'=>[], 'free'=>[], 'pdi_hold'=>[]];
                            @endphp
                            
                            <tr class="model-header black-bg text-white">
                                <td>{{ $model->name }}</td>
                            
                                {{-- ===== Branch wise Model Count ===== --}}
                                @foreach ($branches as $branch)
                                    @php
                                        $branchInventoryIds = [];
                            
                                        foreach ($data[$model->id] ?? [] as $fuelData) {
                                            $statuses = $fuelData[$branch->id] ?? [];
                                            foreach ($statuses['inventory_ids'] ?? [] as $status => $ids) {
                                                if (in_array($status, ['allocated','allocatedDealer','active','pdi_hold'])) {
                                                    $branchInventoryIds = array_merge($branchInventoryIds, $ids);
                                                    $modelInventoryIds['total'] = array_merge($modelInventoryIds['total'], $ids);
                            
                                                    if (in_array($status,['allocated','allocatedDealer'])) {
                                                        $modelInventoryIds['allocated'] = array_merge($modelInventoryIds['allocated'],$ids);
                                                    } elseif ($status === 'active') {
                                                        $modelInventoryIds['free'] = array_merge($modelInventoryIds['free'],$ids);
                                                    } elseif ($status === 'pdi_hold') {
                                                        $modelInventoryIds['pdi_hold'] = array_merge($modelInventoryIds['pdi_hold'],$ids);
                                                    }
                                                }
                                            }
                                        }
                                    @endphp
                            
                                    <td>
                                        @if(count($branchInventoryIds))
                                            <a class="black_anchor_tag"
                                               onclick='showCarDetails(@json(array_unique($branchInventoryIds)))'>
                                                {{ count(array_unique($branchInventoryIds)) }}
                                            </a>
                                        @else
                                            0
                                        @endif
                                    </td>
                                @endforeach
                            
                                {{-- ===== Model Total ===== --}}
                                @php
                                    foreach ($modelInventoryIds as $key => $ids) {
                                        $grandInventoryIds[$key] = array_merge($grandInventoryIds[$key], $ids);
                                    }
                                    $modelTotal = count(array_unique($modelInventoryIds['total']));
                                @endphp
                            
                                <td class="custom-total-data-color">
                                    @if($modelTotal)
                                        <a class="black_anchor_tag"
                                           onclick='showCarDetails(@json(array_unique($modelInventoryIds["total"])))'>
                                            {{ $modelTotal }}
                                        </a>
                                    @else
                                        0
                                    @endif
                                </td>
                            
                                {{-- ===== Model Transit ===== --}}
                                @php
                                    $transitIds = [];
                            
                                    if (isset($transitData[$model->id])) {
                                        foreach ($branches as $branch) {
                                            foreach ($transitData[$model->id] as $fuelTransit) {
                                                if (!empty($fuelTransit[$branch->id]['inventory_ids'])) {
                                                    $transitIds = array_merge(
                                                        $transitIds,
                                                        $fuelTransit[$branch->id]['inventory_ids']
                                                    );
                                                }
                                            }
                                        }
                                    }
                            
                                    $transitIds = array_unique($transitIds);
                                    $grandTransitIdsAll = array_merge($grandTransitIdsAll, $transitIds);
                                @endphp
                            
                                <td>
                                    @if(count($transitIds))
                                        <a class="black_anchor_tag"
                                           onclick='showCarDetails([], @json($transitIds))'>
                                            {{ count($transitIds) }}
                                        </a>
                                    @else
                                        0
                                    @endif
                                </td>
                            
                                {{-- ===== Model Grand Total ===== --}}
                                @php
                                    $grandTotalIdsModel = array_unique(array_merge(
                                        $modelInventoryIds['total'],
                                        $transitIds
                                    ));
                                @endphp
                            
                                <td class="custom-total-data-color">
                                    @if(count($grandTotalIdsModel))
                                        <a class="black_anchor_tag"
                                           onclick='showCarDetails(
                                                @json(array_unique($modelInventoryIds["total"])),
                                                @json($transitIds)
                                           )'>
                                            {{ count($grandTotalIdsModel) }}
                                        </a>
                                    @else
                                        0
                                    @endif
                                </td>
                            
                                {{-- Allocated / Free / PDI --}}
                                <td>{{ count(array_unique($modelInventoryIds['allocated'])) }}</td>
                                <td>{{ count(array_unique($modelInventoryIds['free'])) }}</td>
                                <td>{{ count(array_unique($modelInventoryIds['pdi_hold'])) }}</td>
                            </tr>
                            
                            {{-- ================= FUEL ROWS ================= --}}
                            @foreach ($data[$model->id] ?? [] as $fuelTypeId => $branchData)
                            @php
                                $fuelInventoryIds = ['total'=>[], 'allocated'=>[], 'free'=>[], 'pdi_hold'=>[]];
                            @endphp
                            
                            <tr>
                                <td>{{ $fuelTypes[$fuelTypeId] ?? 'Unknown' }}</td>
                            
                                @foreach ($branches as $branch)
                                    @php
                                        $cellIds = [];
                                        $statuses = $branchData[$branch->id] ?? [];
                            
                                        foreach ($statuses['inventory_ids'] ?? [] as $status => $ids) {
                                            if (in_array($status,['allocated','allocatedDealer','active','pdi_hold'])) {
                                                $cellIds = array_merge($cellIds, $ids);
                                                $fuelInventoryIds['total'] = array_merge($fuelInventoryIds['total'],$ids);
                            
                                                if (in_array($status,['allocated','allocatedDealer'])) {
                                                    $fuelInventoryIds['allocated'] = array_merge($fuelInventoryIds['allocated'],$ids);
                                                } elseif ($status === 'active') {
                                                    $fuelInventoryIds['free'] = array_merge($fuelInventoryIds['free'],$ids);
                                                } elseif ($status === 'pdi_hold') {
                                                    $fuelInventoryIds['pdi_hold'] = array_merge($fuelInventoryIds['pdi_hold'],$ids);
                                                }
                                            }
                                        }
                                    @endphp
                            
                                    <td>
                                        @if(count($cellIds))
                                            <a class="white_anchor_tag"
                                               onclick='showCarDetails(@json(array_unique($cellIds)))'>
                                                {{ count(array_unique($cellIds)) }}
                                            </a>
                                        @else
                                            0
                                        @endif
                                    </td>
                                @endforeach
                            
                                {{-- Fuel Total --}}
                                <td>{{ count(array_unique($fuelInventoryIds['total'])) }}</td>
                            
                                {{-- Fuel Transit --}}
                                @php
                                    $fuelTransitIds = [];
                            
                                    if (isset($transitData[$model->id][$fuelTypeId])) {
                                        foreach ($branches as $branch) {
                                            $fuelTransitIds = array_merge(
                                                $fuelTransitIds,
                                                $transitData[$model->id][$fuelTypeId][$branch->id]['inventory_ids'] ?? []
                                            );
                                        }
                                    }
                                    $fuelTransitIds = array_unique($fuelTransitIds);
                                @endphp
                            
                                <td>{{ count($fuelTransitIds) }}</td>
                            
                                {{-- Fuel Grand --}}
                                <td>{{ count(array_unique(array_merge($fuelInventoryIds['total'],$fuelTransitIds))) }}</td>
                            
                                <td>{{ count(array_unique($fuelInventoryIds['allocated'])) }}</td>
                                <td>{{ count(array_unique($fuelInventoryIds['free'])) }}</td>
                                <td>{{ count(array_unique($fuelInventoryIds['pdi_hold'])) }}</td>
                            </tr>
                            @endforeach
                            @endforeach
                            
                            {{-- ================= FOOTER ================= --}}
                            <!--<tr class="table-footer">
                                <th>Total Stock</th>
                            
                                @foreach ($branches as $branch)
                                    @php
                                        $branchInventoryIds = [];
                            
                                        foreach ($models as $model) {
                                            foreach ($data[$model->id] ?? [] as $fuelData) {
                                                foreach ($fuelData[$branch->id]['inventory_ids'] ?? [] as $status => $ids) {
                                                    if (in_array($status,['allocated','allocatedDealer','active','pdi_hold'])) {
                                                        $branchInventoryIds = array_merge($branchInventoryIds,$ids);
                                                    }
                                                }
                                            }
                                        }
                                    @endphp
                            
                                    <th>{{ count(array_unique($branchInventoryIds)) }}</th>
                                @endforeach
                            
                                @php
                                    $inventoryUnique = array_unique($grandInventoryIds['total']);
                                    $transitUnique   = array_unique($grandTransitIdsAll);
                                    $footerGrandIds  = array_unique(array_merge($inventoryUnique,$transitUnique));
                                @endphp
                            
                                <th>{{ count($inventoryUnique) }}</th>
                                <th>{{ count($transitUnique) }}</th>
                                <th>{{ count($footerGrandIds) }}</th>
                                <th>{{ count(array_unique($grandInventoryIds['allocated'])) }}</th>
                                <th>{{ count(array_unique($grandInventoryIds['free'])) }}</th>
                                <th>{{ count(array_unique($grandInventoryIds['pdi_hold'])) }}</th>
                            </tr>-->
                            <tr class="table-footer">
                                <th>Total Stock</th>
                            
                                @foreach ($branches as $branch)
                                    @php
                                        $branchInventoryIds = [];
                            
                                        foreach ($models as $model) {
                                            foreach ($data[$model->id] ?? [] as $fuelData) {
                                                foreach ($fuelData[$branch->id]['inventory_ids'] ?? [] as $status => $ids) {
                                                    if (in_array($status,['allocated','allocatedDealer','active','pdi_hold'])) {
                                                        $branchInventoryIds = array_merge($branchInventoryIds,$ids);
                                                    }
                                                }
                                            }
                                        }
                            
                                        $branchInventoryIds = array_unique($branchInventoryIds);
                                    @endphp
                            
                                    <th>
                                        @if(count($branchInventoryIds))
                                            <a class="black_anchor_tag"
                                               onclick='showCarDetails(@json($branchInventoryIds))'>
                                                {{ count($branchInventoryIds) }}
                                            </a>
                                        @else
                                            0
                                        @endif
                                    </th>
                                @endforeach
                            
                                @php
                                    $inventoryUnique = array_unique($grandInventoryIds['total']);
                                    $transitUnique   = array_unique($grandTransitIdsAll);
                                    $footerGrandIds  = array_unique(array_merge($inventoryUnique,$transitUnique));
                                @endphp
                            
                                {{-- Total --}}
                                <th>
                                    @if(count($inventoryUnique))
                                        <a class="black_anchor_tag"
                                           onclick='showCarDetails(@json($inventoryUnique))'>
                                            {{ count($inventoryUnique) }}
                                        </a>
                                    @else
                                        0
                                    @endif
                                </th>
                            
                                {{-- Transit --}}
                                <th>
                                    @if(count($transitUnique))
                                        <a class="black_anchor_tag"
                                           onclick='showCarDetails([], @json($transitUnique))'>
                                            {{ count($transitUnique) }}
                                        </a>
                                    @else
                                        0
                                    @endif
                                </th>
                            
                                {{-- Grand Total --}}
                                <th>
                                    @if(count($footerGrandIds))
                                        <a class="black_anchor_tag"
                                           onclick='showCarDetails(@json($inventoryUnique), @json($transitUnique))'>
                                            {{ count($footerGrandIds) }}
                                        </a>
                                    @else
                                        0
                                    @endif
                                </th>
                            
                                {{-- Allocated --}}
                                <th>
                                    <a class="black_anchor_tag"
                                       onclick='showCarDetails(@json(array_unique($grandInventoryIds["allocated"])))'>
                                        {{ count(array_unique($grandInventoryIds['allocated'])) }}
                                    </a>
                                </th>
                            
                                {{-- Free --}}
                                <th>
                                    <a class="black_anchor_tag"
                                       onclick='showCarDetails(@json(array_unique($grandInventoryIds["free"])))'>
                                        {{ count(array_unique($grandInventoryIds['free'])) }}
                                    </a>
                                </th>
                            
                                {{-- PDI Hold --}}
                                <th>
                                    <a class="black_anchor_tag"
                                       onclick='showCarDetails(@json(array_unique($grandInventoryIds["pdi_hold"])))'>
                                        {{ count(array_unique($grandInventoryIds['pdi_hold'])) }}
                                    </a>
                                </th>
                            
                            </tr>

                        </tbody>
                    </table>
    
                </div>
            </div>
        </div>
        @endif


        <!-- Customer Booking Report Card -->
        @if(auth()->user()->can('customer-booking-advance-report'))
        <div class="card shadow">
            <!--<div class="card-header bg-success text-white mb-2">-->
            <!--    <h6 class="mb-0">Customer Booking & Advance Amount Report</h6>-->
            <!--</div>-->
            <div class="card-header bg-success text-white d-flex justify-content-between align-items-center">
    
                <h6 class="mb-0">Customer Booking & Advance Amount Report</h6>
            
                <form method="GET" action="" class="d-flex align-items-center" style="gap:8px;">
                    <input type="date" name="from_date" value="{{ request('from_date') }}" class="form-control form-control-sm">
                    
                    <span>to</span>
                    
                    <input type="date" name="to_date" value="{{ request('to_date') }}" class="form-control form-control-sm">
                    
                    <button type="submit" class="btn btn-success btn-sm">Filter</button>
            
                    <a href="{{ url()->current() }}" class="btn btn-dark btn-sm">Reset</a>
                </form>
            
            </div>
            <div class="card-body p-2">
                <!-- Booking Report & Customer Advance -->
                <div class="row mb-4">
                    <div class="col-md-6 mb-3 mb-md-0">
                        <div class="table-responsive">  
                            <table class="table table-bordered w-100">
                                <thead class="table-header">
                                    <tr>
                                        <th>Model</th>
                                        @foreach ($branches as $branch)
                                            @php
                                                $branch_full = $branch->name ?? ''; // or use $branch->branch_name if that's the column
                                                $b_name = '';
                                    
                                                if (str_contains($branch_full, 'Rajkot')) {
                                                    $b_name = 'Rajkot';
                                                } elseif (str_contains($branch_full, 'Rjt_1s')) {
                                                    $b_name = 'Rajkot-1s';
                                                } elseif (str_contains($branch_full, 'Jamnagar')) {
                                                    $b_name = 'Jamnagar';
                                                } elseif (str_contains($branch_full, 'Junagadh')) {
                                                    $b_name = 'Junagadh';
                                                } elseif (str_contains($branch_full, 'Morbi')) {
                                                    $b_name = 'Morbi';
                                                } elseif (str_contains($branch_full, '_M')) {
                                                    $b_name = 'Mehsana';
                                                } elseif (str_contains($branch_full, '_K')) {
                                                    $b_name = 'Kadi';
                                                } elseif (str_contains($branch_full, 'Porbandar')) { 
                                                    $b_name = 'Porbandar';
                                                } else {
                                                    $b_name = $branch_full; // fallback to full name if no match
                                                }
                                            @endphp
                                            <th>{{ $b_name }}</th>
                                        @endforeach
                                        <th class="custom-total-color">Total</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @php
                                        $grandObfIds = [];
                                        $totalsPerBranchObfIds = [];
                                    @endphp
                                    
                                    @foreach ($booking_fuel_type_data as $fuelType => $records)
                                        <tr data-model="{{ $fuelType }}" class="table-footer model-header black-bg text-white">
                                            <td>{{ $fuelType }}</td>
                                    
                                            @php
                                                $rowTotal = 0;
                                                $rowObfIds = [];
                                            @endphp
                                    
                                            @foreach ($branches as $branch)
                                                @php
                                                    $record = collect($records)->firstWhere('branch_id', $branch->id);
                                                    $branchCount = $record->total ?? 0;
                                    
                                                    // Get obf_ids and convert to array if it's a string
                                                    $obfIds = $record->obf_ids ?? [];
                                                    if (!is_array($obfIds)) {
                                                        $obfIds = is_string($obfIds) ? explode(',', $obfIds) : [];
                                                    }
                                    
                                                    $rowTotal += $branchCount;
                                                    $rowObfIds = array_merge($rowObfIds, $obfIds);
                                    
                                                    // For column total later
                                                    $totalsPerBranchObfIds[$branch->id] = array_merge(
                                                        $totalsPerBranchObfIds[$branch->id] ?? [],
                                                        $obfIds
                                                    );
                                    
                                                    // For grand total
                                                    $grandObfIds = array_merge($grandObfIds, $obfIds);
                                                @endphp
                                                <td>
                                                    @if ($branchCount > 0)
                                                        <a href="#"
                                                           class="open-booking-modal black_anchor_tag"
                                                           data-obf-ids='@json($obfIds)'
                                                           data-branch-id="{{ $branch->id }}"
                                                           data-fuel-type="{{ $fuelType }}">
                                                            {{ $branchCount }}
                                                        </a>
                                                    @else
                                                        0
                                                    @endif
                                                </td>
                                            @endforeach
                                    
                                            <td class="border custom-total-data-color">
                                                @php $uniqueRowObfIds = array_unique($rowObfIds); @endphp
                                                @if (count($uniqueRowObfIds) > 0)
                                                    <a href="#"
                                                       class="open-booking-modal black_anchor_tag"
                                                       data-obf-ids='@json($uniqueRowObfIds)'
                                                       data-branch-id="all"
                                                       data-fuel-type="{{ $fuelType }}">
                                                        {{ count($uniqueRowObfIds) }}
                                                    </a>
                                                @else
                                                    0
                                                @endif
                                            </td>
                                        </tr>
                                    @endforeach
                                    
                                    {{-- Total row (column totals) --}}
                                    <tr class="table-footer">
                                        <td>Total</td>
                                    
                                        @php $uniqueGrandObfIds = []; @endphp
                                    
                                        @foreach ($branches as $branch)
                                            @php
                                                $branchObfIds = $totalsPerBranchObfIds[$branch->id] ?? [];
                                                $uniqueBranchObfIds = array_unique($branchObfIds);
                                                $branchTotal = count($uniqueBranchObfIds);
                                                $uniqueGrandObfIds = array_merge($uniqueGrandObfIds, $uniqueBranchObfIds);
                                            @endphp
                                            <td>
                                                @if ($branchTotal > 0)
                                                    <a href="#"
                                                       class="open-booking-modal black_anchor_tag"
                                                       data-obf-ids='@json($uniqueBranchObfIds)'
                                                       data-branch-id="{{ $branch->id }}"
                                                       data-fuel-type="all">
                                                        {{ $branchTotal }}
                                                    </a>
                                                @else
                                                    0
                                                @endif
                                            </td>
                                        @endforeach
                                    
                                        @php
                                            $uniqueGrandObfIds = array_unique($uniqueGrandObfIds);
                                            $grandTotal = count($uniqueGrandObfIds);
                                        @endphp
                                        <td class="border custom-total-color">
                                            @if ($grandTotal > 0)
                                                <a href="#"
                                                   class="open-booking-modal black_anchor_tag"
                                                   data-obf-ids='@json($uniqueGrandObfIds)'
                                                   data-branch-id="all"
                                                   data-fuel-type="all">
                                                    {{ $grandTotal }}
                                                </a>
                                            @else
                                                0
                                            @endif
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="table-responsive">
                            <table class="table table-bordered w-100">
                                <thead class="table-header">
                                    <tr>
                                        <th>Customer Advance</th>
                                        @foreach ($branches as $branch)
                                            @php
                                                $branch_full = $branch->name ?? ''; // or use $branch->branch_name if that's the column
                                                $b_name = '';
                                    
                                                if (str_contains($branch_full, 'Rajkot')) {
                                                    $b_name = 'Rajkot';
                                                } elseif (str_contains($branch_full, 'Rjt_1s')) {
                                                    $b_name = 'Rajkot-1s';
                                                } elseif (str_contains($branch_full, 'Jamnagar')) {
                                                    $b_name = 'Jamnagar';
                                                } elseif (str_contains($branch_full, 'Junagadh')) {
                                                    $b_name = 'Junagadh';
                                                } elseif (str_contains($branch_full, 'Morbi')) {
                                                    $b_name = 'Morbi';
                                                } elseif (str_contains($branch_full, '_M')) {
                                                    $b_name = 'Mehsana';
                                                } elseif (str_contains($branch_full, '_K')) {
                                                    $b_name = 'Kadi';    
                                                } elseif (str_contains($branch_full, '_Porbandar')) {
                                                    $b_name = 'Porbandar';
                                                } else {
                                                    $b_name = $branch_full; // fallback to full name if no match
                                                }
                                            @endphp
                                            <th>{{ $b_name }}</th>
                                        @endforeach
                                        <th class="custom-total-color">Total</th>
                                    </tr>
                                </thead>
                                <!--<tbody>
                                    @php
                                        $grandTotal = 0;
                                        $grandTotalsPerBranch = array_fill_keys($branches->pluck('id')->toArray(), 0);
                                    @endphp
                                
                                    @foreach ($booking_fuel_type_amt_wise_data as $fuelType => $records)
                                        @php
                                            $rowTotal = 0;
                                            $rowObfIds = [];
                                        @endphp
                                        <tr data-model="{{ $fuelType }}" class="model-header black-bg text-white">
                                            <td>{{ $fuelType }}</td>
                                            @foreach ($branches as $branch)
                                                @php
                                                    $record = collect($records)->firstWhere('branch_id', $branch->id);
                                                    $amount = isset($record->total) ? round($record->total) : 0;
                                                    $obfIds = $record->obf_ids ?? '';
                                                    $obfIdsArray = is_string($obfIds) ? explode(',', $obfIds) : [];
                                
                                                    $rowTotal += $amount;
                                                    $grandTotalsPerBranch[$branch->id] += $amount;
                                                    $grandTotal += $amount;
                                
                                                    $rowObfIds = array_merge($rowObfIds, $obfIdsArray);
                                                @endphp
                                                <td>
                                                    @if ($amount > 0)
                                                        <a href="#"
                                                           class="open-booking-modal black_anchor_tag"
                                                           data-obf-ids='@json($obfIdsArray)'
                                                           data-branch-id="{{ $branch->id }}"
                                                           data-fuel-type="{{ $fuelType }}">
                                                            {{ number_format($amount) }}
                                                        </a>
                                                    @else
                                                        {{ number_format($amount) }}
                                                    @endif
                                                </td>
                                            @endforeach
                                            <td class="border custom-total-data-color">
                                                @if ($rowTotal > 0)
                                                    <a href="#"
                                                       class="open-booking-modal black_anchor_tag"
                                                       data-obf-ids='@json(array_values(array_filter($rowObfIds)))'
                                                       data-fuel-type="{{ $fuelType }}">
                                                        {{ number_format($rowTotal) }}
                                                    </a>
                                                @else
                                                    {{ number_format($rowTotal) }}
                                                @endif
                                            </td>
                                        </tr>
                                    @endforeach
                                
                                    {{-- Grand Total Row --}}
                                    <tr data-model="Total" class="table-footer">
                                        <td>Total</td>
                                        @php $grandObfIds = []; @endphp
                                        @foreach ($branches as $branch)
                                            @php
                                                $branchTotal = $grandTotalsPerBranch[$branch->id] ?? 0;
                                                $branchObfIds = collect($booking_fuel_type_amt_wise_data)
                                                    ->flatten()
                                                    ->filter(fn($item) => $item->branch_id == $branch->id)
                                                    ->pluck('obf_ids')
                                                    ->map(fn($ids) => explode(',', $ids))
                                                    ->flatten()
                                                    ->filter()
                                                    ->unique()
                                                    ->values()
                                                    ->all();
                                
                                                $grandObfIds = array_merge($grandObfIds, $branchObfIds);
                                            @endphp
                                            <td>
                                                @if ($branchTotal > 0)
                                                    <a href="#"
                                                       class="open-booking-modal black_anchor_tag"
                                                       data-obf-ids='@json($branchObfIds)'
                                                       data-branch-id="{{ $branch->id }}">
                                                        {{ number_format($branchTotal) }}
                                                    </a>
                                                @else
                                                    {{ number_format($branchTotal) }}
                                                @endif
                                            </td>
                                        @endforeach
                                        <td class="custom-total-color">
                                            @if ($grandTotal > 0)
                                                <a href="#"
                                                   class="open-booking-modal black_anchor_tag"
                                                   data-obf-ids='@json(array_values(array_filter($grandObfIds)))'>
                                                    {{ number_format($grandTotal) }}
                                                </a>
                                            @else
                                                {{ number_format($grandTotal) }}
                                            @endif
                                        </td>
                                    </tr>
                                </tbody>-->
                                <tbody>
                                    @php
                                        $grandTotal = 0;
                                        $grandTotalsPerBranch = array_fill_keys($branches->pluck('id')->toArray(), 0);
                                    @endphp
                                
                                    @foreach ($booking_fuel_type_amt_wise_data as $fuelType => $records)
                                        @php
                                            $rowTotal = 0;
                                            $rowObfIds = [];
                                        @endphp
                                        <tr data-model="{{ $fuelType }}" class="model-header black-bg text-white">
                                            <td>{{ $fuelType }}</td>
                                            @foreach ($branches as $branch)
                                                @php
                                                    $record = collect($records)->firstWhere('branch_id', $branch->id);
                                                    $amount = isset($record->total) ? floor($record->total / 1000) : 0;
                                                    $obfIds = $record->obf_ids ?? '';
                                                    $obfIdsArray = is_string($obfIds) ? explode(',', $obfIds) : [];
                                
                                                    $rowTotal += $amount;
                                                    $grandTotalsPerBranch[$branch->id] += $amount;
                                                    $grandTotal += $amount;
                                
                                                    $rowObfIds = array_merge($rowObfIds, $obfIdsArray);
                                                @endphp
                                                <td>
                                                    @if ($amount > 0)
                                                        <a href="#"
                                                           class="open-booking-modal black_anchor_tag"
                                                           data-obf-ids='@json($obfIdsArray)'
                                                           data-branch-id="{{ $branch->id }}"
                                                           data-fuel-type="{{ $fuelType }}">
                                                            {{ number_format($amount) }}
                                                        </a>
                                                    @else
                                                        {{ number_format($amount) }}
                                                    @endif
                                                </td>
                                            @endforeach
                                            <td class="border custom-total-data-color">
                                                @if ($rowTotal > 0)
                                                    <a href="#"
                                                       class="open-booking-modal black_anchor_tag"
                                                       data-obf-ids='@json(array_values(array_filter($rowObfIds)))'
                                                       data-fuel-type="{{ $fuelType }}">
                                                        {{ number_format($rowTotal) }}
                                                    </a>
                                                @else
                                                    {{ number_format($rowTotal) }}
                                                @endif
                                            </td>
                                        </tr>
                                    @endforeach
                                
                                    {{-- Grand Total Row --}}
                                    <tr data-model="Total" class="table-footer">
                                        <td>Total</td>
                                        @php $grandObfIds = []; @endphp
                                        @foreach ($branches as $branch)
                                            @php
                                                $branchTotal = $grandTotalsPerBranch[$branch->id] ?? 0;
                                                $branchObfIds = collect($booking_fuel_type_amt_wise_data)
                                                    ->flatten()
                                                    ->filter(fn($item) => $item->branch_id == $branch->id)
                                                    ->pluck('obf_ids')
                                                    ->map(fn($ids) => explode(',', $ids))
                                                    ->flatten()
                                                    ->filter()
                                                    ->unique()
                                                    ->values()
                                                    ->all();
                                
                                                $grandObfIds = array_merge($grandObfIds, $branchObfIds);
                                            @endphp
                                            <td>
                                                @if ($branchTotal > 0)
                                                    <a href="#"
                                                       class="open-booking-modal black_anchor_tag"
                                                       data-obf-ids='@json($branchObfIds)'
                                                       data-branch-id="{{ $branch->id }}">
                                                        {{ number_format($branchTotal) }}
                                                    </a>
                                                @else
                                                    {{ number_format($branchTotal) }}
                                                @endif
                                            </td>
                                        @endforeach
                                        <td class="custom-total-color">
                                            @if ($grandTotal > 0)
                                                <a href="#"
                                                   class="open-booking-modal black_anchor_tag"
                                                   data-obf-ids='@json(array_values(array_filter($grandObfIds)))'>
                                                    {{ number_format($grandTotal) }}
                                                </a>
                                            @else
                                                {{ number_format($grandTotal) }}
                                            @endif
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>

                <!-- Model Wise Booking & Amount Wise Booking -->
                <div class="row">
                    <div class="col-md-6 mb-3 mb-md-0">
                        <div class="table-responsive">
                            <table class="table table-bordered w-100 booking-report-table">
                                <thead class="table-header">
                                    <tr>
                                        <th>Model</th>
                                        @foreach ($branches as $branch)
                                            @php
                                                $branch_full = $branch->name ?? '';
                                                $b_name = '';
                            
                                                if (str_contains($branch_full, 'Rajkot')) {
                                                    $b_name = 'Rajkot';
                                                } elseif (str_contains($branch_full, 'Rjt_1s')) {
                                                    $b_name = 'Rajkot-1s';
                                                } elseif (str_contains($branch_full, 'Jamnagar')) {
                                                    $b_name = 'Jamnagar';
                                                } elseif (str_contains($branch_full, 'Junagadh')) {
                                                    $b_name = 'Junagadh';
                                                } elseif (str_contains($branch_full, 'Morbi')) {
                                                    $b_name = 'Morbi';
                                                } elseif (str_contains($branch_full, '_M')) {
                                                    $b_name = 'Mehsana';
                                                } elseif (str_contains($branch_full, '_K')) {
                                                    $b_name = 'Kadi';
                                                } elseif (str_contains($branch_full, '_Porbandar')) {
                                                    $b_name = 'Porbandar';
                                                } else {
                                                    $b_name = $branch_full;
                                                }
                                            @endphp
                                            <th>{{ $b_name }}</th>
                                        @endforeach
                                        <th class="custom-total-color">Total</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach ($modelbookingdata as $modelId => $fuelTypeData)
                                        @php
                                            $model = $models->firstWhere('id', $modelId);
                                            $modelName = $model ? $model->name : 'Unknown';
                                            $branchTotals = [];
                                            $modelTotal = 0;
                                        @endphp
                            
                                        {{-- Model Header Row --}}
                                        <tr class="booking-header black-bg" data-model="{{ $modelName }}">
                                            <td>{{ $modelName }}</td>
                                            @foreach ($branches as $branch)
                                                @php
                                                    $branchTotal = 0;
                                                    foreach ($fuelTypeData as $fuelData) {
                                                        $branchTotal += $fuelData[$branch->id]['total'] ?? 0;
                                                    }
                                                    $modelTotal += $branchTotal;
                                                    $branchTotals[$branch->id] = $branchTotal;
                                                @endphp
                                                <td>
                                                    @php
                                                        $obfIds = collect($fuelTypeData)->pluck("{$branch->id}.obf_ids")->flatten()->filter()->all();
                                                    @endphp
                                                    
                                                    @if ($branchTotal > 0 && count($obfIds))
                                                        <a href="#"
                                                           class="open-booking-modal black_anchor_tag"
                                                           data-obf-ids='@json($obfIds)'
                                                           data-branch-id="{{ $branch->id }}"
                                                           data-fuel-type="{{ $fuelTypeId ?? '' }}">
                                                            {{ $branchTotal }}
                                                        </a>
                                                    @else
                                                        {{ $branchTotal }}
                                                    @endif
                                                </td>
                                            @endforeach
                                            @php
                                                $modelObfIds = collect($fuelTypeData)
                                                    ->flatMap(function ($fuelData) {
                                                        return collect($fuelData)->pluck('obf_ids');
                                                    })->flatten()->filter()->all();
                                            @endphp
                                            <td class="border custom-total-data-color">
                                                @if ($modelTotal > 0 && count($modelObfIds))
                                                    <a href="#"
                                                       class="open-booking-modal black_anchor_tag"
                                                       data-obf-ids='@json($modelObfIds)'>
                                                        {{ $modelTotal }}
                                                    </a>
                                                @else
                                                    {{ $modelTotal }}
                                                @endif
                                            </td>
                                        </tr>
                            
                                        {{-- Fuel Type Rows --}}
                                        @foreach ($fuelTypeData as $fuelTypeId => $branchData)
                                            @php
                                                $rowTotal = 0;
                                            @endphp
                                            <tr data-model="{{ $modelName }}">
                                                <td>{{ $fuelTypes[$fuelTypeId] ?? 'Unknown' }}</td>
                                                @foreach ($branches as $branch)
                                                    @php
                                                        $total = $branchData[$branch->id]['total'] ?? 0;
                                                        $obf_ids = $branchData[$branch->id]['obf_ids'] ?? [];
                                                        $rowTotal += $total;
                                                    @endphp
                                                    <td>
                                                        @if ($total > 0 && count($obf_ids))
                                                            <a href="#" class="open-booking-modal white_anchor_tag" data-obf-ids='@json($obf_ids)'>
                                                                {{ $total }}
                                                            </a>
                                                        @else
                                                            {{ $total }}
                                                        @endif
                                                    </td>
                                                @endforeach
                                                <td class="custom-total-fuel-color">
                                                    @php
                                                        $rowObfIds = collect($branchData)->pluck('obf_ids')->flatten()->filter()->all();
                                                    @endphp
                                                
                                                    @if ($rowTotal > 0 && count($rowObfIds))
                                                        <a href="#" class="open-booking-modal white_anchor_tag"
                                                           data-obf-ids='@json($rowObfIds)'>
                                                            {{ $rowTotal }}
                                                        </a>
                                                    @else
                                                        {{ $rowTotal }}
                                                    @endif
                                                </td>
                                            </tr>
                                        @endforeach
                                    @endforeach
                                    
                                    {{-- Grand Total Row --}}
                                    <tr class="table-footer">
                                        <td>Total Stock</td>
                                        @php
                                            $grandTotal = 0;
                                            $grandObfIds = [];
                                        @endphp
                                        @foreach ($branches as $branch)
                                            @php
                                                $branchTotal = 0;
                                                $branchObfIds = [];
                                    
                                                foreach ($modelbookingdata as $fuelDataPerModel) {
                                                    foreach ($fuelDataPerModel as $branchSet) {
                                                        if (!empty($branchSet[$branch->id])) {
                                                            $branchTotal += $branchSet[$branch->id]['total'] ?? 0;
                                                            $branchObfIds = array_merge($branchObfIds, $branchSet[$branch->id]['obf_ids'] ?? []);
                                                        }
                                                    }
                                                }
                                    
                                                $grandTotal += $branchTotal;
                                                $grandObfIds = array_merge($grandObfIds, $branchObfIds);
                                            @endphp
                                            <td>
                                                @if ($branchTotal > 0 && count($branchObfIds))
                                                    <a href="#" class="open-booking-modal black_anchor_tag" data-obf-ids='@json($branchObfIds)'>
                                                        {{ $branchTotal }}
                                                    </a>
                                                @else
                                                    {{ $branchTotal }}
                                                @endif
                                            </td>
                                        @endforeach
                                        <td class="custom-total-color">
                                            @if ($grandTotal > 0 && count($grandObfIds))
                                                <a class="open-booking-modal black_anchor_tag" href="javascript:void(0);" data-obf-ids='@json($grandObfIds)'>
                                                    {{ $grandTotal }}
                                                </a>
                                            @else
                                                {{ $grandTotal }}
                                            @endif
                                        </td>
                                    </tr>

                                </tbody>
                            </table>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="table-responsive">
                            
                            
                           <table class="table table-bordered w-100 booking-report-table">
                                <thead class="table-header">
                                    <tr>
                                        <th>Amount Wise</th>
                                        @foreach ($branches as $branch)
                                            @php
                                                $branch_full = $branch->name ?? '';
                                                $b_name = '';
                            
                                                if (str_contains($branch_full, 'Rajkot')) {
                                                    $b_name = 'Rajkot';
                                                } elseif (str_contains($branch_full, 'Rjt_1s')) {
                                                    $b_name = 'Rajkot-1s';
                                                } elseif (str_contains($branch_full, 'Jamnagar')) {
                                                    $b_name = 'Jamnagar';
                                                } elseif (str_contains($branch_full, 'Junagadh')) {
                                                    $b_name = 'Junagadh';
                                                } elseif (str_contains($branch_full, 'Morbi')) {
                                                    $b_name = 'Morbi';
                                                } elseif (str_contains($branch_full, '_M')) {
                                                    $b_name = 'Mehsana';
                                                } elseif (str_contains($branch_full, '_K')) {
                                                    $b_name = 'Kadi';
                                                } elseif (str_contains($branch_full, '_Porbandar')) {
                                                    $b_name = 'Porbandar';
                                                } else {
                                                    $b_name = $branch_full;
                                                }
                                            @endphp
                                            <th>{{ $b_name }}</th>
                                        @endforeach
                                        <th class="custom-total-color">Total</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @php
                                        $grandTotal = 0;
                                        $grandObfIds = [];
                                    @endphp
                                
                                    @foreach ($modelFuelbookingdata as $modelId => $fuelTypesData)
                                        
                                        @php
                                            $model = $models->firstWhere('id', $modelId);
                                            $modelName = $model ? $model->name : 'Unknown';
                                            $modelTotal = 0;
                                        @endphp
                                
                                        {{-- Model Header Row --}}
                                        <tr class="booking-header black-bg" data-model="{{ $modelName }}">
                                            <td>{{ $modelName }}</td>
                                            @foreach ($branches as $branch)
                                                @php
                                                    $branchTotal = 0;
                                                    foreach ($fuelTypesData as $fuelData) {
                                                        $branchTotal += $fuelData[$branch->id]['total'] ?? 0;
                                                    }
                                                    $modelTotal += $branchTotal;
                                
                                                    $branchObfIds = collect($fuelTypesData)
                                                        ->pluck("{$branch->id}.obf_ids")
                                                        ->flatten()
                                                        ->filter()
                                                        ->all();
                                                @endphp
                                                <td>
                                                    @if ($branchTotal > 0)
                                                        <a href="#" class="open-booking-modal black_anchor_tag"
                                                           data-obf-ids='@json($branchObfIds)'
                                                           data-branch-id="{{ $branch->id }}"
                                                           data-model="{{ $modelName }}">
                                                            {{ number_format(floor($branchTotal / 1000)) }}
                                                        </a>
                                                    @else
                                                        {{ number_format(floor($branchTotal / 1000)) }}
                                                    @endif
                                                </td>
                                            @endforeach
                                            <td class="border custom-total-data-color">
                                                @php
                                                    $modelObfIds = collect($fuelTypesData)
                                                        ->flatMap(fn($fuelData) => collect($fuelData)->pluck('obf_ids'))
                                                        ->flatten()
                                                        ->filter()
                                                        ->all();
                                                @endphp
                                                @if ($modelTotal > 0)
                                                    <a href="#" class="open-booking-modal black_anchor_tag"
                                                       data-obf-ids='@json($modelObfIds)'>
                                                        {{ number_format(floor($modelTotal / 1000)) }}
                                                    </a>
                                                @else
                                                    {{ number_format(floor($modelTotal / 1000)) }}
                                                @endif
                                            </td>
                                        </tr>
                                
                                        {{-- Fuel Type Rows --}}
                                        @foreach ($fuelTypesData as $fuelTypeId => $branchData)
                                            @php
                                                $rowTotal = 0;
                                                $fuelTypeName = $fuelTypes[$fuelTypeId] ?? $fuelTypeId;
                                            @endphp
                                            <tr data-model="{{ $modelName }}">
                                                <td>{{ $fuelTypeName }}</td>
                                                @foreach ($branches as $branch)
                                                    @php
                                                        $total = $branchData[$branch->id]['total'] ?? 0;
                                                        $obf_ids = $branchData[$branch->id]['obf_ids'] ?? [];
                                                        $rowTotal += $total;
                                                    @endphp
                                                    <td>
                                                        @if ($total > 0)
                                                            <a href="#" class="open-booking-modal white_anchor_tag"
                                                               data-obf-ids='@json($obf_ids)'>
                                                                {{ number_format(floor($total / 1000)) }}
                                                            </a>
                                                        @else
                                                            {{ number_format(floor($total / 1000)) }}
                                                        @endif
                                                    </td>
                                                @endforeach
                                                <td class="custom-total-fuel-color">
                                                    @php
                                                        $rowObfIds = collect($branchData)->pluck('obf_ids')->flatten()->filter()->all();
                                                    @endphp
                                                    @if ($rowTotal > 0)
                                                        <a href="#" class="open-booking-modal white_anchor_tag"
                                                           data-obf-ids='@json($rowObfIds)'>
                                                            {{ number_format(floor($rowTotal / 1000)) }}
                                                        </a>
                                                    @else
                                                        {{ number_format(floor($rowTotal / 1000)) }}
                                                    @endif
                                                </td>
                                            </tr>
                                        @endforeach
                                    @endforeach
                                
                                    {{-- Grand Total Row --}}
                                    <tr class="table-footer">
                                        <td>Total Stock</td>
                                        @php
                                            $grandTotal = 0;
                                            $grandObfIds = [];
                                        @endphp
                                        @foreach ($branches as $branch)
                                            @php
                                                $branchTotal = 0;
                                                $branchObfIds = [];
                                
                                                foreach ($modelFuelbookingdata as $fuelDataPerModel) {
                                                    foreach ($fuelDataPerModel as $branchSet) {
                                                        if (!empty($branchSet[$branch->id])) {
                                                            $branchTotal += $branchSet[$branch->id]['total'] ?? 0;
                                
                                                            $branchObfIds = array_merge(
                                                                $branchObfIds,
                                                                $branchSet[$branch->id]['obf_ids'] ?? []
                                                            );
                                                        }
                                                    }
                                                }
                                
                                                $grandTotal += $branchTotal;
                                                $grandObfIds = array_merge($grandObfIds, $branchObfIds);
                                            @endphp
                                            <td>
                                                @if ($branchTotal > 0)
                                                    <a href="#" class="open-booking-modal black_anchor_tag"
                                                       data-obf-ids='@json(array_unique($branchObfIds))'>
                                                        {{ number_format(floor($branchTotal / 1000)) }}
                                                    </a>
                                                @else
                                                    {{ number_format(floor($branchTotal / 1000)) }}
                                                @endif
                                            </td>
                                        @endforeach
                                        <td class="custom-total-color">
                                            @php
                                                $grandObfIds = array_unique($grandObfIds);
                                            @endphp
                                            @if (count($grandObfIds) > 0)
                                                <a href="javascript:void(0);" class="open-booking-modal black_anchor_tag"
                                                   data-obf-ids='@json($grandObfIds)'>
                                                    {{ number_format(floor($grandTotal / 1000)) }}
                                                </a>
                                            @else
                                                {{ number_format(floor($grandTotal / 1000)) }}
                                            @endif
                                        </td>
                                    </tr>
                                
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        @endif
        
        
        <!-- Retail Report Card -->
        @if(auth()->user()->can('target-report'))
        <div class="card shadow">
            <div class="card-header bg-success text-white mb-2">
                <h6 class="mb-0">Target Report</h6>
            </div>
            <div class="card-body p-2">
                <!-- Row 2 -->
                <div class="row mb-4">
                    <div class="col-md-12 card-header bg-success text-white d-flex justify-content-between align-items-center">
    
                        <h6 class="mb-0">Retail Target</h6>
                    
                        <form method="GET" action="" class="d-flex align-items-center" style="gap:8px;">
                            <input type="date" name="taget_from_date" value="{{ request('taget_from_date') }}" class="form-control form-control-sm">
                            
                            <span>to</span>
                            
                            <input type="date" name="taget_to_date" value="{{ request('taget_to_date') }}" class="form-control form-control-sm">
                            
                            <button type="submit" class="btn btn-success btn-sm">Filter</button>
                    
                            <a href="{{ url()->current() }}" class="btn btn-dark btn-sm">Reset</a>
                        </form>
                    
                    </div>
                
                    <div class="col-md-12">
                        <div class="table-responsive">
                            <table class="table table-bordered w-100 booking-report-table">
                                <thead class="table-header">
                                    <tr>
                                        <th>Model</th>
                                        @foreach ($branches as $branch)
                                            @php
                                                $branch_full = $branch->name ?? '';
                                                $b_name = '';
                
                                                if (str_contains($branch_full, 'Rajkot')) {
                                                    $b_name = 'Rajkot';
                                                } elseif (str_contains($branch_full, 'Rjt_1s')) {
                                                    $b_name = 'Rajkot-1s';
                                                } elseif (str_contains($branch_full, 'Jamnagar')) {
                                                    $b_name = 'Jamnagar';
                                                } elseif (str_contains($branch_full, 'Junagadh')) {
                                                    $b_name = 'Junagadh';
                                                } elseif (str_contains($branch_full, 'Morbi')) {
                                                    $b_name = 'Morbi';
                                                } elseif (str_contains($branch_full, '_M')) {
                                                    $b_name = 'Mehsana';
                                                } elseif (str_contains($branch_full, '_K')) {
                                                    $b_name = 'Kadi';
                                                } elseif (str_contains($branch_full, '_Porbandar')) {
                                                    $b_name = 'Porbandar';
                                                } else {
                                                    $b_name = $branch_full;
                                                }
                                            @endphp
                                            <th>{{ $b_name }}</th>
                                        @endforeach
                                        <th class="custom-total-color">Total</th>
                                    </tr>
                                </thead>
                
                                <tbody>
                
                                    @foreach ($targetbookingdata as $modelId => $fuelTypeData)
                
                                        @php
                                            $model = $models->firstWhere('id', $modelId);
                                            $modelName = $model ? $model->name : 'Unknown';
                
                                            $branchTotals = [];
                                            $modelTotal = 0;
                                        @endphp
                
                                        {{-- Model Header Row --}}
                                        <tr class="booking-header black-bg" data-model="{{ $modelName }}">
                                            <td>{{ $modelName }}</td>
                
                                            @foreach ($branches as $branch)
                                                @php
                                                    $branchTotal = 0;
                
                                                    foreach ($fuelTypeData as $fuelData) {
                                                        $branchTotal += $fuelData[$branch->id]['total'] ?? 0;
                                                    }
                
                                                    $modelTotal += $branchTotal;
                                                    $branchTotals[$branch->id] = $branchTotal;
                
                                                    $obfIds = collect($fuelTypeData)
                                                        ->map(function ($fuelData) use ($branch) {
                                                            return $fuelData[$branch->id]['obf_ids'] ?? [];
                                                        })
                                                        ->flatten()
                                                        ->filter()
                                                        ->all();
                                                @endphp
                
                                                <td>
                                                    @if ($branchTotal > 0 && count($obfIds))
                                                        <a href="#"
                                                            class="open-target-modal black_anchor_tag"
                                                            data-obf-ids='@json($obfIds)'
                                                            data-branch-id="{{ $branch->id }}"
                                                            data-fuel-type="all">
                                                            {{ $branchTotal }}
                                                        </a>
                                                    @else
                                                        {{ $branchTotal }}
                                                    @endif
                                                </td>
                                            @endforeach
                
                                            @php
                                                $modelObfIds = collect($fuelTypeData)
                                                    ->flatMap(function ($fuelData) {
                                                        return collect($fuelData)->pluck('obf_ids');
                                                    })
                                                    ->flatten()
                                                    ->filter()
                                                    ->all();
                                            @endphp
                
                                            <td class="border custom-total-data-color">
                                                @if ($modelTotal > 0 && count($modelObfIds))
                                                    <a href="#"
                                                        class="open-target-modal black_anchor_tag"
                                                        data-obf-ids='@json($modelObfIds)'>
                                                        {{ $modelTotal }}
                                                    </a>
                                                @else
                                                    {{ $modelTotal }}
                                                @endif
                                            </td>
                                        </tr>
                
                                        {{-- Fuel Type Rows --}}
                                        @foreach ($fuelTypeData as $fuelTypeId => $branchData)
                
                                            @php $rowTotal = 0; @endphp
                
                                            <tr data-model="{{ $modelName }}">
                                                <td>{{ $fuelTypes[$fuelTypeId] ?? 'Unknown' }}</td>
                
                                                @foreach ($branches as $branch)
                                                    @php
                                                        $total = $branchData[$branch->id]['total'] ?? 0;
                                                        $obf_ids = $branchData[$branch->id]['obf_ids'] ?? [];
                                                        $rowTotal += $total;
                                                    @endphp
                
                                                    <td>
                                                        @if ($total > 0 && count($obf_ids))
                                                            <a href="#"
                                                                class="open-target-modal white_anchor_tag"
                                                                data-obf-ids='@json($obf_ids)'>
                                                                {{ $total }}
                                                            </a>
                                                        @else
                                                            {{ $total }}
                                                        @endif
                                                    </td>
                                                @endforeach
                
                                                @php
                                                    $rowObfIds = collect($branchData)
                                                        ->pluck('obf_ids')
                                                        ->flatten()
                                                        ->filter()
                                                        ->all();
                                                @endphp
                
                                                <td class="custom-total-fuel-color">
                                                    @if ($rowTotal > 0 && count($rowObfIds))
                                                        <a href="#"
                                                            class="open-target-modal white_anchor_tag"
                                                            data-obf-ids='@json($rowObfIds)'>
                                                            {{ $rowTotal }}
                                                        </a>
                                                    @else
                                                        {{ $rowTotal }}
                                                    @endif
                                                </td>
                                            </tr>
                
                                        @endforeach
                                    @endforeach
                
                                    {{-- Grand Total Row --}}
                                    <tr class="table-footer">
                                        <td>Total Stock</td>
                
                                        @php
                                            $grandTotal = 0;
                                            $grandObfIds = [];
                                        @endphp
                
                                        @foreach ($branches as $branch)
                                            @php
                                                $branchTotal = 0;
                                                $branchObfIds = [];
                
                                                foreach ($targetbookingdata as $fuelDataPerModel) {
                                                    foreach ($fuelDataPerModel as $branchSet) {
                                                        if (!empty($branchSet[$branch->id])) {
                                                            $branchTotal += $branchSet[$branch->id]['total'] ?? 0;
                                                            $branchObfIds = array_merge($branchObfIds, $branchSet[$branch->id]['obf_ids'] ?? []);
                                                        }
                                                    }
                                                }
                
                                                $grandTotal += $branchTotal;
                                                $grandObfIds = array_merge($grandObfIds, $branchObfIds);
                                            @endphp
                
                                            <td>
                                                @if ($branchTotal > 0 && count($branchObfIds))
                                                    <a href="#"
                                                        class="open-target-modal black_anchor_tag"
                                                        data-obf-ids='@json($branchObfIds)'>
                                                        {{ $branchTotal }}
                                                    </a>
                                                @else
                                                    {{ $branchTotal }}
                                                @endif
                                            </td>
                                        @endforeach
                
                                        <td class="custom-total-color">
                                            @if ($grandTotal > 0 && count($grandObfIds))
                                                <a href="javascript:void(0);"
                                                    class="open-target-modal black_anchor_tag"
                                                    data-obf-ids='@json($grandObfIds)'>
                                                    {{ $grandTotal }}
                                                </a>
                                            @else
                                                {{ $grandTotal }}
                                            @endif
                                        </td>
                                    </tr>
                
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            
                <!-- Row 3 -->
                <div class="row mb-4">
                    <div class="col-md-6">
                        <h6 class="text-left"><strong>Retail Achieve</strong></h6>
                    </div>
                    
                      <!-- Left Column -->
                    <div class="col-md-12 mb-3 mb-md-0">
                        <!-- Table 1 -->
                        <div class="table-responsive">
                            <table class="table table-bordered w-100 booking-report-table">
                                <thead class="table-header">
                                    <tr>
                                        <th>Model</th>
                                        @foreach ($branches as $branch)
                                            @php
                                                $branch_full = $branch->name ?? '';
                                                $b_name = '';
                            
                                                if (str_contains($branch_full, 'Rajkot')) {
                                                    $b_name = 'Rajkot';
                                                } elseif (str_contains($branch_full, 'Rjt_1s')) {
                                                    $b_name = 'Rajkot-1s';
                                                } elseif (str_contains($branch_full, 'Jamnagar')) {
                                                    $b_name = 'Jamnagar';
                                                } elseif (str_contains($branch_full, 'Junagadh')) {
                                                    $b_name = 'Junagadh';
                                                } elseif (str_contains($branch_full, 'Morbi')) {
                                                    $b_name = 'Morbi';
                                                } elseif (str_contains($branch_full, '_M')) {
                                                    $b_name = 'Mehsana';
                                                } elseif (str_contains($branch_full, '_K')) {
                                                    $b_name = 'Kadi';
                                                } elseif (str_contains($branch_full, '_Porbandar')) {
                                                    $b_name = 'Porbandar';
                                                } else {
                                                    $b_name = $branch_full;
                                                }
                                            @endphp
                                            <th>
                                                {{ $b_name }}
                                                <br>
                                                <small>(Retail / Target)</small>
                                            </th>
                                        @endforeach
                                        <th class="custom-total-color">Total</th>
                                        <th>%</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach ($targetbookingdata as $modelId => $fuelTypeData)
                                    
                                        @php
                                            $model = $models->firstWhere('id', $modelId);
                                            $modelName = $model ? $model->name : 'Unknown';
                                    
                                            $modelTargetTotal = 0;
                                            $modelAchievedTotal = 0;
                                        @endphp
                                    
                                        {{-- MODEL HEADER ROW --}}
                                        <tr class="booking-header black-bg" data-model="{{ $modelName }}">
                                            <td>{{ $modelName }}</td>
                                    
                                            @foreach ($branches as $branch)
                                    
                                                @php
                                                    $target = 0;
                                                    $achieved = 0;
                                                    $obfIds = [];
                                    
                                                    foreach ($fuelTypeData as $fuelTypeId => $fuelData) {
                                                        $target += $fuelData[$branch->id]['total'] ?? 0;
                                    
                                                        $achieved += $achievedData[$modelId][$fuelTypeId][$branch->id]['total'] ?? 0;
                                    
                                                        $obfIds = array_merge(
                                                            $obfIds,
                                                            $achievedData[$modelId][$fuelTypeId][$branch->id]['obf_ids'] ?? []
                                                        );
                                                    }
                                    
                                                    $modelTargetTotal += $target;
                                                    $modelAchievedTotal += $achieved;
                                    
                                                    $percentage = $target > 0 ? round(($achieved / $target) * 100) : 0;
                                                @endphp
                                    
                                                <td>
                                                    @if ($achieved > 0)
                                                        <a href="#"
                                                            class="open-booking-modal black_anchor_tag"
                                                            data-obf-ids='@json($obfIds)'>
                                                            {{ $achieved }} / {{ $target }}
                                                        </a>
                                                    @else
                                                        {{ $achieved }} / {{ $target }}
                                                    @endif
                                                    <br>
                                                    <small>({{ $percentage }}%)</small>
                                                </td>
                                    
                                            @endforeach
                                    
                                            @php
                                                $modelPercentage = $modelTargetTotal > 0 ? round(($modelAchievedTotal / $modelTargetTotal) * 100) : 0;
                                            @endphp
                                    
                                            <td class="custom-total-data-color">
                                                {{ $modelAchievedTotal }} / {{ $modelTargetTotal }}
                                                <br>
                                                <small>({{ $modelPercentage }}%)</small>
                                            </td>
                                    
                                            <td class="text-center font-weight-bold">
                                                {{ $modelPercentage }}%
                                            </td>
                                        </tr>
                                    
                                        {{-- FUEL TYPE ROW --}}
                                        @foreach ($fuelTypeData as $fuelTypeId => $branchData)
                                    
                                            @php
                                                $rowTarget = 0;
                                                $rowAchieved = 0;
                                            @endphp
                                    
                                            <tr data-model="{{ $modelName }}">
                                                <td>{{ $fuelTypes[$fuelTypeId] ?? 'Unknown' }}</td>
                                    
                                                @foreach ($branches as $branch)
                                    
                                                    @php
                                                        $target = $branchData[$branch->id]['total'] ?? 0;
                                                        $achieved = $achievedData[$modelId][$fuelTypeId][$branch->id]['total'] ?? 0;
                                    
                                                        $obf_ids = $achievedData[$modelId][$fuelTypeId][$branch->id]['obf_ids'] ?? [];
                                    
                                                        $rowTarget += $target;
                                                        $rowAchieved += $achieved;
                                    
                                                        $percentage = $target > 0 ? round(($achieved / $target) * 100) : 0;
                                                    @endphp
                                    
                                                    <td>
                                                        @if ($achieved > 0)
                                                            <a href="#" class="open-booking-modal white_anchor_tag"
                                                               data-obf-ids='@json($obf_ids)'>
                                                                {{ $achieved }} / {{ $target }}
                                                            </a>
                                                        @else
                                                            {{ $achieved }} / {{ $target }}
                                                        @endif
                                                        <br>
                                                        <small>({{ $percentage }}%)</small>
                                                    </td>
                                    
                                                @endforeach
                                    
                                                @php
                                                    $rowPercentage = $rowTarget > 0 ? round(($rowAchieved / $rowTarget) * 100) : 0;
                                                @endphp
                                    
                                                <td class="custom-total-fuel-color">
                                                    {{ $rowAchieved }} / {{ $rowTarget }}
                                                    <br>
                                                    <small>({{ $rowPercentage }}%)</small>
                                                </td>
                                    
                                                <td>{{ $rowPercentage }}%</td>
                                            </tr>
                                    
                                        @endforeach
                                    
                                    @endforeach
                                    
                                    
                                    {{-- GRAND TOTAL --}}
                                    <tr class="table-footer">
                                        <td>Total</td>
                                    
                                        @php
                                            $grandTarget = 0;
                                            $grandAchieved = 0;
                                        @endphp
                                    
                                        @foreach ($branches as $branch)
                                    
                                            @php
                                                $branchTarget = 0;
                                                $branchAchieved = 0;
                                    
                                                foreach ($targetbookingdata as $modelId => $fuelTypeData) {
                                                    foreach ($fuelTypeData as $fuelTypeId => $branchSet) {
                                    
                                                        $branchTarget += $branchSet[$branch->id]['total'] ?? 0;
                                    
                                                        $branchAchieved += $achievedData[$modelId][$fuelTypeId][$branch->id]['total'] ?? 0;
                                                    }
                                                }
                                    
                                                $grandTarget += $branchTarget;
                                                $grandAchieved += $branchAchieved;
                                    
                                                $percentage = $branchTarget > 0 ? round(($branchAchieved / $branchTarget) * 100) : 0;
                                            @endphp
                                    
                                            <td>
                                                {{ $branchAchieved }} / {{ $branchTarget }}
                                                <br>
                                                <small>({{ $percentage }}%)</small>
                                            </td>
                                    
                                        @endforeach
                                    
                                        @php
                                            $grandPercentage = $grandTarget > 0 ? round(($grandAchieved / $grandTarget) * 100) : 0;
                                        @endphp
                                    
                                        <td class="custom-total-color">
                                            {{ $grandAchieved }} / {{ $grandTarget }}
                                            <br>
                                            <small>({{ $grandPercentage }}%)</small>
                                        </td>
                                    
                                        <td>{{ $grandPercentage }}%</td>
                                    </tr>
                                    
                                    </tbody>
                            </table>
                        </div>
                      </div>
                      
                      <!-- Wholesale Archive Header -->
                    <div class="col-md-6 mt-3">
                        <h6 class="text-left"><strong>Retail Pending</strong></h6>
                    </div>
                
                    <!-- Right Column -->
                    <div class="col-md-12">
                        <div class="table-responsive">
                            <table class="table table-bordered w-100 booking-report-table">
                                <thead class="table-header">
                                    <tr>
                                        <th>Model</th>
                                        @foreach ($branches as $branch)
                                            @php
                                                $branch_full = $branch->name ?? '';
                                                $b_name = '';
                            
                                                if (str_contains($branch_full, 'Rajkot')) {
                                                    $b_name = 'Rajkot';
                                                } elseif (str_contains($branch_full, 'Rjt_1s')) {
                                                    $b_name = 'Rajkot-1s';
                                                } elseif (str_contains($branch_full, 'Jamnagar')) {
                                                    $b_name = 'Jamnagar';
                                                } elseif (str_contains($branch_full, 'Junagadh')) {
                                                    $b_name = 'Junagadh';
                                                } elseif (str_contains($branch_full, 'Morbi')) {
                                                    $b_name = 'Morbi';
                                                } elseif (str_contains($branch_full, '_M')) {
                                                    $b_name = 'Mehsana';
                                                } elseif (str_contains($branch_full, '_K')) {
                                                    $b_name = 'Kadi';
                                                } elseif (str_contains($branch_full, '_Porbandar')) {
                                                    $b_name = 'Porbandar';
                                                } else {
                                                    $b_name = $branch_full;
                                                }
                                            @endphp
                                            <th>{{ $b_name }}</th>
                                        @endforeach
                                        <th class="custom-total-color">Total</th>
                                        <th>%</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach ($targetbookingdata as $modelId => $fuelTypeData)
                                    
                                        @php
                                            $model = $models->firstWhere('id', $modelId);
                                            $modelName = $model ? $model->name : 'Unknown';
                                    
                                            $modelPendingTotal = 0;
                                            $modelTargetTotal = 0;
                                        @endphp
                                    
                                        {{-- MODEL ROW --}}
                                        <tr class="booking-header black-bg">
                                            <td>{{ $modelName }}</td>
                                    
                                            @foreach ($branches as $branch)
                                    
                                                @php
                                                    $target = 0;
                                                    $achieved = 0;
                                    
                                                    foreach ($fuelTypeData as $fuelTypeId => $fuelData) {
                                                        $target += $fuelData[$branch->id]['total'] ?? 0;
                                                        $achieved += $achievedData[$modelId][$fuelTypeId][$branch->id]['total'] ?? 0;
                                                    }
                                    
                                                    $pending = max($target - $achieved, 0);
                                    
                                                    $modelPendingTotal += $pending;
                                                    $modelTargetTotal += $target;
                                    
                                                    $percentage = $target > 0 ? round(($pending / $target) * 100) : 0;
                                                @endphp
                                    
                                                <td>
                                                    {{ $pending }}
                                                    <br>
                                                    <small>({{ $percentage }}%)</small>
                                                </td>
                                    
                                            @endforeach
                                    
                                            @php
                                                $modelPercentage = $modelTargetTotal > 0 ? round(($modelPendingTotal / $modelTargetTotal) * 100) : 0;
                                            @endphp
                                    
                                            <td class="custom-total-data-color">
                                                {{ $modelPendingTotal }}
                                                <br>
                                                <small>({{ $modelPercentage }}%)</small>
                                            </td>
                                    
                                            <td>{{ $modelPercentage }}%</td>
                                        </tr>
                                    
                                        {{-- FUEL ROW --}}
                                        @foreach ($fuelTypeData as $fuelTypeId => $branchData)
                                    
                                            @php
                                                $rowPending = 0;
                                                $rowTarget = 0;
                                            @endphp
                                    
                                            <tr>
                                                <td>{{ $fuelTypes[$fuelTypeId] ?? 'Unknown' }}</td>
                                    
                                                @foreach ($branches as $branch)
                                    
                                                    @php
                                                        $target = $branchData[$branch->id]['total'] ?? 0;
                                                        $achieved = $achievedData[$modelId][$fuelTypeId][$branch->id]['total'] ?? 0;
                                    
                                                        $pending = max($target - $achieved, 0);
                                    
                                                        $rowPending += $pending;
                                                        $rowTarget += $target;
                                    
                                                        $percentage = $target > 0 ? round(($pending / $target) * 100) : 0;
                                                    @endphp
                                    
                                                    <td>
                                                        {{ $pending }}
                                                        <br>
                                                        <small>({{ $percentage }}%)</small>
                                                    </td>
                                    
                                                @endforeach
                                    
                                                @php
                                                    $rowPercentage = $rowTarget > 0 ? round(($rowPending / $rowTarget) * 100) : 0;
                                                @endphp
                                    
                                                <td class="custom-total-fuel-color">
                                                    {{ $rowPending }}
                                                    <br>
                                                    <small>({{ $rowPercentage }}%)</small>
                                                </td>
                                    
                                                <td>{{ $rowPercentage }}%</td>
                                            </tr>
                                    
                                        @endforeach
                                    
                                    @endforeach
                                    
                                    
                                    {{-- GRAND TOTAL --}}
                                    <tr class="table-footer">
                                        <td>Total Pending</td>
                                    
                                        @php
                                            $grandPending = 0;
                                            $grandTarget = 0;
                                        @endphp
                                    
                                        @foreach ($branches as $branch)
                                    
                                            @php
                                                $branchPending = 0;
                                                $branchTarget = 0;
                                    
                                                foreach ($targetbookingdata as $modelId => $fuelTypeData) {
                                                    foreach ($fuelTypeData as $fuelTypeId => $branchSet) {
                                    
                                                        $target = $branchSet[$branch->id]['total'] ?? 0;
                                                        $achieved = $achievedData[$modelId][$fuelTypeId][$branch->id]['total'] ?? 0;
                                    
                                                        $branchPending += max($target - $achieved, 0);
                                                        $branchTarget += $target;
                                                    }
                                                }
                                    
                                                $grandPending += $branchPending;
                                                $grandTarget += $branchTarget;
                                    
                                                $percentage = $branchTarget > 0 ? round(($branchPending / $branchTarget) * 100) : 0;
                                            @endphp
                                    
                                            <td>
                                                {{ $branchPending }}
                                                <br>
                                                <small>({{ $percentage }}%)</small>
                                            </td>
                                    
                                        @endforeach
                                    
                                        @php
                                            $grandPercentage = $grandTarget > 0 ? round(($grandPending / $grandTarget) * 100) : 0;
                                        @endphp
                                    
                                        <td class="custom-total-color">
                                            {{ $grandPending }}
                                            <br>
                                            <small>({{ $grandPercentage }}%)</small>
                                        </td>
                                    
                                        <td>{{ $grandPercentage }}%</td>
                                    </tr>
                                    
                                    </tbody>
                            </table>
                        </div>
                    </div>
                </div>
                
                <!-- Row 4 -->
                <div class="row mb-4">
                    <div class="col-md-12 card-header bg-success text-white d-flex justify-content-between align-items-center">
                        <h6 class="mb-0">Wholesale Target</h6>
                    </div>
                
                    <div class="col-md-12">
                        <div class="table-responsive">
                            <table class="table table-bordered w-100 booking-report-table">
                                <thead class="table-header">
                                    <tr>
                                        <th>Model</th>
                                        @foreach ($branches as $branch)
                                            @php
                                                $branch_full = $branch->name ?? '';
                                                $b_name = '';
                
                                                if (str_contains($branch_full, 'Rajkot')) {
                                                    $b_name = 'Rajkot';
                                                } elseif (str_contains($branch_full, 'Rjt_1s')) {
                                                    $b_name = 'Rajkot-1s';
                                                } elseif (str_contains($branch_full, 'Jamnagar')) {
                                                    $b_name = 'Jamnagar';
                                                } elseif (str_contains($branch_full, 'Junagadh')) {
                                                    $b_name = 'Junagadh';
                                                } elseif (str_contains($branch_full, 'Morbi')) {
                                                    $b_name = 'Morbi';
                                                } elseif (str_contains($branch_full, '_M')) {
                                                    $b_name = 'Mehsana';
                                                } elseif (str_contains($branch_full, '_K')) {
                                                    $b_name = 'Kadi';
                                                } elseif (str_contains($branch_full, '_Porbandar')) {
                                                    $b_name = 'Porbandar';
                                                } else {
                                                    $b_name = $branch_full;
                                                }
                                            @endphp
                                            <th>{{ $b_name }}</th>
                                        @endforeach
                                        <th class="custom-total-color">Total</th>
                                    </tr>
                                </thead>
                
                                <tbody>
                
                                    @foreach ($targetwholesalebookingdata as $modelId => $fuelTypeData)
                
                                        @php
                                            $model = $models->firstWhere('id', $modelId);
                                            $modelName = $model ? $model->name : 'Unknown';
                
                                            $branchTotals = [];
                                            $modelTotal = 0;
                                        @endphp
                
                                        {{-- Model Header Row --}}
                                        <tr class="booking-header black-bg" data-model="{{ $modelName }}">
                                            <td>{{ $modelName }}</td>
                
                                            @foreach ($branches as $branch)
                                                @php
                                                    $branchTotal = 0;
                
                                                    foreach ($fuelTypeData as $fuelData) {
                                                        $branchTotal += $fuelData[$branch->id]['total'] ?? 0;
                                                    }
                
                                                    $modelTotal += $branchTotal;
                                                    $branchTotals[$branch->id] = $branchTotal;
                
                                                    $obfIds = collect($fuelTypeData)
                                                        ->map(function ($fuelData) use ($branch) {
                                                            return $fuelData[$branch->id]['obf_ids'] ?? [];
                                                        })
                                                        ->flatten()
                                                        ->filter()
                                                        ->all();
                                                @endphp
                
                                                <td>
                                                    @if ($branchTotal > 0 && count($obfIds))
                                                        <a href="#"
                                                            class="open-target-modal black_anchor_tag"
                                                            data-obf-ids='@json($obfIds)'
                                                            data-branch-id="{{ $branch->id }}"
                                                            data-fuel-type="all">
                                                            {{ $branchTotal }}
                                                        </a>
                                                    @else
                                                        {{ $branchTotal }}
                                                    @endif
                                                </td>
                                            @endforeach
                
                                            @php
                                                $modelObfIds = collect($fuelTypeData)
                                                    ->flatMap(function ($fuelData) {
                                                        return collect($fuelData)->pluck('obf_ids');
                                                    })
                                                    ->flatten()
                                                    ->filter()
                                                    ->all();
                                            @endphp
                
                                            <td class="border custom-total-data-color">
                                                @if ($modelTotal > 0 && count($modelObfIds))
                                                    <a href="#"
                                                        class="open-target-modal black_anchor_tag"
                                                        data-obf-ids='@json($modelObfIds)'>
                                                        {{ $modelTotal }}
                                                    </a>
                                                @else
                                                    {{ $modelTotal }}
                                                @endif
                                            </td>
                                        </tr>
                
                                        {{-- Fuel Type Rows --}}
                                        @foreach ($fuelTypeData as $fuelTypeId => $branchData)
                
                                            @php $rowTotal = 0; @endphp
                
                                            <tr data-model="{{ $modelName }}">
                                                <td>{{ $fuelTypes[$fuelTypeId] ?? 'Unknown' }}</td>
                
                                                @foreach ($branches as $branch)
                                                    @php
                                                        $total = $branchData[$branch->id]['total'] ?? 0;
                                                        $obf_ids = $branchData[$branch->id]['obf_ids'] ?? [];
                                                        $rowTotal += $total;
                                                    @endphp
                
                                                    <td>
                                                        @if ($total > 0 && count($obf_ids))
                                                            <a href="#"
                                                                class="open-target-modal white_anchor_tag"
                                                                data-obf-ids='@json($obf_ids)'>
                                                                {{ $total }}
                                                            </a>
                                                        @else
                                                            {{ $total }}
                                                        @endif
                                                    </td>
                                                @endforeach
                
                                                @php
                                                    $rowObfIds = collect($branchData)
                                                        ->pluck('obf_ids')
                                                        ->flatten()
                                                        ->filter()
                                                        ->all();
                                                @endphp
                
                                                <td class="custom-total-fuel-color">
                                                    @if ($rowTotal > 0 && count($rowObfIds))
                                                        <a href="#"
                                                            class="open-target-modal white_anchor_tag"
                                                            data-obf-ids='@json($rowObfIds)'>
                                                            {{ $rowTotal }}
                                                        </a>
                                                    @else
                                                        {{ $rowTotal }}
                                                    @endif
                                                </td>
                                            </tr>
                
                                        @endforeach
                                    @endforeach
                
                                    {{-- Grand Total Row --}}
                                    <tr class="table-footer">
                                        <td>Total Stock</td>
                
                                        @php
                                            $grandTotal = 0;
                                            $grandObfIds = [];
                                        @endphp
                
                                        @foreach ($branches as $branch)
                                            @php
                                                $branchTotal = 0;
                                                $branchObfIds = [];
                
                                                foreach ($targetwholesalebookingdata as $fuelDataPerModel) {
                                                    foreach ($fuelDataPerModel as $branchSet) {
                                                        if (!empty($branchSet[$branch->id])) {
                                                            $branchTotal += $branchSet[$branch->id]['total'] ?? 0;
                                                            $branchObfIds = array_merge($branchObfIds, $branchSet[$branch->id]['obf_ids'] ?? []);
                                                        }
                                                    }
                                                }
                
                                                $grandTotal += $branchTotal;
                                                $grandObfIds = array_merge($grandObfIds, $branchObfIds);
                                            @endphp
                
                                            <td>
                                                @if ($branchTotal > 0 && count($branchObfIds))
                                                    <a href="#"
                                                        class="open-target-modal black_anchor_tag"
                                                        data-obf-ids='@json($branchObfIds)'>
                                                        {{ $branchTotal }}
                                                    </a>
                                                @else
                                                    {{ $branchTotal }}
                                                @endif
                                            </td>
                                        @endforeach
                
                                        <td class="custom-total-color">
                                            @if ($grandTotal > 0 && count($grandObfIds))
                                                <a href="javascript:void(0);"
                                                    class="open-target-modal black_anchor_tag"
                                                    data-obf-ids='@json($grandObfIds)'>
                                                    {{ $grandTotal }}
                                                </a>
                                            @else
                                                {{ $grandTotal }}
                                            @endif
                                        </td>
                                    </tr>
                
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
                
                <!-- Row 5 -->
                <div class="row mb-4">
                    <div class="col-md-6">
                        <h6 class="text-left"><strong>Wholesale Achieve</strong></h6>
                    </div>
                    
                      <!-- Left Column -->
                    <div class="col-md-12 mb-3 mb-md-0">
                        <!-- Table 1 -->
                        <div class="table-responsive">
                            <table class="table table-bordered w-100 booking-report-table">
                                <thead class="table-header">
                                    <tr>
                                        <th>Model</th>
                                        @foreach ($branches as $branch)
                                            @php
                                                $branch_full = $branch->name ?? '';
                                                $b_name = '';
                            
                                                if (str_contains($branch_full, 'Rajkot')) {
                                                    $b_name = 'Rajkot';
                                                } elseif (str_contains($branch_full, 'Rjt_1s')) {
                                                    $b_name = 'Rajkot-1s';
                                                } elseif (str_contains($branch_full, 'Jamnagar')) {
                                                    $b_name = 'Jamnagar';
                                                } elseif (str_contains($branch_full, 'Junagadh')) {
                                                    $b_name = 'Junagadh';
                                                } elseif (str_contains($branch_full, 'Morbi')) {
                                                    $b_name = 'Morbi';
                                                } elseif (str_contains($branch_full, '_M')) {
                                                    $b_name = 'Mehsana';
                                                } elseif (str_contains($branch_full, '_K')) {
                                                    $b_name = 'Kadi';
                                                } elseif (str_contains($branch_full, '_Porbandar')) {
                                                    $b_name = 'Porbandar';
                                                } else {
                                                    $b_name = $branch_full;
                                                }
                                            @endphp
                                            <th>
                                                {{ $b_name }}
                                                <br>
                                                <small>(Retail / Target)</small>
                                            </th>
                                        @endforeach
                                        <th class="custom-total-color">Total</th>
                                        <th>%</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach ($targetwholesalebookingdata as $modelId => $fuelTypeData)
                                    
                                        @php
                                            $model = $models->firstWhere('id', $modelId);
                                            $modelName = $model ? $model->name : 'Unknown';
                                    
                                            $modelTargetTotal = 0;
                                            $modelAchievedTotal = 0;
                                        @endphp
                                    
                                        {{-- MODEL HEADER ROW --}}
                                        <tr class="booking-header black-bg" data-model="{{ $modelName }}">
                                            <td>{{ $modelName }}</td>
                                    
                                            @foreach ($branches as $branch)
                                    
                                                @php
                                                    $target = 0;
                                                    $achieved = 0;
                                                    $obfIds = [];
                                    
                                                    foreach ($fuelTypeData as $fuelTypeId => $fuelData) {
                                                        $target += $fuelData[$branch->id]['total'] ?? 0;
                                    
                                                        $achieved += $achievedWholesaleData[$modelId][$fuelTypeId][$branch->id]['total'] ?? 0;
                                    
                                                        $obfIds = array_merge(
                                                            $obfIds,
                                                            $achievedWholesaleData[$modelId][$fuelTypeId][$branch->id]['obf_ids'] ?? []
                                                        );
                                                    }
                                    
                                                    $modelTargetTotal += $target;
                                                    $modelAchievedTotal += $achieved;
                                    
                                                    $percentage = $target > 0 ? round(($achieved / $target) * 100) : 0;
                                                @endphp
                                    
                                                <td>
                                                    @if ($achieved > 0)
                                                        <a href="#"
                                                            class="open-booking-modal black_anchor_tag"
                                                            data-obf-ids='@json($obfIds)'>
                                                            {{ $achieved }} / {{ $target }}
                                                        </a>
                                                    @else
                                                        {{ $achieved }} / {{ $target }}
                                                    @endif
                                                    <br>
                                                    <small>({{ $percentage }}%)</small>
                                                </td>
                                    
                                            @endforeach
                                    
                                            @php
                                                $modelPercentage = $modelTargetTotal > 0 ? round(($modelAchievedTotal / $modelTargetTotal) * 100) : 0;
                                            @endphp
                                    
                                            <td class="custom-total-data-color">
                                                {{ $modelAchievedTotal }} / {{ $modelTargetTotal }}
                                                <br>
                                                <small>({{ $modelPercentage }}%)</small>
                                            </td>
                                    
                                            <td class="text-center font-weight-bold">
                                                {{ $modelPercentage }}%
                                            </td>
                                        </tr>
                                    
                                        {{-- FUEL TYPE ROW --}}
                                        @foreach ($fuelTypeData as $fuelTypeId => $branchData)
                                    
                                            @php
                                                $rowTarget = 0;
                                                $rowAchieved = 0;
                                            @endphp
                                    
                                            <tr data-model="{{ $modelName }}">
                                                <td>{{ $fuelTypes[$fuelTypeId] ?? 'Unknown' }}</td>
                                    
                                                @foreach ($branches as $branch)
                                    
                                                    @php
                                                        $target = $branchData[$branch->id]['total'] ?? 0;
                                                        $achieved = $achievedWholesaleData[$modelId][$fuelTypeId][$branch->id]['total'] ?? 0;
                                    
                                                        $obf_ids = $achievedWholesaleData[$modelId][$fuelTypeId][$branch->id]['obf_ids'] ?? [];
                                    
                                                        $rowTarget += $target;
                                                        $rowAchieved += $achieved;
                                    
                                                        $percentage = $target > 0 ? round(($achieved / $target) * 100) : 0;
                                                    @endphp
                                    
                                                    <td>
                                                        @if ($achieved > 0)
                                                            <a href="#" class="open-booking-modal white_anchor_tag"
                                                               data-obf-ids='@json($obf_ids)'>
                                                                {{ $achieved }} / {{ $target }}
                                                            </a>
                                                        @else
                                                            {{ $achieved }} / {{ $target }}
                                                        @endif
                                                        <br>
                                                        <small>({{ $percentage }}%)</small>
                                                    </td>
                                    
                                                @endforeach
                                    
                                                @php
                                                    $rowPercentage = $rowTarget > 0 ? round(($rowAchieved / $rowTarget) * 100) : 0;
                                                @endphp
                                    
                                                <td class="custom-total-fuel-color">
                                                    {{ $rowAchieved }} / {{ $rowTarget }}
                                                    <br>
                                                    <small>({{ $rowPercentage }}%)</small>
                                                </td>
                                    
                                                <td>{{ $rowPercentage }}%</td>
                                            </tr>
                                    
                                        @endforeach
                                    
                                    @endforeach
                                    
                                    
                                    {{-- GRAND TOTAL --}}
                                    <tr class="table-footer">
                                        <td>Total</td>
                                    
                                        @php
                                            $grandTarget = 0;
                                            $grandAchieved = 0;
                                        @endphp
                                    
                                        @foreach ($branches as $branch)
                                    
                                            @php
                                                $branchTarget = 0;
                                                $branchAchieved = 0;
                                    
                                                foreach ($targetwholesalebookingdata as $modelId => $fuelTypeData) {
                                                    foreach ($fuelTypeData as $fuelTypeId => $branchSet) {
                                    
                                                        $branchTarget += $branchSet[$branch->id]['total'] ?? 0;
                                    
                                                        $branchAchieved += $achievedWholesaleData[$modelId][$fuelTypeId][$branch->id]['total'] ?? 0;
                                                    }
                                                }
                                    
                                                $grandTarget += $branchTarget;
                                                $grandAchieved += $branchAchieved;
                                    
                                                $percentage = $branchTarget > 0 ? round(($branchAchieved / $branchTarget) * 100) : 0;
                                            @endphp
                                    
                                            <td>
                                                {{ $branchAchieved }} / {{ $branchTarget }}
                                                <br>
                                                <small>({{ $percentage }}%)</small>
                                            </td>
                                    
                                        @endforeach
                                    
                                        @php
                                            $grandPercentage = $grandTarget > 0 ? round(($grandAchieved / $grandTarget) * 100) : 0;
                                        @endphp
                                    
                                        <td class="custom-total-color">
                                            {{ $grandAchieved }} / {{ $grandTarget }}
                                            <br>
                                            <small>({{ $grandPercentage }}%)</small>
                                        </td>
                                    
                                        <td>{{ $grandPercentage }}%</td>
                                    </tr>
                                    
                                    </tbody>
                            </table>
                        </div>
                      </div>
                      
                      <!-- Wholesale Archive Header -->
                    
                    <div class="col-md-6 mt-3">
                        <h6 class="text-left"><strong>Wholesale Pending</strong></h6>
                    </div>
                
                    <!-- Right Column -->
                    <div class="col-md-12">
                        <div class="table-responsive">
                            <table class="table table-bordered w-100 booking-report-table">
                                <thead class="table-header">
                                    <tr>
                                        <th>Model</th>
                                        @foreach ($branches as $branch)
                                            @php
                                                $branch_full = $branch->name ?? '';
                                                $b_name = '';
                            
                                                if (str_contains($branch_full, 'Rajkot')) {
                                                    $b_name = 'Rajkot';
                                                } elseif (str_contains($branch_full, 'Rjt_1s')) {
                                                    $b_name = 'Rajkot-1s';
                                                } elseif (str_contains($branch_full, 'Jamnagar')) {
                                                    $b_name = 'Jamnagar';
                                                } elseif (str_contains($branch_full, 'Junagadh')) {
                                                    $b_name = 'Junagadh';
                                                } elseif (str_contains($branch_full, 'Morbi')) {
                                                    $b_name = 'Morbi';
                                                } elseif (str_contains($branch_full, '_M')) {
                                                    $b_name = 'Mehsana';
                                                } elseif (str_contains($branch_full, '_K')) {
                                                    $b_name = 'Kadi';
                                                } elseif (str_contains($branch_full, '_Porbandar')) {
                                                    $b_name = 'Porbandar';
                                                } else {
                                                    $b_name = $branch_full;
                                                }
                                            @endphp
                                            <th>{{ $b_name }}</th>
                                        @endforeach
                                        <th class="custom-total-color">Total</th>
                                        <th>%</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach ($targetwholesalebookingdata as $modelId => $fuelTypeData)
                                    
                                        @php
                                            $model = $models->firstWhere('id', $modelId);
                                            $modelName = $model ? $model->name : 'Unknown';
                                    
                                            $modelPendingTotal = 0;
                                            $modelTargetTotal = 0;
                                        @endphp
                                    
                                        {{-- MODEL ROW --}}
                                        <tr class="booking-header black-bg">
                                            <td>{{ $modelName }}</td>
                                    
                                            @foreach ($branches as $branch)
                                    
                                                @php
                                                    $target = 0;
                                                    $achieved = 0;
                                    
                                                    foreach ($fuelTypeData as $fuelTypeId => $fuelData) {
                                                        $target += $fuelData[$branch->id]['total'] ?? 0;
                                                        $achieved += $achievedWholesaleData[$modelId][$fuelTypeId][$branch->id]['total'] ?? 0;
                                                    }
                                    
                                                    $pending = max($target - $achieved, 0);
                                    
                                                    $modelPendingTotal += $pending;
                                                    $modelTargetTotal += $target;
                                    
                                                    $percentage = $target > 0 ? round(($pending / $target) * 100) : 0;
                                                @endphp
                                    
                                                <td>
                                                    {{ $pending }}
                                                    <br>
                                                    <small>({{ $percentage }}%)</small>
                                                </td>
                                    
                                            @endforeach
                                    
                                            @php
                                                $modelPercentage = $modelTargetTotal > 0 ? round(($modelPendingTotal / $modelTargetTotal) * 100) : 0;
                                            @endphp
                                    
                                            <td class="custom-total-data-color">
                                                {{ $modelPendingTotal }}
                                                <br>
                                                <small>({{ $modelPercentage }}%)</small>
                                            </td>
                                    
                                            <td>{{ $modelPercentage }}%</td>
                                        </tr>
                                    
                                        {{-- FUEL ROW --}}
                                        @foreach ($fuelTypeData as $fuelTypeId => $branchData)
                                    
                                            @php
                                                $rowPending = 0;
                                                $rowTarget = 0;
                                            @endphp
                                    
                                            <tr>
                                                <td>{{ $fuelTypes[$fuelTypeId] ?? 'Unknown' }}</td>
                                    
                                                @foreach ($branches as $branch)
                                    
                                                    @php
                                                        $target = $branchData[$branch->id]['total'] ?? 0;
                                                        $achieved = $achievedWholesaleData[$modelId][$fuelTypeId][$branch->id]['total'] ?? 0;
                                    
                                                        $pending = max($target - $achieved, 0);
                                    
                                                        $rowPending += $pending;
                                                        $rowTarget += $target;
                                    
                                                        $percentage = $target > 0 ? round(($pending / $target) * 100) : 0;
                                                    @endphp
                                    
                                                    <td>
                                                        {{ $pending }}
                                                        <br>
                                                        <small>({{ $percentage }}%)</small>
                                                    </td>
                                    
                                                @endforeach
                                    
                                                @php
                                                    $rowPercentage = $rowTarget > 0 ? round(($rowPending / $rowTarget) * 100) : 0;
                                                @endphp
                                    
                                                <td class="custom-total-fuel-color">
                                                    {{ $rowPending }}
                                                    <br>
                                                    <small>({{ $rowPercentage }}%)</small>
                                                </td>
                                    
                                                <td>{{ $rowPercentage }}%</td>
                                            </tr>
                                    
                                        @endforeach
                                    
                                    @endforeach
                                    
                                    
                                    {{-- GRAND TOTAL --}}
                                    <tr class="table-footer">
                                        <td>Total Pending</td>
                                    
                                        @php
                                            $grandPending = 0;
                                            $grandTarget = 0;
                                        @endphp
                                    
                                        @foreach ($branches as $branch)
                                    
                                            @php
                                                $branchPending = 0;
                                                $branchTarget = 0;
                                    
                                                foreach ($targetwholesalebookingdata as $modelId => $fuelTypeData) {
                                                    foreach ($fuelTypeData as $fuelTypeId => $branchSet) {
                                    
                                                        $target = $branchSet[$branch->id]['total'] ?? 0;
                                                        $achieved = $achievedWholesaleData[$modelId][$fuelTypeId][$branch->id]['total'] ?? 0;
                                    
                                                        $branchPending += max($target - $achieved, 0);
                                                        $branchTarget += $target;
                                                    }
                                                }
                                    
                                                $grandPending += $branchPending;
                                                $grandTarget += $branchTarget;
                                    
                                                $percentage = $branchTarget > 0 ? round(($branchPending / $branchTarget) * 100) : 0;
                                            @endphp
                                    
                                            <td>
                                                {{ $branchPending }}
                                                <br>
                                                <small>({{ $percentage }}%)</small>
                                            </td>
                                    
                                        @endforeach
                                    
                                        @php
                                            $grandPercentage = $grandTarget > 0 ? round(($grandPending / $grandTarget) * 100) : 0;
                                        @endphp
                                    
                                        <td class="custom-total-color">
                                            {{ $grandPending }}
                                            <br>
                                            <small>({{ $grandPercentage }}%)</small>
                                        </td>
                                    
                                        <td>{{ $grandPercentage }}%</td>
                                    </tr>
                                    
                                    </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        @endif
    </div>
</div>
    
    <div id="carDetailsModal" class="modal fade" tabindex="-1" role="dialog">
        <div class="modal-dialog modal-xl modal-dialog-scrollable">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">Car Details</h5>
                    <button type="button" class="btn-close btn-close-black" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <div class="row mb-3">
                        <div class="col-12 text-end">
                            <form id="exportForm" method="POST" action="{{ route('Cardetails.export') }}" target="_blank">
                                @csrf
                                <input type="hidden" name="ids" id="exportInventoryIds">
                                <input type="hidden" name="transit_ids" id="exportTransitInventoryIds">
                                <button type="submit" class="btn waves-effect waves-light btn-rounded btn-outline-primary">
                                    Export to Excel
                                </button>
                            </form>
                        </div>
                    </div>
                
                    <div id="carDetailsContent">
                        Loading...
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Booking Report Modal -->
    <div id="bookingDetailsModal" class="modal fade" tabindex="-1" role="dialog">
        <div class="modal-dialog modal-xl modal-dialog-scrollable">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">Booking Details</h5>
                    <button type="button" class="btn-close btn-close-black" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                   <div class="col-12 text-end">
                            <form id="exportForm" method="POST" action="{{ route('Bookingdetails.export') }}" target="_blank">
                                @csrf
                                <input type="hidden" name="ids" id="exportBookingIds">
                                <button type="submit" class="btn waves-effect waves-light btn-rounded btn-outline-primary">
                                    Export to Excel
                                </button>
                            </form>
                        </div>
                    <div id="bookingDetailsContent">
                        Loading...
                    </div>
                </div>
            </div>
        </div>
    </div>


<div class="container-fluid">
    <div class="row">
        @if(auth()->user()->can('vin-report'))
            <div class="col-xl-2 col-md-3 mb-2 custom_height">
                <div class="card border-left-success shadow h-100 py-2">
                    <div class="card-body">
                        <a href="{{route('vinReport')}}">
                        <div class="row no-gutters align-items-center">
                            <div class="col mr-2">
                                <div class="text-xs font-weight-bold text-success text-uppercase mb-1">
                                    All Report</div>
                                <div class="h6 mb-0 font-weight-bold">V.I.N Report</div>
                            </div>
                            <div class="col-auto">
                                <i class="fas fa-bookmark fa-2x text-gray-300"></i>
                            </div>
                        </div>
                    </a>
                    </div>
                </div>
            </div>
        @endif 
        
        <div class="col-xl-2 col-md-3 mb-2 custom_height" style="display:none;">
            <div class="card border-left-success shadow h-100 py-2">
                <div class="card-body">
                    <a href="{{route('ModelReport')}}">
                    <div class="row no-gutters align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-success text-uppercase mb-1">
                                All Report</div>
                            <div class="h6 mb-0 font-weight-bold">Model Wise Report</div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-bookmark fa-2x text-gray-300"></i>
                        </div>
                    </div>
                </a>
                </div>
            </div>
        </div>
        
        @if(auth()->user()->can('sales-report'))
            <div class="col-xl-2 col-md-3 mb-2 custom_height">
                <div class="card border-left-success shadow h-100 py-2">
                    <div class="card-body">
                        <a href="{{route('SalesReportFetch')}}">
                        <div class="row no-gutters align-items-center">
                            <div class="col mr-2">
                                <div class="text-xs font-weight-bold text-success text-uppercase mb-1">
                                    All Report</div>
                                <div class="h6 mb-0 font-weight-bold">Sales Report</div>
                            </div>
                            <div class="col-auto">
                                <i class="fas fa-bookmark fa-2x text-gray-300"></i>
                            </div>
                        </div>
                    </a>
                    </div>
                </div>
            </div>
        @endif
        
        @if(auth()->user()->can('purchase-report'))
            <div class="col-xl-2 col-md-3 mb-2 custom_height">
                <div class="card border-left-success shadow h-100 py-2">
                    <div class="card-body">
                        <a href="{{route('PurchaseReport')}}">
                        <div class="row no-gutters align-items-center">
                            <div class="col mr-2">
                                <div class="text-xs font-weight-bold text-success text-uppercase mb-1">
                                    All Report</div>
                                <div class="h6 mb-0 font-weight-bold">Purchase Report</div>
                            </div>
                            <div class="col-auto">
                                <i class="fas fa-bookmark fa-2x text-gray-300"></i>
                            </div>
                        </div>
                    </a>
                    </div>
                </div>
            </div>
        @endif
        
        @if(auth()->user()->can('rsa-report'))
            <div class="col-xl-2 col-md-3 mb-2 custom_height">
                <div class="card border-left-success shadow h-100 py-2">
                    <div class="card-body">
                        <a href="{{route('RsaReport')}}">
                        <div class="row no-gutters align-items-center">
                            <div class="col mr-2">
                                <div class="text-xs font-weight-bold text-success text-uppercase mb-1">
                                    All Report</div>
                                <div class="h6 mb-0 font-weight-bold">RSA Report</div>
                            </div>
                            <div class="col-auto">
                                <i class="fas fa-bookmark fa-2x text-gray-300"></i>
                            </div>
                        </div>
                    </a>
                    </div>
                </div>
            </div>
        @endif
        
        @if(auth()->user()->can('ccs-report'))
            <div class="col-xl-2 col-md-3 mb-2 custom_height">
                <div class="card border-left-success shadow h-100 py-2">
                    <div class="card-body">
                        <a href="{{route('CcsReport')}}">
                        <div class="row no-gutters align-items-center">
                            <div class="col mr-2">
                                <div class="text-xs font-weight-bold text-success text-uppercase mb-1">
                                    All Report</div>
                                <div class="h6 mb-0 font-weight-bold">CCS Report</div>
                            </div>
                            <div class="col-auto">
                                <i class="fas fa-bookmark fa-2x text-gray-300"></i>
                            </div>
                        </div>
                    </a>
                    </div>
                </div>
            </div>
        @endif
        
        @if(auth()->user()->can('mcp-report'))
            <div class="col-xl-2 col-md-3 mb-2 custom_height">
                <div class="card border-left-success shadow h-100 py-2">
                    <div class="card-body">
                        <a href="{{route('McpReport')}}">
                        <div class="row no-gutters align-items-center">
                            <div class="col mr-2">
                                <div class="text-xs font-weight-bold text-success text-uppercase mb-1">
                                    All Report</div>
                                <div class="h6 mb-0 font-weight-bold">MCP Report</div>
                            </div>
                            <div class="col-auto">
                                <i class="fas fa-bookmark fa-2x text-gray-300"></i>
                            </div>
                        </div>
                    </a>
                    </div>
                </div>
            </div>
        @endif
        
        @if(auth()->user()->can('ew-report'))
            <div class="col-xl-2 col-md-3 mb-2 custom_height">
                <div class="card border-left-success shadow h-100 py-2">
                    <div class="card-body">
                        <a href="{{route('EwReport')}}">
                        <div class="row no-gutters align-items-center">
                            <div class="col mr-2">
                                <div class="text-xs font-weight-bold text-success text-uppercase mb-1">
                                    All Report</div>
                                <div class="h6 mb-0 font-weight-bold">EW Report</div>
                            </div>
                            <div class="col-auto">
                                <i class="fas fa-bookmark fa-2x text-gray-300"></i>
                            </div>
                        </div>
                    </a>
                    </div>
                </div>
            </div>
        @endif
        
        @if(auth()->user()->can('inventory-report'))
            <div class="col-xl-2 col-md-3 mb-2 custom_height">
                <div class="card border-left-success shadow h-100 py-2">
                    <div class="card-body">
                        <a href="{{route('InventoryReport')}}">
                        <div class="row no-gutters align-items-center">
                            <div class="col mr-2">
                                <div class="text-xs font-weight-bold text-success text-uppercase mb-1">
                                    All Report</div>
                                <div class="h6 mb-0 font-weight-bold">Inventory Report</div>
                            </div>
                            <div class="col-auto">
                                <i class="fas fa-bookmark fa-2x text-gray-300"></i>
                            </div>
                        </div>
                    </a>
                    </div>
                </div>
            </div>
            
            <div class="col-xl-2 col-md-3 mb-2 custom_height">
                <div class="card border-left-success shadow h-100 py-2">
                    <div class="card-body">
                        <a href="{{route('DemoInventoryReport')}}">
                        <div class="row no-gutters align-items-center">
                            <div class="col mr-2">
                                <div class="text-xs font-weight-bold text-success text-uppercase mb-1">
                                    All Report</div>
                                <div class="h6 mb-0 font-weight-bold">Demo Inventory Report</div>
                            </div>
                            <div class="col-auto">
                                <i class="fas fa-bookmark fa-2x text-gray-300"></i>
                            </div>
                        </div>
                    </a>
                    </div>
                </div>
            </div>
            
            <div class="col-xl-2 col-md-3 mb-2 custom_height">
                <div class="card border-left-success shadow h-100 py-2">
                    <div class="card-body">
                        <a href="{{route('UsedCarReport')}}"> 
                        <div class="row no-gutters align-items-center">
                            <div class="col mr-2">
                                <div class="text-xs font-weight-bold text-success text-uppercase mb-1">
                                    All Report</div>
                                <div class="h6 mb-0 font-weight-bold">Used Car Inventory Report</div>
                            </div>
                            <div class="col-auto">
                                <i class="fas fa-bookmark fa-2x text-gray-300"></i>
                            </div>
                        </div>
                    </a>
                    </div>
                </div>
            </div>
        @endif
        
        @if(auth()->user()->can('team-report'))
            <div class="col-xl-2 col-md-3 mb-2 custom_height">
                <div class="card border-left-success shadow h-100 py-2">
                    <div class="card-body">
                        <a href="{{route('TeamReport')}}">
                        <div class="row no-gutters align-items-center">
                            <div class="col mr-2">
                                <div class="text-xs font-weight-bold text-success text-uppercase mb-1">
                                    All Report</div>
                                <div class="h6 mb-0 font-weight-bold">Team Report</div>
                            </div>
                            <div class="col-auto">
                                <i class="fas fa-bookmark fa-2x text-gray-300"></i>
                            </div>
                        </div>
                    </a>
                    </div>
                </div>
            </div>
        @endif
        
        @if(auth()->user()->can('user-report'))
            <div class="col-xl-2 col-md-3 mb-2 custom_height">
                <div class="card border-left-success shadow h-100 py-2">
                    <div class="card-body">
                        <a href="{{route('UserReport')}}">
                        <div class="row no-gutters align-items-center">
                            <div class="col mr-2">
                                <div class="text-xs font-weight-bold text-success text-uppercase mb-1">
                                    All Report</div>
                                <div class="h6 mb-0 font-weight-bold">User Report</div>
                            </div>
                            <div class="col-auto">
                                <i class="fas fa-bookmark fa-2x text-gray-300"></i>
                            </div>
                        </div>
                    </a>
                    </div>
                </div>
            </div>
        @endif
        
        @if(auth()->user()->can('rto-report'))
            <div class="col-xl-2 col-md-3 mb-2 custom_height">
                <div class="card border-left-success shadow h-100 py-2">
                    <div class="card-body">
                        <a href="{{route('RTOReport')}}">
                        <div class="row no-gutters align-items-center">
                            <div class="col mr-2">
                                <div class="text-xs font-weight-bold text-success text-uppercase mb-1">
                                    All Report</div>
                                <div class="h6 mb-0 font-weight-bold">RTO Report</div>
                            </div>
                            <div class="col-auto">
                                <i class="fas fa-bookmark fa-2x text-gray-300"></i>
                            </div>
                        </div>
                    </a>
                    </div>
                </div>
            </div>
        @endif
        
        @if(auth()->user()->can('transfer-report'))
            <div class="col-xl-2 col-md-3 mb-2 custom_height">
                <div class="card border-left-success shadow h-100 py-2">
                    <div class="card-body">
                        <a href="{{route('TransferReport')}}">
                        <div class="row no-gutters align-items-center">
                            <div class="col mr-2">
                                <div class="text-xs font-weight-bold text-success text-uppercase mb-1">
                                    All Report</div>
                                <div class="h6 mb-0 font-weight-bold">Transfer Report</div>
                            </div>
                            <div class="col-auto">
                                <i class="fas fa-bookmark fa-2x text-gray-300"></i>
                            </div>
                        </div>
                    </a>
                    </div>
                </div>
            </div>
        @endif
        
        @if(auth()->user()->can('insurance-report'))
            <div class="col-xl-2 col-md-3 mb-2 custom_height">
                <div class="card border-left-success shadow h-100 py-2">
                    <div class="card-body">
                        <a href="{{route('InsuranceReport')}}">
                        <div class="row no-gutters align-items-center">
                            <div class="col mr-2">
                                <div class="text-xs font-weight-bold text-success text-uppercase mb-1">
                                    All Report</div>
                                <div class="h6 mb-0 font-weight-bold">Insurance Report</div>
                            </div>
                            <div class="col-auto">
                                <i class="fas fa-bookmark fa-2x text-gray-300"></i>
                            </div>
                        </div>
                    </a>
                    </div>
                </div>
            </div>
        @endif
        
        @if(auth()->user()->can('finance-report'))
            <div class="col-xl-2 col-md-3 mb-2 custom_height">
                <div class="card border-left-success shadow h-100 py-2">
                    <div class="card-body">
                        <a href="{{route('FinanceReport')}}">
                        <div class="row no-gutters align-items-center">
                            <div class="col mr-2">
                                <div class="text-xs font-weight-bold text-success text-uppercase mb-1">
                                    All Report</div>
                                <div class="h6 mb-0 font-weight-bold">Finance Report</div>
                            </div>
                            <div class="col-auto">
                                <i class="fas fa-bookmark fa-2x text-gray-300"></i>
                            </div>
                        </div>
                    </a>
                    </div>
                </div>
            </div>
        @endif
        
        @if(auth()->user()->can('status-wise-report'))
            <div class="col-xl-2 col-md-3 mb-2 custom_height">
                <div class="card border-left-success shadow h-100 py-2">
                    <div class="card-body">
                        <a href="{{route('StatusReport')}}">
                        <div class="row no-gutters align-items-center">
                            <div class="col mr-2">
                                <div class="text-xs font-weight-bold text-success text-uppercase mb-1">
                                    All Report</div>
                                <div class="h6 mb-0 font-weight-bold">Status Wise Report</div>
                            </div>
                            <div class="col-auto">
                                <i class="fas fa-bookmark fa-2x text-gray-300"></i>
                            </div>
                        </div>
                    </a>
                    </div>
                </div>
            </div>
        @endif
        
        @if(auth()->user()->can('day-book-report'))
            <div class="col-xl-2 col-md-3 mb-2 custom_height">
                <div class="card border-left-success shadow h-100 py-2">
                    <div class="card-body">
                        <a href="{{route('DayBookReportFetch')}}">
                        <div class="row no-gutters align-items-center">
                            <div class="col mr-2">
                                <div class="text-xs font-weight-bold text-success text-uppercase mb-1">
                                    All Report</div>
                                <div class="h6 mb-0 font-weight-bold">Day Book Report</div>
                            </div>
                            <div class="col-auto">
                                <i class="fas fa-bookmark fa-2x text-gray-300"></i>
                            </div>
                        </div>
                    </a>
                    </div>
                </div>
            </div>
        @endif
        
        @if(auth()->user()->can('cash-receipt-report'))	
            <div class="col-xl-2 col-md-3 mb-2 custom_height">
                <div class="card border-left-success shadow h-100 py-2">
                    <div class="card-body">
                        <a href="{{route('CashReceiptReportFetch')}}">
                        <div class="row no-gutters align-items-center">
                            <div class="col mr-2">
                                <div class="text-xs font-weight-bold text-success text-uppercase mb-1">
                                    All Report</div>
                                <div class="h6 mb-0 font-weight-bold">Cash Receipt Report</div>
                            </div>
                            <div class="col-auto">
                                <i class="fas fa-bookmark fa-2x text-gray-300"></i>
                            </div>
                        </div>
                    </a>
                    </div>
                </div>
            </div>
        @endif
        
        @if(auth()->user()->can('cancellation-report'))	
            <div class="col-xl-2 col-md-3 mb-2 custom_height">
                <div class="card border-left-success shadow h-100 py-2">
                    <div class="card-body">
                        <a href="{{route('CancelReasonReport')}}">
                        <div class="row no-gutters align-items-center">
                            <div class="col mr-2">
                                <div class="text-xs font-weight-bold text-success text-uppercase mb-1">
                                    All Report</div>
                                <div class="h6 mb-0 font-weight-bold">Cancellation Report</div>
                            </div>
                            <div class="col-auto">
                                <i class="fas fa-bookmark fa-2x text-gray-300"></i>
                            </div>
                        </div>
                    </a>
                    </div>
                </div>
            </div>
        @endif
        
        @if(auth()->user()->can('make-deal-report'))
            <div class="col-xl-2 col-md-3 mb-2 custom_height">
                <div class="card border-left-success shadow h-100 py-2">
                    <div class="card-body">
                        <a href="{{route('MakeDealReport')}}">
                        <div class="row no-gutters align-items-center">
                            <div class="col mr-2">
                                <div class="text-xs font-weight-bold text-success text-uppercase mb-1">
                                    All Report</div>
                                <div class="h6 mb-0 font-weight-bold">Make Deal Report</div>
                            </div>
                            <div class="col-auto">
                                <i class="fas fa-bookmark fa-2x text-gray-300"></i>
                            </div>
                        </div>
                    </a>
                    </div>
                </div>
            </div>
        @endif
        
        @if(auth()->user()->can('booking-report'))
            <div class="col-xl-2 col-md-3 mb-2 custom_height">
                <div class="card border-left-success shadow h-100 py-2">
                    <div class="card-body">
                        <a href="{{route('BookingReport')}}">
                        <div class="row no-gutters align-items-center">
                            <div class="col mr-2">
                                <div class="text-xs font-weight-bold text-success text-uppercase mb-1">
                                    All Report</div>
                                <div class="h6 mb-0 font-weight-bold">Booking Report</div>
                            </div>
                            <div class="col-auto">
                                <i class="fas fa-bookmark fa-2x text-gray-300"></i>
                            </div>
                        </div>
                    </a>
                    </div>
                </div>
            </div>
        @endif
        
        @if(auth()->user()->can('booking-retail-report'))
            <div class="col-xl-2 col-md-3 mb-2 custom_height">
                <div class="card border-left-success shadow h-100 py-2">
                    <div class="card-body">
                        <a href="{{route('BookingRetailReport')}}">
                        <div class="row no-gutters align-items-center">
                            <div class="col mr-2">
                                <div class="text-xs font-weight-bold text-success text-uppercase mb-1">
                                    All Report</div>
                                <div class="h6 mb-0 font-weight-bold">Booking-Retail Report</div>
                            </div>
                            <div class="col-auto">
                                <i class="fas fa-bookmark fa-2x text-gray-300"></i>
                            </div>
                        </div>
                    </a>
                    </div>
                </div>
            </div>
        @endif
        
        @if(auth()->user()->can('rmc-report'))
            <div class="col-xl-2 col-md-3 mb-2 custom_height">
                <div class="card border-left-success shadow h-100 py-2">
                    <div class="card-body">
                        <a href="{{route('MunicipleTaxReport')}}">
                        <div class="row no-gutters align-items-center">
                            <div class="col mr-2">
                                <div class="text-xs font-weight-bold text-success text-uppercase mb-1">
                                    All Report</div>
                                <div class="h6 mb-0 font-weight-bold">Municiple Tax Report</div>
                            </div>
                            <div class="col-auto">
                                <i class="fas fa-bookmark fa-2x text-gray-300"></i>
                            </div>
                        </div>
                    </a>
                    </div>
                </div>
            </div>
        @endif
        
        @if(auth()->user()->can('order-status-report'))
            <div class="col-xl-2 col-md-3 mb-2 custom_height">
                <div class="card border-left-success shadow h-100 py-2">
                    <div class="card-body">
                        <a href="{{route('ObfReport')}}">
                        <div class="row no-gutters align-items-center">
                            <div class="col mr-2">
                                <div class="text-xs font-weight-bold text-success text-uppercase mb-1">
                                    All Report</div>
                                <div class="h6 mb-0 font-weight-bold">Order Status Report</div>
                            </div>
                            <div class="col-auto">
                                <i class="fas fa-bookmark fa-2x text-gray-300"></i>
                            </div>
                        </div>
                    </a>
                    </div>
                </div>
            </div>
        @endif
        
        @if(auth()->user()->can('yet-to-deliver-report'))
            <div class="col-xl-2 col-md-3 mb-2 custom_height">
                <div class="card border-left-success shadow h-100 py-2">
                    <div class="card-body">
                        <a href="{{route('yettodeliveryReport')}}">
                        <div class="row no-gutters align-items-center">
                            <div class="col mr-2">
                                <div class="text-xs font-weight-bold text-success text-uppercase mb-1">
                                    All Report</div>
                                <div class="h6 mb-0 font-weight-bold">Yet To Deliver Report</div>
                            </div>
                            <div class="col-auto">
                                <i class="fas fa-bookmark fa-2x text-gray-300"></i>
                            </div>
                        </div>
                    </a>
                    </div>
                </div>
            </div>
        @endif
        
        @if(auth()->user()->can('customer-outstanding-report'))
            <div class="col-xl-2 col-md-3 mb-2 custom_height">
                <div class="card border-left-success shadow h-100 py-2">
                    <div class="card-body">
                        <a href="{{route('customeroutstanding')}}">
                        <div class="row no-gutters align-items-center">
                            <div class="col mr-2">
                                <div class="text-xs font-weight-bold text-success text-uppercase mb-1">
                                    All Report</div>
                                <div class="h6 mb-0 font-weight-bold">Customer Outstanding Report</div>
                            </div>
                            <div class="col-auto">
                                <i class="fas fa-bookmark fa-2x text-gray-300"></i>
                            </div>
                        </div>
                    </a>
                    </div>
                </div>
            </div>
        @endif    
        
        @if(auth()->user()->can('refund-report'))
            <div class="col-xl-2 col-md-3 mb-2 custom_height">
                <div class="card border-left-success shadow h-100 py-2">
                    <div class="card-body">
                        <a href="{{route('refundReport')}}">
                        <div class="row no-gutters align-items-center">
                            <div class="col mr-2">
                                <div class="text-xs font-weight-bold text-success text-uppercase mb-1">
                                    All Report</div>
                                <div class="h6 mb-0 font-weight-bold">Refund Report</div>
                            </div>
                            <div class="col-auto">
                                <i class="fas fa-bookmark fa-2x text-gray-300"></i>
                            </div>
                        </div>
                    </a>
                    </div>
                </div>
            </div>
        @endif 
        
        @if(auth()->user()->can('deliver-report'))
            <div class="col-xl-2 col-md-3 mb-2 custom_height">
                <div class="card border-left-success shadow h-100 py-2">
                    <div class="card-body">
                        <a href="{{route('deliveryReport')}}">
                        <div class="row no-gutters align-items-center">
                            <div class="col mr-2">
                                <div class="text-xs font-weight-bold text-success text-uppercase mb-1">
                                    All Report</div>
                                <div class="h6 mb-0 font-weight-bold">Delivered Vehicle Report</div>
                            </div>
                            <div class="col-auto">
                                <i class="fas fa-bookmark fa-2x text-gray-300"></i>
                            </div>
                        </div>
                    </a>
                    </div>
                </div>
            </div>
        @endif
        
        @if(auth()->user()->can('PPC-report'))
            <div class="col-xl-2 col-md-3 mb-2 custom_height">
                <div class="card border-left-success shadow h-100 py-2">
                    <div class="card-body">
                        <a href="{{route('PPCReportFetch')}}">
                        <div class="row no-gutters align-items-center">
                            <div class="col mr-2">
                                <div class="text-xs font-weight-bold text-success text-uppercase mb-1">
                                    All Report</div>
                                <div class="h6 mb-0 font-weight-bold">PPC Report</div>
                            </div>
                            <div class="col-auto">
                                <i class="fas fa-bookmark fa-2x text-gray-300"></i>
                            </div>
                        </div>
                    </a>
                    </div>
                </div>
            </div>
        @endif
    </div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.min.js"></script>
  


<script>

var xValues = ["Rajkot", "Junagadh", "jamnagar", "Morbi"];
var yValues = [5, 4, 3, 2, 1];
var barColors = ["red", "green","blue","orange","brown"];

new Chart("myChart", {
  type: "bar",
  data: {
    labels: xValues,
    datasets: [{
      backgroundColor: barColors,
      data: yValues
    }]
  },
  options: {
    legend: {display: false},
    title: {
      display: true,
      text: "Bar chart"
    }
  }
});
</script>
@endsection

@section('scripts')

<!--<script src="{{ asset('assets/vendor/chart.js/Chart.min.js') }}"></script>-->
<!--<script src="{{ asset('assets/js/demo/chart-area-demo.js') }}"></script>-->
<!--<script src="{{ asset('assets/js/demo/chart-pie-demo.js') }}"></script>-->
<!--<script src="https://cdn.tailwindcss.com"></script>-->

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>
    
    <script>
        // function showCarDetails(inventoryIds, transitIds = []) { 
        //     // if (!inventoryIds || inventoryIds.length === 0) return;
        //     if ((!inventoryIds || inventoryIds.length === 0) && (!transitIds || transitIds.length === 0)) return;
            
        //     $('#exportInventoryIds').val(JSON.stringify(inventoryIds));
        
        //     $.ajax({
        //         url: '{{ route("Cardetails") }}',
        //         type: 'POST',
        //         data: {
        //             _token: '{{ csrf_token() }}',
        //             ids: inventoryIds,
        //             transit_ids: transitIds
        //         },
        //         success: function(response) {
        //             $('#carDetailsContent').html(response);
        //             $('#carDetailsModal').modal('show');
        //         },
        //         error: function() {
        //             alert('Failed to fetch car details.');
        //         }
        //     });
        // }
        
        /*function showCarDetails(inventoryIds = [], transitIds = []) {
            if ((!inventoryIds || inventoryIds.length === 0) && (!transitIds || transitIds.length === 0)) {
                return;
            }
        
            $('#exportInventoryIds').val(JSON.stringify(inventoryIds));
            $('#exportTransitInventoryIds').val(JSON.stringify(transitIds));
        
            $.ajax({
                url: '{{ route("Cardetails") }}',
                type: 'POST',
                contentType: 'application/json', // Important: sending JSON
                data: JSON.stringify({
                    _token: '{{ csrf_token() }}',
                    ids: inventoryIds,
                    transit_ids: transitIds
                }),
                success: function(response) {
                    $('#carDetailsContent').html(response);
                    $('#carDetailsModal').modal('show');
                },
                error: function() {
                    alert('Failed to fetch car details.');
                }
            });
        }*/
        
        function showCarDetails(inventoryIds = [], transitIds = []) {
            if ((!inventoryIds || inventoryIds.length === 0) && (!transitIds || transitIds.length === 0)) {
                return;
            }
        
            // Set both inventory and transit IDs for Excel Export
            $('#exportInventoryIds').val(JSON.stringify(inventoryIds));
            $('#exportTransitInventoryIds').val(JSON.stringify(transitIds));
        
            // Send both arrays separately in JSON, as your controller expects
            $.ajax({
                url: '{{ route("Cardetails") }}',
                type: 'POST',
                contentType: 'application/json',
                data: JSON.stringify({
                    _token: '{{ csrf_token() }}',
                    ids: inventoryIds,        // total IDs
                    transit_ids: transitIds   // transit IDs
                }),
                success: function(response) {
                    $('#carDetailsContent').html(response);
                    $('#carDetailsModal').modal('show');
                },
                error: function() {
                    alert('Failed to fetch car details.');
                }
            });
        }

        
        $(document).on('click', '.open-booking-modal', function (e) {
            e.preventDefault();
    
            const obfIds = $(this).data('obf-ids');
            
            $('#exportBookingIds').val(JSON.stringify(obfIds));
    
            $.ajax({
                url: '{{ route("Bookingdetails") }}',
                method: 'GET',
                data: {
                    obf_ids: obfIds
                },
                success: function (response) {
                    $('#bookingDetailsContent').html(response);
                    $('#bookingDetailsModal').modal('show');
                },
                error: function () {
                    alert('Something went wrong while loading the data.');
                }
            });
        });
        
        document.addEventListener("DOMContentLoaded", function () {
            // ------------------- MIS REPORT TABLE -------------------
            const misRows = document.querySelectorAll(".mis-report-table tbody tr");

            // Hide only fuel-type rows (not model headers or footer)
            misRows.forEach((row, index) => {
                const isModelHeader = row.classList.contains("model-header");
                const isFooter = row.classList.contains("table-footer");
                if (!isModelHeader && !isFooter) {
                    row.style.display = "none";
                }
            });

            // On clicking a model-header, toggle its fuel-type rows
            misRows.forEach((row, index) => {
                if (row.classList.contains("model-header")) {
                    row.addEventListener("click", function () {
                        let i = index + 1;
                        while (
                            i < misRows.length &&
                            !misRows[i].classList.contains("model-header") &&
                            !misRows[i].classList.contains("table-footer")
                        ) {
                            misRows[i].style.display =
                                misRows[i].style.display === "none" ? "table-row" : "none";
                            i++;
                        }
                    });
                }
            });

            // Modal logic for MIS Report table
            misRows.forEach((row, rowIndex) => {
                const cells = row.querySelectorAll("td:not(:first-child)");
                cells.forEach((cell) => {
                    cell.style.cursor = "pointer";
                    cell.addEventListener("click", function (e) {
                        const value = e.target.textContent;
                        const columnIndex = Array.from(cell.parentElement.children).indexOf(cell);
                        const columnHeader = document.querySelectorAll(".mis-report-table thead th")[columnIndex].textContent;

                        let modelName = "Unknown Model";
                        for (let i = rowIndex; i >= 0; i--) {
                            if (misRows[i].classList.contains("model-header")) {
                                modelName = misRows[i].querySelector("td").textContent;
                                break;
                            }
                        }

                        document.getElementById("modalContent").innerHTML = 
                            `<strong>${value}</strong> in ${columnHeader} for <strong>${modelName}</strong>`;

                        const modal = new bootstrap.Modal(document.getElementById("infoModal"));
                        modal.show();
                    });
                });
            });

            // ------------------- BOOKING REPORT TABLE -------------------
            const bookingRows = document.querySelectorAll(".booking-report-table tbody tr");
                
            bookingRows.forEach((row, index) => {
                const isModelHeader = row.classList.contains("booking-header");
                const isFooter = row.classList.contains("table-footer");
                if (!isModelHeader && !isFooter) {
                    row.style.display = "none";
                }
            });

            // On clicking a booking-header, toggle its fuel-type rows
            bookingRows.forEach((row, index) => {
                if (row.classList.contains("booking-header")) {
                    row.addEventListener("click", function () {
                        let i = index + 1;
                        while (
                            i < bookingRows.length &&
                            !bookingRows[i].classList.contains("booking-header") &&
                            !bookingRows[i].classList.contains("table-footer")
                        ) {
                            bookingRows[i].style.display =
                                bookingRows[i].style.display === "none" ? "table-row" : "none";
                            i++;
                        }
                    });
                }
            });
                
            bookingRows.forEach((row) => {
                const modelName = row.dataset.model || "Unknown Model"; // Read from data-model
            
                const cells = row.querySelectorAll("td:not(:first-child)");
                cells.forEach((cell) => {
                    cell.style.cursor = "pointer";
                    cell.addEventListener("click", function (e) {
                        const value = e.target.textContent.trim();
                        const columnIndex = Array.from(cell.parentElement.children).indexOf(cell);
                        const columnHeader = document.querySelectorAll(".booking-report-table thead th")[columnIndex].textContent.trim();
            
                        document.getElementById("bookingContent").innerHTML = 
                            `<strong>${value}</strong> in <strong>${columnHeader}</strong> for <strong>${modelName}</strong>`;
            
                        const modal = new bootstrap.Modal(document.getElementById("bookingModal"));
                        modal.show();
                    });
                });
            });
        });
    </script>
@endsection



