<?php

namespace App\Exports;

use App\Models\Activity;
use App\Models\MedicalReport;
use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithStyles;
use Maatwebsite\Excel\Events\AfterSheet;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;

class OrderMedicalReportExport implements FromArray,WithHeadings,WithStyles,WithEvents
{
    public function __construct($data)
    {
        $this->data = $data;
    }

    public function array():array
    {
        $items = [];
        $xueya = "未选择";
        $tang = "未选择";
        $shen = "未选择";
        $xuezhi = "未选择";
        $xuechanggui = "未选择";
        $ganggong = "未选择";
        $MedicalReport = new MedicalReport();
        $anomaly = $MedicalReport->anomalyTranfer($this->data['user_info']->anomaly);
        if(is_array($anomaly)){
            $xueya = implode(',',$anomaly[0]??[]);
            $tang = implode(',',$anomaly[1]??[]);
            $shen = implode(',',$anomaly[2]??[]);
            $xuezhi = implode(',',$anomaly[3]??[]);
            $xuechanggui = implode(',',$anomaly[4]??[]);
            $ganggong = implode(',',$anomaly[5]??[]);
        }
        $items[] = [
            $xueya,
            $tang,
            $shen,
            $xuezhi,
            $xuechanggui,
            $ganggong
        ];

        return $items;
    }


    public function headings(): array
    {
        return [
            ['血压','糖检测', '肾功能检测', '血脂四项检查','五分类血常规','肝功三项']
        ];
    }

    public function styles(Worksheet $sheet)
    {
        $sheet->getStyle('A')->getNumberFormat()->setFormatCode(NumberFormat::FORMAT_TEXT);
        $sheet->getStyle('C')->getNumberFormat()->setFormatCode(NumberFormat::FORMAT_TEXT);

        return [
            1 => [
                'alignment' => [
                    'horizontal' => Alignment::HORIZONTAL_CENTER,
                ],
                'font' => [
                    'bold' => true,
                ],
                'borders' => [
                    'allBorders' => [
                        'borderStyle' => Border::BORDER_THIN,
                    ],
                ],
            ],
        ];
    }

    public function registerEvents(): array
    {
        return [
            AfterSheet::class => function (AfterSheet $event) {
                $event->sheet->getDelegate()->calculateColumnWidths();
            },
        ];
    }
}
