<?php

namespace App\Exports;

use App\HotelReservation;
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 HotelReservationsExport 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 = HotelReservation::orderBy('hotel_reservations.id', 'DESC');

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

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

        $result->leftJoin('users', 'users.id', 'hotel_reservations.user_id')
            ->join('cities', 'cities.id', 'hotel_reservations.city_id')
            ->join('city_descriptions as cityDesc', 'cityDesc.city_id', 'cities.id')
            ->where('cityDesc.language_id', $language_id)
            ->join('hotels', 'hotels.id', 'hotel_reservations.hotel_id')
            ->join('hotel_descriptions as hotelDesc', 'hotelDesc.hotel_id', 'hotels.id')
            ->where('hotelDesc.language_id', $language_id)
            ->select(
                [
                    'hotel_reservations.*',
                    'users.name as user_name',
                    'users.email as user_email',
                    'users.phone as user_phone',
                    'cityDesc.name as city_name',
                    'hotels.email as hotel_email',
                    'hotels.address as hotel_address',
                    'hotelDesc.name as hotel_name',
                ]
            );

        return  $result->cursor();

    }


    public function map($data): array
    {
        $added_service = array_map(function ($value){
            return __('hotels.'.$value);
        }, $data->added_service);

        $roomTypes = [
            1 => __('hotels.individually'),
            2 => __('hotels.couple'),
            3 => __('hotels.tripartite'),
            4 => __('hotels.quadrilateral'),
        ];


        return [
            $data->id,
            $data->user_name ?? $data->name,
            $data->user_email ?? $data->email,
            $data->user_phone ?? $data->phone,
            $data->hotel_name,
            $data->hotel_email,
            $data->hotel_address,
            $data->city_name,
            $roomTypes[$data->room_type],
            $data->days_count,
            $data->person_count,
            date('Y-m-d', strtotime($data->going_date)),
            date('Y-m-d', strtotime($data->return_date)),
            implode(' - ', $added_service),
            implode(' - ', $data->added_service_price),
            $data->total,

        ];

    }


    public function headings(): array
    {
        return [
            [
                '#',
                __('hotel_reservations.name'),
                __('hotel_reservations.user_email'),
                __('hotel_reservations.user_phone'),
                __('hotel_reservations.hotel_name'),
                __('hotel_reservations.hotel_email'),
                __('hotel_reservations.hotel_address'),
                __('hotel_reservations.city_name'),
                __('hotel_reservations.room_type'),
                __('hotel_reservations.days_count'),
                __('hotel_reservations.person_count'),
                __('hotel_reservations.going_date'),
                __('hotel_reservations.return_date'),
                __('hotel_reservations.added_services'),
                __('hotel_reservations.added_services_price'),
                __('hotel_reservations.total'),
            ],
        ];
    }


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

}
