<?php

namespace App\Exports\Activities\Two\Roshanaei;

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 NegahdariToolCount,
        SUM(CASE WHEN sub_item = 1 THEN sub_item_data END) as NegahdariToolSum,
        COUNT(CASE WHEN sub_item = 2 THEN 1 END) as NegahdariHashiehCount,
        SUM(CASE WHEN sub_item = 2 THEN sub_item_data END) as NegahdariHashiehSum,
        COUNT(CASE WHEN sub_item = 3 THEN 1 END) as NegahdariNoghteyiCount,
        SUM(CASE WHEN sub_item = 3 THEN sub_item_data END) as NegahdariNoghteyiSum,
        COUNT(CASE WHEN sub_item = 4 THEN 1 END) as NegahdariNavahiCount,
        SUM(CASE WHEN sub_item = 4 THEN sub_item_data END) as NegahdariNavahiSum,
        COUNT(CASE WHEN sub_item = 5 THEN 1 END) as NegahdariMehgirCount,
        SUM(CASE WHEN sub_item = 5 THEN sub_item_data END) as NegahdariMehgirSum,
        COUNT(CASE WHEN sub_item = 6 THEN 1 END) as NegahdariToonelCount,
        SUM(CASE WHEN sub_item = 6 THEN sub_item_data END) as NegahdariToonelSum,
        COUNT(CASE WHEN sub_item = 7 THEN 1 END) as TamiratCableCount,
        SUM(CASE WHEN sub_item = 7 THEN sub_item_data END) as TamiratCableSum,
        COUNT(CASE WHEN sub_item = 8 THEN 1 END) as TamiratKaleCheraghCount,
        SUM(CASE WHEN sub_item = 8 THEN sub_item_data END) as TamiratKaleCheraghSum,
        COUNT(CASE WHEN sub_item = 9 THEN 1 END) as TamiratMafselzaniCount,
        SUM(CASE WHEN sub_item = 9 THEN sub_item_data END) as TamiratMafselzaniSum,
        COUNT(CASE WHEN sub_item = 10 THEN 1 END) as TamiratTabloCount,
        SUM(CASE WHEN sub_item = 10 THEN sub_item_data END) as TamiratTabloSum,
        COUNT(CASE WHEN sub_item = 11 THEN 1 END) as TamiratBorjNooriCount,
        SUM(CASE WHEN sub_item = 11 THEN sub_item_data END) as TamiratBorjNooriSum,
        province_id,
        province_fa
        FROM road_items_projects
        WHERE item = 5 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 NegahdariToolCount,
        SUM(CASE WHEN sub_item = 1 THEN sub_item_data END) as NegahdariToolSum,
        COUNT(CASE WHEN sub_item = 2 THEN 1 END) as NegahdariHashiehCount,
        SUM(CASE WHEN sub_item = 2 THEN sub_item_data END) as NegahdariHashiehSum,
        COUNT(CASE WHEN sub_item = 3 THEN 1 END) as NegahdariNoghteyiCount,
        SUM(CASE WHEN sub_item = 3 THEN sub_item_data END) as NegahdariNoghteyiSum,
        COUNT(CASE WHEN sub_item = 4 THEN 1 END) as NegahdariNavahiCount,
        SUM(CASE WHEN sub_item = 4 THEN sub_item_data END) as NegahdariNavahiSum,
        COUNT(CASE WHEN sub_item = 5 THEN 1 END) as NegahdariMehgirCount,
        SUM(CASE WHEN sub_item = 5 THEN sub_item_data END) as NegahdariMehgirSum,
        COUNT(CASE WHEN sub_item = 6 THEN 1 END) as NegahdariToonelCount,
        SUM(CASE WHEN sub_item = 6 THEN sub_item_data END) as NegahdariToonelSum,
        COUNT(CASE WHEN sub_item = 7 THEN 1 END) as TamiratCableCount,
        SUM(CASE WHEN sub_item = 7 THEN sub_item_data END) as TamiratCableSum,
        COUNT(CASE WHEN sub_item = 8 THEN 1 END) as TamiratKaleCheraghCount,
        SUM(CASE WHEN sub_item = 8 THEN sub_item_data END) as TamiratKaleCheraghSum,
        COUNT(CASE WHEN sub_item = 9 THEN 1 END) as TamiratMafselzaniCount,
        SUM(CASE WHEN sub_item = 9 THEN sub_item_data END) as TamiratMafselzaniSum,
        COUNT(CASE WHEN sub_item = 10 THEN 1 END) as TamiratTabloCount,
        SUM(CASE WHEN sub_item = 10 THEN sub_item_data END) as TamiratTabloSum,
        COUNT(CASE WHEN sub_item = 11 THEN 1 END) as TamiratBorjNooriCount,
        SUM(CASE WHEN sub_item = 11 THEN sub_item_data END) as TamiratBorjNooriSum
        FROM road_items_projects
        WHERE item = 5 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.Roshanaei.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('W1');
        return [$drawing, $drawing2];
    }
}
