<?php

namespace App\Exports;

use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithStyles;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;

class AgentOrdersExport implements FromArray,WithHeadings
{
    protected $data;
    public function __construct($data)
    {
        $this->data = $data;
    }


    public function headings(): array
    {
        return [
            ['商品ID','商品图片','商品名称','商品规格','购买数量', '支付金额',"姓名", "手机号", "订单号", '子商户号','推荐人','推荐人手机号',"线上/线下",'下单时间']
        ];
    }

    public function array():array
    {
        $rows =[];
        $orders = $this->data;

        foreach ($orders as $order)
        {
            $name = $order->name?:($order->user?$order->user->name:'');
            $trade_no = $order->trade_no??'';
            $trade_no = ' '.$trade_no;
            $mobile = $order->mobile?:($order->user?$order->user->mobile:'');
            $recommend_user = $order->recommendUser;
            $recommend_user_name = $recommend_user? $recommend_user['name']:'';
            $recommend_user_mobile = $recommend_user? $recommend_user['mobile']:'';
            $type = $order['is_online']?"线上":"线下";
            $title = $order['shop_info']['title']??'';
            $pic = $order['shop_info']['pic']??'';
            $sku_name = $order['shop_info']['sku']['name']??"";
            $rows[] = ['id'=>$order["id"], 'pic'=>$pic,'title'=>$title,'sku_name'=>$sku_name,'num'=>$order['num'],'price'=>$order['price'], "name"=>$name,"mobile"=>$mobile,'trade_no'=>$trade_no,'sub_mch_id'=>$order->sub_mch_id??'','recommend_user_name'=>$recommend_user_name, "recommend_user_mobile"=>$recommend_user_mobile,'type'=>$type,'time'=>$order['created_at']];
        }
        return $rows;
    }

}
