@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;
        }

        .fuel-row {
            background-color: #6c757d;
        }
        
        /*.custom-total-color {
            background-color:#463862!important;
        }
        
        .custom-total-data-color{
            background-color:#5c5283!important;
        }
        
        .custom-total-fuel-color{
            background-color:#9186bb!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="container-fluid">
    <div class="d-sm-flex align-items-center justify-content-between mb-4">
        <h1 class="h3 mb-0 text-gray-800">Dashboard</h1>
    </div>
    <div class="row">
        @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 1 -->
                <div class="row mb-4">
                    <div class="col-md-6">
                        <h6 class="text-left">Wholesale Target</h6>
                    </div>
                    
                    <!-- Wholesale Archive Header -->
                    <div class="col-md-6">
                        <h6 class="text-left">Wholesale Achive</h6>
                    </div>
                      <!-- Left Column -->
                    <div class="col-md-6 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';
                                                } else {
                                                    $b_name = $branch_full;
                                                }
                                            @endphp
                                            <th>{{ $b_name }}</th>
                                        @endforeach
                                        <th class="custom-total-color">Total</th>
                                        <th>%</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @php
                                        $percentages = ['32%', '12%', '27%', '6%', '22%', '1%', '0%']; // static example
                                        $modelIndex = 0;
                                    @endphp
                                    @foreach ($targetbookingdata as $modelId => $fuelTypeData)
                                        @php
                                            $modelName = $models->firstWhere('id', $modelId)?->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>
                                            <td class="text-center font-weight-bold">
                                                {{ $percentages[$modelIndex++] ?? '0%' }}
                                            </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 ($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-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>
                                    <tr class="table-footer">
                                        <td>%</td>
                                        <td class="text-success font-weight-bold">39%</td>
                                        <td class="text-warning font-weight-bold">14%</td> 
                                        <td class="text-success font-weight-bold">27%</td>
                                        <td class="text-danger font-weight-bold">0%</td>
                                        <td class="text-success font-weight-bold">19%</td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                    </div>
                
                    <!-- Right Column -->
                    <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>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';
                                                } else {
                                                    $b_name = $branch_full;
                                                }
                                            @endphp
                                            <th>{{ $b_name }}</th>
                                        @endforeach
                                        <th class="custom-total-color">Total</th>
                                        <th>%</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @php
                                        $percentages = ['32%', '12%', '27%', '6%', '22%', '1%', '0%']; // static example
                                        $modelIndex = 0;
                                    @endphp
                                    @foreach ($targetbookingdata as $modelId => $fuelTypeData)
                                        @php
                                            $modelName = $models->firstWhere('id', $modelId)?->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>
                                            <td class="text-center font-weight-bold">
                                                {{ $percentages[$modelIndex++] ?? '0%' }}
                                            </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 ($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-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>
                                    <tr class="table-footer">
                                        <td>%</td>
                                        <td class="text-success font-weight-bold">39%</td>
                                        <td class="text-warning font-weight-bold">14%</td> 
                                        <td class="text-success font-weight-bold">27%</td>
                                        <td class="text-danger font-weight-bold">0%</td>
                                        <td class="text-success font-weight-bold">19%</td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            
                <!-- Row 2 -->
                <div class="row mb-4">
                    <div class="col-md-6">
                        <h6 class="text-left">Wholesale Pending</h6>
                    </div>
                    
                    <!-- Wholesale Archive Header -->
                    <div class="col-md-6">
                        <h6 class="text-left">Retail Target</h6>
                    </div>
                    <!-- Left Column -->
                    <div class="col-md-6 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';
                                                } else {
                                                    $b_name = $branch_full;
                                                }
                                            @endphp
                                            <th>{{ $b_name }}</th>
                                        @endforeach
                                        <th class="custom-total-color">Total</th>
                                        <th>%</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @php
                                        $percentages = ['32%', '12%', '27%', '6%', '22%', '1%', '0%']; // static example
                                        $modelIndex = 0;
                                    @endphp
                                    @foreach ($targetbookingdata as $modelId => $fuelTypeData)
                                        @php
                                            $modelName = $models->firstWhere('id', $modelId)?->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>
                                            <td class="text-center font-weight-bold">
                                                {{ $percentages[$modelIndex++] ?? '0%' }}
                                            </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 ($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-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>
                                    <tr class="table-footer">
                                        <td>%</td>
                                        <td class="text-success font-weight-bold">39%</td>
                                        <td class="text-warning font-weight-bold">14%</td> 
                                        <td class="text-success font-weight-bold">27%</td>
                                        <td class="text-danger font-weight-bold">0%</td>
                                        <td class="text-success font-weight-bold">19%</td>
                                    </tr>
                
                                </tbody>
                            </table>
                        </div>
                    </div>
                    <!-- Right Column -->
                    <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>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';
                                                } else {
                                                    $b_name = $branch_full;
                                                }
                                            @endphp
                                            <th>{{ $b_name }}</th>
                                        @endforeach
                                        <th class="custom-total-color">Total</th>
                                        <th>%</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @php
                                        $percentages = ['32%', '12%', '27%', '6%', '22%', '1%', '0%']; // static example
                                        $modelIndex = 0;
                                    @endphp
                                    @foreach ($targetbookingdata as $modelId => $fuelTypeData)
                                        @php
                                            $modelName = $models->firstWhere('id', $modelId)?->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>
                                            <td class="text-center font-weight-bold">
                                                {{ $percentages[$modelIndex++] ?? '0%' }}
                                            </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 ($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-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>
                                    <tr class="table-footer">
                                        <td>%</td>
                                        <td class="text-success font-weight-bold">39%</td>
                                        <td class="text-warning font-weight-bold">14%</td> 
                                        <td class="text-success font-weight-bold">27%</td>
                                        <td class="text-danger font-weight-bold">0%</td>
                                        <td class="text-success font-weight-bold">19%</td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            
                <!-- Row 3 -->
                <div class="row mb-4">
                    <div class="col-md-6">
                        <h6 class="text-left">Retail Achieve</h6>
                    </div>
                    
                    <!-- Wholesale Archive Header -->
                    <div class="col-md-6">
                        <h6 class="text-left">Retail Pending</h6>
                    </div>

                    <!-- Left Column -->
                    <div class="col-md-6 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';
                                                } else {
                                                    $b_name = $branch_full;
                                                }
                                            @endphp
                                            <th>{{ $b_name }}</th>
                                        @endforeach
                                        <th class="custom-total-color">Total</th>
                                        <th>%</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @php
                                        $percentages = ['32%', '12%', '27%', '6%', '22%', '1%', '0%']; // static example
                                        $modelIndex = 0;
                                    @endphp
                                    @foreach ($targetbookingdata as $modelId => $fuelTypeData)
                                        @php
                                            $modelName = $models->firstWhere('id', $modelId)?->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>
                                            <td class="text-center font-weight-bold">
                                                {{ $percentages[$modelIndex++] ?? '0%' }}
                                            </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 ($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-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>
                                    <tr class="table-footer">
                                        <td>%</td>
                                        <td class="text-success font-weight-bold">39%</td>
                                        <td class="text-warning font-weight-bold">14%</td> 
                                        <td class="text-success font-weight-bold">27%</td>
                                        <td class="text-danger font-weight-bold">0%</td>
                                        <td class="text-success font-weight-bold">19%</td>
                                    </tr>
                
                                </tbody>
                            </table>
                        </div>
                    </div>
                
                    <!-- Right Column -->
                    <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>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';
                                                } else {
                                                    $b_name = $branch_full;
                                                }
                                            @endphp
                                            <th>{{ $b_name }}</th>
                                        @endforeach
                                        <th class="custom-total-color">Total</th>
                                        <th>%</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @php
                                        $percentages = ['32%', '12%', '27%', '6%', '22%', '1%', '0%']; // static example
                                        $modelIndex = 0;
                                    @endphp
                                    @foreach ($targetbookingdata as $modelId => $fuelTypeData)
                                        @php
                                            $modelName = $models->firstWhere('id', $modelId)?->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>
                                            <td class="text-center font-weight-bold">
                                                {{ $percentages[$modelIndex++] ?? '0%' }}
                                            </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 ($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-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>
                                    <tr class="table-footer">
                                        <td>%</td>
                                        <td class="text-success font-weight-bold">39%</td>
                                        <td class="text-warning font-weight-bold">14%</td> 
                                        <td class="text-success font-weight-bold">27%</td>
                                        <td class="text-danger font-weight-bold">0%</td>
                                        <td class="text-success font-weight-bold">19%</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>
@endsection

@section('scripts')

<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) && (!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



