<?php

namespace App\Exports\V2\SafetyAndPrivacy\SafetyAndPrivacyTable;

use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;
use App\Models\SafetyAndPrivacy;
use App\Models\Province;
use App\Models\EdarateShahri;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Events\AfterSheet;
use Maatwebsite\Excel\Concerns\WithEvents;
use Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Concerns\WithDrawings;
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;

class Cities implements FromView, ShouldAutoSize, WithEvents, WithDrawings
{
    public function __construct($from_date, $date_to)
    {
        $this->from_date = $from_date;
        $this->date_to = $date_to;
    }

    public function view(): View
    {
        $from_date = $this->from_date ?? Date('Y-m-d'). " 00:00:00";
        $date_to = $this->date_to ?? Date('Y-m-d')." 23:59:59";

        $all_province_data = SafetyAndPrivacy::provinceData($from_date, $date_to);
        foreach (Province::all() as $province) {
            $edarat_data = SafetyAndPrivacy::EdareData($from_date, $date_to, $province->id);
            foreach (EdarateShahri::where("province_id", $province->id)->get() as $edare_shahri) {
                $edarat_data[$edare_shahri->id]['is_province'] = 0;
                $array[] = $edarat_data[$edare_shahri->id];
            }

            $all_province_data[$province->id]["is_province"] = 1;
            $array[] = $all_province_data[$province->id];

        }

        return view('excel.V2.SafetyAndPrivacy.SafetyAndPrivacyTable.City', [
            'data' => $array,
            'fromFa' => $from_date,
            'toFa' => $date_to
        ]);
    }

    /**
     * @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('K1');
        return [$drawing, $drawing2];
    }
}
