<?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\ShopGoods;
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' => 'Number Of Periods', // 期数
                    'headerOther' => ['width' => '140'],
                ],
                'USER_NAME' => [
                    'header' => 'Member Code', // 会员编号
                    'headerOther' => ['width' => '120'],
                ],
                'SN' => [
                    'header' => 'Order Code', // 订单号
                    'headerOther' => ['width' => '200'],
                ],
                'CONSIGNEE' => [
                    'header' => 'Recipient', // 收货人
                    'headerOther' => [
                        'width' => '150',
                    ],
                ],
                'MOBILE' => [
                    'header' => 'Contact 1', // 联系方式1
                    'headerOther' => [
                        'width' => '150',
                    ],
                ],
                'TEL' => [
                    'header' => 'Contact 2', // 联系方式2
                ],
                'PROVINCE' => [
                    'header' => 'State', // 州
                    'headerOther' => ['width' => '120'],
                    'value' => function ($row) use($regionConfig) {
                        return $regionConfig[$row['PROVINCE']]['REGION_NAME'] ?? '';
                    },
                ],
                'CITY' => [
                    'header' => 'Local Government Area', // 地方政府
                    'headerOther' => ['width' => '170'],
                    'value' => function ($row) use($regionConfig) {
                        return $regionConfig[$row['CITY']]['REGION_NAME'] ?? '';
                    },
                ],
                'COUNTY' => [
                    'header' => 'City', // 区
                    'headerOther' => ['width' => '120'],
                    'value' => function ($row) use($regionConfig) {
                        return $regionConfig[$row['COUNTY']]['REGION_NAME'] ?? '';
                    },
                ],
                'ADDRESS' => [
                    'header' => 'Detailed Address', // 详细地址
                    'headerOther' => [
                        'width' => '300',
                    ],
                ],
                'WAREHOUSE' => [
                    'header' => 'Delivery Warehouse', // 发货仓
                    'headerOther' => ['width' => '150'],
                ],
                'CREATED_AT' => [
                    'header' => 'Creation Time', // 创建时间
                    'value' => function ($row) {
                        return (new DateTime([
                            'value' => $row['CREATED_AT'],
                        ]))->result();
                    },
                    'headerOther' => ['width' => '190'],
                ],
                'PAY_AMOUNT' => [
                    'header' => 'Payment Amount', // 支付金额
                    'headerOther' => [
                        'width' => '150',
                    ],
                    'value' => function ($row) {
                        return Tool::formatPrice($row['PAY_AMOUNT']);
                    },
                    'valueOther' => [
                        'tag' => ['type' => 'danger', 'size' => 'small', 'class' => 'no-border']
                    ],
                ],
                'PAY_PV' => [
                    'header' => 'Pay BV', // 支付BV
                    'headerOther' => [
                        'width' => '150',
                    ],
                    'value' => function ($row) {
                        return Tool::formatPrice($row['PAY_PV']);
                    },
                    'valueOther' => [
                        'tag' => ['type' => 'danger', 'size' => 'small', 'class' => 'no-border']
                    ],
                ],
                'PAY_AT' => [
                    'header' => 'Payment Time', // 支付时间
                    'value' => function ($row) {
                        return (new DateTime([
                            'value' => $row['PAY_AT'],
                        ]))->result();
                    },
                    'headerOther' => ['width' => '190'],
                ],
                'PAY_TYPE' => [
                    'header' => 'Pay Type', // 支付方式
                    'value' => function ($row) {
                        return ShopGoods::payTypes()[$row['PAY_TYPE']]['name'] ?? ShopGoods::payTypes()['cash']['name'];
                    },
                    'headerOther' => ['width' => '190'],
                ],
                'DELIVERY_AT' => [
                    'header' => 'Delivery Time', // 发货时间
                    'value' => function ($row) {
                        return (new DateTime([
                            'value' => $row['DELIVERY_AT'],
                        ]))->result();
                    },
                    'headerOther' => ['width' => '190'],
                ],
                'PAY_FREIGHT' => [
                    'header' => 'Freight', // 运费
                    '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' => 'Courier Services Company', // 快递公司
                    'headerOther' => ['width' => '190'],
                ],
                'ORDER_TRACK_NO' => [
                    'header' => 'Courier Number', // 快递单号
                    'headerOther' => ['width' => '200'],
                ],
                'EXPRESS_TYPE' => [
                    'header' => 'Shipping Method', // 发货方式
                    'headerOther' => ['width' => '150'],
                    'value' => function ($row) {
                        return $row['EXPRESS_TYPE']==0 ? 'mailing ':' auto pick '; // '邮寄' : '自提'
                    },
                ],
                'FRONT_REMARK' => [
                    'header' => 'Member Notes', // 会员备注
                    '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'=> 'Order No'],//订单编号
                'USER_NAME'=> ['name'=> 'Member Code'],//会员编号
                'PERIOD_NUM'=> ['name'=> 'Number Of Periods'],//期数
                'CREATED_AT'=> ['name'=> 'Creation Time', 'other'=>'date'],//创建时间
            ];
        }
        return $this->filterTypes;
    }
}