<?php

namespace App\Exports\Activities\Two\Paksazi;

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 City 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();
        
        $query = "SELECT 
        COUNT(CASE WHEN sub_item = 1 THEN 1 END) as PaksaziSathRahCount,
        SUM(CASE WHEN sub_item = 1 THEN sub_item_data END) as PaksaziSathRahSum,
        COUNT(CASE WHEN sub_item = 2 THEN 1 END) as TanfieAbroCount,
        SUM(CASE WHEN sub_item = 2 THEN sub_item_data END) as TanfieAbroSum,
        COUNT(CASE WHEN sub_item = 3 THEN 1 END) as PaksaziLasheCount,
        SUM(CASE WHEN sub_item = 3 THEN sub_item_data END) as PaksaziLasheSum,
        COUNT(CASE WHEN sub_item = 4 THEN 1 END) as PaksaziGhonoCount,
        SUM(CASE WHEN sub_item = 4 THEN sub_item_data END) as PaksaziGhonoSum,
        COUNT(CASE WHEN sub_item = 5 THEN 1 END) as PaksaziTasadofatCount,
        SUM(CASE WHEN sub_item = 5 THEN sub_item_data END) as PaksaziTasadofatSum,
        COUNT(CASE WHEN sub_item = 6 THEN 1 END) as AlafKaniCount,
        SUM(CASE WHEN sub_item = 6 THEN sub_item_data END) as AlafKaniSum,
        COUNT(CASE WHEN sub_item = 7 THEN 1 END) as PaksaziHarimCount,
        SUM(CASE WHEN sub_item = 7 THEN sub_item_data END) as PaksaziHarimSum,
        COUNT(CASE WHEN sub_item = 8 THEN 1 END) as JamavariGhateatCount,
        SUM(CASE WHEN sub_item = 8 THEN sub_item_data END) as JamavariGhateatSum,
        COUNT(CASE WHEN sub_item = 9 THEN 1 END) as PaksaziParkingCount,
        SUM(CASE WHEN sub_item = 9 THEN sub_item_data END) as PaksaziParkingSum,
        COUNT(CASE WHEN sub_item = 10 THEN 1 END) as PaksaziKenarGharnizPolCount,
        SUM(CASE WHEN sub_item = 10 THEN sub_item_data END) as PaksaziKenarGharnizPolSum,
        COUNT(CASE WHEN sub_item = 11 THEN 1 END) as PaksaziJazireCount,
        SUM(CASE WHEN sub_item = 11 THEN sub_item_data END) as PaksaziJazireSum,
        COUNT(CASE WHEN sub_item = 12 THEN 1 END) as PaksaziTanfieDakhelPolCount,
        SUM(CASE WHEN sub_item = 12 THEN sub_item_data END) as PaksaziTanfieDakhelPolSum,
        COUNT(CASE WHEN sub_item = 13 THEN 1 END) as PaksaziSifonCount,
        SUM(CASE WHEN sub_item = 13 THEN sub_item_data END) as PaksaziSifonSum,
        COUNT(CASE WHEN sub_item = 14 THEN 1 END) as PaksaziShakheDerakhtanCount,
        SUM(CASE WHEN sub_item = 14 THEN sub_item_data END) as PaksaziShakheDerakhtanSum,
        COUNT(CASE WHEN sub_item = 15 THEN 1 END) as PaksaziMavadNaftiCount,
        SUM(CASE WHEN sub_item = 15 THEN sub_item_data END) as PaksaziMavadNaftiSum,
        COUNT(CASE WHEN sub_item = 16 THEN 1 END) as AlafKaniDastiCount,
        SUM(CASE WHEN sub_item = 16 THEN sub_item_data END) as AlafKaniDastiSum,
        COUNT(CASE WHEN sub_item = 17 THEN 1 END) as RizeshBardariCount,
        SUM(CASE WHEN sub_item = 17 THEN sub_item_data END) as RizeshBardariSum,
        COUNT(CASE WHEN sub_item = 18 THEN 1 END) as JamavariSangCount,
        SUM(CASE WHEN sub_item = 18 THEN sub_item_data END) as JamavariSangSum,
        COUNT(CASE WHEN sub_item = 19 THEN 1 END) as LayeroobiCount,
        SUM(CASE WHEN sub_item = 19 THEN sub_item_data END) as LayeroobiSum,

        province_id,
        province_fa,
        user_name as city_fa
        FROM road_items_projects
        WHERE item = 2 and status = 1";
        if ($this->province) {
            $query .= " AND province_id = {$this->province}";
        }

        $query .= " AND activity_date_time BETWEEN '{$fromDate}' AND '{$toDate}' GROUP BY city_id ORDER BY province_id ASC;";
        $data = DB::select($query);

        return view('excel.CurrentActivities.Paksazi.City', [
            '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('AN1');
        return [$drawing, $drawing2];
    }
}

