<?php

namespace App\Exports;

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

    public function array():array
    {
        $items = [];
        foreach ($this->data as $value){
            $office_name = Office::where('id',$value->office_id)->value('office_name');
            $chef_user = implode(',',$value->chef_user);

            switch ($value->food_type){
                case Restaurant::FOOD_TYPE_TRIM:
                    $food_type = '装修日餐';
                    break;
                default:
                    $food_type = '清洁日餐';
                    break;
            }

            switch ($value->diet_type){
                case Restaurant::DIET_TYPE_LUNCH:
                    $diet_type = '午餐';
                    break;
                default:
                    $diet_type = '晚餐';
                    break;
            }

            switch ($value->status){
                case Restaurant::STATUS_PAY:
                    $status = '等待出餐';
                    break;
                case Restaurant::STATUS_WAIT:
                    $status = '等待出餐';
                    break;
                case Restaurant::STATUS_FINISHED:
                    $status = '等待评价';
                    break;
                case Restaurant::STATUS_EVALUATE:
                    $status = '已评价';
                    break;
                case Restaurant::STATUS_CANCEL:
                    $status = '已取消';
                    break;
                default:
                    $status = '';
                    break;
            }

            $items[] = [
                $value->user_id,
                $value->name,
                $value->mobile,
                $chef_user,
                $value->star,
                $value->remark,
                $value->updated_at,
            ];
        }

        return $items;
    }


    public function headings(): array
    {
        return [
            ['用户id','姓名', '手机号', '厨师','分数','建议','评价时间']
        ];
    }

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