<?php

namespace App\Exports\Activities\One;

use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;
use App\Models\RoadItemsProject;
use App\Models\City;
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($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 ?? '2021-03-21';
        $toDate = $this->toDate ?? \Carbon\Carbon::now();
        $province = $this->province;

        if ($this->province) {
            foreach (EdarateShahri::when($province, function ($query, $province) {
                return $query->where('province_id', $province);
            })->get() as $item) {
                $array[$item->province_id]['city'][$item->id]['name']= $item->name_fa;
                $array[$item->province_id]['province_name']= $item->province->name_fa;

                $array[$item->province_id]['city'][$item->id]['data']['MarematRooyeCount']= 0;
                $array[$item->province_id]['city'][$item->id]['data']['PaksaziCount']= 0;
                $array[$item->province_id]['city'][$item->id]['data']['AlaemCount']= 0;
                $array[$item->province_id]['city'][$item->id]['data']['HefazCount']= 0;
                $array[$item->province_id]['city'][$item->id]['data']['RoshanayiCount']= 0;
                $array[$item->province_id]['city'][$item->id]['data']['KhatkeshiCount']= 0;
                $array[$item->province_id]['city'][$item->id]['data']['RangamiziCount']= 0;
                $array[$item->province_id]['city'][$item->id]['data']['WashingCount']= 0;
                $array[$item->province_id]['city'][$item->id]['data']['ImenSaziCount']= 0;
                $array[$item->province_id]['city'][$item->id]['data']['HarimCount']= 0;
                $array[$item->province_id]['city'][$item->id]['data']['PolCount']= 0;
                $array[$item->province_id]['city'][$item->id]['data']['ToonelCount']= 0;
                $array[$item->province_id]['city'][$item->id]['data']['AmaliatZemestaniCount']= 0;
                $array[$item->province_id]['city'][$item->id]['data']['MashinAlatCount']= 0;
                $array[$item->province_id]['city'][$item->id]['data']['TollhouseCount']= 0;
                $array[$item->province_id]['city'][$item->id]['data']['EmergencyCount']= 0;
                $array[$item->province_id]['city'][$item->id]['data']['Count']= 0;
            }
        }

        $query = "select
        COUNT(CASE WHEN item = 1 THEN 1 END) as MarematRooyeCount,
        COUNT(CASE WHEN item = 2 THEN 1 END) as PaksaziCount,
        COUNT(CASE WHEN item = 3 THEN 1 END) as AlaemCount,
        COUNT(CASE WHEN item = 4 THEN 1 END) as HefazCount,
        COUNT(CASE WHEN item = 5 THEN 1 END) as RoshanayiCount,
        COUNT(CASE WHEN item = 6 THEN 1 END) as KhatkeshiCount,
        COUNT(CASE WHEN item = 7 THEN 1 END) as RangamiziCount,
        COUNT(CASE WHEN item = 8 THEN 1 END) as WashingCount,
        COUNT(CASE WHEN item = 9 THEN 1 END) as ImenSaziCount,
        COUNT(CASE WHEN item = 10 THEN 1 END) as HarimCount,
        COUNT(CASE WHEN item = 11 THEN 1 END) as PolCount,
        COUNT(CASE WHEN item = 12 THEN 1 END) as ToonelCount,
        COUNT(CASE WHEN item = 13 THEN 1 END) as AmaliatZemestaniCount,
        COUNT(CASE WHEN item = 14 THEN 1 END) as MashinAlatCount,
        COUNT(CASE WHEN item = 15 THEN 1 END) as TollhouseCount,
        COUNT(CASE WHEN item = 16 THEN 1 END) as EmergencyCount,
        province_fa,
        province_id,
        edarat_name,
        edarat_id,
        COUNT(CASE WHEN item != -1 THEN 1 END) as count
        from (
            SELECT 
            province_fa,
            province_id,
            edarat_name,
            edarat_id,
            item,
            status
            FROM road_items_projects
            where activity_date_time BETWEEN '{$fromDate}' and '{$toDate}'
            ) AS merged_table
        where status=1";

        if ($this->province) {
            $query .= " and province_id = {$this->province}";
        }

        $query .= " group by edarat_id order by province_id ASC;";

        $item_data = DB::select($query);

        $array = array();
        foreach ($item_data as $key => $value) {
            $array[$value->province_id]['city'][$value->edarat_id]['data']['MarematRooyeCount']= $value->MarematRooyeCount;
            $array[$value->province_id]['city'][$value->edarat_id]['data']['PaksaziCount']= $value->PaksaziCount;
            $array[$value->province_id]['city'][$value->edarat_id]['data']['AlaemCount']= $value->AlaemCount;
            $array[$value->province_id]['city'][$value->edarat_id]['data']['HefazCount']= $value->HefazCount;
            $array[$value->province_id]['city'][$value->edarat_id]['data']['RoshanayiCount']= $value->RoshanayiCount;
            $array[$value->province_id]['city'][$value->edarat_id]['data']['KhatkeshiCount']= $value->KhatkeshiCount;
            $array[$value->province_id]['city'][$value->edarat_id]['data']['RangamiziCount']= $value->RangamiziCount;
            $array[$value->province_id]['city'][$value->edarat_id]['data']['WashingCount']= $value->WashingCount;
            $array[$value->province_id]['city'][$value->edarat_id]['data']['ImenSaziCount']= $value->ImenSaziCount;
            $array[$value->province_id]['city'][$value->edarat_id]['data']['HarimCount']= $value->HarimCount;
            $array[$value->province_id]['city'][$value->edarat_id]['data']['PolCount']= $value->PolCount;
            $array[$value->province_id]['city'][$value->edarat_id]['data']['ToonelCount']= $value->ToonelCount;
            $array[$value->province_id]['city'][$value->edarat_id]['data']['AmaliatZemestaniCount']= $value->AmaliatZemestaniCount;
            $array[$value->province_id]['city'][$value->edarat_id]['data']['MashinAlatCount']= $value->MashinAlatCount;
            $array[$value->province_id]['city'][$value->edarat_id]['data']['TollhouseCount']= $value->TollhouseCount;
            $array[$value->province_id]['city'][$value->edarat_id]['data']['EmergencyCount']= $value->EmergencyCount;
            $array[$value->province_id]['city'][$value->edarat_id]['data']['Count']= $value->count;
    
            $array[$value->province_id]['city'][$value->edarat_id]['name']= $value->edarat_name;
            $array[$value->province_id]['province_name']= $value->province_fa;

            if (!isset($array[$value->province_id]['all'])) {
                $array[$value->province_id]['all']['MarematRooyeCount'] = 0;
                $array[$value->province_id]['all']['PaksaziCount'] = 0;
                $array[$value->province_id]['all']['AlaemCount'] = 0;
                $array[$value->province_id]['all']['HefazCount'] = 0;
                $array[$value->province_id]['all']['RoshanayiCount'] = 0;
                $array[$value->province_id]['all']['KhatkeshiCount'] = 0;
                $array[$value->province_id]['all']['RangamiziCount'] = 0;
                $array[$value->province_id]['all']['WashingCount'] = 0;
                $array[$value->province_id]['all']['ImenSaziCount'] = 0;
                $array[$value->province_id]['all']['HarimCount'] = 0;
                $array[$value->province_id]['all']['PolCount'] = 0;
                $array[$value->province_id]['all']['ToonelCount'] = 0;
                $array[$value->province_id]['all']['AmaliatZemestaniCount'] = 0;
                $array[$value->province_id]['all']['MashinAlatCount'] = 0;
                $array[$value->province_id]['all']['TollhouseCount'] = 0;
                $array[$value->province_id]['all']['EmergencyCount'] = 0;
                $array[$value->province_id]['all']['Count'] = 0;
            }

            $array[$value->province_id]['all']['MarematRooyeCount'] += $value->MarematRooyeCount;
            $array[$value->province_id]['all']['PaksaziCount'] += $value->PaksaziCount;
            $array[$value->province_id]['all']['AlaemCount'] += $value->AlaemCount;
            $array[$value->province_id]['all']['HefazCount'] += $value->HefazCount;
            $array[$value->province_id]['all']['RoshanayiCount'] += $value->RoshanayiCount;
            $array[$value->province_id]['all']['KhatkeshiCount'] += $value->KhatkeshiCount;
            $array[$value->province_id]['all']['RangamiziCount'] += $value->RangamiziCount;
            $array[$value->province_id]['all']['WashingCount'] += $value->WashingCount;
            $array[$value->province_id]['all']['ImenSaziCount'] += $value->ImenSaziCount;
            $array[$value->province_id]['all']['HarimCount'] += $value->HarimCount;
            $array[$value->province_id]['all']['PolCount'] += $value->PolCount;
            $array[$value->province_id]['all']['ToonelCount'] += $value->ToonelCount;
            $array[$value->province_id]['all']['AmaliatZemestaniCount'] += $value->AmaliatZemestaniCount;
            $array[$value->province_id]['all']['MashinAlatCount'] += $value->MashinAlatCount;
            $array[$value->province_id]['all']['TollhouseCount'] += $value->TollhouseCount;
            $array[$value->province_id]['all']['EmergencyCount'] += $value->EmergencyCount;
            $array[$value->province_id]['all']['Count'] += $value->count;
        }
        
        return view('excel.CurrentActivities.Report1-City', [
            'roads' => $array,
            'fromFa' => $fromDate,
            'toFa' => $toDate, 
            'has_province_filter' => $province ? true : false,
        ]);
    }
  
    /**
     * @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('R1');
        return [$drawing, $drawing2];
    }
}
