<?php

namespace App\Exports\Maintanance;

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 Maatwebsite\Excel\Concerns\WithDrawings;
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
use Maatwebsite\Excel\Concerns\WithStyles;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use Illuminate\Support\Facades\DB;
use Hekmatinasser\Verta\Verta;


class summaryAcidentPerformanceProvince 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,
                
                'AsphaltGarmDone' => 0,
                'MaseAsphaltDone' => 0,
                'BazyaftSardGarmDone' => 0,
                'RokeshTghviyatiTonajeKol' => 0,
                'mirosurfacingDone' => 0,
                'ChipSailDone' => 0,
                'SlarySaildDone' => 0,
                'ScrubSailDone' => 0,
                'FogSailDone' => 0,
                'SailKatDone' => 0,
                'KipSailDone' => 0,
                'AsphaltRedmixDone' => 0,
                'RokeshHefazatiTonajeKol' => 0,
                'LakegiriDone' => 0,
                'DarzgiriDone' => 0,
                'LakeGiriTonajkol' => 0,

                'qty' => 0,
                'progress_project' => 0,
            ];
        }

        $query =  "SELECT COUNT( 
                    CASE
                        WHEN progress_project > 0 THEN 1
                        WHEN operation_type_progress1 > 0 THEN 1
                        WHEN operation_type_progress2 > 0 THEN 1
                        WHEN operation_type_progress3 > 0 THEN 1
                        WHEN operation_type_progress4 > 0 THEN 1
                    END
                    ) AS qty,
                    province_fa,
                    province_id,
                    date_,
                    ROUND(SUM(
                        CASE
                            WHEN operation_type_id1 = 101 THEN (
                                operation_type_amount1 * operation_type_progress1
                            ) / 100
                            WHEN operation_type_id2 = 101 THEN (
                                operation_type_amount2 * operation_type_progress2
                            ) / 100
                            WHEN operation_type_id3 = 101 THEN (
                                operation_type_amount3 * operation_type_progress3
                            ) / 100
                            WHEN operation_type_id4 = 101 THEN (
                                operation_type_amount4 * operation_type_progress4
                            ) / 100
                        END
                    ),2) AS AsphaltGarmDone,
                    ROUND(SUM(
                        CASE
                            WHEN operation_type_id1 = 102 THEN (
                                operation_type_amount1 * operation_type_progress1
                            ) / 100
                            WHEN operation_type_id2 = 102 THEN (
                                operation_type_amount2 * operation_type_progress2
                            ) / 100
                            WHEN operation_type_id3 = 102 THEN (
                                operation_type_amount3 * operation_type_progress3
                            ) / 100
                            WHEN operation_type_id4 = 102 THEN (
                                operation_type_amount4 * operation_type_progress4
                            ) / 100
                        END
                    ),2) AS MaseAsphaltDone,
                    ROUND(SUM(
                        CASE
                            WHEN operation_type_id1 = 103 THEN (
                                operation_type_amount1 * operation_type_progress1
                            ) / 100
                            WHEN operation_type_id2 = 103 THEN (
                                operation_type_amount2 * operation_type_progress2
                            ) / 100
                            WHEN operation_type_id3 = 103 THEN (
                                operation_type_amount3 * operation_type_progress3
                            ) / 100
                            WHEN operation_type_id4 = 103 THEN (
                                operation_type_amount4 * operation_type_progress4
                            ) / 100
                        END
                    ),2) AS BazyaftSardGarmDone,
                    ROUND(SUM(
                        CASE
                            WHEN operation_type_id1 = 101 THEN operation_type_tonaj1
                            WHEN operation_type_id2 = 101 THEN operation_type_tonaj2
                            WHEN operation_type_id3 = 101 THEN operation_type_tonaj3
                            WHEN operation_type_id4 = 101 THEN operation_type_tonaj4
                            WHEN operation_type_id1 = 102 THEN operation_type_tonaj1
                            WHEN operation_type_id2 = 102 THEN operation_type_tonaj2
                            WHEN operation_type_id3 = 102 THEN operation_type_tonaj3
                            WHEN operation_type_id4 = 102 THEN operation_type_tonaj4
                            WHEN operation_type_id1 = 103 THEN operation_type_tonaj1
                            WHEN operation_type_id2 = 103 THEN operation_type_tonaj2
                            WHEN operation_type_id3 = 103 THEN operation_type_tonaj3
                            WHEN operation_type_id4 = 103 THEN operation_type_tonaj4
                        END
                    ),2) AS RokeshTghviyatiTonajeKol,
                    ROUND(SUM(
                        CASE
                            WHEN operation_type_id1 = 104 THEN (
                                operation_type_amount1 * operation_type_progress1
                            ) / 100
                            WHEN operation_type_id2 = 104 THEN (
                                operation_type_amount2 * operation_type_progress2
                            ) / 100
                            WHEN operation_type_id3 = 104 THEN (
                                operation_type_amount3 * operation_type_progress3
                            ) / 100
                            WHEN operation_type_id4 = 104 THEN (
                                operation_type_amount4 * operation_type_progress4
                            ) / 100
                        END
                    ),2) AS mirosurfacingDone,
                    ROUND(SUM(
                        CASE
                            WHEN operation_type_id1 = 105 THEN (
                                operation_type_amount1 * operation_type_progress1
                            ) / 100
                            WHEN operation_type_id2 = 105 THEN (
                                operation_type_amount2 * operation_type_progress2
                            ) / 100
                            WHEN operation_type_id3 = 105 THEN (
                                operation_type_amount3 * operation_type_progress3
                            ) / 100
                            WHEN operation_type_id4 = 105 THEN (
                                operation_type_amount4 * operation_type_progress4
                            ) / 100
                        END
                    ),2) AS ChipSailDone,
                    ROUND(SUM(
                        CASE
                            WHEN operation_type_id1 = 106 THEN (
                                operation_type_amount1 * operation_type_progress1
                            ) / 100
                            WHEN operation_type_id2 = 106 THEN (
                                operation_type_amount2 * operation_type_progress2
                            ) / 100
                            WHEN operation_type_id3 = 106 THEN (
                                operation_type_amount3 * operation_type_progress3
                            ) / 100
                            WHEN operation_type_id4 = 106 THEN (
                                operation_type_amount4 * operation_type_progress4
                            ) / 100
                        END
                    ),2) AS SlarySaildDone,
                    ROUND(SUM(
                        CASE
                            WHEN operation_type_id1 = 107 THEN (
                                operation_type_amount1 * operation_type_progress1
                            ) / 100
                            WHEN operation_type_id2 = 107 THEN (
                                operation_type_amount2 * operation_type_progress2
                            ) / 100
                            WHEN operation_type_id3 = 107 THEN (
                                operation_type_amount3 * operation_type_progress3
                            ) / 100
                            WHEN operation_type_id4 = 107 THEN (
                                operation_type_amount4 * operation_type_progress4
                            ) / 100
                        END
                    ),2) AS ScrubSailDone,
                    ROUND(SUM(
                        CASE
                            WHEN operation_type_id1 = 108 THEN (
                                operation_type_amount1 * operation_type_progress1
                            ) / 100
                            WHEN operation_type_id2 = 108 THEN (
                                operation_type_amount2 * operation_type_progress2
                            ) / 100
                            WHEN operation_type_id3 = 108 THEN (
                                operation_type_amount3 * operation_type_progress3
                            ) / 100
                            WHEN operation_type_id4 = 108 THEN (
                                operation_type_amount4 * operation_type_progress4
                            ) / 100
                        END
                    ),2) AS FogSailDone,
                    ROUND(SUM(
                        CASE
                            WHEN operation_type_id1 = 109 THEN (
                                operation_type_amount1 * operation_type_progress1
                            ) / 100
                            WHEN operation_type_id2 = 109 THEN (
                                operation_type_amount2 * operation_type_progress2
                            ) / 100
                            WHEN operation_type_id3 = 109 THEN (
                                operation_type_amount3 * operation_type_progress3
                            ) / 100
                            WHEN operation_type_id4 = 109 THEN (
                                operation_type_amount4 * operation_type_progress4
                            ) / 100
                        END
                    ),2) AS SailKatDone,
                    ROUND(SUM(
                        CASE
                            WHEN operation_type_id1 = 110 THEN (
                                operation_type_amount1 * operation_type_progress1
                            ) / 100
                            WHEN operation_type_id2 = 110 THEN (
                                operation_type_amount2 * operation_type_progress2
                            ) / 100
                            WHEN operation_type_id3 = 110 THEN (
                                operation_type_amount3 * operation_type_progress3
                            ) / 100
                            WHEN operation_type_id4 = 110 THEN (
                                operation_type_amount4 * operation_type_progress4
                            ) / 100
                        END
                    ),2) AS KipSailDone,
                    ROUND(SUM(
                        CASE
                            WHEN operation_type_id1 = 111 THEN (
                                operation_type_amount1 * operation_type_progress1
                            ) / 100
                            WHEN operation_type_id2 = 111 THEN (
                                operation_type_amount2 * operation_type_progress2
                            ) / 100
                            WHEN operation_type_id3 = 111 THEN (
                                operation_type_amount3 * operation_type_progress3
                            ) / 100
                            WHEN operation_type_id4 = 111 THEN (
                                operation_type_amount4 * operation_type_progress4
                            ) / 100
                        END
                    ),2) AS AsphaltRedmixDone,
                    ROUND(SUM(
                        CASE
                            WHEN operation_type_id1 = 104 THEN operation_type_tonaj1
                            WHEN operation_type_id2 = 104 THEN operation_type_tonaj2
                            WHEN operation_type_id3 = 104 THEN operation_type_tonaj3
                            WHEN operation_type_id4 = 104 THEN operation_type_tonaj4
                            WHEN operation_type_id1 = 105 THEN operation_type_tonaj1
                            WHEN operation_type_id2 = 105 THEN operation_type_tonaj2
                            WHEN operation_type_id3 = 105 THEN operation_type_tonaj3
                            WHEN operation_type_id4 = 105 THEN operation_type_tonaj4
                            WHEN operation_type_id1 = 106 THEN operation_type_tonaj1
                            WHEN operation_type_id2 = 106 THEN operation_type_tonaj2
                            WHEN operation_type_id3 = 106 THEN operation_type_tonaj3
                            WHEN operation_type_id4 = 106 THEN operation_type_tonaj4
                            WHEN operation_type_id1 = 107 THEN operation_type_tonaj1
                            WHEN operation_type_id2 = 107 THEN operation_type_tonaj2
                            WHEN operation_type_id3 = 107 THEN operation_type_tonaj3
                            WHEN operation_type_id4 = 107 THEN operation_type_tonaj4
                            WHEN operation_type_id1 = 108 THEN operation_type_tonaj1
                            WHEN operation_type_id2 = 108 THEN operation_type_tonaj2
                            WHEN operation_type_id3 = 108 THEN operation_type_tonaj3
                            WHEN operation_type_id4 = 108 THEN operation_type_tonaj4
                            WHEN operation_type_id1 = 109 THEN operation_type_tonaj1
                            WHEN operation_type_id2 = 109 THEN operation_type_tonaj2
                            WHEN operation_type_id3 = 109 THEN operation_type_tonaj3
                            WHEN operation_type_id4 = 109 THEN operation_type_tonaj4
                            WHEN operation_type_id1 = 110 THEN operation_type_tonaj1
                            WHEN operation_type_id2 = 110 THEN operation_type_tonaj2
                            WHEN operation_type_id3 = 110 THEN operation_type_tonaj3
                            WHEN operation_type_id4 = 110 THEN operation_type_tonaj4
                            WHEN operation_type_id1 = 111 THEN operation_type_tonaj1
                            WHEN operation_type_id2 = 111 THEN operation_type_tonaj2
                            WHEN operation_type_id3 = 111 THEN operation_type_tonaj3
                            WHEN operation_type_id4 = 111 THEN operation_type_tonaj4
                        END
                    ),2) AS RokeshHefazatiTonajeKol,
                    ROUND(SUM(
                        CASE
                            WHEN operation_type_id1 = 112 THEN (
                                operation_type_amount1 * operation_type_progress1
                            ) / 100
                            WHEN operation_type_id2 = 112 THEN (
                                operation_type_amount2 * operation_type_progress2
                            ) / 100
                            WHEN operation_type_id3 = 112 THEN (
                                operation_type_amount3 * operation_type_progress3
                            ) / 100
                            WHEN operation_type_id4 = 112 THEN (
                                operation_type_amount4 * operation_type_progress4
                            ) / 100
                        END
                    ),2) AS LakegiriDone,
                    ROUND(SUM(
                        CASE
                            WHEN operation_type_id1 = 113 THEN (
                                operation_type_amount1 * operation_type_progress1
                            ) / 100
                            WHEN operation_type_id2 = 113 THEN (
                                operation_type_amount2 * operation_type_progress2
                            ) / 100
                            WHEN operation_type_id3 = 113 THEN (
                                operation_type_amount3 * operation_type_progress3
                            ) / 100
                            WHEN operation_type_id4 = 113 THEN (
                                operation_type_amount4 * operation_type_progress4
                            ) / 100
                        END
                    ),2) AS DarzgiriDone,
                    ROUND(SUM(
                        CASE
                            WHEN operation_type_id1 = 112 THEN operation_type_tonaj1
                            WHEN operation_type_id2 = 112 THEN operation_type_tonaj2
                            WHEN operation_type_id3 = 112 THEN operation_type_tonaj3
                            WHEN operation_type_id4 = 112 THEN operation_type_tonaj4
                            WHEN operation_type_id1 = 113 THEN operation_type_tonaj1
                            WHEN operation_type_id2 = 113 THEN operation_type_tonaj2
                            WHEN operation_type_id3 = 113 THEN operation_type_tonaj3
                            WHEN operation_type_id4 = 113 THEN operation_type_tonaj4
                        END
                    ),2) AS LakeGiriTonajkol,
                    ROUND(AVG(progress_project)) as progress_project
                FROM (
                        SELECT unique_code,
                            date_,
                            province_id,
                            province_fa,
                            operation_type_id1,
                            operation_type_amount1,
                            MAX(operation_type_tonaj1) - MIN(operation_type_tonaj1) AS operation_type_tonaj1,
                            MAX(operation_type_progress1) - MIN(operation_type_progress1) AS operation_type_progress1,
                            operation_type_id2,
                            operation_type_amount2,
                            MAX(operation_type_tonaj2) - MIN(operation_type_tonaj2) AS operation_type_tonaj2,
                            MAX(operation_type_progress2) - MIN(operation_type_progress2) AS operation_type_progress2,
                            operation_type_id3,
                            operation_type_amount3,
                            MAX(operation_type_tonaj3) - MIN(operation_type_tonaj3) AS operation_type_tonaj3,
                            MAX(operation_type_progress3) - MIN(operation_type_progress3) AS operation_type_progress3,
                            operation_type_id4,
                            operation_type_amount4,
                            MAX(operation_type_tonaj4) - MIN(operation_type_tonaj4) AS operation_type_tonaj4,
                            MAX(operation_type_progress4) - MIN(operation_type_progress4) AS operation_type_progress4,
                            MAX(progress_project) - MIN(progress_project) AS progress_project
                        FROM (
                                SELECT project_type_id,
                                    unique_code,
                                    last_date_update AS date_,
                                    province_id,
                                    province_fa,
                                    operation_type_id1,
                                    operation_type_amount1,
                                    operation_type_progress1,
                                    operation_type_tonaj1,
                                    operation_type_id2,
                                    operation_type_amount2,
                                    operation_type_progress2,
                                    operation_type_tonaj2,
                                    operation_type_id3,
                                    operation_type_amount3,
                                    operation_type_progress3,
                                    operation_type_tonaj3,
                                    operation_type_id4,
                                    operation_type_amount4,
                                    operation_type_progress4,
                                    operation_type_tonaj4,
                                    progress_project
                                FROM contract_subitems
                                UNION
                                SELECT project_type_id,
                                    unique_code,
                                    last_date_update AS date_,
                                    province_id,
                                    province_fa,
                                    operation_type_id1,
                                    operation_type_amount1,
                                    operation_type_progress1,
                                    operation_type_tonaj1,
                                    operation_type_id2,
                                    operation_type_amount2,
                                    operation_type_progress2,
                                    operation_type_tonaj2,
                                    operation_type_id3,
                                    operation_type_amount3,
                                    operation_type_progress3,
                                    operation_type_tonaj3,
                                    operation_type_id4,
                                    operation_type_amount4,
                                    operation_type_progress4,
                                    operation_type_tonaj4,
                                    progress_project
                                FROM contract_sub_items_histories
                            ) AS merged_table
                        WHERE project_type_id = 1
                        GROUP BY unique_code
                    ) AS mreged_statistics_table WHERE 1=1  ";



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

        if ($this->fromDate) {
            $query .= " AND date_ BETWEEN '{$this->fromDate} 00:00:00' AND '{$this->toDate} 23:59:59'";
        }

        $query .= " GROUP BY province_id
        ORDER BY province_id ASC;";
        
        $data = DB::select($query);

        $sum = [
            'AsphaltGarmDone' => 0,
            'MaseAsphaltDone' => 0,
            'BazyaftSardGarmDone' => 0,
            'RokeshTghviyatiTonajeKol' => 0,
            'mirosurfacingDone' => 0,
            'ChipSailDone' => 0,
            'SlarySaildDone' => 0,
            'ScrubSailDone' => 0,
            'FogSailDone' => 0,
            'SailKatDone' => 0,
            'KipSailDone' => 0,
            'AsphaltRedmixDone' => 0,
            'RokeshHefazatiTonajeKol' => 0,
            'LakegiriDone' => 0,
            'DarzgiriDone' => 0,
            'LakeGiriTonajkol' => 0,
            'qty' => 0,
            'progress_project' => 0
        ];

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

                'AsphaltGarmDone' => $value->AsphaltGarmDone,
                'MaseAsphaltDone' => $value->MaseAsphaltDone,
                'BazyaftSardGarmDone' => $value->BazyaftSardGarmDone,
                'RokeshTghviyatiTonajeKol' => $value->RokeshTghviyatiTonajeKol,
                'mirosurfacingDone' => $value->mirosurfacingDone,
                'ChipSailDone' => $value->ChipSailDone,
                'SlarySaildDone' => $value->SlarySaildDone,
                'ScrubSailDone' => $value->ScrubSailDone,
                'FogSailDone' => $value->FogSailDone,
                'SailKatDone' => $value->SailKatDone,
                'KipSailDone' => $value->KipSailDone,
                'AsphaltRedmixDone' => $value->AsphaltRedmixDone,
                'RokeshHefazatiTonajeKol' => $value->RokeshHefazatiTonajeKol,
                'LakegiriDone' => $value->LakegiriDone,
                'DarzgiriDone' => $value->DarzgiriDone,
                'LakeGiriTonajkol' => $value->LakeGiriTonajkol,
                
                'qty' => $value->qty,
                'progress_project' => $value->progress_project,
            ];
            $sum['AsphaltGarmDone'] += $value->AsphaltGarmDone;
            $sum['MaseAsphaltDone'] += $value->MaseAsphaltDone;
            $sum['BazyaftSardGarmDone'] += $value->BazyaftSardGarmDone;
            $sum['RokeshTghviyatiTonajeKol'] += $value->RokeshTghviyatiTonajeKol;
            $sum['mirosurfacingDone'] += $value->mirosurfacingDone;
            $sum['ChipSailDone'] += $value->ChipSailDone;
            $sum['SlarySaildDone'] += $value->SlarySaildDone;
            $sum['ScrubSailDone'] += $value->ScrubSailDone;
            $sum['FogSailDone'] += $value->FogSailDone;
            $sum['SailKatDone'] += $value->SailKatDone;
            $sum['KipSailDone'] += $value->KipSailDone;
            $sum['AsphaltRedmixDone'] += $value->AsphaltRedmixDone;
            $sum['RokeshHefazatiTonajeKol'] += $value->RokeshHefazatiTonajeKol;
            $sum['LakegiriDone'] += $value->LakegiriDone;
            $sum['DarzgiriDone'] += $value->DarzgiriDone;
            $sum['LakeGiriTonajkol'] += $value->LakeGiriTonajkol;
            $sum['qty'] += $value->qty;
            $sum['progress_project'] += $value->progress_project;
        }

        return view('excel.Maintanance.summaryAccidentPerformanceProvince', [
            'sum' => $sum,
            'array' => $array,
            'sumShow' => $this->province ? 0 :1,
            'fromFa' => $this->fromDate ?? null,
            'toFa' => $this->toDate ?? null
        ]);
    }
  
    /**
     * @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'
                ]
            ],
        ];
    }
}
