<?php

namespace App\Exports;

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

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


    public function headings(): array
    {
        return [
            ['用户id','姓名', '手机号', '推荐人姓名', '推荐人手机号', "类型", '购买活动订单', '推荐票订单', '推荐票余额', '购买商品数']
        ];
    }

    public function array():array
    {
        $rows =[];
        $users = $this->data;
        foreach ($users as $user)
        {
            $recommend_user = $user->recommendUser;
            $level = AgentUser::agentName($user['level']);
            $rows[] = ['id'=>$user['user_id'], 'name'=>$user['user']['name'], 'mobile'=>$user['user']['mobile'], 'recommend_name'=>$recommend_user?$recommend_user->name:'',
                'recommend_mobile'=>$recommend_user?$recommend_user->mobile:'', "level"=>$level,'activity_count'=>$user->activity_count?:0, 'introduce_activity_count'=>$user->introduce_activity_count?:0, 'residue_amount'=>$user->residue_amount?:0, 'order_count'=>$user->order_count?:0];
        }
        return $rows;
    }
}
