<?php

namespace App\Exports;

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

    public function array(): array
    {
        $items = [];
        foreach ($this->data as $value) {
            switch ($value->material_type) {
                case Agreement::MATERIAL_TYPE_ELECTRON:
                    $material_type = '电子版';
                    break;
                default:
                    $material_type = '纸质';
                    break;
            }
            switch ($value->is_notice) {
                case Agreement::IS_NOTICE_YES:
                    $is_notice = '已签发';
                    break;
                default:
                    $is_notice = '未签发';
                    break;
            }

            switch ($value->is_receive) {
                case Agreement::IS_RECEIVE_YES:
                    $is_receive = '已领取';
                    break;
                default:
                    $is_receive = '未领取';
                    break;
            }
            $items[] = [
                $value->id,
                $value->orderInfo->trade_no,
                $value->number,
                $value->sign_date,
                $value->signInfo->name ?? '',
                $material_type,
                $is_notice,
                $value->introduceInfo->name ?? '',
                implode(',',$value->introduce_rights),
                $is_receive,
                $value->quota_id,
                $value->created_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();
            },
        ];
    }
}
