<?php
if (Auth('admin')->User()->dashboard_style == 'light') {
    $text = 'dark';
    $bg = 'light';
} else {
    $bg = 'dark';
    $text = 'light';
}
?>
@extends('layouts.app')

@section('title', $title ?? 'Flight Bookings Management')

@section('content')
    @include('admin.topmenu')
    @include('admin.sidebar')
    <div class="main-panel">
        <div class="content">
            <div class="page-inner">
                <div class="mt-2 mb-4">
                    <h1 class="title1 text-{{ $text }}">{{ $title ?? 'Flight Bookings Management' }}</h1>
                    <p class="text-muted mt-1">Manage and review military flight booking requests</p>
                </div>

                <x-danger-alert />
                <x-success-alert />

                <!-- Statistics Cards -->
                <div class="row mb-4">
                    <div class="col-xl-3 col-md-6 mb-4">
                        <div class="card border-left-primary shadow h-100 py-2">
                            <div class="card-body">
                                <div class="row no-gutters align-items-center">
                                    <div class="col mr-2">
                                        <div class="text-xs font-weight-bold text-primary text-uppercase mb-1">Total Bookings</div>
                                        <div class="h5 mb-0 font-weight-bold text-{{ $text }}">{{ $stats['total'] }}</div>
                                    </div>
                                    <div class="col-auto">
                                        <i class="fas fa-plane fa-2x text-gray-300"></i>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="col-xl-3 col-md-6 mb-4">
                        <div class="card border-left-warning shadow h-100 py-2">
                            <div class="card-body">
                                <div class="row no-gutters align-items-center">
                                    <div class="col mr-2">
                                        <div class="text-xs font-weight-bold text-warning text-uppercase mb-1">Unread</div>
                                        <div class="h5 mb-0 font-weight-bold text-{{ $text }}">{{ $stats['unread'] }}</div>
                                    </div>
                                    <div class="col-auto">
                                        <i class="fas fa-envelope fa-2x text-gray-300"></i>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="col-xl-3 col-md-6 mb-4">
                        <div class="card border-left-success shadow h-100 py-2">
                            <div class="card-body">
                                <div class="row no-gutters align-items-center">
                                    <div class="col mr-2">
                                        <div class="text-xs font-weight-bold text-success text-uppercase mb-1">Processed</div>
                                        <div class="h5 mb-0 font-weight-bold text-{{ $text }}">{{ $stats['read'] }}</div>
                                    </div>
                                    <div class="col-auto">
                                        <i class="fas fa-check-circle fa-2x text-gray-300"></i>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="col-xl-3 col-md-6 mb-4">
                        <div class="card border-left-info shadow h-100 py-2">
                            <div class="card-body">
                                <div class="row no-gutters align-items-center">
                                    <div class="col mr-2">
                                        <div class="text-xs font-weight-bold text-info text-uppercase mb-1">This Week</div>
                                        <div class="h5 mb-0 font-weight-bold text-{{ $text }}">{{ $stats['recent'] }}</div>
                                    </div>
                                    <div class="col-auto">
                                        <i class="fas fa-calendar-week fa-2x text-gray-300"></i>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Search and Filter Section -->
                <div class="mb-5 row">
                    <div class="col-md-12">
                        <div class="card p-md-4 p-2">
                            <div class="card-header py-3">
                                <h6 class="m-0 font-weight-bold text-primary">
                                    <i class="fas fa-search mr-2"></i>Search & Filter Bookings
                                </h6>
                            </div>
                            <div class="card-body">
                                <form method="GET" action="{{ route('admin.flight-bookings.index') }}" class="row">
                                    <div class="col-md-4 mb-3">
                                        <label for="search" class="form-label text-{{ $text }} font-weight-bold">Search Bookings</label>
                                        <input type="text" class="form-control text-{{ $text }}" id="search" name="search"
                                               value="{{ request('search') }}"
                                               placeholder="Search by name, email, cities, or service branch...">
                                    </div>
                                    <div class="col-md-3 mb-3">
                                        <label for="status" class="form-label text-{{ $text }} font-weight-bold">Status Filter</label>
                                        <select class="form-control text-{{ $text }}" id="status" name="status">
                                            <option value="">All Status</option>
                                            <option value="unread" {{ request('status') === 'unread' ? 'selected' : '' }}>Unread</option>
                                            <option value="read" {{ request('status') === 'read' ? 'selected' : '' }}>Read</option>
                                            <option value="replied" {{ request('status') === 'replied' ? 'selected' : '' }}>Replied</option>
                                            <option value="archived" {{ request('status') === 'archived' ? 'selected' : '' }}>Archived</option>
                                        </select>
                                    </div>
                                    <div class="col-md-3 d-flex align-items-end mb-3">
                                        <button type="submit" class="btn btn-primary mr-2">
                                            <i class="fas fa-search mr-1"></i>Search
                                        </button>
                                        <a href="{{ route('admin.flight-bookings.index') }}" class="btn btn-secondary">
                                            <i class="fas fa-undo mr-1"></i>Reset
                                        </a>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Flight Bookings Table -->
                <div class="mb-5 row">
                    <div class="col-md-12">
                        <div class="card p-md-4 p-2">
                            <div class="card-header py-3">
                                <h6 class="m-0 font-weight-bold text-primary">
                                    <i class="fas fa-plane mr-2"></i>Flight Bookings ({{ $bookings->total() }} total)
                                </h6>
                            </div>
                            <div class="card-body">
                                @if($bookings->count() > 0)
                                    <div class="table-responsive">
                                        <table class="table table-bordered" width="100%" cellspacing="0">
                                            <thead class="thead-light">
                                                <tr>
                                                    <th class="text-{{ $text }}">ID</th>
                                                    <th class="text-{{ $text }}">Passenger Details</th>
                                                    <th class="text-{{ $text }}">Flight Details</th>
                                                    <th class="text-{{ $text }}">Service Info</th>
                                                    <th class="text-{{ $text }}">Submitted</th>
                                                    <th class="text-{{ $text }}">Status</th>
                                                    <th class="text-{{ $text }}">Actions</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                @foreach($bookings as $booking)
                                                    <tr>
                                                        <td class="font-weight-bold text-{{ $text }}">
                                                            FB-{{ str_pad($booking->id, 6, '0', STR_PAD_LEFT) }}
                                                        </td>
                                                        <td class="text-{{ $text }}">
                                                            <div class="font-weight-bold">{{ $booking->fname }}</div>
                                                            <small class="text-muted">{{ $booking->email }}</small>
                                                            <br>
                                                            <small class="text-muted">📞 {{ $booking->phone }}</small>
                                                        </td>
                                                        <td class="text-{{ $text }}">
                                                            @if($booking->departure_city && $booking->destination_city)
                                                                <div>
                                                                    <span class="badge badge-info">{{ $booking->departure_city }}</span>
                                                                    <i class="fas fa-arrow-right mx-1"></i>
                                                                    <span class="badge badge-success">{{ $booking->destination_city }}</span>
                                                                </div>
                                                                @if($booking->departure_date)
                                                                    <small class="text-muted">📅 {{ date('M d, Y', strtotime($booking->departure_date)) }}</small>
                                                                @endif
                                                                @if($booking->passengers)
                                                                    <br><small class="text-muted">👥 {{ $booking->passengers }}</small>
                                                                @endif
                                                            @else
                                                                <span class="text-muted">No flight details</span>
                                                            @endif
                                                        </td>
                                                        <td class="text-{{ $text }}">
                                                            @if($booking->rank)
                                                                <span class="badge badge-secondary">{{ $booking->rank }}</span><br>
                                                            @endif
                                                            @if($booking->service_branch)
                                                                <small class="text-muted">{{ $booking->service_branch }}</small>
                                                            @endif
                                                            @if($booking->military_status)
                                                                <br><small class="text-muted">{{ $booking->military_status }}</small>
                                                            @endif
                                                        </td>
                                                        <td class="text-{{ $text }}">{{ $booking->formatted_created_at }}</td>
                                                        <td>
                                                            @if($booking->status === 'unread')
                                                                <span class="badge badge-warning">
                                                                    <i class="fas fa-envelope mr-1"></i>Unread
                                                                </span>
                                                            @elseif($booking->status === 'read')
                                                                <span class="badge badge-info">
                                                                    <i class="fas fa-envelope-open mr-1"></i>Read
                                                                </span>
                                                            @elseif($booking->status === 'replied')
                                                                <span class="badge badge-success">
                                                                    <i class="fas fa-reply mr-1"></i>Replied
                                                                </span>
                                                            @elseif($booking->status === 'archived')
                                                                <span class="badge badge-secondary">
                                                                    <i class="fas fa-archive mr-1"></i>Archived
                                                                </span>
                                                            @endif
                                                        </td>
                                                        <td>
                                                            <div class="btn-group" role="group">
                                                                <a href="{{ route('admin.flight-bookings.show', $booking) }}"
                                                                   class="btn btn-sm btn-info" title="View Details">
                                                                    <i class="fas fa-eye"></i>
                                                                </a>

                                                                @if($booking->status === 'unread')
                                                                    <form method="POST" action="{{ route('admin.flight-bookings.update-status', $booking) }}" class="d-inline">
                                                                        @csrf
                                                                        @method('PATCH')
                                                                        <input type="hidden" name="status" value="read">
                                                                        <button type="submit" class="btn btn-sm btn-primary" title="Mark as Read"
                                                                                onclick="return confirm('Mark this booking as read?')">
                                                                            <i class="fas fa-envelope-open"></i>
                                                                        </button>
                                                                    </form>
                                                                @endif

                                                                @if($booking->status !== 'replied')
                                                                    <form method="POST" action="{{ route('admin.flight-bookings.update-status', $booking) }}" class="d-inline">
                                                                        @csrf
                                                                        @method('PATCH')
                                                                        <input type="hidden" name="status" value="replied">
                                                                        <button type="submit" class="btn btn-sm btn-success" title="Mark as Replied"
                                                                                onclick="return confirm('Mark this booking as replied?')">
                                                                            <i class="fas fa-reply"></i>
                                                                        </button>
                                                                    </form>
                                                                @endif

                                                                <form method="POST" action="{{ route('admin.flight-bookings.destroy', $booking) }}" class="d-inline">
                                                                    @csrf
                                                                    @method('DELETE')
                                                                    <button type="submit" class="btn btn-sm btn-danger" title="Delete"
                                                                            onclick="return confirm('Are you sure you want to delete this flight booking? This action cannot be undone.')">
                                                                        <i class="fas fa-trash"></i>
                                                                    </button>
                                                                </form>
                                                            </div>
                                                        </td>
                                                    </tr>
                                                @endforeach
                                            </tbody>
                                        </table>
                                    </div>

                                    <!-- Pagination -->
                                    <div class="d-flex justify-content-center">
                                        {{ $bookings->appends(request()->query())->links() }}
                                    </div>
                                @else
                                    <div class="text-center py-5">
                                        <i class="fas fa-plane fa-3x text-gray-300 mb-3"></i>
                                        <h5 class="text-{{ $text }}">No flight bookings found</h5>
                                        <p class="text-muted">
                                            @if(request('search') || request('status'))
                                                Try adjusting your search criteria or filters.
                                            @else
                                                No flight booking requests have been submitted yet.
                                            @endif
                                        </p>
                                    </div>
                                @endif
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

@if(session('success'))
    <script>
        Swal.fire({
            icon: 'success',
            title: 'Success!',
            text: '{{ session('success') }}',
            confirmButtonColor: '#28a745'
        });
    </script>
@endif

@if(session('error'))
    <script>
        Swal.fire({
            icon: 'error',
            title: 'Error!',
            text: '{{ session('error') }}',
            confirmButtonColor: '#dc3545'
        });
    </script>
@endif
@endsection
