<?php

namespace App\Exports;

use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use App\Models\AgentUser;
class TestOrderUsersExport implements FromArray,WithHeadings
{
    protected $orders;
    public function __construct($orders)
    {
        $this->orders = $orders;
    }

    /**
    * @return \Illuminate\Support\Collection
    */
    public function array():array
    {
        $orders = $this->orders;
        $rows = [];
        foreach ($orders as $order)
        {   
            $sign_activity_logs = collect($order->signActivities)->pluck("title")->toArray();
            $sign_regular_activities = collect($order->signRegularActivities)->pluck("title")->toArray();
            $wine_orders_num = $order->wineOrders()->sum('num');
            $band_orders_num = $order->bandOrders()->sum('num');
            $rows[] = [
                $order->id,
                $order->user_id,
                $order->name,
                $order->mobile,
                $order->recommendUser?$order->recommendUser->name:"",
                $order->recommendUser?$order->recommendUser->mobile:"",
                $order->sign_activities_count,
                json_encode($sign_activity_logs, JSON_UNESCAPED_UNICODE),
                $order->sign_regular_activities_count,
                json_encode($sign_regular_activities, JSON_UNESCAPED_UNICODE),
                // $order->wine_orders_count,
                // $order->band_orders_count,
                $wine_orders_num,
                $band_orders_num,
                $order->win_lotteries_count,
                AgentUser::agentName($order->agentUser?$order->agentUser->level:0),
                $order->sign_amount,
                $order->refund_amount,
                $order->residue_amount,
            ];
        }
        return $rows;
    }

    public function headings(): array
    {
        return [
            ["订单id","用户id",'姓名','手机号', "分享人-姓名", "分享人-手机号", '大会次数', "大会", "小会次数", "小会", "酒订单数", "手环订单数", "中奖次", "等级", "签到扣费", "退款扣费", "余额"]
        ];
    }
}
