<?php

namespace App\Domains\Virtual\Exports;

use Carbon\Carbon;
use App\Core\AbstractExport;
use Dipper\Excel\Concerns\WithRows;
use Dipper\Excel\Concerns\FromQuery;
use Dipper\Excel\Concerns\WithHeadings;
use App\Domains\Virtual\Services\CommonService;
use Dipper\Excel\Concerns\WithColumnFormatting;
use App\Domains\Virtual\Services\CompanyService;
use App\Domains\Virtual\Services\PackageService;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use App\Domains\Virtual\Repositories\OrderCardPartitionRepository;
use App\Dicts;

class OrderCardExport extends AbstractExport implements FromQuery, WithHeadings, WithRows, WithColumnFormatting
{
    public $conditions;

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

    public function query()
    {
        $builder = app(OrderCardPartitionRepository::class)->with('order')->forceNoReset()->select(['sim', 'order_id', 'counts', 'refunded_at'])
            ->withConditions($this->conditions)->orderBy('order_id')->orderBy('sim');

        return $builder;
    }

    public function headings(): array
    {
        $headings = [
            '订单编号',
            '企业名称',
            '运营商',
            '套餐名称',
            '套餐单价',
            '支付方式',
            '支付流水号',
            '订单时间',
            'SIM',
            '数量',
            '退货',
        ];

        if ($this->conditions['type'] == 0) {
            array_splice($headings, 6, 0, [
                '订单状态',
                // '收款状态',
            ]);
        }

        return $headings;
    }

    /**
     * @param mixed $row
     *
     * @return mixed
     */
    public function rows($rows)
    {
        $array = [];

        $carrierOperators = app(Dicts::class)->get('carrier_operator');
        $orderStatues = app(Dicts::class)->get('order_status');
        $transactionStatuses = app(Dicts::class)->get('transaction_status');

        foreach ($rows as $item) {
            $carrier_operator = PackageService::load($item['order']['package_id'])['carrier_operator'];

            $data = [
                "{$item['order']['sn']}\t",
                CompanyService::load($item['order']['company_id'])['name'] ?? '',
                $carrierOperators[$carrier_operator],
                PackageService::load($item['order']['package_id'])['name'] ?? '',
                sprintf('%.02f', $item['order']['unit_price']/100),
                CommonService::namePayChannel($item['order']['pay_channel']),
                "{$item['order']['transaction_no']}\t",
                Carbon::parse($item['order']['order_at'])->format('Y-m-d'),
                "{$item['sim']}\t",
                $item['counts'],
                $item['refunded_at'] ? '是' : '',
            ];

            if ($this->conditions['type'] == 0) {
                array_splice($data, 6, 0, [
                    $orderStatues[$item['order']['order_status']],
                    // $transactionStatuses[$item['order']['transaction_status']],
                ]);
            }

            $array[] =  $data;
        }

        return $array;
    }

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