<?php

namespace App\Exports;

use App\Models\Office;
use App\Models\Partner;
use App\Models\Restaurant;
use App\Models\User;
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 RestaurantExport 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');

            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->type){
                case Restaurant::TYPE_SELF:
                    $type = '用户';
                    break;
                default:
                    $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->date,
                $value->name,
                $value->mobile,
                $office_name,
                $diet_type,
                $food_type,
                $value->num,
                $value->amount,
                $type,
                $value->created_at,
                $status
            ];
        }

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