<?php

namespace App\Exports;

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

class ThreadsExport implements FromCollection,WithHeadings
{
    protected  $data;

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

    /**
    * @return \Illuminate\Support\Collection
    */
    public function collection()
    {
        $collection = [];
        foreach ($this->data as $item){
            $contact = $item->contact?$item->contact->phone:'暂无联系人信息';
            $ower_name = $item->ower->real_name;
            $companyName = $item->company->companyName;
            $companyWebsite = $item->company->website;
            $regCapital = $item->company->regCapital;
            $lastest = $item->latestProgress();
            $created_at = $item->created_at;
            $collection[] = [
                $contact,$ower_name, $companyName,$companyWebsite,$regCapital,$lastest,$created_at
            ];
        }

        return collect($collection);
    }

    public function headings(): array
    {
        return [
            '联系方式',
            '线索拥有者',
            '企业名字',
            '企业网站',
            '注册资本(万)',
            '最新跟进',
            '领取时间',
        ];
    }
}
