<?php

namespace Modules\Mall\Http\Exporter;

use Encore\Admin\Grid\Exporters\ExcelExporter;
use Maatwebsite\Excel\Facades\Excel;

class OrderExporter extends ExcelExporter
{

    protected $fileName = '订单.xlsx';

    protected $headings = [
        '订单编号', '所属店铺', '下单用户', '商品金额', '运费', '发货数量', '订单金额', '订单状态', '订单类型',
        '支付时间', '下单时间'
    ];

    public function export()
    {
        $rows   = [];
        $rows[] = $this->headings;

        $data = $this->getData();
        if (count($data)) {
            foreach ($data as $v) {
                $rows[] = [
                    $v['order_no'],
                    $v['shop']['name'],
                    $v['user']['username'],
                    $v['amount'],
                    $v['freight'],
                    $v['items'][0]['qty'],
                    $v['total'],
                    $v['state'],
                    $v['type'],
                    $v['paid_at'],
                    $v['created_at'],
                ];
            }
        }

        // 数据格式化
        $export = new OrderFromArray($rows);

        // 导出
        return Excel::download($export, $this->fileName)->send();
    }

}