<?php

namespace App\Exports;

use App\Models\Partner;
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 ServiceUserExport implements FromArray,WithHeadings,WithStyles,WithEvents
{
    public function __construct($data)
    {
        $this->keyword = $data['keyword']??null;
        $this->status = $data['status']??null;
        $this->serviceUser = $data['serviceUser']??[];
    }

    public function array():array
    {
        $items = [];
        $keyword = $this->keyword;
        $status = $this->status;
        $list = $this->serviceUser->with('partner:id,name,mobile,birthday,sex,pic,weight', 'service_user')
            ->when($keyword,function ($query) use ($keyword){
                $query->whereHas('partner',function ($query) use ($keyword){
                    $query->where(function ($query)use ($keyword){
                        $query->where("name", 'like', '%'.$keyword.'%')
                            ->orWhere('mobile', 'like', '%'.$keyword.'%');
                    });
                });
            })
            ->when($status !== null,function ($query)use($status){
                $query->where('status',$status);
            })
            ->orderByDesc('id')
            ->get();

        foreach ($list as $value){
            $partner = $value->partner;
            $serviceUser = $value->service_user;
            if(empty($partner) || empty($serviceUser)){
                continue;
            }
            $remark = '';
            if($serviceUser->work_user_id??""){
                $remark = '已绑定'.$serviceUser->work_user_id;
            }else{
                $remark = '未绑定企业微信';
            }
            $sex = '男';
            if($partner->sex == 2){
                $sex = '女';
            }
            $items[] = [
                $value->id,
                $partner->name,
                $partner->mobile,
                $partner->birthday,
                $partner->weight.'kg',
                $sex,
                $value->customer_num,
                $remark,
                $value->reason,
                $value->think,
                $value->outlook,
                $value->idea,
                $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();
            },
        ];
    }
}
