<?php

namespace App\Exports;

use App\Models\DailyReport;
use App\Models\Shop;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Illuminate\Support\Collection;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;

class BusinessOrderExport implements FromCollection, ShouldAutoSize
{
    private $data;

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

    /**
     * @return \Illuminate\Support\Collection
     */
    public function collection()
    {
        return new Collection($this->data);
    }


    private function createData($orders)
    {
        $headTitle = [
            '订单id/编号',
            '商品信息|个数',
            '店铺名称',
            '跟团号',
            '收件人',
            '收件地址',
            '下单时间',
            '发货状态',
            '订单状态',
            '售后状态',
        ];
        $bodyData = [];
        foreach ($orders as $order) {
            $productInfo = "";
            foreach ($order['items'] as $item) {
                $productInfo .= $item['goods_name']. "|" . $item['goods_number'] . ",";
            }
            rtrim($productInfo, ",");
            $bodyData[] = [
                $order['id'] . "/" . $order['order_sn'],
                $productInfo,
                $order['shop']['name'],
                $order['is_supplier'].":".$order['participate_no'],
                $order['receiver_name'],
                $order['receiver_address_province']." ". $order['receiver_address_city']." ".$order['receiver_address_district']." ".$order['receiver_address_detail'],
                $order['confirm_at'],
                $order['shipping_status'],
                $order['cancel_status'],
                $order['after_sales_status']
            ];
        }


        return [$headTitle, $bodyData];
    }
}
