<?php

namespace App\Exports;

use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;
use App\Models\RoadObserved;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Events\AfterSheet;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Concerns\WithDrawings;
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
use Maatwebsite\Excel\Concerns\WithStyles;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;

class FastNewReactDoneExport implements FromView, ShouldAutoSize, WithEvents, WithDrawings, WithStyles
{
    public function __construct($fromDate = null, $toDate = null, $province = null)
    {
        $this->fromDate = $fromDate ?? null;
        $this->toDate = $toDate ?? null;
        $this->province = $province ?? null;
    }

    public function view(): View
    {
        $fromDate = $this->fromDate ?? \Carbon\Carbon::now()->subDays(3);
        $toDate = $this->toDate ?? \Carbon\Carbon::now();
        $province = $this->province;
        
        $data = \App\Models\RoadObserved::select('id', 'ProvinceName', 'TownName', 'FeatureTypeTitle', 'created_at', 'Title', 'Description', 'MobileForSendEventSms', 'lat', 'lng', 'rms_description', 'updated_at', 'rms_status')
                                        ->where('rms_status', '!=','0')
                                        ->whereIn('fk_FeatureType', ['1', '2', '3', '4', '5', '6'])
                                        ->whereBetween('created_at', [$fromDate . ' 00:00:00', $toDate . ' 23:59:59'])
                                        ->when($province, function ($query, $province) {
                                            return $query->where('rms_province_id', $province);
                                        })
                                        ->get();
        // $array = [];
        // foreach ($data as $item) {
        //     // switch ($item->rms_status) {
        //     //     case '0':
        //     //         $rms_status_fa = 'رسیدگی نشده';
        //     //         break;
        //     //     case '1':
        //     //         $rms_status_fa = 'رسیدگی شده';
        //     //         break;
        //     //     case '2':
        //     //         $rms_status_fa = 'رسیدگی شده';
        //     //         break;
        //     // }

        //     $array[] = [
        //         'id' => $item->id,
        //         'ProvinceName' => $item->ProvinceName,
        //         'TownName' => $item->TownName,
        //         'FeatureTypeTitle' => $item->FeatureTypeTitle,
        //         'Title' => $item->Title,
        //         'created_at' => verta($item->created_at)->format('Y/m/d'),
        //         'Description' => $item->Description,
        //         'MobileForSendEventSms' => $item->MobileForSendEventSms,
        //         'lat' => $item->lat,
        //         'lng' => $item->lng,
        //         'rms_description' => $item->rms_description,
        //         'updated_at' => verta($item->updated_at)->format('Y/m/d'),
        //         'rms_status' => $item->rms_status,
        //     ];
        // }

        return view('excel.FastNewReactDone', [
            'roads' => $data,
            'fromFa' => $fromDate,
            'toFa' => $toDate
        ]);
    }

    /**
     * @return array
    */
    public function registerEvents(): array
    {
        return [
            AfterSheet::class    => function(AfterSheet $event) {
                $event->sheet->getDelegate()->setRightToLeft(true);
            },
        ];
    }

    public function drawings()
    {
        $drawing = new Drawing();
        $drawing->setName('Logo');
        $drawing->setDescription('This is my logo');
        $drawing->setPath(public_path('/dist/logo.png'));
        $drawing->setWidth(50);
        $drawing->setHeight(50);
        $drawing->setOffsetX(5);
        $drawing->setOffsetY(5);
        $drawing->setCoordinates('A1');
        
        $drawing2 = new Drawing();
        $drawing2->setName('Logo');
        $drawing2->setDescription('This is my logo');
        $drawing2->setPath(public_path('/dist/141icon.png'));
        $drawing2->setWidth(50);
        $drawing2->setHeight(50);
        $drawing2->setOffsetX(5);
        $drawing2->setOffsetY(5);
        $drawing2->setCoordinates('B1');
        return [$drawing, $drawing2];
    }

    public function styles(Worksheet $sheet)
    {

        return [
            // Style the first row as bold text.
            'A:BA' =>    [
                'font' => [
                    'name' => 'B Nazanin'
                ]
            ],
        ];

    }
}
