<?php

namespace App\Exports\Activities\Two\Pol;

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 Province 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 EhdasGharnizPolCount,
        SUM(CASE WHEN sub_item = 1 THEN sub_item_data END) as EhdasGharnizPolSum,
        
        COUNT(CASE WHEN sub_item = 2 THEN 1 END) as LoleCount,
        SUM(CASE WHEN sub_item = 2 THEN sub_item_data END) as LoleSum,
        
        COUNT(CASE WHEN sub_item = 3 THEN 1 END) as TatvilCount,
        SUM(CASE WHEN sub_item = 3 THEN sub_item_data END) as TatvilSum,
        
        COUNT(CASE WHEN sub_item = 4 THEN 1 END) as BazsaziCount,
        SUM(CASE WHEN sub_item = 4 THEN sub_item_data END) as BazsaziSum,
        
        COUNT(CASE WHEN sub_item = 5 THEN 1 END) as TamiratAbroCount,
        SUM(CASE WHEN sub_item = 5 THEN sub_item_data END) as TamiratAbroSum,
        
        COUNT(CASE WHEN sub_item = 6 THEN 1 END) as MarematCount,
        SUM(CASE WHEN sub_item = 6 THEN sub_item_data END) as MarematSum,
        
        COUNT(CASE WHEN sub_item = 7 THEN 1 END) as RadieCount,
        SUM(CASE WHEN sub_item = 7 THEN sub_item_data END) as RadieSum,
        province_id,
        province_fa
        FROM road_items_projects
        WHERE item = 11 and status = 1";

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

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

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

        $countryQuery = "SELECT 
        COUNT(CASE WHEN sub_item = 1 THEN 1 END) as EhdasGharnizPolCount,
        SUM(CASE WHEN sub_item = 1 THEN sub_item_data END) as EhdasGharnizPolSum,
        
        COUNT(CASE WHEN sub_item = 2 THEN 1 END) as LoleCount,
        SUM(CASE WHEN sub_item = 2 THEN sub_item_data END) as LoleSum,
        
        COUNT(CASE WHEN sub_item = 3 THEN 1 END) as TatvilCount,
        SUM(CASE WHEN sub_item = 3 THEN sub_item_data END) as TatvilSum,
        
        COUNT(CASE WHEN sub_item = 4 THEN 1 END) as BazsaziCount,
        SUM(CASE WHEN sub_item = 4 THEN sub_item_data END) as BazsaziSum,
        
        COUNT(CASE WHEN sub_item = 5 THEN 1 END) as TamiratAbroCount,
        SUM(CASE WHEN sub_item = 5 THEN sub_item_data END) as TamiratAbroSum,
        
        COUNT(CASE WHEN sub_item = 6 THEN 1 END) as MarematCount,
        SUM(CASE WHEN sub_item = 6 THEN sub_item_data END) as MarematSum,
        
        COUNT(CASE WHEN sub_item = 7 THEN 1 END) as RadieCount,
        SUM(CASE WHEN sub_item = 7 THEN sub_item_data END) as RadieSum
        FROM road_items_projects
        WHERE item = 11 and status = 1";

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

        $countryQuery .= " AND activity_date_time BETWEEN '{$fromDate}' AND '{$toDate}';";

        $country = DB::select($countryQuery);


        return view('excel.CurrentActivities.Pol.Province', [
            'data' => $data,
            'country' => $country,
            '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('O1');
        return [$drawing, $drawing2];
    }
}
