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

class RoadMaintanance 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
    {
        $province = $this->province;

        $data = ContractSubItems::where('project_type_id', 1)
                                ->when($this->province, function ($query, $province) {
                                    return $query->where('province_id', $province);
                                })
                                ->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,
                '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,
                'priority_project_fa' => $road->priority_project_fa,
                'followup_priority_project' => $road->followup_priority_project,
                'operator_name' => $road->operator_name,
                'operator_phone' => $road->operator_phone,
                'updated_at_fa' =>  verta($road->last_date_update)->format('Y/n/j'),
                'code' => $road->unique_code,

                'isDefined' => [
                    'rokeshAsphalt' => $road->operation_type_id1 == '101' ? $road->operation_type_amount1 :
                                        ($road->operation_type_id2 == '101' ? $road->operation_type_amount2 :
                                        ($road->operation_type_id3 == '101' ? $road->operation_type_amount3 :
                                        ($road->operation_type_id4 == '101' ? $road->operation_type_amount4 : "-"))),
                    'maseAsphalt' => $road->operation_type_id1 == '102' ? $road->operation_type_amount1 :
                                        ($road->operation_type_id2 == '102' ? $road->operation_type_amount2 :
                                        ($road->operation_type_id3 == '102' ? $road->operation_type_amount3 :
                                        ($road->operation_type_id4 == '102' ? $road->operation_type_amount4 : "-"))),
                    'baziaftSard' => $road->operation_type_id1 == '103' ? $road->operation_type_amount1 :
                                        ($road->operation_type_id2 == '103' ? $road->operation_type_amount2 :
                                        ($road->operation_type_id3 == '103' ? $road->operation_type_amount3 :
                                        ($road->operation_type_id4 == '103' ? $road->operation_type_amount4 : "-"))),
        
                    'microsurfacing' => $road->operation_type_id1 == '104' ? $road->operation_type_amount1 :
                                        ($road->operation_type_id2 == '104' ? $road->operation_type_amount2 :
                                        ($road->operation_type_id3 == '104' ? $road->operation_type_amount3 :
                                        ($road->operation_type_id4 == '104' ? $road->operation_type_amount4 : "-"))),
                    'chipSail' => $road->operation_type_id1 == '105' ? $road->operation_type_amount1 :
                                        ($road->operation_type_id2 == '105' ? $road->operation_type_amount2 :
                                        ($road->operation_type_id3 == '105' ? $road->operation_type_amount3 :
                                        ($road->operation_type_id4 == '105' ? $road->operation_type_amount4 : "-"))),
                    'slarySail' => $road->operation_type_id1 == '106' ? $road->operation_type_amount1 :
                                        ($road->operation_type_id2 == '106' ? $road->operation_type_amount2 :
                                        ($road->operation_type_id3 == '106' ? $road->operation_type_amount3 :
                                        ($road->operation_type_id4 == '106' ? $road->operation_type_amount4 : "-"))),
                    'scrubSail' => $road->operation_type_id1 == '107' ? $road->operation_type_amount1 :
                                        ($road->operation_type_id2 == '107' ? $road->operation_type_amount2 :
                                        ($road->operation_type_id3 == '107' ? $road->operation_type_amount3 :
                                        ($road->operation_type_id4 == '107' ? $road->operation_type_amount4 : "-"))),
                    'fogSail' => $road->operation_type_id1 == '108' ? $road->operation_type_amount1 :
                                        ($road->operation_type_id2 == '108' ? $road->operation_type_amount2 :
                                        ($road->operation_type_id3 == '108' ? $road->operation_type_amount3 :
                                        ($road->operation_type_id4 == '108' ? $road->operation_type_amount4 : "-"))),
                    'sailKat' => $road->operation_type_id1 == '109' ? $road->operation_type_amount1 :
                                        ($road->operation_type_id2 == '109' ? $road->operation_type_amount2 :
                                        ($road->operation_type_id3 == '109' ? $road->operation_type_amount3 :
                                        ($road->operation_type_id4 == '109' ? $road->operation_type_amount4 : "-"))),
                    'kipSail' => $road->operation_type_id1 == '110' ? $road->operation_type_amount1 :
                                        ($road->operation_type_id2 == '110' ? $road->operation_type_amount2 :
                                        ($road->operation_type_id3 == '110' ? $road->operation_type_amount3 :
                                        ($road->operation_type_id4 == '110' ? $road->operation_type_amount4 : "-"))),
                    'redmix' => $road->operation_type_id1 == '111' ? $road->operation_type_amount1 :
                                        ($road->operation_type_id2 == '111' ? $road->operation_type_amount2 :
                                        ($road->operation_type_id3 == '111' ? $road->operation_type_amount3 :
                                        ($road->operation_type_id4 == '111' ? $road->operation_type_amount4 : "-"))),
                    
                    'lakeGiri' => $road->operation_type_id1 == '112' ? $road->operation_type_amount1 :
                                        ($road->operation_type_id2 == '112' ? $road->operation_type_amount2 :
                                        ($road->operation_type_id3 == '112' ? $road->operation_type_amount3 :
                                        ($road->operation_type_id4 == '112' ? $road->operation_type_amount4 : "-"))),
                    'darzGiri' => $road->operation_type_id1 == '113' ? $road->operation_type_amount1 :
                                        ($road->operation_type_id2 == '113' ? $road->operation_type_amount2 :
                                        ($road->operation_type_id3 == '113' ? $road->operation_type_amount3 :
                                        ($road->operation_type_id4 == '113' ? $road->operation_type_amount4 : "-"))),
                    
                ],

                'isDone' => [
                    'rokeshAsphalt' => $road->operation_type_id1 == '101' ? $road->operation_type_amount1 * $road->operation_type_progress1 / 100 :
                                        ($road->operation_type_id2 == '101' ? $road->operation_type_amount2 * $road->operation_type_progress2 / 100 :
                                        ($road->operation_type_id3 == '101' ? $road->operation_type_amount3 * $road->operation_type_progress3 / 100 :
                                        ($road->operation_type_id4 == '101' ? $road->operation_type_amount4 * $road->operation_type_progress4 / 100 : "-"))),
                    'maseAsphalt' => $road->operation_type_id1 == '102' ? $road->operation_type_amount1 * $road->operation_type_progress1 / 100 :
                                        ($road->operation_type_id2 == '102' ? $road->operation_type_amount2 * $road->operation_type_progress2 / 100 :
                                        ($road->operation_type_id3 == '102' ? $road->operation_type_amount3 * $road->operation_type_progress3 / 100 :
                                        ($road->operation_type_id4 == '102' ? $road->operation_type_amount4 * $road->operation_type_progress4 / 100 : "-"))),
                    'baziaftSard' => $road->operation_type_id1 == '103' ? $road->operation_type_amount1 * $road->operation_type_progress1 / 100 :
                                        ($road->operation_type_id2 == '103' ? $road->operation_type_amount2 * $road->operation_type_progress2 / 100 :
                                        ($road->operation_type_id3 == '103' ? $road->operation_type_amount3 * $road->operation_type_progress3 / 100 :
                                        ($road->operation_type_id4 == '103' ? $road->operation_type_amount4 * $road->operation_type_progress4 / 100 : "-"))),
        
                    'microsurfacing' => $road->operation_type_id1 == '104' ? $road->operation_type_amount1 * $road->operation_type_progress1 / 100 :
                                        ($road->operation_type_id2 == '104' ? $road->operation_type_amount2 * $road->operation_type_progress2 / 100 :
                                        ($road->operation_type_id3 == '104' ? $road->operation_type_amount3 * $road->operation_type_progress3 / 100 :
                                        ($road->operation_type_id4 == '104' ? $road->operation_type_amount4 * $road->operation_type_progress4 / 100 : "-"))),
                    'chipSail' => $road->operation_type_id1 == '105' ? $road->operation_type_amount1 * $road->operation_type_progress1 / 100 :
                                        ($road->operation_type_id2 == '105' ? $road->operation_type_amount2 * $road->operation_type_progress2 / 100 :
                                        ($road->operation_type_id3 == '105' ? $road->operation_type_amount3 * $road->operation_type_progress3 / 100 :
                                        ($road->operation_type_id4 == '105' ? $road->operation_type_amount4 * $road->operation_type_progress4 / 100 : "-"))),
                    'slarySail' => $road->operation_type_id1 == '106' ? $road->operation_type_amount1 * $road->operation_type_progress1 / 100 :
                                        ($road->operation_type_id2 == '106' ? $road->operation_type_amount2 * $road->operation_type_progress2 / 100 :
                                        ($road->operation_type_id3 == '106' ? $road->operation_type_amount3 * $road->operation_type_progress3 / 100 :
                                        ($road->operation_type_id4 == '106' ? $road->operation_type_amount4 * $road->operation_type_progress4 / 100 : "-"))),
                    'scrubSail' => $road->operation_type_id1 == '107' ? $road->operation_type_amount1 * $road->operation_type_progress1 / 100 :
                                        ($road->operation_type_id2 == '107' ? $road->operation_type_amount2 * $road->operation_type_progress2 / 100 :
                                        ($road->operation_type_id3 == '107' ? $road->operation_type_amount3 * $road->operation_type_progress3 / 100 :
                                        ($road->operation_type_id4 == '107' ? $road->operation_type_amount4 * $road->operation_type_progress4 / 100 : "-"))),
                    'fogSail' => $road->operation_type_id1 == '108' ? $road->operation_type_amount1 * $road->operation_type_progress1 / 100 :
                                        ($road->operation_type_id2 == '108' ? $road->operation_type_amount2 * $road->operation_type_progress2 / 100 :
                                        ($road->operation_type_id3 == '108' ? $road->operation_type_amount3 * $road->operation_type_progress3 / 100 :
                                        ($road->operation_type_id4 == '108' ? $road->operation_type_amount4 * $road->operation_type_progress4 / 100 : "-"))),
                    'sailKat' => $road->operation_type_id1 == '109' ? $road->operation_type_amount1 * $road->operation_type_progress1 / 100 :
                                        ($road->operation_type_id2 == '109' ? $road->operation_type_amount2 * $road->operation_type_progress2 / 100 :
                                        ($road->operation_type_id3 == '109' ? $road->operation_type_amount3 * $road->operation_type_progress3 / 100 :
                                        ($road->operation_type_id4 == '109' ? $road->operation_type_amount4 * $road->operation_type_progress4 / 100 : "-"))),
                    'kipSail' => $road->operation_type_id1 == '110' ? $road->operation_type_amount1 * $road->operation_type_progress1 / 100 :
                                        ($road->operation_type_id2 == '110' ? $road->operation_type_amount2 * $road->operation_type_progress2 / 100 :
                                        ($road->operation_type_id3 == '110' ? $road->operation_type_amount3 * $road->operation_type_progress3 / 100 :
                                        ($road->operation_type_id4 == '110' ? $road->operation_type_amount4 * $road->operation_type_progress4 / 100 : "-"))),
                    'redmix' => $road->operation_type_id1 == '111' ? $road->operation_type_amount1 * $road->operation_type_progress1 / 100 :
                                        ($road->operation_type_id2 == '111' ? $road->operation_type_amount2 * $road->operation_type_progress2 / 100 :
                                        ($road->operation_type_id3 == '111' ? $road->operation_type_amount3 * $road->operation_type_progress3 / 100 :
                                        ($road->operation_type_id4 == '111' ? $road->operation_type_amount4 * $road->operation_type_progress4 / 100 : "-"))),
                    
                    'lakeGiri' => $road->operation_type_id1 == '112' ? $road->operation_type_amount1 * $road->operation_type_progress1 / 100 :
                                        ($road->operation_type_id2 == '112' ? $road->operation_type_amount2 * $road->operation_type_progress2 / 100 :
                                        ($road->operation_type_id3 == '112' ? $road->operation_type_amount3 * $road->operation_type_progress3 / 100 :
                                        ($road->operation_type_id4 == '112' ? $road->operation_type_amount4 * $road->operation_type_progress4 / 100 : "-"))),
                    'darzGiri' => $road->operation_type_id1 == '113' ? $road->operation_type_amount1 * $road->operation_type_progress1 / 100 :
                                        ($road->operation_type_id2 == '113' ? $road->operation_type_amount2 * $road->operation_type_progress2 / 100 :
                                        ($road->operation_type_id3 == '113' ? $road->operation_type_amount3 * $road->operation_type_progress3 / 100 :
                                        ($road->operation_type_id4 == '113' ? $road->operation_type_amount4 * $road->operation_type_progress4 / 100 : "-"))),
                ],
            ];
        }


        return view('excel.Maintanance.RoadMaintanance', [
            'roads' => $array,
        ]);
    }

    /**
     * @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'
                ]
            ],
        ];
    }
}
