<?php

namespace App\Exports\V2\RoadObservation;

use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;
use App\Models\RoadItemsProject;
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;
use Hekmatinasser\Verta\Verta;

use App\Models\RoadObserved;

class PendingComplaintsExport implements FromView, ShouldAutoSize, WithEvents, WithDrawings, WithStyles
{
    public function __construct($province_id, $fromDate, $toDate)
    {
        $this->province_id = $province_id;
        $this->fromDate = $fromDate;
        $this->toDate = $toDate;
    }

    public function view(): View
    {
        $fromDate = $this->fromDate ? $this->fromDate . ' 00:00:00' : null;
        $toDate = $this->toDate ? $this->toDate . ' 23:59:59' : null;

        if (auth()->user()->hasPermissionTo('show-fast-react')) {
            $data = RoadObserved::leftjoin('edarate_shahri', 'road_observeds.edarate_shahri_id', 'edarate_shahri.id')
                ->where('rms_status', 0)
                ->whereNotNull('edarate_shahri_id')
                ->when($this->province_id, function ($query, $province_id) {
                    return $query->where('road_observeds.province_id', $province_id);
                })
                ->when(($fromDate && $toDate), function ($query) use ($fromDate, $toDate) {
                    return $query->whereBetween('road_observeds.created_at', [$fromDate, $toDate]);
                })
                ->selectRaw('fk_RegisteredEventMessage, road_observeds.id, Title, FeatureTypeTitle, MobileForSendEventSms,
                                             road_observeds.created_at, rms_last_activity, rms_last_activity_fa, status, 
                                             status_fa, supervisor_description, rms_description, province_fa, city_fa, Description, edarate_shahri.name_fa as edarate_shahri_name_fa, lat, lng')
                ->orderBy('status')
                ->orderBy('road_observeds.id', 'desc')
                ->get();


        } elseif (auth()->user()->hasPermissionTo('show-fast-react-province')) {
            if (is_null(auth()->user()->province_id)) {
                return response()->json([
                    'message' => 'استانی برای شما در سامانه ثبت نشده است!'
                ], 400);
            }

            $data = RoadObserved::leftjoin('edarate_shahri', 'road_observeds.edarate_shahri_id', 'edarate_shahri.id')
                ->where('rms_province_id', auth()->user()->province_id)
                ->where('rms_status', 0)
                ->whereNotNull('road_observeds.province_id')
                ->when(($fromDate && $toDate), function ($query) use ($fromDate, $toDate) {
                    return $query->whereBetween('road_observeds.created_at', [$fromDate, $toDate]);
                })
                ->selectRaw('fk_RegisteredEventMessage, road_observeds.id, Title, FeatureTypeTitle, MobileForSendEventSms,
                                             road_observeds.created_at, rms_last_activity, rms_last_activity_fa, status, 
                                             status_fa, supervisor_description, rms_description, province_fa, city_fa, Description, edarate_shahri.name_fa as edarate_shahri_name_fa, lat, lng')
                ->orderBy('status')
                ->orderBy('road_observeds.id', 'desc')
                ->get();

        } elseif (auth()->user()->hasPermissionTo('show-fast-react-edarate-shahri')) {
            if (is_null(auth()->user()->edarate_shahri_id)) {
                return response()->json([
                    'message' => 'اداره ای برای شما در سامانه ثبت نشده است!'
                ], 400);
            }

            $data = RoadObserved::leftjoin('edarate_shahri', 'road_observeds.edarate_shahri_id', 'edarate_shahri.id')
                ->where('edarate_shahri_id', auth()->user()->edarate_shahri_id)
                ->where('rms_status', 0)
                ->whereNotNull('edarate_shahri_id')
                ->when(($fromDate && $toDate), function ($query) use ($fromDate, $toDate) {
                    return $query->whereBetween('road_observeds.created_at', [$fromDate, $toDate]);
                })
                ->selectRaw('fk_RegisteredEventMessage, road_observeds.id, Title, FeatureTypeTitle, MobileForSendEventSms,
                                             road_observeds.created_at, rms_last_activity, rms_last_activity_fa, status, 
                                             status_fa, supervisor_description, rms_description, province_fa, city_fa, Description, edarate_shahri.name_fa as edarate_shahri_name_fa, lat, lng')
                ->orderBy('status')
                ->orderBy('road_observeds.id', 'desc')
                ->get();


        }

        return view('excel.V2.RoadObservation.pending_complaints', [
            'data' => $data,
            'fromDate' => $fromDate,
            'toDate' => $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'
                ]
            ],
        ];

    }
}