<?php

namespace App\Exports;

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 ReappraiseExport implements FromArray, WithHeadings, WithStyles, WithEvents
{
    public function __construct($data)
    {
        $this->data = $data;
    }

    public function array(): array
    {
        $items = [];
        foreach ($this->data as $value) {
            $coordinate = ($value['content']['coordinate'] ?? 0) * 2;
            $firstAnswer = ($value['content']['firstAnswer'] ?? 0) * 2;
            $secondAnswer = ($value['content']['secondAnswer'] ?? 0) * 2;
            $thirdAnswer = ($value['content']['thirdAnswer'] ?? 0) * 2;
            $fourthAnswer = ($value['content']['fourthAnswer'] ?? 0) * 2;
            if ($value['role_id'] == 1) {
                $content = "1.用户方案配合度 " . $coordinate . "分 \n" .
                    "2.积极主动带领服务团队帮助客户顺利执行方案 " . $firstAnswer . "分 \n" .
                    "3.认真指导副教练学习并积极主动带领副教练完成服务工作 " . $secondAnswer . "分 \n" .
                    "4.认真审核餐单，及时解决服务过程中的突发疑难问题 " . $thirdAnswer . "分 \n" .
                    "5.着装整洁，体态标准，仪容端庄，言谈得体 " . $fourthAnswer . "分 \n";
            } elseif ($value['role_id'] == 2) {
                $content = "1.用户方案配合度 " . $coordinate . "分 \n" .
                    "2.明确工作岗位职责，熟练服务流程，每天按时发餐单，正确指导客户使用餐单 " . $firstAnswer . "分 \n" .
                    "3.积极主动与主教练、客服沟通、商讨客户遇到的问题并及时反馈，并遵行主教练指导 " . $secondAnswer . "分 \n" .
                    "4.每天主动关心客户，生活化服务，关注数据变化并及时沟通，及时回复客户信息 " . $thirdAnswer . "分 \n" .
                    "5.着装整洁，体态标准，仪容端庄，言谈得体 " . $fourthAnswer . "分 \n";
            } elseif ($value['role_id'] == 3) {
                $content = "1.用户方案配合度 " . $coordinate . "分 \n" .
                    "2.及时收集客户资料，跟进客户合同签订、方案设计进程，按时发放餐单 " . $firstAnswer . "分 \n" .
                    "3.主动协助主教指导副教练服务工作 " . $secondAnswer . "分 \n" .
                    "4.积极主动提醒或者辅助回复服务群客户的信息问答 " . $thirdAnswer . "分 \n" .
                    "5.着装整洁，体态标准，仪容端庄，言谈得体 " . $fourthAnswer . "分 \n";
            }
            $score = /*$coordinate+*/ $firstAnswer + $secondAnswer + $thirdAnswer + $fourthAnswer;
            $items[] = [
                $value['operate_service_user']['name'] ?? '未填写',
                $value['operate_service_user']['mobile'] ?? '未填写',
                $value['service_role_order']['service_user']['name'] ?? '未填写',
                $value['service_role_order']['service_user']['mobile'] ?? '未填写',
                $value['service_role_order']['role']['name'] ?? '未填写',
                $content,
                $value['content']['selectValue'] ?? '无复评',
                $value['content']['recommendReason'] ?? '无复评',
                $value['content']['advantage'] ?? '无复评',
                $value['content']['suggest'] ?? '无复评',
                $score,
                $value['updated_at'],
            ];
        }
        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();
            },
        ];
    }
}
