<?php

namespace App\Exports;

use App\Flight;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;
use Maatwebsite\Excel\Concerns\WithStyles;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
use Illuminate\Contracts\Queue\ShouldQueue;


class FlightsExport implements FromCollection, WithHeadings, WithStyles, WithMapping
{
    use Exportable;

    private $dateFrom;
    private $dateTo;
    private $role;

//    public function __construct($role, $dateFrom, $dateTo)
//    {
//        $this->dateFrom = $dateFrom;
//        $this->dateTo = $dateTo;
//        $this->role = $role;
//    }

    /**
     * @return \Illuminate\Support\Collection
     */
    public function collection()
    {

        $result = Flight::latest();

        if (request()->query('ids')){
            $ids = request()->query('ids');
            $ids = explode(',', $ids);

            $result->whereIn('flights.id', $ids);
        }
        $language_id = currentLanguage()->id;

        $result->join('cities as departureCity', 'departureCity.id', 'flights.departure_city')
            ->join('city_descriptions as departureDesc', 'departureDesc.city_id', 'departureCity.id')
            ->where('departureDesc.language_id', $language_id)
            ->join('cities as arrivalCity', 'arrivalCity.id', 'flights.arrival_city')
            ->join('city_descriptions as arrivalDesc', 'arrivalDesc.city_id', 'arrivalCity.id')
            ->where('arrivalDesc.language_id', $language_id)

            ->join('airports as departureAir', 'departureAir.id', 'flights.departure_airport')
            ->join('airport_descriptions as departureAirDesc', 'departureAirDesc.airport_id', 'departureAir.id')
            ->where('departureAirDesc.language_id', $language_id)
            ->join('airports as arrivalAir', 'arrivalAir.id', 'flights.arrival_airport')
            ->join('airport_descriptions as arrivalAirDesc', 'arrivalAirDesc.airport_id', 'arrivalAir.id')
            ->where('arrivalAirDesc.language_id', $language_id)
            ->select(
                [
                    'flights.*',
                    'departureDesc.name as departure_city',
                    'arrivalDesc.name as arrival_city',
                    'departureAirDesc.name as departure_airport',
                    'arrivalAirDesc.name as arrival_airport',
                ]
            );

        return  $result->cursor();

    }


    public function map($data): array
    {
        return [
            $data->id,
            $data->name,
            $data->email,
            $data->phone,
            $data->babies_count,
            $data->children_count,
            $data->adult_count,
            __('flights.'.$data->trip_type),
            date('Y-m-d', strtotime($data->going_date)),
            date('Y-m-d', strtotime($data->return_date)),
            __('flights.'.$data->trip_direction),
            __('flights.'.$data->class_type),
            $data->departure_city,
            $data->arrival_city,
            $data->departure_airport,
            $data->arrival_airport,

        ];

    }


    public function headings(): array
    {
        return [
            [
                '#',
                __('flights.name'),
                __('flights.email'),
                __('flights.phone'),
                __('flights.babies_count'),
                __('flights.children_count'),
                __('flights.adult_count'),
                __('flights.trip_type'),
                __('flights.going_date'),
                __('flights.return_date'),
                __('flights.trip_direction'),
                __('flights.class_type'),
                __('flights.departure_city'),
                __('flights.arrival_city'),
                __('flights.departure_airport'),
                __('flights.arrival_airport'),
            ],
        ];
    }


    public function styles(Worksheet $sheet)
    {
        return [
            1 => ['font' => ['bold' => true]],
        ];
    }

}
