<?php

namespace App\Exports;

use App\Models\Order;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;

class OfflineOrderExportV2 implements FromCollection,WithHeadings
{

    protected $orders;
    public function __construct($orders)
    {
        $this->orders = $orders;
    }

    /**
    * @return \Illuminate\Support\Collection
    */
    public function collection()
    {
        $items =[];
        $orders = $this->orders;
        foreach ($orders as $order)
        {

            $status = "未知";
            $role1 = $role2 = $role3 = [];
            if ($order->main_order) {
                switch ($order->main_order->status) {
                    case Order::NOTSTART_STATUS:
                        $status = "未开始";
                        break;
                    case Order::STARTING_STATUS:
                        $status = "进行中";
                        break;
                    case Order::FINISHED_STATUS:
                        $status = "已完成";
                        break;
                    case Order::SUSPEND_STATUS:
                        $status = "暂停中";
                        break;
                    default:
                        $status = "未知";

                }
                $service_role = $order->main_order->service_role;
                foreach ($service_role as $role) {

                    if ($role->role_id == 1) {
                        $role1[] = $role->serviceUser->name??'';
                    }
                    if ($role->role_id == 2) {
                        $role2[] = $role->serviceUser->name??'';
                    }
                    if ($role->role_id == 3) {
                        $role3[] = $role->serviceUser->name??'';
                    }
                }

            }


            $city = $province = '';
            if ($order->agreement && $order->agreement->quotaInfo && $order->agreement->quotaInfo->agencyInfo) {
                $city = $order->agreement->quotaInfo->agencyInfo->city;
                $province = $order->agreement->quotaInfo->agencyInfo->province;
            }
            $collaborator_name = '';

            if ($order->agreement && $order->agreement->quotaInfo && $order->agreement->quotaInfo->collaboratorInfo) {
                $collaborator_name = $order->agreement->quotaInfo->collaboratorInfo->name;
            }
            $city = $city?:$province;
            $agency = $city.'-'.$collaborator_name;

            $items[] = [
                $order->id,
                $order->name,
                $order->mobile,
                $order->price,
                "虚拟订单",
                $status,
                $agency,
                implode(',', $role1),
                implode(',', $role2),
                implode(',', $role3),
                $order->contract_no,
                $order->created_at->toDateTimeString(),
            ];
        }
        return collect($items);
    }

    public function headings(): array
    {
        return [
            ["订单id", "联系人姓名", "联系人手机号","订单价格", "订单类型", "服务状态", '子公司', "主教练",'副教练', '客服',"合同号", "操作时间"]
        ];
    }
}
