<?php

namespace App\Exports\Activities\One;

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 Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Concerns\WithDrawings;
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;

class Provinces 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;

        foreach (\App\Models\Province::when($province, function ($query, $province) {
            return $query->where('id', $province);
        })->get() as $key => $value) {
            $array[$value->id] = [
                'province_id' => $value->id,
                'province_fa' => $value->name_fa,
                'MarematRooyeCount' => '0',
                'PaksaziCount' => '0',
                'AlaemCount' => '0',
                'HefazCount' => '0',
                'RoshanayiCount' => '0',
                'KhatkeshiCount' => '0',
                'RangamiziCount' => '0',
                'WashingCount' => '0',
                'ImenSaziCount' => '0',
                'HarimCount' => '0',
                'PolCount' => '0',
                'ToonelCount' => '0',
                'AmaliatZemestaniCount' => '0',
                'MashinAlatCount' => '0',
                'TollhouseCount' => '0',
                'EmergencyCount' => '0',
                '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,
        COUNT(CASE WHEN item != -1 THEN 1 END) as count
        FROM(
            SELECT 
            province_fa,
            province_id,
            city_fa,
            city_id,
            item,
            status
            FROM road_items_projects
            where activity_date_time BETWEEN '{$fromDate} 00:00:00' and '{$toDate} 23:59:59'
            ) AS merged_table
        where status=1 ";

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

        $query .= " group by province_id;";

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

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

            $sum['MarematRooyeCount'] += $value->MarematRooyeCount;
            $sum['PaksaziCount'] += $value->PaksaziCount;
            $sum['AlaemCount'] += $value->AlaemCount;
            $sum['HefazCount'] += $value->HefazCount;
            $sum['RoshanayiCount'] += $value->RoshanayiCount;
            $sum['KhatkeshiCount'] += $value->KhatkeshiCount;
            $sum['RangamiziCount'] += $value->RangamiziCount;
            $sum['WashingCount'] += $value->WashingCount;
            $sum['ImenSaziCount'] += $value->ImenSaziCount;
            $sum['HarimCount'] += $value->HarimCount;
            $sum['PolCount'] += $value->PolCount;
            $sum['ToonelCount'] += $value->ToonelCount;
            $sum['AmaliatZemestaniCount'] += $value->AmaliatZemestaniCount;
            $sum['MashinAlatCount'] += $value->MashinAlatCount;
            $sum['TollhouseCount'] += $value->TollhouseCount;
            $sum['EmergencyCount'] += $value->EmergencyCount;
            $sum['Count'] += $value->count ;
        }

        return view('excel.CurrentActivities.Report1-Province', [
            'array' => $array,
            'sum' => $sum,
            'fromFa' => $fromDate,
            'sumShow' => $this->province ? 0 :1,
            '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('q1');
        return [$drawing, $drawing2];
    }
}
