<?php

namespace App\Exports\Activities\Two\Mashinalat;

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 BenzinMasrafiCount,
        SUM(CASE WHEN sub_item = 1 THEN sub_item_data END) as BenzinMasrafiSum,
        
        COUNT(CASE WHEN sub_item = 2 THEN 1 END) as GazMasrafiCount,
        SUM(CASE WHEN sub_item = 2 THEN sub_item_data END) as GazMasrafiSum,

        COUNT(CASE WHEN sub_item = 3 THEN 1 END) as MavadNaftiCount,
        SUM(CASE WHEN sub_item = 3 THEN sub_item_data END) as MavadNaftiSum,
        
        COUNT(CASE WHEN sub_item = 4 THEN 1 END) BazsaziCount,
        SUM(CASE WHEN sub_item = 4 THEN sub_item_data END) BazsaziSum,
        
        COUNT(CASE WHEN sub_item = 8 THEN 1 END) as ServiceCount,
        SUM(CASE WHEN sub_item = 8 THEN sub_item_data END) as ServiceSum,
        
        COUNT(CASE WHEN sub_item = 9 THEN 1 END) as NamakPashCount,
        SUM(CASE WHEN sub_item = 9 THEN sub_item_data END) as NamakPashSum,

        COUNT(CASE WHEN sub_item = 10 THEN 1 END) as JabejayiCount,
        SUM(CASE WHEN sub_item = 10 THEN sub_item_data END) as JabejayiSum,

        province_id,
        province_fa,
        user_name as city_fa
        FROM road_items_projects
        WHERE item = 14 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.Mashinalat.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('P1');
        return [$drawing, $drawing2];
    }
}
