<?php

namespace App\Exports\Village;

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,
                
                'KhakiDoing' => 0,
                'KhakiDone' => 0,
                
                'RosaziDoing' => 0,
                'RosaziDone' => 0,
                
                'AbneyehDoing' => 0,
                'AbneyehDone' => 0,
                
                'AsphaltDoing' => 0,
                'AsphaltDone' => 0,
                
                'countAll' => 0,
            ];
        }
        
        $query = "SELECT
        ROUND(SUM(CASE WHEN operation_type_id1 = 601 THEN operation_type_amount1
            WHEN operation_type_id2 = 601 THEN operation_type_amount2
            WHEN operation_type_id3 = 601 THEN operation_type_amount3
            WHEN operation_type_id4 = 601 THEN operation_type_amount4
        END)) AS KhakiDoing,
        ROUND(SUM(CASE WHEN operation_type_id1 = 601 THEN (cast(operation_type_amount1 as unsigned) * cast(operation_type_progress1 as unsigned))/100
            WHEN operation_type_id2 = 601 THEN (cast(operation_type_amount2 as unsigned) * cast(operation_type_progress2 as unsigned))/100
            WHEN operation_type_id3 = 601 THEN (cast(operation_type_amount3 as unsigned) * cast(operation_type_progress3 as unsigned))/100
            WHEN operation_type_id4 = 601 THEN (cast(operation_type_amount4 as unsigned) * cast(operation_type_progress4 as unsigned))/100
        END)) AS KhakiDone,

        ROUND(SUM(CASE WHEN operation_type_id1 = 602 THEN operation_type_amount1
            WHEN operation_type_id2 = 602 THEN operation_type_amount2
            WHEN operation_type_id3 = 602 THEN operation_type_amount3
            WHEN operation_type_id4 = 602 THEN operation_type_amount4
        END)) AS RosaziDoing,
        ROUND(SUM(CASE WHEN operation_type_id1 = 602 THEN (cast(operation_type_amount1 as unsigned) * cast(operation_type_progress1 as unsigned))/100
            WHEN operation_type_id2 = 602 THEN (cast(operation_type_amount2 as unsigned) * cast(operation_type_progress2 as unsigned))/100
            WHEN operation_type_id3 = 602 THEN (cast(operation_type_amount3 as unsigned) * cast(operation_type_progress3 as unsigned))/100
            WHEN operation_type_id4 = 602 THEN (cast(operation_type_amount4 as unsigned) * cast(operation_type_progress4 as unsigned))/100
        END)) AS RosaziDone,

        ROUND(SUM(CASE WHEN operation_type_id1 = 603 THEN operation_type_amount1
            WHEN operation_type_id2 = 603 THEN operation_type_amount2
            WHEN operation_type_id3 = 603 THEN operation_type_amount3
            WHEN operation_type_id4 = 603 THEN operation_type_amount4
        END)) AS AbneyehDoing,
        ROUND(SUM(CASE WHEN operation_type_id1 = 603 THEN (cast(operation_type_amount1 as unsigned) * cast(operation_type_progress1 as unsigned))/100
            WHEN operation_type_id2 = 603 THEN (cast(operation_type_amount2 as unsigned) * cast(operation_type_progress2 as unsigned))/100
            WHEN operation_type_id3 = 603 THEN (cast(operation_type_amount3 as unsigned) * cast(operation_type_progress3 as unsigned))/100
            WHEN operation_type_id4 = 603 THEN (cast(operation_type_amount4 as unsigned) * cast(operation_type_progress4 as unsigned))/100
        END)) AS AbneyehDone,

        ROUND(SUM(CASE WHEN operation_type_id1 = 604 THEN operation_type_amount1
            WHEN operation_type_id2 = 604 THEN operation_type_amount2
            WHEN operation_type_id3 = 604 THEN operation_type_amount3
            WHEN operation_type_id4 = 604 THEN operation_type_amount4
        END)) AS AsphaltDoing,
        ROUND(SUM(CASE WHEN operation_type_id1 = 604 THEN (cast(operation_type_amount1 as unsigned) * cast(operation_type_progress1 as unsigned))/100
            WHEN operation_type_id2 = 604 THEN (cast(operation_type_amount2 as unsigned) * cast(operation_type_progress2 as unsigned))/100
            WHEN operation_type_id3 = 604 THEN (cast(operation_type_amount3 as unsigned) * cast(operation_type_progress3 as unsigned))/100
            WHEN operation_type_id4 = 604 THEN (cast(operation_type_amount4 as unsigned) * cast(operation_type_progress4 as unsigned))/100
        END)) AS AsphaltDone,
        COUNT(*) AS countAll,
        province_fa,
        province_id
        FROM contract_subitems
        WHERE project_type_id = 6
        ";

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

        if ($this->fromDate) {
            $query .= " AND last_date_update 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 = [
            'KhakiDoing' => 0,
            'RosaziDoing' => 0,
            'AbneyehDoing' => 0,
            'AsphaltDoing' => 0,
            'KhakiDone' => 0,
            'RosaziDone' => 0,
            'AbneyehDone' => 0,
            'AsphaltDone' => 0,
            'countAll' => 0,
        ];
        
        foreach ($data as $key => $value) {
            $array[$value->province_id] = [
                'province_fa' => $value->province_fa,
                'KhakiDoing' => round($value->KhakiDoing, 2),
                'RosaziDoing' => round($value->RosaziDoing, 2),
                'AbneyehDoing' => round($value->AbneyehDoing, 2),
                'AsphaltDoing' => round($value->AsphaltDoing, 2),
                'KhakiDone' => round($value->KhakiDone, 2),
                'RosaziDone' => round($value->RosaziDone, 2),
                'AbneyehDone' => round($value->AbneyehDone, 2),
                'AsphaltDone' => round($value->AsphaltDone, 2),
                'countAll' => $value->countAll,
            ];

            $sum['KhakiDoing'] += $value->KhakiDoing;
            $sum['RosaziDoing'] += $value->RosaziDoing;
            $sum['AbneyehDoing'] += $value->AbneyehDoing;
            $sum['AsphaltDoing'] += $value->AsphaltDoing;
            $sum['KhakiDone'] += $value->KhakiDone;
            $sum['RosaziDone'] += $value->RosaziDone;
            $sum['AbneyehDone'] += $value->AbneyehDone;
            $sum['AsphaltDone'] += $value->AsphaltDone;
            $sum['countAll'] += $value->countAll;
        }


        return view('excel.Village.CompareProgramAndFunction', [
            'roads' => $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'
                ]
            ],
        ];
    }
}
