<?php

namespace App\Exports\Activities\Two;

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 Cities implements FromView, ShouldAutoSize, WithEvents, WithDrawings
{
    public function __construct($fromDate = null, $toDate = null, $province = null, $last_status_id = null)
    {
        $this->fromDate = $fromDate ?? null;
        $this->toDate = $toDate ?? null;
        $this->province = $province ?? null;
        $this->last_status_id = $last_status_id ?? null;
    }

    public function view(): View
    {
        $fromDate = $this->fromDate ?? '2021-03-21';
        $toDate = $this->toDate ?? \Carbon\Carbon::now();
        $province = $this->province;

        $query = "SELECT
        COUNT(CASE WHEN sub_item = 1 THEN 1 END) as LabeAsphaltCount,
        SUM(CASE WHEN sub_item = 1 THEN sub_item_data END) as LabeAsphaltSum,
        COUNT(CASE WHEN sub_item = 2 THEN 1 END) as ShaneSaziCount,
        SUM(CASE WHEN sub_item = 2 THEN sub_item_data END) as ShaneSaziSum,   
        COUNT(CASE WHEN sub_item = 3 THEN 1 END) as GodOftadegiCount,
        SUM(CASE WHEN sub_item = 3 THEN sub_item_data END) as GodOftadegiSum,       
        COUNT(CASE WHEN sub_item = 4 THEN 1 END) as LakegiriCount,
        SUM(CASE WHEN sub_item = 4 THEN sub_item_data END) as LakegiriSum,
        COUNT(CASE WHEN sub_item = 5 THEN 1 END) as MarematCount,
        SUM(CASE WHEN sub_item = 5 THEN sub_item_data END) as MarematSum,
        COUNT(CASE WHEN sub_item = 6 THEN 1 END) as BastarHarimCount,
        SUM(CASE WHEN sub_item = 6 THEN sub_item_data END) as BastarHarimSum,
        COUNT(CASE WHEN sub_item = 8 THEN 1 END) as DarzgiriCount,
        SUM(CASE WHEN sub_item = 8 THEN sub_item_data END) as DarzgiriSum,   
        COUNT(CASE WHEN sub_item = 9 THEN 1 END) as MozresCount,
        SUM(CASE WHEN sub_item = 9 THEN sub_item_data END) as MozresSum,
        COUNT(CASE WHEN sub_item = 10 THEN 1 END) as TarashRooyeCount,
        SUM(CASE WHEN sub_item = 10 THEN sub_item_data END) as TarashRooyeSum,
        COUNT(CASE WHEN sub_item = 11 THEN 1 END) as TighzaniCount,
        SUM(CASE WHEN sub_item = 11 THEN sub_item_data END) as TighzaniSum,
        COUNT(CASE WHEN sub_item = 12 THEN 1 END) as ShenriziCount,
        SUM(CASE WHEN sub_item = 12 THEN sub_item_data END) as ShenriziSum,
        COUNT(CASE WHEN sub_item = 13 THEN 1 END) as KhakbardariCount,
        SUM(CASE WHEN sub_item = 13 THEN sub_item_data END) as KhakbardariSum,
        province_id, province_fa, city_id, user_name as city_fa
        FROM road_items_projects
        WHERE item=1  and status = 1";

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

        $query .= " AND activity_date_time BETWEEN '{$fromDate}' AND '{$toDate}' GROUP BY city_id ORDER BY province_id ASC;";

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

        $temp =[];
        foreach ($data as $key => $value) {
            $temp[$value->province_id]['city'][$value->city_id]['data']['LabeAsphaltCount']= $value->LabeAsphaltCount;
            $temp[$value->province_id]['city'][$value->city_id]['data']['LabeAsphaltSum']= $value->LabeAsphaltSum;

            $temp[$value->province_id]['city'][$value->city_id]['data']['ShaneSaziCount']= $value->ShaneSaziCount;
            $temp[$value->province_id]['city'][$value->city_id]['data']['ShaneSaziSum']= $value->ShaneSaziSum;

            $temp[$value->province_id]['city'][$value->city_id]['data']['GodOftadegiCount']= $value->GodOftadegiCount;
            $temp[$value->province_id]['city'][$value->city_id]['data']['GodOftadegiSum']= $value->GodOftadegiSum;

            $temp[$value->province_id]['city'][$value->city_id]['data']['LakegiriCount']= $value->LakegiriCount;
            $temp[$value->province_id]['city'][$value->city_id]['data']['LakegiriSum']= $value->LakegiriSum;

            $temp[$value->province_id]['city'][$value->city_id]['data']['MarematCount']= $value->MarematCount;
            $temp[$value->province_id]['city'][$value->city_id]['data']['MarematSum']= $value->MarematSum;
            
            $temp[$value->province_id]['city'][$value->city_id]['data']['BastarHarimCount']= $value->BastarHarimCount;
            $temp[$value->province_id]['city'][$value->city_id]['data']['BastarHarimSum']= $value->BastarHarimSum;

            $temp[$value->province_id]['city'][$value->city_id]['data']['DarzgiriCount']= $value->DarzgiriCount;
            $temp[$value->province_id]['city'][$value->city_id]['data']['DarzgiriSum']= $value->DarzgiriSum;

            $temp[$value->province_id]['city'][$value->city_id]['data']['MozresCount']= $value->MozresCount;
            $temp[$value->province_id]['city'][$value->city_id]['data']['MozresSum']= $value->MozresSum;

            $temp[$value->province_id]['city'][$value->city_id]['data']['TarashRooyeCount']= $value->TarashRooyeCount;
            $temp[$value->province_id]['city'][$value->city_id]['data']['TarashRooyeSum']= $value->TarashRooyeSum;

            $temp[$value->province_id]['city'][$value->city_id]['data']['TighzaniCount']= $value->TighzaniCount;
            $temp[$value->province_id]['city'][$value->city_id]['data']['TighzaniSum']= $value->TighzaniSum;

            $temp[$value->province_id]['city'][$value->city_id]['data']['ShenriziCount']= $value->ShenriziCount;
            $temp[$value->province_id]['city'][$value->city_id]['data']['ShenriziSum']= $value->ShenriziSum;

            $temp[$value->province_id]['city'][$value->city_id]['data']['KhakbardariCount']= $value->KhakbardariCount;
            $temp[$value->province_id]['city'][$value->city_id]['data']['KhakbardariSum']= $value->KhakbardariSum;


            $temp[$value->province_id]['city'][$value->city_id]['name']= $value->city_fa;

            if (!isset($temp[$value->province_id]['all'])) {
                $temp[$value->province_id]['all']['LabeAsphaltCount'] = 0;
                $temp[$value->province_id]['all']['LabeAsphaltSum'] = 0;

                $temp[$value->province_id]['all']['ShaneSaziCount'] = 0;
                $temp[$value->province_id]['all']['ShaneSaziSum'] = 0;

                $temp[$value->province_id]['all']['GodOftadegiCount'] = 0;
                $temp[$value->province_id]['all']['GodOftadegiSum'] = 0;

                $temp[$value->province_id]['all']['LakegiriCount'] = 0;
                $temp[$value->province_id]['all']['LakegiriSum'] = 0;
                
                $temp[$value->province_id]['all']['MarematCount'] = 0;
                $temp[$value->province_id]['all']['MarematSum'] = 0;
                
                $temp[$value->province_id]['all']['BastarHarimCount'] = 0;
                $temp[$value->province_id]['all']['BastarHarimSum'] = 0;

                $temp[$value->province_id]['all']['DarzgiriCount'] = 0;
                $temp[$value->province_id]['all']['DarzgiriSum'] = 0;

                $temp[$value->province_id]['all']['MozresCount'] = 0;
                $temp[$value->province_id]['all']['MozresSum'] = 0;

                $temp[$value->province_id]['all']['TarashRooyeCount'] = 0;
                $temp[$value->province_id]['all']['TarashRooyeSum'] = 0;

                $temp[$value->province_id]['all']['TighzaniCount'] = 0;
                $temp[$value->province_id]['all']['TighzaniSum'] = 0;
                
                $temp[$value->province_id]['all']['ShenriziCount'] = 0;
                $temp[$value->province_id]['all']['ShenriziSum'] = 0;
                
                $temp[$value->province_id]['all']['KhakbardariCount'] = 0;
                $temp[$value->province_id]['all']['KhakbardariSum'] = 0;
            }
            $temp[$value->province_id]['all']['LabeAsphaltCount'] += $value->LabeAsphaltCount;
            $temp[$value->province_id]['all']['LabeAsphaltSum'] += $value->LabeAsphaltSum;

            $temp[$value->province_id]['all']['ShaneSaziCount'] += $value->ShaneSaziCount;
            $temp[$value->province_id]['all']['ShaneSaziSum'] += $value->ShaneSaziSum;

            $temp[$value->province_id]['all']['GodOftadegiCount'] += $value->GodOftadegiCount;
            $temp[$value->province_id]['all']['GodOftadegiSum'] += $value->GodOftadegiSum;

            $temp[$value->province_id]['all']['LakegiriCount'] += $value->LakegiriCount;
            $temp[$value->province_id]['all']['LakegiriSum'] += $value->LakegiriSum;
                
            $temp[$value->province_id]['all']['MarematCount'] += $value->MarematCount;
            $temp[$value->province_id]['all']['MarematSum'] += $value->MarematSum;
                
            $temp[$value->province_id]['all']['BastarHarimCount'] += $value->BastarHarimCount;
            $temp[$value->province_id]['all']['BastarHarimSum'] += $value->BastarHarimSum;

            $temp[$value->province_id]['all']['DarzgiriCount'] += $value->DarzgiriCount;
            $temp[$value->province_id]['all']['DarzgiriSum'] += $value->DarzgiriSum;

            $temp[$value->province_id]['all']['MozresCount'] += $value->MozresCount;
            $temp[$value->province_id]['all']['MozresSum'] += $value->MozresSum;

            $temp[$value->province_id]['all']['TarashRooyeCount'] += $value->TarashRooyeCount;
            $temp[$value->province_id]['all']['TarashRooyeSum'] += $value->TarashRooyeSum;

            $temp[$value->province_id]['all']['TighzaniCount'] += $value->TighzaniCount;
            $temp[$value->province_id]['all']['TighzaniSum'] += $value->TighzaniSum;
                
            $temp[$value->province_id]['all']['ShenriziCount'] += $value->ShenriziCount;
            $temp[$value->province_id]['all']['ShenriziSum'] += $value->ShenriziSum;
                
            $temp[$value->province_id]['all']['KhakbardariCount'] += $value->KhakbardariCount;
            $temp[$value->province_id]['all']['KhakbardariSum'] += $value->KhakbardariSum;
            
            $temp[$value->province_id]['province_name']= $value->province_fa;
        }


        return view('excel.CurrentActivities.Report2-City', [
            'roads' => $temp,
            '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('Z1');
        return [$drawing, $drawing2];
    }
}
