<?php

namespace App\Exports;

use App\Models\Cruise;
use App\Models\Ticket;
use Carbon\Carbon;
use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;

class PassengerReportExport implements FromView
{
    public function view(): View
    {
        $cruise = Cruise::findOrFail(request()->query('cruise_id', Cruise::where('status', 1)->firstOrFail()->id));

        $qb = Ticket::with(['segment.cruise', 'boardingPass', 'boardingPass.room'])->whereHas('segment.cruise', function ($query) use ($cruise) {
            $query->where('id', $cruise->id);
        });
        
        if(request()->query('status') == "O" ){
            $qb->where('ticket_status', '=', 'O')->orderBy('created_at', 'desc');
        }else if(request()->query('status') == "U" ){
            $qb->whereHas('boardingPass', function ($query) {
                $query->whereNotNull('created_at');
            })
            ->where('ticket_status', '=', 'U')->orderBy('created_at', 'desc');
        }
           else if(request()->query('status')=='D'){
            $qb = Ticket::with(['segment.cruise', 'boardingPass', 'boardingPass.room','car']);
                    $qb->whereHas('boardingPass', function ($query) {
            $query->wheredate('created_at',Carbon::now());
        })
        ->where('ticket_status', '=', 'U')->orderBy('created_at', 'desc');
        }
        
        return view('passengerReports.exports', [
            'tickets' => $qb->get(),
            'status'=>'passenger'
        ]);
    }
}
