<?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 EarningGradeUsersExport 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_TEXT,
        ];
    }

    public function collection()
    {
        $arr = [];
        foreach ($this->data as $log)
        {
            $arr[] = ['name'=>$log['nickname'], 'mobile'=>$log['mobile'],'created_at'=>$log['created_at']];
        }
        return collect($arr);
    }
}
