@extends('backend.layouts.app')

@section('content')
<style>
    .badge {
        --bs-badge-padding-x: 0.80em;
        --bs-badge-padding-y: 0.80em;
        --bs-badge-font-size: 0.80em;
        --bs-badge-font-weight: 700;
        --bs-badge-color: #fff;
        --bs-badge-border-radius: 0.375rem;
        display: inline-block;
        padding: var(--bs-badge-padding-y) var(--bs-badge-padding-x);
        font-size: var(--bs-badge-font-size);
        font-weight: var(--bs-badge-font-weight);
        line-height: 1;
        color: var(--bs-badge-color);
        text-align: center;
        white-space: nowrap;
        vertical-align: baseline;
        border-radius: var(--bs-badge-border-radius);
    }
</style>
<div class="row page-titles">
   <div class="col-md-5 align-self-center">
      <h4 class="text-themecolor">Flight Booking </h4>
   </div>
   <div class="col-md-7 align-self-center text-right">
      <div class="d-flex justify-content-end align-items-center">
         <ol class="breadcrumb">
            <li class="breadcrumb-item"><a href="{{ route('dashboard') }}">Dashboard</a></li>
            
            <li class="breadcrumb-item active">Flight Booking </li>
         </ol>
        
        </div>
   </div>
</div>
<div class="container-fluid">
   

    <div class="content-wrapper">
    @if (session('status'))
            <div class="alert alert-success alert-dismissible fade show" role="alert"><i class="far fa-check-circle"></i>
                {{ session('status') }}
                <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
        @endif
        @if (session('error'))
        <div class="alert alert-danger alert-dismissible fade show" role="alert"><i class="far fa-check-circle"></i>
            {{ session('error') }}
            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                <span aria-hidden="true">&times;</span>
            </button>
        </div>
    @endif

        

        <section class="content">
            <div class="row">
                <div class="col-12">
                    <div class="card">
                        <div class="card-body">
                                    <div class="col-md-12">
                                        <form action="{{ route('flight') }}" method="get" id="form">
                                            <div class="align-items-start">
                                                <div class="row">
                                                    <div class="col-md-2">
                                                        <label>From Date: &nbsp;</label>
                                                        <div>
                                                            <input class="form-control dateInput mr-3 mb-3" id="adminSerchField1" value="{{$from}}" name="from_date" type="date" onchange="updateToDate()" onkeydown="return false;">
                                                        </div>
                                                    </div>
                                                    <div class="col-md-2">
                                                        <label>To Date: &nbsp;</label>
                                                        <div>
                                                            <input class="form-control dateInput mr-3 mb-3" id="adminSerchField2" value="{{$to}}" name="to_date" type="date" @if(!$to) disabled @endif onkeydown="return false;">
                                                        </div>
                                                    </div>
                                                    <div class="col-md-2">
                                                        <label>User</label>
                                                        <div>
                                                            <select name="user_id" class="form-control mb-3">
                                                                <option value="">Select User</option>
                                                                @foreach($users as $user)
                                                                    <option value="{{ $user->id }}" {{ request()->query('user_id') == $user->id ? 'selected' : '' }}>
                                                                        {{ $user->first_name }} {{ $user->last_name }}
                                                                    </option>
                                                                @endforeach
                                                            </select>
                                                        </div> 
                                                    </div>
                                                    <div class="col-md-2">
                                                        <label>Booking ID</label>
                                                        <div class="">
                                                        <input type="text" name="search" class="form-control dateInput mr-3 mb-3" placeholder="Booking ID" value="{{request()->query('search')}}" >
                                                        </div> 
                                                    </div>
                                                    <div class="col-md-2">
                                                        <label>Status</label>
                
                                                        <div class="">
                                                            <select name="status"class="form-control mr-3 mb-3" >
                                                                <option value="">Select Status</option>
                                                                <option {{(request()->query('status') == 'Confirm') ? "selected" :''}} value="Confirm">CONFIRM</option>
                                                                <option {{(request()->query('status') == 'Cancel') ? "selected" :''}} value="Cancel">CANCEL</option>
                                                                <option {{(request()->query('status') == 'Pending') ? "selected" :''}} value="Pending">Pending</option>
                                                                
                                                            </select>
                                                        </div>
                                                        </div>
                                                        <div class="col-md-2">
                                                            <div class="input-group-append" style="margin-top: 29px;">
                                                                <button class="btn btn-secondary" type="submit"><i class="fas fa-search fa-fw"></i></button>
                                                                <a href="{{ route('flight') }}" class="btn btn-secondary" title="Reload"><i class="fas fa-sync"></i></a>
                                                                <a href="{{ route('flight.export', ['from_date' => request('from_date'), 'to_date' => request('to_date'), 'user_id' => request('user_id'), 'status' => request('status')]) }}" class="btn btn-secondary" title="Export to Excel">
                                                                    <i class="fas fa-file-excel"></i>
                                                                </a>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                        </form>
                                    </div>
                                <br>
                                <div class="table-responsive">
                            <table id="city" class="table table-bordered table-hover">
                                <thead>
                                    <tr>
                                    <th>S. No.</th>
                                    <th>@sortablelink('booking_no', 'Booking Id')</th>
                                    <th>@sortablelink('user_id', 'User')</th>
                                    <th>Flight Number</th> <!-- This may not work directly -->
                                    <th>Departure Airport</th>
                                    <th>Arrival Airport</th>
                                    <th>Departure Date</th>
                                    <th>Arrival Date</th>
                                    <th>Booking Type</th>
                                    <th>Status</th>
                                     <th>Action</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @if(count($bookings) > 0)
                                    @php 
                                                $i = ($bookings->perPage() * ($bookings->currentPage() - 1));
                                            @endphp
                                            @foreach($bookings as $booking)
                                                @foreach($booking->bookingFlights as $flight)
                                                    @php
                                                        // Get the first stop entry for the flight
                                                        $stop = $flight->bookingFlightStops->first(); 
                                                    @endphp
                                                    @if($stop)
                                                    @if($stop && $flight->journey_type != 'return') 
                                                        <tr>
                                                            <td>{{ ++$i }}</td>
                                                            <td>{{ $booking->booking_no }}</td>
                                                            <td>{{ $booking->user->first_name }}</td>
                                                            <td>{{ $stop->flight_number }}</td>
                                                            <td>{{ $stop->departureAirport ? $stop->departureAirport->state->name : 'No Departure Airport' }} ({{ $stop->departureAirport ? $stop->departureAirport->iata_code : 'N/A' }})</td>
                                                            <td>{{ $stop->arrivalAirport ? $stop->arrivalAirport->state->name : 'No Arrival Airport' }} ({{ $stop->arrivalAirport ? $stop->arrivalAirport->iata_code : 'N/A' }})</td>

                                                            <td>
                                                                {{ \Carbon\Carbon::parse($stop->departure_date)->format(config('GET.admin_date_time_format')) }}
                                                            </td>
                                                            <td>
                                                                {{ \Carbon\Carbon::parse($stop->arrival_date)->format(config('GET.admin_date_time_format')) }}
                                                            </td>
                                                            <td>
                                                                {{ $booking->flight_booking_type == 'return' ? 'Return' : 'Onway' }}
                                                            </td>
                                                            <td>
                                                                @if($booking->booking_status == 'Confirm')
                                                                    <span class="badge bg-success">{{ $booking->booking_status }}</span>
                                                                @elseif($booking->booking_status == 'Pending')
                                                                    <span class="badge bg-warning">{{ $booking->booking_status }}</span>
                                                                @elseif($booking->booking_status == 'Cancel')
                                                                    <span class="badge bg-danger">{{ $booking->booking_status }}</span>
                                                                @else
                                                                    <span class="badge bg-info">CONFIRMED</span>
                                                                @endif
                                                            </td>
                                                            <td>
                                                                @can('Flight Booking-view')
                                                                    <a href="{{ route('flight.view', ['id' => $booking->id]) }}">
                                                                        <button class="btn btn-info btn-sm" title="View">
                                                                            <i class="fas fa-eye"></i>
                                                                        </button>
                                                                    </a>
                                                                @endcan
                                                            </td>
                                                        </tr>
                                                      @endif
                                                    @endif
                                                @endforeach
                                            @endforeach

                                        @else
                                            <tr>
                                                <td colspan="11">
                                                    <h6><center>No Records Found</center></h6>
                                                </td>
                                            </tr>
                                        @endif
                                    </tbody>
                                </table>
                                </div>
                                @if ($bookings->total() > 0)
                                    <div class="d-flex justify-content-between align-items-center">
                                        <!-- Display custom number of entries -->
                                        <div class="entries-info">
                                            Showing {{ $bookings->firstItem() }} to {{ $bookings->lastItem() }} of {{ $bookings->total() }} entries
                                        </div>

                                        <!-- Pagination -->
                                        <div class="pagination-wrapper float-right">
                                            {{ $bookings->links('vendor.pagination.bootstrap-4') }}
                                        </div>
                                    </div>
                                @endif
                            </div>
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </div>
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const dateInput = document.getElementById('adminSerchField1');
            dateInput.addEventListener('click', function() {
                this.showPicker();
            });
        });
    </script>

<script>
    function updateToDate() {
        var fromDateInput = document.getElementById('adminSerchField1');
        var toDateInput = document.getElementById('adminSerchField2');
        
        // Get today's date
        var today = new Date().toISOString().split('T')[0];
        
        // Set the maximum value of "from_date" and "to_date" to today's date
        fromDateInput.max = today;
        toDateInput.max = today;

        // Enable the "to_date" input
        toDateInput.disabled = false;

        // Set the minimum value of "to_date" to be the same as "from_date"
        toDateInput.min = fromDateInput.value;

        // If the current value of "to_date" is less than "from_date", update it
        if (toDateInput.value < fromDateInput.value) {
            toDateInput.value = fromDateInput.value;
        }

        // Add an event listener to show the date picker
        toDateInput.addEventListener('click', function() {
            this.showPicker();
        });
    }

    // Set the max date to today on page load
    window.onload = function() {
        var fromDateInput = document.getElementById('adminSerchField1');
        var toDateInput = document.getElementById('adminSerchField2');
        var today = new Date().toISOString().split('T')[0];

        // Set the maximum value for both date fields
        fromDateInput.max = today;
        toDateInput.max = today;
    };
</script>

@stop
