<?php

namespace App\Exports;

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

class OrdersExportV2 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)
        {
            switch ($order->pay_status) {
                case Order::UNPAID_STATUS:
                    $pay_status = "未支付";
                    break;
                case Order::PAID_STATUS:
                    $pay_status = "已支付";
                    break;
                default:
                    $pay_status = "未知";
            }
            switch ($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->service_role;
            $role1 = $role2 = $role3 = [];
            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->recommendUser->name??"",
                $order->recommendUser->mobile??"",
                $order->price,
                $pay_status,
                $order->offline_order?'虚拟订单':'线上订单',
                $status,
                $order->is_show?'展示':'不展示',
                $agency,
                implode(',', $role1),
                implode(',', $role2),
                implode(',', $role3),
                $order->trade_no,
                $order->desc,
                $order->created_at->toDateTimeString(),
            ];
        }
        return collect($items);
    }

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