<?php

namespace App\Exports;

use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;
use App\Models\Contracts;
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 ContractsExport implements FromView, ShouldAutoSize, WithEvents, WithDrawings, WithStyles
{
    public function __construct($fromDate = null, $toDate = null, $province = null, $status = null, $projectType = null, $statusRel = null)
    {
        $this->fromDate = $fromDate ?? null;
        $this->toDate = $toDate ?? null;
        $this->province = $province ?? null;
        $this->status = $status ?? null;
        $this->projectType = $projectType ?? null;
        $this->status_rel = $statusRel ?? null;
    }

    public function view(): View
    {
        $fromDate = $this->fromDate;
        $toDate = $this->toDate;
        $province = isset($this->province) ? explode(',', $this->province) : null;
        $status = isset($this->status) ? explode(',', $this->status) : null;
        $projectType = $this->projectType ? explode(',', $this->projectType) : null;
        $statusRel = $this->status_rel;

        $data = Contracts::when($fromDate, function ($query) use ($fromDate, $toDate) {
                                return $query->whereBetween('contract_date', [$fromDate, $toDate]);
                            })
                            ->when($province, function ($query) use ($province) {
                                return $query->whereIn('contracts.province_id', $province);
                            })
                            ->when($status, function ($query) use ($status) {
                                return $query->whereIn('contracts.status_id', $status);
                            })
                            ->when($statusRel, function ($query) use ($statusRel) {
                                return $query->where('contracts.status_rel', $statusRel);
                            })
                            ->with('subitems')
                            ->when($projectType, function ($query) use ($projectType) {
                                return $query->whereHas('subitems', function ($query) use ($projectType) {
                                    $query->whereIn('project_type_id', $projectType ?? []);
                                });
                            })
                            ->orderBy('province_id')
                            ->get();
        $array = [];
        foreach ($data as $key => $item) {
            $array[] = $item;
            $array[$key]['projects_count'] = count($item->subitems);
        }

        return view('excel.contract', [
            'roads' => $array,
            'fromFa' => $fromDate,
            'toFa' => $toDate
        ]);
    }

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

    }
}
