<?php

namespace App\Exports;

use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
use Maatwebsite\Excel\Concerns\WithHeadings;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;

class EarningGradeLogsExport implements FromCollection, WithHeadings, ShouldAutoSize, WithColumnFormatting
{
    private $data;
    public function __construct($data)
    {
        $this->data = $data;
    }

    public function headings(): array
    {
        return [
            "姓名",
            "手机号",
            "订单号",
            "商品名",
            "规格",
            "价格",
            '退款',
            "分享人姓名",
            "分享人手机号",
            "分享人收益",
            "友福收益",
            "微信手续费",
        ];
    }

    //设置列格式
    public function columnFormats(): array
    {
        return [
            'A' => NumberFormat::FORMAT_TEXT,
            'C' => NumberFormat::FORMAT_TEXT,
            "F" => NumberFormat::FORMAT_NUMBER_00,
            "G" => NumberFormat::FORMAT_NUMBER_00,
            'I' => NumberFormat::FORMAT_TEXT,
            "J" => NumberFormat::FORMAT_NUMBER_00,
            "K" => NumberFormat::FORMAT_NUMBER_00,
            "L" => NumberFormat::FORMAT_NUMBER_00,
        ];
    }

    public function collection()
    {
        $arr = [];
        foreach ($this->data as $log)
        {
            $arr[] = ['name'=>$log['name'], 'mobile'=>$log['mobile'],'trade_no'=>(string)$log['trade_no'],
                'goods'=>$log['goods'], 'sku'=>$log['sku'], 'price'=>$log['price'], 'refund_fee'=>$log['refund_fee'],
                'sharer_name'=>$log['earning']['sharer']['name'], 'sharer_mobile'=>$log['earning']['sharer']['mobile'], 'sharer_amount'=>$log['earning']['sharer']['amount'],
                "ufutx_amount"=>$log['earning']['ufutx']['amount'], 'weixin_amount'=>$log['earning']['weixin']['amount']];
        }
        return collect($arr);
    }
}
