<?php

namespace App\Exports;

use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;

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


    public function headings(): array
    {
        return [
            ['id','商品图片','商品名称', '规格名称/库存', "客户价/准批发商价/批发商价格/员工价", '邮费', "生成抽奖码", "上架状态","平台费"]
        ];
    }

    public function array():array
    {
        $rows =[];
        $shops = $this->data;
        foreach ($shops as $shop)
        {
            $sku_name ='';
            $freight = '';
            $price = '';
            foreach ($shop['sku'] as $sku) {
                if (!collect($shop['shop_agent']['sku'])->firstWhere('sku_id', $sku['sku_id'])) continue;
                $sku_name .= $sku['name'] . "/" . $sku["stock"] . ' | ';
                if (isset($sku['ship_num'])) {
                    $ready_agent_price = $sku['ready_agent_price']??"无";
                    $freight .= "前{$sku['ship_num']}件 {$sku['ship_price']}元，每增加一件{$sku['ship_each_price']}元，最高{$sku['ship_max_price']}元 | ";
                    $price .= "{$sku["price"]}/{$ready_agent_price}/{$sku['agent_price']}/{$sku['staff_price']} | ";
                }
            }
            $rows[] = ['id'=>$shop['id'], 'name'=>$shop['icon'], 'title'=>$shop['title'], 'sku_name'=>$sku_name, "price"=>$price,'freight'=>$freight, "lotto_code"=>$shop['shop_agent']['lotto_code']?"生成":"不生成",'is_show'=>$shop['shop_agent']['is_show']?"上架":"下架", "ratio"=>$shop['shop_agent']['ratio']];
        }
        return $rows;
    }
}
