<?php

namespace App\Export;

use Carbon\Carbon;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Facades\Input;
use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\FromView;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Events\AfterSheet;
use PhpOffice\PhpSpreadsheet\Shared\Date;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;

class PropertyBookingListExport implements FromArray, WithHeadings, ShouldAutoSize, WithEvents, WithColumnFormatting
{
    public function __construct($dataTableData = "")
    {
        $this->dataTableData = $dataTableData;
    }

    public function registerEvents(): array
    {
        return [
            AfterSheet::class => function (AfterSheet $event) {
                $cellRange = 'A1:J1'; // All headers
                $event->sheet->getDelegate()->getStyle($cellRange)->getFont()->setBold(true);
                //$event->sheet->setAutoFilter($cellRange);
                $event->sheet->setTitle('Property Booking List');
            },
        ];
    }

    public function columnFormats(): array
    {
        return [
            'D' => NumberFormat::FORMAT_DATE_DDMMYYYY,
            'E' => NumberFormat::FORMAT_DATE_DDMMYYYY,
            'J' => NumberFormat::FORMAT_DATE_DATETIME,
        ];
    }

    public function headings(): array
    {
        $resetData = reset($this->dataTableData);
        $header = array_keys($resetData);

        return $header;
    }

    public function array(): array
    {

        $dataTableData = [];

        foreach ($this->dataTableData as $dataOrderKey => $dataBooking) {

            foreach ($dataBooking as $dataKey => $value) {

                if (in_array($dataKey, ['checkin_date', 'checkout_date', 'reservation_time'])) {
                    $value = Date::dateTimeToExcel(Carbon::parse($value));
                }

                $dataTableData[$dataOrderKey][$dataKey] = $value;

            }

        }


        return [$dataTableData];
    }

}
