<?php

namespace App\Exports\RoadUpgrade;

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


class RoadUpgradeSafeties 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
    {
        $fromDate = $this->fromDate;
        $toDate = $this->toDate;
        $province = $this->province;
        $last_status_id = $this->last_status_id;

        $data = ContractSubItems::where('project_type_id', 3)
                                ->when($this->province, function ($query, $province) {
                                    return $query->where('province_id', $province);
                                })
                                ->when($this->last_status_id, function ($query, $last_status_id) {
                                    return $query->where('last_status_id', $last_status_id);
                                })
                                ->with('Contracts:id,contract_peymankar')
                                ->orderBy('province_id', 'ASC')
                                ->get();

        $array = [];
        foreach ($data as $road) {

            $array[] = [
                'province' => $road->province_fa,
                'city' => $road->city_fa,
                'axis_type_fa' => $road->axis_type_fa,
                'axis_name_fa' => $road->axis_name_fa,
                'project_title' => $road->project_title,

                'isDefined' => [
                    'hefazBotoni' => $road->operation_type_id1 == '301' ? $road->operation_type_amount1 :
                                        ($road->operation_type_id2 == '301' ? $road->operation_type_amount2 :
                                        ($road->operation_type_id3 == '301' ? $road->operation_type_amount3 :
                                        ($road->operation_type_id4 == '301' ? $road->operation_type_amount4 : "-"))),
                    'hefazFelezi' => $road->operation_type_id1 == '302' ? $road->operation_type_amount1 :
                                        ($road->operation_type_id2 == '302' ? $road->operation_type_amount2 :
                                        ($road->operation_type_id3 == '302' ? $road->operation_type_amount3 :
                                        ($road->operation_type_id4 == '302' ? $road->operation_type_amount4 : "-"))),
                    'RoshanayiTooli' => $road->operation_type_id1 == '303' ? $road->operation_type_amount1 :
                                        ($road->operation_type_id2 == '303' ? $road->operation_type_amount2 :
                                        ($road->operation_type_id3 == '303' ? $road->operation_type_amount3 :
                                        ($road->operation_type_id4 == '303' ? $road->operation_type_amount4 : "-"))),
                    'RoshanayiNoghteyi' => $road->operation_type_id1 == '304' ? $road->operation_type_amount1 :
                                        ($road->operation_type_id2 == '304' ? $road->operation_type_amount2 :
                                        ($road->operation_type_id3 == '304' ? $road->operation_type_amount3 :
                                        ($road->operation_type_id4 == '304' ? $road->operation_type_amount4 : 0))),
                    'AlaemImeni' => $road->operation_type_id1 == '305' ? $road->operation_type_amount1 :
                                        ($road->operation_type_id2 == '305' ? $road->operation_type_amount2 :
                                        ($road->operation_type_id3 == '305' ? $road->operation_type_amount3 :
                                        ($road->operation_type_id4 == '305' ? $road->operation_type_amount4 : 0))),
                    'Khatkeshi' => $road->operation_type_id1 == '306' ? $road->operation_type_amount1 :
                                        ($road->operation_type_id2 == '306' ? $road->operation_type_amount2 :
                                        ($road->operation_type_id3 == '306' ? $road->operation_type_amount3 :
                                        ($road->operation_type_id4 == '306' ? $road->operation_type_amount4 : "-"))),
                    'TaransheBardari' => $road->operation_type_id1 == '307' ? $road->operation_type_amount1 :
                                        ($road->operation_type_id2 == '307' ? $road->operation_type_amount2 :
                                        ($road->operation_type_id3 == '307' ? $road->operation_type_amount3 :
                                        ($road->operation_type_id4 == '307' ? $road->operation_type_amount4 : "-"))),
                        
                ],

                'isDone' => [
                    'hefazBotoni' => $road->operation_type_id1 == '301' ? $road->operation_type_amount1 * $road->operation_type_progress1 / 100 :
                                        ($road->operation_type_id2 == '301' ? $road->operation_type_amount2 * $road->operation_type_progress2 / 100 :
                                        ($road->operation_type_id3 == '301' ? $road->operation_type_amount3 * $road->operation_type_progress3 / 100 :
                                        ($road->operation_type_id4 == '301' ? $road->operation_type_amount4 * $road->operation_type_progress4 / 100 : "-"))),
                    'hefazFelezi' => $road->operation_type_id1 == '302' ? $road->operation_type_amount1 * $road->operation_type_progress1 / 100 :
                                        ($road->operation_type_id2 == '302' ? $road->operation_type_amount2 * $road->operation_type_progress2 / 100 :
                                        ($road->operation_type_id3 == '302' ? $road->operation_type_amount3 * $road->operation_type_progress3 / 100 :
                                        ($road->operation_type_id4 == '302' ? $road->operation_type_amount4 * $road->operation_type_progress4 / 100 : "-"))),
                    'RoshanayiTooli' => $road->operation_type_id1 == '303' ? $road->operation_type_amount1 * $road->operation_type_progress1 / 100 :
                                        ($road->operation_type_id2 == '303' ? $road->operation_type_amount2 * $road->operation_type_progress2 / 100 :
                                        ($road->operation_type_id3 == '303' ? $road->operation_type_amount3 * $road->operation_type_progress3 / 100 :
                                        ($road->operation_type_id4 == '303' ? $road->operation_type_amount4 * $road->operation_type_progress4 / 100 : "-"))),
                    'RoshanayiNoghteyi' => $road->operation_type_id1 == '304' ? $road->operation_type_amount1 * $road->operation_type_progress1 / 100 :
                                        ($road->operation_type_id2 == '304' ? $road->operation_type_amount2 * $road->operation_type_progress2 / 100 :
                                        ($road->operation_type_id3 == '304' ? $road->operation_type_amount3 * $road->operation_type_progress3 / 100 :
                                        ($road->operation_type_id4 == '304' ? $road->operation_type_amount4 * $road->operation_type_progress4 / 100 : 0))),
                    'AlaemImeni' => $road->operation_type_id1 == '305' ? $road->operation_type_amount1 * $road->operation_type_progress1 / 100 :
                                        ($road->operation_type_id2 == '305' ? $road->operation_type_amount2 * $road->operation_type_progress2 / 100 :
                                        ($road->operation_type_id3 == '305' ? $road->operation_type_amount3 * $road->operation_type_progress3 / 100 :
                                        ($road->operation_type_id4 == '305' ? $road->operation_type_amount4 * $road->operation_type_progress4 / 100 : 0))),
                    'Khatkeshi' => $road->operation_type_id1 == '306' ? $road->operation_type_amount1 * $road->operation_type_progress1 / 100 :
                                        ($road->operation_type_id2 == '306' ? $road->operation_type_amount2 * $road->operation_type_progress2 / 100 :
                                        ($road->operation_type_id3 == '306' ? $road->operation_type_amount3 * $road->operation_type_progress3 / 100 :
                                        ($road->operation_type_id4 == '306' ? $road->operation_type_amount4 * $road->operation_type_progress4 / 100 : "-"))),
                    'TaransheBardari' => $road->operation_type_id1 == '307' ? $road->operation_type_amount1 * $road->operation_type_progress1 / 100 :
                                        ($road->operation_type_id2 == '307' ? $road->operation_type_amount2 * $road->operation_type_progress2 / 100 :
                                        ($road->operation_type_id3 == '307' ? $road->operation_type_amount3 * $road->operation_type_progress3 :
                                        ($road->operation_type_id4 == '307' ? $road->operation_type_amount4 * $road->operation_type_progress4 : "-"))),
                ],
                'contract_date_from_parent_contract' => verta($road->contract_date_from_parent_contract)->format('Y/n/j'),
                'last_status_fa' => $road->last_status_fa,
                'progress_project' => $road->progress_project,
                'last_digit_project' => $road->last_digit_project,
                'last_function_operator' => $road->last_function_operator,
                'last_payable_operator' => $road->last_payable_operator,
                'complete_payable' => $road->complete_payable,
                'contract_peymankar' => $road->contracts->contract_peymankar,
                'contract_moshaver_tarahi' => $road->contract_moshaver_tarahi,
                'contract_moshaver_nezarat' => $road->contract_moshaver_nezarat,
                'operator_name' => $road->operator_name,
                'priority_project' => $road->priority_project_fa,
                'followup_priority_project' => $road->followup_priority_project,
                'operator_phone' => $road->operator_phone,
                'unique_code' => $road->unique_code,
                'updated_at_fa' =>  verta($road->last_date_update)->format('Y/n/j'),
            ];
        }

        return view('excel.Upgrade.RoadUpgradeSafeties', [
            'roads' => $array,
            'fromFa' => $fromDate ?? null,
            'toFa' => $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'
                ]
            ],
        ];

    }
}