<?php

namespace App\Http\Controllers;

use App\DataTables\TransactionDataTable;
use Illuminate\Http\Request;
use App\Exports\TransactionExport;
use Carbon\Carbon;
use App\Models\BoardingPass;
use App\Models\Office;
use Maatwebsite\Excel\Facades\Excel;


class BoardingPassController extends Controller
{
    public function index()
    {
           if(auth()->user()->can('show_boarding_pases')){
               
            $boarding_pases=BoardingPass::where(function($q)
            {
               if(request()->query('ticket_number')){
                    $q->whereHas('ticket',function($q){
                      $boarding_pases= $q->where('ticket_number','LiKE','%'.request()->query('ticket_number').'%');
                    });
               }
          
               if(request()->query('code')){
                   $boarding_pases= $q->where('code','like','%'.request()->query('code').'%');
               }
               if(request()->query('fullName')){
                    $q->whereHas('ticket',function($q){
                      $boarding_pases=  $q->where('firstName','LiKE','%'.request()->query('fullName').'%')->orwhere('lastName','LiKE','%'.request()->query('fullName').'%');
                    });
               }
               if(request()->query('cruise_id')){
                    $q->whereHas('ticket.segment.cruise',function($q){
                      $boarding_pases=  $q->where('id',request()->query('cruise_id'));
                    });
               }
               if(request()->query('passport')){
                    $q->whereHas('ticket',function($q){
                      $boarding_pases=  $q->where('passport','LiKE','%'.request()->query('passport').'%');
                    });
               }
           })->paginate(20);       

             return view('boarding_pases.index',compact('boarding_pases'));
           }
           else{
                abort(401,'you cant see');
           }
    
}
      public function export() 
    {
        $office = Office::find(request()->query('office_id'));
        $name = $office?$office->name:auth()->user()->name;
        $name .= '-'.request()->query('from').'-'.request()->query('to');
        return Excel::download(new TransactionExport, $name.'.xlsx');
    }
}
