<?php

namespace App\Exports;

use App\Models\AgentUser;
use Maatwebsite\Excel\Concerns\FromCollection;

class RegularActivityMemberExport implements FromCollection
{

    protected $members;
    public function __construct($members)
    {
        $this->members = $members;
    }

    /**
    * @return \Illuminate\Support\Collection
    */
    public function collection()
    {
        $rows = [];
        $rows[] = ['名称', '手机号', '批发商资格', '签到时间'];
        $members = $this->members;
        foreach ($members as $member) {
            $level = $member->agentUser->level??0;
            $name = AgentUser::agentName($level);

            $res = aesDecryptCBC($member->user->mobile);
            if (empty($res)) {
                $mobile = $member->user->mobile;
            }else {
                $mobile = $res;
            }
            $rows[] = [$member->user->name, $mobile, $name, $member->updated_at->toDateTimeString()];
        }
        return collect($rows);
    }

    public function headings(): array
    {
        return [
            ['名称', '手机号', '批发商资格', '签到时间']
        ];
    }
}
