<?php

namespace App\Exports\Maintanance;

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

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,
                
                'AsphaltGarmDoing' => 0,
                'MaseAsphaltDoing' => 0,
                'BazyaftSardGarmDoing' => 0,
                'mirosurfacingDoing' => 0,
                'ChipSailDoing' => 0,
                'SlarySaildDoing' => 0,
                'ScrubSailDoing' => 0,
                'FogSailDoing' => 0,
                'SailKatDoing' => 0,
                'KipSailDoing' => 0,
                'AsphaltRedmixDoing' => 0,
                'LakegiriDoing' => 0,
                'DarzgiriDoing' => 0,

                'AsphaltGarmDone' => 0,
                'MaseAsphaltDone' => 0,
                'BazyaftSardGarmDone' => 0,
                'mirosurfacingDone' => 0,
                'ChipSailDone' => 0,
                'SlarySaildDone' => 0,
                'ScrubSailDone' => 0,
                'FogSailDone' => 0,
                'SailKatDone' => 0,
                'KipSailDone' => 0,
                'AsphaltRedmixDone' => 0,
                'LakegiriDone' => 0,
                'DarzgiriDone' => 0,
                
                'countAll' => 0,
            ];
        }

        $query = "SELECT province_fa, province_id,
                ROUND(SUM(CASE WHEN operation_type_id1 = 101 THEN operation_type_amount1
                    WHEN operation_type_id2 = 101 THEN operation_type_amount2
                    WHEN operation_type_id3 = 101 THEN operation_type_amount3
                    WHEN operation_type_id4 = 101 THEN operation_type_amount4
                END)) AS AsphaltGarmDoing,
                ROUND(SUM(CASE WHEN operation_type_id1 = 101 THEN (CAST(operation_type_amount1 AS signed) * CAST(operation_type_progress1 AS signed))/100
                    WHEN operation_type_id2 = 101 THEN (CAST(operation_type_amount2 AS signed) * CAST(operation_type_progress2 AS signed))/100
                    WHEN operation_type_id3 = 101 THEN (CAST(operation_type_amount3 AS signed) * CAST(operation_type_progress3 AS signed))/100
                    WHEN operation_type_id4 = 101 THEN (CAST(operation_type_amount4 AS signed) * CAST(operation_type_progress4 AS signed))/100
                END)) AS AsphaltGarmDone,

                ROUND(SUM(CASE WHEN operation_type_id1 = 102 THEN operation_type_amount1
                    WHEN operation_type_id2 = 102 THEN operation_type_amount2
                    WHEN operation_type_id3 = 102 THEN operation_type_amount3
                    WHEN operation_type_id4 = 102 THEN operation_type_amount4
                END)) AS MaseAsphaltDoing,
                ROUND(SUM(CASE WHEN operation_type_id1 = 102 THEN (CAST(operation_type_amount1 AS signed) * CAST(operation_type_progress1 AS signed))/100
                    WHEN operation_type_id2 = 102 THEN (CAST(operation_type_amount2 AS signed) * CAST(operation_type_progress2 AS signed))/100
                    WHEN operation_type_id3 = 102 THEN (CAST(operation_type_amount3 AS signed) * CAST(operation_type_progress3 AS signed))/100
                    WHEN operation_type_id4 = 102 THEN (CAST(operation_type_amount4 AS signed) * CAST(operation_type_progress4 AS signed))/100
                END)) AS MaseAsphaltDone,

                ROUND(SUM(CASE WHEN operation_type_id1 = 103 THEN operation_type_amount1
                    WHEN operation_type_id2 = 103 THEN operation_type_amount2
                    WHEN operation_type_id3 = 103 THEN operation_type_amount3
                    WHEN operation_type_id4 = 103 THEN operation_type_amount4
                END)) AS BazyaftSardGarmDoing,
                ROUND(SUM(CASE WHEN operation_type_id1 = 103 THEN (CAST(operation_type_amount1 AS signed) * CAST(operation_type_progress1 AS signed))/100
                    WHEN operation_type_id2 = 103 THEN (CAST(operation_type_amount2 AS signed) * CAST(operation_type_progress2 AS signed))/100
                    WHEN operation_type_id3 = 103 THEN (CAST(operation_type_amount3 AS signed) * CAST(operation_type_progress3 AS signed))/100
                    WHEN operation_type_id4 = 103 THEN (CAST(operation_type_amount4 AS signed) * CAST(operation_type_progress4 AS signed))/100
                END)) AS BazyaftSardGarmDone,

                ROUND(SUM(CASE WHEN operation_type_id1 = 104 THEN operation_type_amount1
                    WHEN operation_type_id2 = 104 THEN operation_type_amount2
                    WHEN operation_type_id3 = 104 THEN operation_type_amount3
                    WHEN operation_type_id4 = 104 THEN operation_type_amount4
                END)) AS mirosurfacingDoing,
                ROUND(SUM(CASE WHEN operation_type_id1 = 104 THEN (CAST(operation_type_amount1 AS signed) * CAST(operation_type_progress1 AS signed))/100
                    WHEN operation_type_id2 = 104 THEN (CAST(operation_type_amount2 AS signed) * CAST(operation_type_progress2 AS signed))/100
                    WHEN operation_type_id3 = 104 THEN (CAST(operation_type_amount3 AS signed) * CAST(operation_type_progress3 AS signed))/100
                    WHEN operation_type_id4 = 104 THEN (CAST(operation_type_amount4 AS signed) * CAST(operation_type_progress4 AS signed))/100
                END)) AS mirosurfacingDone,

                ROUND(SUM(CASE WHEN operation_type_id1 = 105 THEN operation_type_amount1
                    WHEN operation_type_id2 = 105 THEN operation_type_amount2
                    WHEN operation_type_id3 = 105 THEN operation_type_amount3
                    WHEN operation_type_id4 = 105 THEN operation_type_amount4
                END)) AS ChipSailDoing,
                ROUND(SUM(CASE WHEN operation_type_id1 = 105 THEN (CAST(operation_type_amount1 AS signed) * CAST(operation_type_progress1 AS signed))/100
                    WHEN operation_type_id2 = 105 THEN (CAST(operation_type_amount2 AS signed) * CAST(operation_type_progress2 AS signed))/100
                    WHEN operation_type_id3 = 105 THEN (CAST(operation_type_amount3 AS signed) * CAST(operation_type_progress3 AS signed))/100
                    WHEN operation_type_id4 = 105 THEN (CAST(operation_type_amount4 AS signed) * CAST(operation_type_progress4 AS signed))/100
                END)) AS ChipSailDone,

                ROUND(SUM(CASE WHEN operation_type_id1 = 106 THEN operation_type_amount1
                    WHEN operation_type_id2 = 106 THEN operation_type_amount2
                    WHEN operation_type_id3 = 106 THEN operation_type_amount3
                    WHEN operation_type_id4 = 106 THEN operation_type_amount4
                END)) AS SlarySaildDoing,
                ROUND(SUM(CASE WHEN operation_type_id1 = 106 THEN (CAST(operation_type_amount1 AS signed) * CAST(operation_type_progress1 AS signed))/100
                    WHEN operation_type_id2 = 106 THEN (CAST(operation_type_amount2 AS signed) * CAST(operation_type_progress2 AS signed))/100
                    WHEN operation_type_id3 = 106 THEN (CAST(operation_type_amount3 AS signed) * CAST(operation_type_progress3 AS signed))/100
                    WHEN operation_type_id4 = 106 THEN (CAST(operation_type_amount4 AS signed) * CAST(operation_type_progress4 AS signed))/100
                END)) AS SlarySaildDone,

                ROUND(SUM(CASE WHEN operation_type_id1 = 107 THEN operation_type_amount1
                    WHEN operation_type_id2 = 107 THEN operation_type_amount2
                    WHEN operation_type_id3 = 107 THEN operation_type_amount3
                    WHEN operation_type_id4 = 107 THEN operation_type_amount4
                END)) AS ScrubSailDoing,
                ROUND(SUM(CASE WHEN operation_type_id1 = 107 THEN (CAST(operation_type_amount1 AS signed) * CAST(operation_type_progress1 AS signed))/100
                    WHEN operation_type_id2 = 107 THEN (CAST(operation_type_amount2 AS signed) * CAST(operation_type_progress2 AS signed))/100
                    WHEN operation_type_id3 = 107 THEN (CAST(operation_type_amount3 AS signed) * CAST(operation_type_progress3 AS signed))/100
                    WHEN operation_type_id4 = 107 THEN (CAST(operation_type_amount4 AS signed) * CAST(operation_type_progress4 AS signed))/100
                END)) AS ScrubSailDone,

                ROUND(SUM(CASE WHEN operation_type_id1 = 108 THEN operation_type_amount1
                    WHEN operation_type_id2 = 108 THEN operation_type_amount2
                    WHEN operation_type_id3 = 108 THEN operation_type_amount3
                    WHEN operation_type_id4 = 108 THEN operation_type_amount4
                END)) AS FogSailDoing,
                ROUND(SUM(CASE WHEN operation_type_id1 = 108 THEN (CAST(operation_type_amount1 AS signed) * CAST(operation_type_progress1 AS signed))/100
                    WHEN operation_type_id2 = 108 THEN (CAST(operation_type_amount2 AS signed) * CAST(operation_type_progress2 AS signed))/100
                    WHEN operation_type_id3 = 108 THEN (CAST(operation_type_amount3 AS signed) * CAST(operation_type_progress3 AS signed))/100
                    WHEN operation_type_id4 = 108 THEN (CAST(operation_type_amount4 AS signed) * CAST(operation_type_progress4 AS signed))/100
                END)) AS FogSailDone,

                ROUND(SUM(CASE WHEN operation_type_id1 = 109 THEN operation_type_amount1
                    WHEN operation_type_id2 = 109 THEN operation_type_amount2
                    WHEN operation_type_id3 = 109 THEN operation_type_amount3
                    WHEN operation_type_id4 = 109 THEN operation_type_amount4
                END)) AS SailKatDoing,
                ROUND(SUM(CASE WHEN operation_type_id1 = 109 THEN (CAST(operation_type_amount1 AS signed) * CAST(operation_type_progress1 AS signed))/100
                    WHEN operation_type_id2 = 109 THEN (CAST(operation_type_amount2 AS signed) * CAST(operation_type_progress2 AS signed))/100
                    WHEN operation_type_id3 = 109 THEN (CAST(operation_type_amount3 AS signed) * CAST(operation_type_progress3 AS signed))/100
                    WHEN operation_type_id4 = 109 THEN (CAST(operation_type_amount4 AS signed) * CAST(operation_type_progress4 AS signed))/100
                END)) AS SailKatDone,

                ROUND(SUM(CASE WHEN operation_type_id1 = 110 THEN operation_type_amount1
                    WHEN operation_type_id2 = 110 THEN operation_type_amount2
                    WHEN operation_type_id3 = 110 THEN operation_type_amount3
                    WHEN operation_type_id4 = 110 THEN operation_type_amount4
                END)) AS KipSailDoing,
                ROUND(SUM(CASE WHEN operation_type_id1 = 110 THEN (CAST(operation_type_amount1 AS signed) * CAST(operation_type_progress1 AS signed))/100
                    WHEN operation_type_id2 = 110 THEN (CAST(operation_type_amount2 AS signed) * CAST(operation_type_progress2 AS signed))/100
                    WHEN operation_type_id3 = 110 THEN (CAST(operation_type_amount3 AS signed) * CAST(operation_type_progress3 AS signed))/100
                    WHEN operation_type_id4 = 110 THEN (CAST(operation_type_amount4 AS signed) * CAST(operation_type_progress4 AS signed))/100
                END)) AS KipSailDone,

                ROUND(SUM(CASE WHEN operation_type_id1 = 111 THEN operation_type_amount1
                    WHEN operation_type_id2 = 111 THEN operation_type_amount2
                    WHEN operation_type_id3 = 111 THEN operation_type_amount3
                    WHEN operation_type_id4 = 111 THEN operation_type_amount4
                END)) AS AsphaltRedmixDoing,
                ROUND(SUM(CASE WHEN operation_type_id1 = 111 THEN (CAST(operation_type_amount1 AS signed) * CAST(operation_type_progress1 AS signed))/100
                    WHEN operation_type_id2 = 111 THEN (CAST(operation_type_amount2 AS signed) * CAST(operation_type_progress2 AS signed))/100
                    WHEN operation_type_id3 = 111 THEN (CAST(operation_type_amount3 AS signed) * CAST(operation_type_progress3 AS signed))/100
                    WHEN operation_type_id4 = 111 THEN (CAST(operation_type_amount4 AS signed) * CAST(operation_type_progress4 AS signed))/100
                END)) AS AsphaltRedmixDone,

                ROUND(SUM(CASE WHEN operation_type_id1 = 112 THEN operation_type_amount1
                    WHEN operation_type_id2 = 112 THEN operation_type_amount2
                    WHEN operation_type_id3 = 112 THEN operation_type_amount3
                    WHEN operation_type_id4 = 112 THEN operation_type_amount4
                END)) AS LakegiriDoing,
                ROUND(SUM(CASE WHEN operation_type_id1 = 112 THEN (CAST(operation_type_amount1 AS signed) * CAST(operation_type_progress1 AS signed))/100
                    WHEN operation_type_id2 = 112 THEN (CAST(operation_type_amount2 AS signed) * CAST(operation_type_progress2 AS signed))/100
                    WHEN operation_type_id3 = 112 THEN (CAST(operation_type_amount3 AS signed) * CAST(operation_type_progress3 AS signed))/100
                    WHEN operation_type_id4 = 112 THEN (CAST(operation_type_amount4 AS signed) * CAST(operation_type_progress4 AS signed))/100
                END)) AS LakegiriDone,

                ROUND(SUM(CASE WHEN operation_type_id1 = 113 THEN operation_type_amount1
                    WHEN operation_type_id2 = 113 THEN operation_type_amount2
                    WHEN operation_type_id3 = 113 THEN operation_type_amount3
                    WHEN operation_type_id4 = 113 THEN operation_type_amount4
                END)) AS DarzgiriDoing,
                ROUND(SUM(CASE WHEN operation_type_id1 = 113 THEN (CAST(operation_type_amount1 AS signed) * CAST(operation_type_progress1 AS signed))/100
                    WHEN operation_type_id2 = 113 THEN (CAST(operation_type_amount2 AS signed) * CAST(operation_type_progress2 AS signed))/100
                    WHEN operation_type_id3 = 113 THEN (CAST(operation_type_amount3 AS signed) * CAST(operation_type_progress3 AS signed))/100
                    WHEN operation_type_id4 = 113 THEN (CAST(operation_type_amount4 AS signed) * CAST(operation_type_progress4 AS signed))/100
                END)) AS DarzgiriDone,
                COUNT(*) AS countAll
                FROM contract_subitems
                WHERE project_type_id = 1
                ";

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

        $query .= " GROUP BY province_id
                ORDER BY province_id ASC;";

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

        $sum = [
            'AsphaltGarmDoingSum' => 0,
            'MaseAsphaltDoingSum' => 0,
            'BazyaftSardGarmDoingSum' => 0,
            'mirosurfacingDoingSum' => 0,
            'ChipSailDoingSum' => 0,
            'SlarySaildDoingSum' => 0,
            'ScrubSailDoingSum' => 0,
            'FogSailDoingSum' => 0,
            'SailKatDoingSum' => 0,
            'KipSailDoingSum' => 0,
            'AsphaltRedmixDoingSum' => 0,
            'LakegiriDoingSum' => 0,
            'DarzgiriDoingSum' => 0,
            'AsphaltGarmDoneSum' => 0,
            'MaseAsphaltDoneSum' => 0,
            'BazyaftSardGarmDoneSum' => 0,
            'mirosurfacingDoneSum' => 0,
            'ChipSailDoneSum' => 0,
            'SlarySaildDoneSum' => 0,
            'ScrubSailDoneSum' => 0,
            'FogSailDoneSum' => 0,
            'SailKatDoneSum' => 0,
            'KipSailDoneSum' => 0,
            'AsphaltRedmixDoneSum' => 0,
            'LakegiriDoneSum' => 0,
            'DarzgiriDoneSum' => 0,
            'countAll' => 0,
        ];

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

                'AsphaltGarmDoing' => $value->AsphaltGarmDoing,
                'MaseAsphaltDoing' => $value->MaseAsphaltDoing,
                'BazyaftSardGarmDoing' => $value->BazyaftSardGarmDoing,
                'mirosurfacingDoing' => $value->mirosurfacingDoing,
                'ChipSailDoing' => $value->ChipSailDoing,
                'SlarySaildDoing' => $value->SlarySaildDoing,
                'ScrubSailDoing' => $value->ScrubSailDoing,
                'FogSailDoing' => $value->FogSailDoing,
                'SailKatDoing' => $value->SailKatDoing,
                'KipSailDoing' => $value->KipSailDoing,
                'AsphaltRedmixDoing' => $value->AsphaltRedmixDoing,
                'LakegiriDoing' => $value->LakegiriDoing,
                'DarzgiriDoing' => $value->DarzgiriDoing,
                'AsphaltGarmDone' => $value->AsphaltGarmDone,
                'MaseAsphaltDone' => $value->MaseAsphaltDone,
                'BazyaftSardGarmDone' => $value->BazyaftSardGarmDone,
                'mirosurfacingDone' => $value->mirosurfacingDone,
                'ChipSailDone' => $value->ChipSailDone,
                'SlarySaildDone' => $value->SlarySaildDone,
                'ScrubSailDone' => $value->ScrubSailDone,
                'FogSailDone' => $value->FogSailDone,
                'SailKatDone' => $value->SailKatDone,
                'KipSailDone' => $value->KipSailDone,
                'AsphaltRedmixDone' => $value->AsphaltRedmixDone,
                'LakegiriDone' => $value->LakegiriDone,
                'DarzgiriDone' => $value->DarzgiriDone,

                'countAll' => $value->countAll,
                
            ];
            $sum['AsphaltGarmDoingSum'] += $value->AsphaltGarmDoing;
            $sum['MaseAsphaltDoingSum'] += $value->MaseAsphaltDoing;
            $sum['BazyaftSardGarmDoingSum'] += $value->BazyaftSardGarmDoing;
            $sum['mirosurfacingDoingSum'] += $value->mirosurfacingDoing;
            $sum['ChipSailDoingSum'] += $value->ChipSailDoing;
            $sum['SlarySaildDoingSum'] += $value->SlarySaildDoing;
            $sum['ScrubSailDoingSum'] += $value->ScrubSailDoing;
            $sum['FogSailDoingSum'] += $value->FogSailDoing;
            $sum['SailKatDoingSum'] += $value->SailKatDoing;
            $sum['KipSailDoingSum'] += $value->KipSailDoing;
            $sum['AsphaltRedmixDoingSum'] += $value->AsphaltRedmixDoing;
            $sum['LakegiriDoingSum'] += $value->LakegiriDoing;
            $sum['DarzgiriDoingSum'] += $value->DarzgiriDoing;
            $sum['AsphaltGarmDoneSum'] += $value->AsphaltGarmDone;
            $sum['MaseAsphaltDoneSum'] += $value->MaseAsphaltDone;
            $sum['BazyaftSardGarmDoneSum'] += $value->BazyaftSardGarmDone;
            $sum['mirosurfacingDoneSum'] += $value->mirosurfacingDone;
            $sum['ChipSailDoneSum'] += $value->ChipSailDone;
            $sum['SlarySaildDoneSum'] += $value->SlarySaildDone;
            $sum['ScrubSailDoneSum'] += $value->ScrubSailDone;
            $sum['FogSailDoneSum'] += $value->FogSailDone;
            $sum['SailKatDoneSum'] += $value->SailKatDone;
            $sum['KipSailDoneSum'] += $value->KipSailDone;
            $sum['AsphaltRedmixDoneSum'] += $value->AsphaltRedmixDone;
            $sum['LakegiriDoneSum'] += $value->LakegiriDone;
            $sum['DarzgiriDoneSum'] += $value->DarzgiriDone;
            $sum['countAll'] += $value->countAll;
        }
        
        return view('excel.Maintanance.CompareProgramAndFunction', [
            'sum' => $sum,
            'roads' => $array,
            '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'
                ]
            ],
        ];
    }
}
