<?php

namespace App\Exports;


use App\Enums\ExcelDesignEnum;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromArray;

use Maatwebsite\Excel\Concerns\WithCustomStartCell;
use Maatwebsite\Excel\Concerns\WithEvents;

use Maatwebsite\Excel\Concerns\WithTitle;
use Maatwebsite\Excel\Events\AfterSheet;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;

class DoctorPhysicianExport implements FromArray, WithTitle, WithCustomStartCell,WithEvents
{
    use Exportable;
    protected array $data;
    protected array $headingTitle;
    protected string $sheet;
    protected string $title;

    public function __construct($data)
    {
        $this->data = $data;
    }

    public function array(): array
    {
        return $this->data;
    }

    public function title(): string
    {
        return 'Doctor-Report';
    }

    public function startCell(): string
    {
        return 'A3';
    }

    public function setTitleHeadingTable($data): array
    {
        return $this->headingTitle = $data;

    }

    public function setTitle($title){
        $this->title = $title;
    }

    public function registerEvents(): array
    {
        return [
            AfterSheet::class => function (AfterSheet $event) {

                $sheet = $event->sheet->getDelegate();
                $sheet->getParent()->getDefaultStyle()->getFont()->setName(ExcelDesignEnum::FONT_KH_OS_SIEMREAP);
                $sheet->getParent()->getDefaultStyle()->getFont()->setSize(ExcelDesignEnum::FONT_SIZE);

                $bgGrandTotal = [
                    'fill' => [
                        'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID,
                        'color' => [
                            'rgb' => 'd9d9d9',
                        ]
                    ],
                    'font' => [
                        'bold'      =>  true,
                    ]
                ];

                $alignmentCenter = [
                    'alignment' => [
                        'horizontal' => 'center'
                    ]
                ];

                $alignmentLeft = [
                    'alignment' => [
                        'horizontal' => 'left',
                    ]
                ];

                $borderAll = [
                    'borders' => [
                        'allBorders' => [
                            'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
                            'color' => ['rgb' => '000000'],
                        ],
                    ]
                ];

                $outBorder = [
                    'borders' => [
                        'outline' => [
                            'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
                            'color' => ['rgb' => '000000'],
                        ],
                    ]
                ];

                $textBold = [
                    'font' => [
                        'bold'      =>  true,
                        'size'      =>  11,
                    ],
                ];

                $title = $this->title;
                $columns = count($this->headingTitle);
                $rows = count($this->data) + 2;

                $lastColumnString = Coordinate::stringFromColumnIndex($columns);

                for($i = 1; $i<=$columns; $i++){
                    $nameCell = Coordinate::stringFromColumnIndex($i);
                    $sheet->getColumnDimension($nameCell)->setWidth(21);
                }

                $sheet->getStyle("A2:".$lastColumnString."2")->applyFromArray($alignmentCenter);
                $sheet->mergeCells("A1:$lastColumnString".'1');
                $sheet->setCellValue('A1',$title);

                foreach ($this->headingTitle as $key => $value){
                    $cellColumn = $key + 1;
                    $cellColumnName = Coordinate::stringFromColumnIndex($cellColumn);
                    $sheet->setCellValue($cellColumnName.'2', $value);
                }

                $sheet->getStyle("A1")->applyFromArray($alignmentCenter);
                $sheet->getRowDimension(1)->setRowHeight(28);
                $sheet->getStyle("A1")->applyFromArray($textBold);
                $sheet->getRowDimension(2)->setRowHeight(25);
                $sheet->getStyle("A2:".$lastColumnString."2")->applyFromArray($alignmentCenter);
                $sheet->getStyle("A2:".$lastColumnString."2")->applyFromArray($borderAll);
                $sheet->getStyle("A2:".$lastColumnString."2")->applyFromArray($bgGrandTotal);
                $sheet->getStyle("A3:A$rows")->applyFromArray($alignmentLeft);
                $sheet->getStyle("A3:".$lastColumnString.$rows)->applyFromArray($outBorder);

            }
        ];
    }
}
