<?php

namespace App\Http\Controllers;

use App\Exports\PassengerReportExport;
use App\Exports\CarReportExport;
use App\Exports\ImageExport;
use App\Models\Cruise;
use App\Models\Ticket;
use URL;
use File;
use Storage;
use ZipArchive;

use Carbon\Carbon;
use Illuminate\Support\Facades\Response;
use Illuminate\Http\Request;

use Maatwebsite\Excel\Facades\Excel;

class PassengerReportController extends Controller
{
    public function index(Request $request)
    {
         if(request()->query('cruise_id') ){
             
            $cruise = Cruise::findOrFail(request()->query('cruise_id', Cruise::where('status', 1)->firstOrFail()->id));
         }
         else{
             
          $cruise = Cruise::findOrFail(1);
         }


        $qb = Ticket::with(['segment.cruise', 'boardingPass', 'boardingPass.room','car'])->whereHas('segment.cruise', function ($query) use ($cruise) {
            $query->where('id', $cruise->id);
        });
        if($request->status == "O" ){
            $qb->where('ticket_status', '=', 'O')->orderBy('created_at', 'desc');
        }else if($request->status == "U"){
            $qb->whereHas('boardingPass', function ($query) {
                $query->whereNotNull('created_at');
            })
            ->where('ticket_status', '=', 'U')->orderBy('created_at', 'desc');
        }
        else if($request->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');
        }
      
        $tickets = $qb->paginate(10);
        return view('passengerReports.index', compact('tickets', 'cruise'));
    }

    public function export()
    { 
        $cruise = Cruise::findOrfail(request()->query('cruise_id'));
        $name = $cruise->name . '-' . $cruise->cruise_number . '.xlsx';
        return Excel::download(new PassengerReportExport, $name);
    }
    
    
    public function exportCar()
    {
        $cruise = Cruise::findOrfail(request()->query('cruise_id'));
        $name = $cruise->name . '-' . $cruise->cruise_number . '.xlsx';
        return Excel::download(new CarReportExport, $name);
    }
    public function exportImage()
    {
  
        $cruise = Cruise::findOrFail(request()->query('cruise_id', Cruise::where('status', 1)->firstOrFail()->id));
    

        $qb = Ticket::with(['segment.cruise', 'boardingPass', 'boardingPass.room','car'])->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');
        }
      
        $tickets = $qb->where('passport_file','!=',null)->pluck('passport_file')->toarray();

        if($tickets!=null){
            File::delete(public_path().'/images.zip');
         
            $files=$tickets;
            $zipname = 'images.zip';
            $zip = new ZipArchive;
            $zip->open($zipname, ZipArchive::CREATE);
            foreach ($files as $file) {
                if (file_exists(public_path('uploads/passport/'.$file))) {

                   $zip->addFile(public_path('uploads/passport/'.$file));
                }
            }
            $zip->close();
            // dd(public_path($zipname));
         return response()->download(public_path($zipname));
        }
        return back();
        
    }
}
