<?php
namespace backendApi\modules\v1\models\lists\shop;

use common\helpers\Cache;
use common\helpers\Tool;
use common\libs\dataList\DataListInterface;
use common\models\OrderDec;
use common\models\User;
use common\libs\dataList\column\DateTime;

class OrderDecList extends \common\libs\dataList\DataList implements DataListInterface
{
    /**
     * 列表名称
     * @return string
     */
    public function getListName(){
        return '外部商城报单列表';
    }

    /**
     * 列表筛选到的数据
     */
    public function dataHandle()
    {
        $this->listData = OrderDec::lists($this->condition, $this->params, [
            'select' => 'O.*,U.REAL_NAME,U.DEC_ID',
            'orderBy' => 'O.PERIOD_NUM DESC, O.ID DESC',
            'from' => OrderDec::tableName() . ' AS O',
            'join' => [
                ['LEFT JOIN', User::tableName() . ' AS U', 'U.ID=O.USER_ID'],
            ],
            'page' => $this->page,
            'pageSize' => $this->pageSize,
        ]);
    }

    /**
     * 要展示和导出的所有字段
     * @return array
     */
    public function getColumn(){
        $regionConfig = Cache::getRegionConfig();
        if(!$this->columns){
            $this->columns = [
                'PERIOD_NUM' => [
                    'header' => '期数',
                ],
                'USER_NAME' => [
                    'header' => '会员编号',
                    'headerOther' => ['width' => '120'],
                ],
                'SN' => [
                    'header' => '订单号',
                    'headerOther' => ['width' => '200'],
                ],
                'CONSIGNEE' => [
                    'header' => '收货人',
                    'headerOther' => [
                        'width' => '150',
                    ],
                ],
                'MOBILE' => [
                    'header' => '联系方式1',
                    'headerOther' => [
                        'width' => '150',
                    ],
                ],
                'TEL' => [
                    'header' => '联系方式2',
                ],
                'PROVINCE' => [
                    'header' => '省',
                    'headerOther' => ['width' => '120'],
                    'value' => function ($row) use($regionConfig) {
                        return $regionConfig[$row['PROVINCE']]['REGION_NAME'] ?? '';
                    },
                ],
                'CITY' => [
                    'header' => '市',
                    'headerOther' => ['width' => '120'],
                    'value' => function ($row) use($regionConfig) {
                        return $regionConfig[$row['CITY']]['REGION_NAME'] ?? '';
                    },
                ],
                'COUNTY' => [
                    'header' => '区',
                    'headerOther' => ['width' => '120'],
                    'value' => function ($row) use($regionConfig) {
                        return $regionConfig[$row['COUNTY']]['REGION_NAME'] ?? '';
                    },
                ],
                'ADDRESS' => [
                    'header' => '详细地址',
                    'headerOther' => [
                        'width' => '300',
                    ],
                ],
                'WAREHOUSE' => [
                    'header' => '发货仓',
                ],
                'CREATED_AT' => [
                    'header' => '创建时间',
                    'value' => function ($row) {
                        return (new DateTime([
                            'value' => $row['CREATED_AT'],
                        ]))->result();
                    },
                    'headerOther' => ['width' => '190'],
                ],
                'PAY_AMOUNT' => [
                    'header' => '支付金额',
                    'headerOther' => [
                        'width' => '150',
                    ],
                    'value' => function ($row) {
                        return Tool::formatPrice($row['PAY_AMOUNT']);
                    },
                    'valueOther' => [
                        'tag' => ['type' => 'danger', 'size' => 'small', 'class' => 'no-border']
                    ],
                ],
                'PAY_PV' => [
                    'header' => '支付PV',
                    'headerOther' => [
                        'width' => '150',
                    ],
                    'value' => function ($row) {
                        return Tool::formatPrice($row['PAY_PV']);
                    },
                    'valueOther' => [
                        'tag' => ['type' => 'danger', 'size' => 'small', 'class' => 'no-border']
                    ],
                ],
                'PAY_AT' => [
                    'header' => '支付时间',
                    'value' => function ($row) {
                        return (new DateTime([
                            'value' => $row['PAY_AT'],
                        ]))->result();
                    },
                    'headerOther' => ['width' => '190'],
                ],
                'DELIVERY_AT' => [
                    'header' => '发货时间',
                    'value' => function ($row) {
                        return (new DateTime([
                            'value' => $row['DELIVERY_AT'],
                        ]))->result();
                    },
                    'headerOther' => ['width' => '190'],
                ],
                'PAY_FREIGHT' => [
                    'header' => '运费',
                    'headerOther' => [
                        'width' => '150',
                    ],
                    'value' => function ($row) {
                        return Tool::formatPrice($row['FREIGHT']);
                    },
                    'valueOther' => function ($row) {
                        return [
                            'tag' => ['type' => 'danger', 'size' => 'small', 'class' => 'no-border']
                        ];
                    },
                ],
                'EXPRESS_COMPANY' => [
                    'header' => '快递公司',
                    'headerOther' => ['width' => '150'],
                ],
                'ORDER_TRACK_NO' => [
                    'header' => '快递单号',
                    'headerOther' => ['width' => '200'],
                ],
                'EXPRESS_TYPE' => [
                    'header' => '发货方式',
                    'headerOther' => ['width' => '150'],
                    'value' => function ($row) {
                        return $row['EXPRESS_TYPE']==0 ? '邮寄' : '自提';
                    },
                ],
                'FRONT_REMARK' => [
                    'header' => '会员备注',
                    'headerOther' => [
                        'width' => '200',
                    ],
                    'value' => function ($row) {
                        return $row['FRONT_REMARK'];
                    },
                    'showValue' => function ($row) {
                        return '<div title="'.$row['FRONT_REMARK'].'" style="width:180px;overflow: hidden;white-space: nowrap;text-overflow: ellipsis;">'.$row['FRONT_REMARK'].'</div>';
                    },
                ],
            ];
        }
        return $this->columns;
    }

    /**
     * 前台用于筛选的类型集合
     * @return mixed
     */
    public function getFilterTypes()
    {
        if(!$this->filterTypes){
            $this->filterTypes = [
                'SN'=> ['name'=> '订单编号'],
                'USER_NAME'=> ['name'=> '会员编号'],
                'PERIOD_NUM'=> ['name'=> '期数'],
                'CREATED_AT'=> ['name'=> '创建时间', 'other'=>'date'],
            ];
        }
        return $this->filterTypes;
    }
}