<?php

namespace App\Exports;

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

    public function array():array
    {
        $items = [];
        foreach ($this->data as $value){
            $is_sign = "未签到";
            if($value->is_sign){
                $is_sign = "已签到";
            }
//            $items[] = [
//                $value->activity->title??'',
//                $value->name,
//                $value->mobile,
//                $value->amount,
//                $value->residue_amount,
//                $value->sku??'',
//                $value->recommendUser->name??'',
//                $value->recommendUser->mobile??'',
//                $value->trade_no,
//                $is_sign,
//                $value->created_at,
//            ];
            $level = $value->agent_level;
            $name = AgentUser::agentName($level);
            $items[] = [
                $value->id,
                $value->name,
                $value->mobile,
                $value->sex == 1?"男":"女",
                $name,
                $value->recommendUser?$value->recommendUser->name:'',
                $value->recommendUser?$value->recommendUser->mobile:'',
                $value->activity?$value->activity->title:'',
                $value->unusualOrder?$value->unusualOrder->name:'',
                $value->unusualOrder?$value->unusualOrder->mobile:'',
                $value->amount,
                $value->residue_amount,
                $value->is_apply_food?"已报餐":"未报餐",
                $value->trade_no,
                $value->created_at->toDateTimeString(),
                $value->remark,
            ];
        }

        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);
        $sheet->getStyle('G')->getNumberFormat()->setFormatCode(NumberFormat::FORMAT_TEXT);
        $sheet->getStyle('J')->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();
            },
        ];
    }
}
