<?php

namespace App\Domains\Virtual\Exports;

use App\Core\AbstractExport;
use Dipper\Excel\Concerns\WithHeadings;
use Dipper\Excel\Concerns\FromCollection;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use App\Domains\Virtual\Services\FlowPoolService;

class FlowPoolExport extends AbstractExport implements FromCollection, WithHeadings
{
    public $conditions;

    public function __construct(array $conditions = [])
    {
        $this->conditions = $conditions;
        parent::__construct();
    }

    public function collection()
    {
        $this->conditions['limit'] = 0;

        $list = app(FlowPoolService::class)->index($this->conditions)->map(function ($item) {
            return collect([
                'id' => $item->id,
                'name' => $item->name,
                'carrier_operator_name' => $item->carrier_operator_name,
                'company_name' => $item->company_name,
                'minimum_flows' => $item->minimum_flows,
                'excess_flows' => $item->excess_flows,
                'minimum_price' => $item->minimum_price,
                'excess_price' => $item->excess_price,
                'members' => $item->members,
                'total_price' => $item->total_price,
            ]);
        });

        $list->push([
            '总计',
            '',
            '',
            '',
            '',
            '',
            '',
            '',
            array_sum($list->pluck('members')->toArray()) ?: 0,
            array_sum($list->pluck('total_price')->toArray()) ?: 0,
        ]);

        return $list;
    }

    public function headings(): array
    {
        return [
            'ID',
            '名称',
            '运营商',
            '客户名称',
            '保底流量',
            '超出流量',
            '保底收入(元)',
            '超出收入(元)',
            '收费用户数',
            '总收入(元)',
        ];
    }

    /**
     * @return array
     */
    public function columnFormats(): array
    {
        return [
            'G' => NumberFormat::FORMAT_NUMBER_00,
            'H' => NumberFormat::FORMAT_NUMBER_00,
            'J' => NumberFormat::FORMAT_NUMBER_00,
        ];
    }
}
