<?php

namespace App\Exports\RoadUpgrade;

use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;
use App\Models\ContractSubItems;
use App\Models\Province;
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;
use Maatwebsite\Excel\Concerns\WithStyles;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;

class CompareProgramAndFunction implements FromView, ShouldAutoSize, WithEvents, WithDrawings, WithStyles
{
    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
    {

        $array = [];
        $province = $this->province;
        
        foreach (Province::when($province, function ($query, $province) {
            return $query->where('id', $province);
        })->get() as $item) {
            $array[$item->id] = [
                'province_fa' => $item->name_fa,
                
                'BotoniDoing' => 0,
                'BotoniDone' => 0,

                'FeleziDoing' => 0,
                'FeleziDone' => 0,

                'TooliLightingDoing' => 0,
                'TooliLightingDone' => 0,

                'NoghteyiLightingDoing' => 0,
                'NoghteyiLightingDone' => 0,

                'AlaemImeniDoing' => 0,
                'AlaemImeniDone' => 0,

                'KhatkeshiDoing' => 0,
                'KhatkeshiDone' => 0,
                
                'ShirvaniDoing' => 0,
                'ShirvaniDone' => 0,

                'SayerDoing' => 0,
                'SayerDone' => 0,
                
                'countAll' => 0,
            ];
        }
        
        $query = "SELECT
        ROUND(SUM(CASE WHEN operation_type_id1 = 301 THEN operation_type_amount1
            WHEN operation_type_id2 = 301 THEN operation_type_amount2
            WHEN operation_type_id3 = 301 THEN operation_type_amount3
            WHEN operation_type_id4 = 301 THEN operation_type_amount4
        END)) AS BotoniDoing,
        ROUND(SUM(CASE WHEN operation_type_id1 = 301 THEN (operation_type_amount1 * operation_type_progress1)/100
            WHEN operation_type_id2 = 301 THEN (operation_type_amount2 * operation_type_progress2)/100
            WHEN operation_type_id3 = 301 THEN (operation_type_amount3 * operation_type_progress3)/100
            WHEN operation_type_id4 = 301 THEN (operation_type_amount4 * operation_type_progress4)/100
        END)) AS BotoniDone,

        ROUND(SUM(CASE WHEN operation_type_id1 = 302 THEN operation_type_amount1
            WHEN operation_type_id2 = 302 THEN operation_type_amount2
            WHEN operation_type_id3 = 302 THEN operation_type_amount3
            WHEN operation_type_id4 = 302 THEN operation_type_amount4
        END)) AS FeleziDoing,
        ROUND(SUM(CASE WHEN operation_type_id1 = 302 THEN (operation_type_amount1 * operation_type_progress1)/100
            WHEN operation_type_id2 = 302 THEN (operation_type_amount2 * operation_type_progress2)/100
            WHEN operation_type_id3 = 302 THEN (operation_type_amount3 * operation_type_progress3)/100
            WHEN operation_type_id4 = 302 THEN (operation_type_amount4 * operation_type_progress4)/100
        END)) AS FeleziDone,

        ROUND(SUM(CASE WHEN operation_type_id1 = 303 THEN operation_type_amount1
            WHEN operation_type_id2 = 303 THEN operation_type_amount2
            WHEN operation_type_id3 = 303 THEN operation_type_amount3
            WHEN operation_type_id4 = 303 THEN operation_type_amount4
        END)) AS TooliLightingDoing,
        ROUND(SUM(CASE WHEN operation_type_id1 = 303 THEN (operation_type_amount1 * operation_type_progress1)/100
            WHEN operation_type_id2 = 303 THEN (operation_type_amount2 * operation_type_progress2)/100
            WHEN operation_type_id3 = 303 THEN (operation_type_amount3 * operation_type_progress3)/100
            WHEN operation_type_id4 = 303 THEN (operation_type_amount4 * operation_type_progress4)/100
        END)) AS TooliLightingDone,

        ROUND(SUM(CASE WHEN operation_type_id1 = 304 THEN operation_type_amount1
            WHEN operation_type_id2 = 304 THEN operation_type_amount2
            WHEN operation_type_id3 = 304 THEN operation_type_amount3
            WHEN operation_type_id4 = 304 THEN operation_type_amount4
        END)) AS NoghteyiLightingDoing,
        ROUND(SUM(CASE WHEN operation_type_id1 = 304 THEN (operation_type_amount1 * operation_type_progress1)/100
            WHEN operation_type_id2 = 304 THEN (operation_type_amount2 * operation_type_progress2)/100
            WHEN operation_type_id3 = 304 THEN (operation_type_amount3 * operation_type_progress3)/100
            WHEN operation_type_id4 = 304 THEN (operation_type_amount4 * operation_type_progress4)/100
        END)) AS NoghteyiLightingDone,

        ROUND(SUM(CASE WHEN operation_type_id1 = 305 THEN operation_type_amount1
            WHEN operation_type_id2 = 305 THEN operation_type_amount2
            WHEN operation_type_id3 = 305 THEN operation_type_amount3
            WHEN operation_type_id4 = 305 THEN operation_type_amount4
        END)) AS AlaemImeniDoing,
        ROUND(SUM(CASE WHEN operation_type_id1 = 305 THEN (operation_type_amount1 * operation_type_progress1)/100
            WHEN operation_type_id2 = 305 THEN (operation_type_amount2 * operation_type_progress2)/100
            WHEN operation_type_id3 = 305 THEN (operation_type_amount3 * operation_type_progress3)/100
            WHEN operation_type_id4 = 305 THEN (operation_type_amount4 * operation_type_progress4)/100
        END)) AS AlaemImeniDone,

        ROUND(SUM(CASE WHEN operation_type_id1 = 306 THEN operation_type_amount1
            WHEN operation_type_id2 = 306 THEN operation_type_amount2
            WHEN operation_type_id3 = 306 THEN operation_type_amount3
            WHEN operation_type_id4 = 306 THEN operation_type_amount4
        END)) AS KhatkeshiDoing,
        ROUND(SUM(CASE WHEN operation_type_id1 = 306 THEN (operation_type_amount1 * operation_type_progress1)/100
            WHEN operation_type_id2 = 306 THEN (operation_type_amount2 * operation_type_progress2)/100
            WHEN operation_type_id3 = 306 THEN (operation_type_amount3 * operation_type_progress3)/100
            WHEN operation_type_id4 = 306 THEN (operation_type_amount4 * operation_type_progress4)/100
        END)) AS KhatkeshiDone,

        ROUND(SUM(CASE WHEN operation_type_id1 = 307 THEN operation_type_amount1
            WHEN operation_type_id2 = 307 THEN operation_type_amount2
            WHEN operation_type_id3 = 307 THEN operation_type_amount3
            WHEN operation_type_id4 = 307 THEN operation_type_amount4
        END)) AS ShirvaniDoing,
        ROUND(SUM(CASE WHEN operation_type_id1 = 307 THEN (operation_type_amount1 * operation_type_progress1)/100
            WHEN operation_type_id2 = 307 THEN (operation_type_amount2 * operation_type_progress2)/100
            WHEN operation_type_id3 = 307 THEN (operation_type_amount3 * operation_type_progress3)/100
            WHEN operation_type_id4 = 307 THEN (operation_type_amount4 * operation_type_progress4)/100
        END)) AS ShirvaniDone,

        ROUND(SUM(CASE WHEN operation_type_id1 = 308 THEN operation_type_amount1
            WHEN operation_type_id2 = 308 THEN operation_type_amount2
            WHEN operation_type_id3 = 308 THEN operation_type_amount3
            WHEN operation_type_id4 = 308 THEN operation_type_amount4
        END)) AS SayerDoing,
        ROUND(SUM(CASE WHEN operation_type_id1 = 308 THEN (operation_type_amount1 * operation_type_progress1)/100
            WHEN operation_type_id2 = 308 THEN (operation_type_amount2 * operation_type_progress2)/100
            WHEN operation_type_id3 = 308 THEN (operation_type_amount3 * operation_type_progress3)/100
            WHEN operation_type_id4 = 308 THEN (operation_type_amount4 * operation_type_progress4)/100
        END)) AS SayerDone,

        COUNT(*) AS countAll,
        province_fa,
        province_id
        FROM contract_subitems
        WHERE project_type_id = 3
        ";

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

        $query .= " GROUP BY province_id
        ORDER BY province_id ASC;";
        
        $data = DB::select($query);
        
        $sum = [
            'BotoniDoing' => 0,
            'BotoniDone' => 0,
            'FeleziDoing' => 0,
            'FeleziDone' => 0,
            'TooliLightingDoing' => 0,
            'TooliLightingDone' => 0,
            'NoghteyiLightingDoing' => 0,
            'NoghteyiLightingDone' => 0,
            'AlaemImeniDoing' => 0,
            'AlaemImeniDone' => 0,
            'KhatkeshiDoing' => 0,
            'KhatkeshiDone' => 0,
            'ShirvaniDoing' => 0,
            'ShirvaniDone' => 0,
            'SayerDoing' => 0,
            'SayerDone' => 0,

            'countAll' => 0,
        ];
        
        foreach ($data as $key => $value) {
            $array[$value->province_id] = [
                'province_fa' => $value->province_fa,

                'BotoniDoing' => $value->BotoniDoing,
                'BotoniDone' => $value->BotoniDone,
                'FeleziDoing' => $value->FeleziDoing,
                'FeleziDone' => $value->FeleziDone,
                'TooliLightingDoing' => $value->TooliLightingDoing,
                'TooliLightingDone' => $value->TooliLightingDone,
                'NoghteyiLightingDoing' => $value->NoghteyiLightingDoing,
                'NoghteyiLightingDone' => $value->NoghteyiLightingDone,
                'AlaemImeniDoing' => $value->AlaemImeniDoing,
                'AlaemImeniDone' => $value->AlaemImeniDone,
                'KhatkeshiDoing' => $value->KhatkeshiDoing,
                'KhatkeshiDone' => $value->KhatkeshiDone,
                'ShirvaniDoing' => $value->ShirvaniDoing,
                'ShirvaniDone' => $value->ShirvaniDone,
                'SayerDoing' => $value->SayerDoing,
                'SayerDone' => $value->SayerDone,

                'countAll' => $value->countAll,
            ];

            $sum['BotoniDoing'] += $value->BotoniDoing;
            $sum['BotoniDone'] += $value->BotoniDone;
            $sum['FeleziDoing'] += $value->FeleziDoing;
            $sum['FeleziDone'] += $value->FeleziDone;
            $sum['TooliLightingDoing'] += $value->TooliLightingDoing;
            $sum['TooliLightingDone'] += $value->TooliLightingDone;
            $sum['NoghteyiLightingDoing'] += $value->NoghteyiLightingDoing;
            $sum['NoghteyiLightingDone'] += $value->NoghteyiLightingDone;
            $sum['AlaemImeniDoing'] += $value->AlaemImeniDoing;
            $sum['AlaemImeniDone'] += $value->AlaemImeniDone;
            $sum['KhatkeshiDoing'] += $value->KhatkeshiDoing;
            $sum['KhatkeshiDone'] += $value->KhatkeshiDone;
            $sum['ShirvaniDoing'] += $value->ShirvaniDoing;
            $sum['ShirvaniDone'] += $value->ShirvaniDone;
            $sum['SayerDoing'] += $value->SayerDoing;
            $sum['SayerDone'] += $value->SayerDone;

            $sum['countAll'] += $value->countAll;
        }

        return view('excel.Upgrade.CompareProgramAndFunction', [
            'array' => $array,
            'sum' => $sum,
            'sumShow' => $this->province ? 0 :1,
        ]);
    }

    /**
     * @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('B1');
        return [$drawing, $drawing2];
    }


    public function styles(Worksheet $sheet)
    {

        return [
            // Style the first row as bold text.
            'A:BA' =>    [
                'font' => [
                    'name' => 'B Nazanin'
                ]
            ],
        ];

    }
}