<?php

namespace App\Exports;

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

    public function array():array
    {
        $items = [];
        foreach ($this->data as $value){
            switch ($value->diet_type){
                case Restaurant::DIET_TYPE_LUNCH:
                    $diet_type = '午餐';
                    break;
                default:
                    $diet_type = '晚餐';
                    break;
            }
            switch ($value->type){
                case ChefPlan::CHEF_MAIN:
                    $chef_type = '主厨';
                    break;
                case ChefPlan::CHEF_SOUS:
                    $chef_type = '副厨';
                    break;
                case ChefPlan::CHEF_HELP:
                    $chef_type = '帮厨';
                    break;
                default:
                    $chef_type = '帮厨';
                    break;
            }

            $items[] = [
                $value->name,
                $value->mobile,
                $value->date,
                $chef_type,
                $diet_type,
                $value->created_at,
            ];
        }

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