<?php

namespace App\Exports\Technical;

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;

class TechnicalbuildingProjects 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', 4)
                                ->when($this->fromDate, function ($query) use ($fromDate, $toDate) {
                                    return $query->whereBetween('created_at', [$fromDate, $toDate]);
                                })
                                ->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,contract_moshaver_tarahi,contract_moshaver_nezarat,created_at_fa')
                                ->orderBy('province_id', 'ASC')
                                ->get();

        $array = [];
        foreach ($data as $key => $value) {

            $code = explode("/", $value->unique_code);

            $array[] = [
                'province_fa' => $value->province_fa,
                'city_fa' => $value->city_fa,
                'axis_name_fa' => $value->axis_name_fa,
                'axis_type_fa' => $value->axis_type_fa,
                'project_title' => $value->project_title,
                'operation_type_fa1' => $value->operation_type_fa1,
                'operation_type_fa2' => $value->operation_type_fa2,
                'operation_type_fa3' => $value->operation_type_fa3,
                'operation_type_fa4' => $value->operation_type_fa4,
                'contract_date_from_parent_contract' => verta($value->contract_date_from_parent_contract)->format('Y/n/j'), 
                'last_status_fa' => $value->last_status_fa,
                'progress_project' => $value->progress_project,
                'last_digit_project' => $value->last_digit_project,
                'last_function_operator' => $value->last_function_operator,
                'last_payable_operator' => $value->last_payable_operator,
                'complete_payable' => $value->complete_payable,
                'operator_name' => $value->operator_name,
                'priority_project' => $value->priority_project_fa,
                'followup_priority_project' => $value->followup_priority_project,
                'contract_peymankar' => $value->contracts->contract_peymankar,
                'contract_moshaver_tarahi' => $value->contracts->contract_moshaver_tarahi,
                'contract_moshaver_nezarat' => $value->contracts->contract_moshaver_nezarat,
                'operator_name' => $value->operator_name,
                'operator_phone' => $value->operator_phone,
                'updated_at_fa' => verta($value->last_date_update)->format('Y/n/j'),
                'code' => $value->unique_code,
            ];
        }
        return view('excel.Technical.TechnicalbuildingProjects', [
            '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(55);
        $drawing2->setOffsetY(5);
        $drawing2->setCoordinates('A1');
        return [$drawing, $drawing2];
    }

    public function styles(Worksheet $sheet)
    {

        return [
            // Style the first row as bold text.
            'A:Z' =>    [
                'font' => [
                    'name' => 'B Nazanin'
                ]
            ],
        ];

    }
}
