<?php

namespace App\Exports;

use App\UmrahTripReservation;
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 UmrahTripReservationsExport 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 = UmrahTripReservation::orderBy('umrah_trip_reservations.id', 'DESC');

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

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

        $result->leftJoin('users', 'users.id', 'umrah_trip_reservations.user_id')

            ->join('umrah_trips', 'umrah_trips.id', 'umrah_trip_reservations.umrah_trip_id')
            ->join('umrah_trip_descriptions as tripDesc', 'tripDesc.umrah_trip_id', 'umrah_trips.id')
            ->where('tripDesc.language_id', $language_id)

            ->join('cities as departureCity', 'departureCity.id', 'umrah_trip_reservations.departure_city')
            ->join('city_descriptions as departureDesc', 'departureDesc.city_id', 'departureCity.id')
            ->where('departureDesc.language_id', $language_id)

            ->join('cities as arrivalCity', 'arrivalCity.id', 'umrah_trip_reservations.arrival_city')
            ->join('city_descriptions as arrivalDesc', 'arrivalDesc.city_id', 'arrivalCity.id')
            ->where('arrivalDesc.language_id', $language_id)->select(
                [
                    'umrah_trip_reservations.*',
                    'users.name as user_name',
                    'users.email as user_email',
                    'users.phone as user_phone',
                    'tripDesc.name as trip_name',
                    'departureDesc.name as departure_city',
                    'arrivalDesc.name as arrival_city',
                ]
            );

        return  $result->cursor();

    }


    public function map($data): array
    {

        $addresses = null;
        $pointsAddress = is_string($data->rally_point_info) ? json_decode($data->rally_point_info, true) : $data->rally_point_info;

        foreach ($pointsAddress as $address) {
            $addresses .= '('. $address['address'] . ') - ';
        }

        return [
            $data->id,
            $data->user_name ?? $data->name,
            $data->user_email ?? $data->email,
            $data->user_phone ?? $data->phone,
//            __('umrahTripReservations.'.$data->trip_type),
//            $data->trip_name,
            $data->trip_date,
            date('Y-m-d', strtotime($data->created_at)),
            $data->departure_city,
            $data->arrival_city,
            trim($addresses, ' - '),
            $data->person_count,
            __('umrahTripReservations.'.$data->house_type),
            $data->price_per_person,



        ];

    }


    public function headings(): array
    {
        return [
            [
                '#',
                __('umrahTripReservations.name'),
                __('umrahTripReservations.email'),
                __('umrahTripReservations.phone'),
//                __('umrahTripReservations.trip_type'),
//                __('umrahTripReservations.trip_name'),
                __('umrahTripReservations.trip_date'),
                __('umrahTripReservations.added_date'),
                __('umrahTripReservations.departure_city'),
                __('umrahTripReservations.arrival_city'),
                __('umrahTripReservations.point_address'),
                __('umrahTripReservations.person_count'),
                __('umrahTripReservations.house_type'),
                __('umrahTripReservations.price_per_person'),

            ],
        ];
    }


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

}
