<?php

namespace App\Exports;

use App\Models\Activity;
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 OrderStepThreeInfoExport implements FromArray,WithHeadings,WithStyles,WithEvents
{
    public function __construct($data)
    {
        $this->data = $data;
    }

    public function array():array
    {
        $items = $knows = $stomachs = $hearts = $sleeps = $womans = $mans = $others = $dietarys = $livings = $mentals = $familys = $family_members = $understands = $reallys = $dutys = $personals = [];
        foreach (json_decode($this->data['user_info']->stomach,true) as $item) {
            $stomachs[] = $item['name'];
        }
        $stomach = implode(',', $stomachs);

        foreach (json_decode($this->data['user_info']->heart,true) as $item) {
            $hearts[] = $item['name'];
        }
        $heart = implode(',', $hearts);

        foreach (json_decode($this->data['user_info']->sleep,true) as $item) {
            $sleeps[] = $item['name'];
        }
        $sleep = implode(',', $sleeps);

        foreach (json_decode($this->data['user_info']->woman,true) as $item) {
            $womans[] = $item['name'];
        }
        $woman = implode(',', $womans);

        foreach (json_decode($this->data['user_info']->man,true) as $item) {
            $mans[] = $item['name'];
        }
        $man = implode(',', $mans);

        foreach (json_decode($this->data['user_info']->other,true) as $item) {
            $others[] = $item['name'];
        }
        $other = implode(',', $others);

        foreach (json_decode($this->data['user_info']->dietary,true) as $item) {
            $dietarys[] = $item['name'];
        }
        $dietary = implode(',', $dietarys);

        foreach (json_decode($this->data['user_info']->living,true) as $item) {
            $livings[] = $item['name'];
        }
        $living = implode(',', $livings);

        foreach (json_decode($this->data['user_info']->mental,true) as $item) {
            $mentals[] = $item['name'];
        }
        $mental = implode(',', $mentals);

        foreach (json_decode($this->data['user_info']->family,true) as $item) {
            $familys[] = $item['name'];
        }
        $family = implode(',', $familys);

        foreach (json_decode($this->data['user_info']->family_member,true) as $item) {
            $family_members[] = $item['name'];
        }
        $family_member = implode(',', $family_members);

        foreach (json_decode($this->data['user_info']->know??[],true) as $item) {
            $knows[] = $item['name'];
        }
        $know = implode(',', $knows);

        foreach (json_decode($this->data['user_info']->understand??[],true) as $item) {
            $understands[] = $item['name'];
        }
        $understand = implode(',', $understands);

        foreach (json_decode($this->data['user_info']->really??[],true) as $item) {
            $reallys[] = $item['name'];
        }
        $really = implode(',', $reallys);

        foreach (json_decode($this->data['user_info']->duty??[],true) as $item) {
            $dutys[] = $item['name'];
        }
        $duty = implode(',', $dutys);

        foreach (json_decode($this->data['user_info']->personal??[],true) as $item) {
            $personals[] = $item['name'];
        }
        $personal = implode(',', $personals);

        $items[] = [
            $stomach,
            $heart,
            $sleep,
            $woman,
            $man,
            $other,
            $dietary,
            $living,
            $mental,
            $family,
            $family_member,
            $know,
            $understand,
            $really,
            $duty,
            $personal,
            $this->data['user_info']->desc
        ];

        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();
            },
        ];
    }
}
