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

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

class DecOrderList extends \common\libs\dataList\DataList implements DataListInterface
{
    /**
     * 列表名称
     * @return string
     */
    public function getListName(){
        return 'Welcome Pack Listing';//报单列表
    }

    /**
     * 列表筛选到的数据
     */
    public function dataHandle()
    {
        $this->listData = DecOrder::lists($this->condition, $this->params, [
            'select' => 'DO.*,U.USER_NAME USER_NAME,TU.USER_NAME TO_USER_NAME,TU.REAL_NAME,TU.ID_CARD,TU.MOBILE,TU.DEC_LV,DU.USER_NAME DEC_USER_NAME,DU.REAL_NAME DEC_REAL_NAME,RU.USER_NAME REC_USER_NAME,RU.REAL_NAME REC_REAL_NAME,CU.USER_NAME CON_USER_NAME,CU.REAL_NAME CON_REAL_NAME,O.*',
            'orderBy' => 'DO.CREATED_AT DESC, DO.ID DESC',
            'from' => DecOrder::tableName() . ' AS DO',
            'join' => [
                ['LEFT JOIN', User::tableName() . ' AS U', 'DO.USER_ID=U.ID'],
                ['LEFT JOIN', User::tableName() . ' AS TU', 'DO.TO_USER_ID=TU.ID'],
                ['LEFT JOIN', User::tableName() . ' AS DU', 'DO.DEC_ID=DU.ID'],
                ['LEFT JOIN', User::tableName() . ' AS RU', 'DO.REC_USER_ID=RU.ID'],
                ['LEFT JOIN', User::tableName() . ' AS CU', 'DO.CON_USER_ID=CU.ID'],
                ['LEFT JOIN', Order::tableName() . ' AS O', 'DO.ORDER_SN=O.SN']
            ],
            'page' => $this->page,
            'pageSize' => $this->pageSize,
        ]);
    }

    /**
     * 要展示和导出的所有字段
     * @return array
     */
    public function getColumn(){
        $decLevelConfig = Cache::getDecLevelConfig();
        $regionConfig = Cache::getRegionConfig();
        if(!$this->columns){
            $this->columns = [
                'ID' => null,
                'DETAIL_TYPE' => null,
                'UPGRADE_TYPE' => null,
                'DEC_SN' => [
                    'header' => 'Entry Code', // 报单编号
                    'headerOther' => ['width' => '200'],
                ],
                'ORDER_SN' => [
                    'header' => 'Order Code', // 订单编号
                    'headerOther' => ['width' => '200'],
                ],
                'USER_NAME' => [
                    'header' => 'Member Code', // 报单人
                    'headerOther' => ['width' => '150'],
                ],
                'TO_USER_NAME' => [
                    'header' => 'Entry Member Code', // 被报单人
                    'headerOther' => ['width' => '150'],
                ],
                'REAL_NAME' => [
                    'header' => 'Member Name', // 会员姓名
                    'headerOther' => ['width' => '150'],
                ],
                'ID_CARD' => [
                    'header' => 'Identity No.', // 会员身份证号
                    'headerOther' => ['width' => '150'],
                ],
                'MOBILE' => [
                    'header' => 'Member Phone Number', // 会员手机号
                    'headerOther' => ['width' => '150'],
                ],
                'DEC_LV_NAME' => [
                    'header' => 'Member Levels', // 会员级别
                    'headerOther' => [
                        'width' => '110',
                    ],
                    'value' => function($row) use($decLevelConfig) {
                        return $decLevelConfig[$row['DEC_LV']]['LEVEL_NAME'];
                    },
                    'valueOther' => [
                        'tag'=>['type'=>'warning', 'size' => 'small', 'class'=>'no-border']
                    ],
                ],
                'DEC_USER_NAME' => [
                    'header' => 'Entry Code', // 报单中心编号
                    'headerOther' => ['width' => '150'],
                ],
                'DEC_REAL_NAME' => [
                    'header' => 'Entry Name', // 报单中心姓名
                    'headerOther' => ['width' => '150'],
                ],
                'DEC_AMOUNT' => [
                    'header' => 'Entry Amount', // 报单金额
                    'headerOther' => [
                        'width' => '150',
                    ],
                    'value' => function ($row) {
                        return Tool::formatPrice($row['DEC_AMOUNT']);
                    },
                    'valueOther' => function ($row) {
                        return [
                            'tag' => ['type' => 'danger', 'size' => 'small', 'class' => 'no-border']
                        ];
                    },
                ],
                'DEC_PV' => [
                    'header' => 'Entry BV', // 报单BV
                    'headerOther' => [
                        'width' => '150',
                    ],
                    'value' => function ($row) {
                        return Tool::formatPrice($row['DEC_PV']);
                    },
                    'valueOther' => function ($row) {
                        return [
                            'tag' => ['type' => 'danger', 'size' => 'small', 'class' => 'no-border']
                        ];
                    },
                ],
                'PAID_WALLET' => [
                    'header' => 'Payment Wallet', // 付款钱包
                    'headerOther' => [
                        'width' => '110',
                    ],
                    'value' => function ($row) {
                        return Tool::paramConvert(\Yii::$app->params['shopWalletType'])[strtolower($row['PAID_WALLET'])]['title'] ?? '';
                    },
                ],
                'REMARK' => [
                    'header' => 'Remark', // 备注
                    'headerOther' => [
                        'width' => '200',
                    ],
                    'value' => function ($row) {
                        return $row['REMARK'];
                    },
                    'showValue' => function ($row) {
                        return '<div title="'.$row['REMARK'].'" style="width:180px;overflow: hidden;white-space: nowrap;text-overflow: ellipsis;">'.$row['REMARK'].'</div>';
                    },
                ],
                'REC_USER_NAME' => [
                    'header' => 'Entry Sponsor', // 报单时开拓者
                    'headerOther' => ['width' => '150'],
                ],
                'REC_REAL_NAME' => [
                    'header' => 'Entry Sponsor Name', // 报单时开拓者姓名
                    'headerOther' => ['width' => '150'],
                ],
                'CON_USER_NAME' => [
                    'header' => 'Entry Superior', // 报单时上级
                    'headerOther' => ['width' => '150'],
                ],
                'CON_REAL_NAME' => [
                    'header' => 'Entry Superior Name', // 报单时上级姓名
                    'headerOther' => ['width' => '150'],
                ],
                'PROVINCE' => [
                    'header' => 'State', // 省
                    'headerOther' => ['width' => '120'],
                    'value' => function ($row) use($regionConfig) {
                        return $regionConfig[$row['PROVINCE']]['REGION_NAME'] ?? '';
                    },
                ],
                'CITY' => [
                    'header' => 'Local Government Area', // 市
                    'headerOther' => ['width' => '120'],
                    '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',
                    ],
                ],
                'EXPRESS_TYPE' => [
                    'header' => 'Delivery Method', // 发货方式
                    'headerOther' => ['width' => '150'],
                    'value' => function ($row) {
                        return $row['EXPRESS_TYPE']==0 ? 'mail' : 'Self Pick-up';//邮寄：自提
                    },
                ],
                'PERIOD_NUM' => [
                    'header' => 'Current Period', // 期数
                ],
                'CREATED_AT' => [
                    'header' => 'Creation Time', // 创建时间
                    'value' => function ($row) {
                        return (new DateTime([
                            'value' => $row['CREATED_AT'],
                        ]))->result();
                    },
                    'headerOther' => ['width' => '190'],
                ],
                'UPDATED_AT' => [
                    'header' => 'Update Time', // 更新时间
                    'value' => function ($row) {
                        return (new DateTime([
                            'value' => $row['UPDATED_AT'],
                        ]))->result();
                    },
                    'headerOther' => ['width' => '190'],
                ],
                'IS_AUTO' => [
                    'header' => 'Is Auto', // 是否自动
                    'value' => function ($row) {
                        switch ($row['IS_AUTO']) {
                            case 1:
                                return 'Yes';
                            case 0:
                                return 'No';
                        }
                    },
                ],
            ];
        }
        return $this->columns;
    }

    /**
     * 前台用于筛选的类型集合
     * @return mixed
     */
    public function getFilterTypes()
    {
        if(!$this->filterTypes){
            $this->filterTypes = [
                'DEC_SN'=> ['name'=> 'Entry Code'], // 报单编号
                'ORDER_SN'=> ['name'=> 'Order Code'], // 订单编号
                'USER_NAME'=> ['name'=> 'Member Code'], // 报单人
                'TO_USER_NAME'=> ['name'=> 'Entry Member Code'], // 被报单人
                'REAL_NAME'=> ['name'=> 'Member Name'], // 会员姓名
                'DEC_REAL_NAME'=> ['name'=> 'Entry Name'], // 报单中心姓名
                'ID_CARD'=> ['name'=> 'Identity No.'], // 会员身份证号
                'MOBILE'=> ['name'=> 'Member Phone Number'], // 会员手机号
                'DEC_LV_NAME'=> ['name'=> 'Member Levels', 'other'=> 'decLevel'], // 会员级别
                'DEC_AMOUNT'=> ['name'=> 'Entry Amount'], // 报单金额
                'DEC_PV'=> ['name'=> 'Entry BV'], // 报单BV
                'PERIOD_NUM'=> ['name'=> 'Current Period'], // 期数
                'REC_USER_NAME'=> ['name'=> '报单时开拓者'], // 报单时开拓者
                'CREATED_AT'=> ['name'=> 'Creation Time', 'other'=> 'date'], // 创建时间
                'UPDATED_AT'=> ['name'=> 'Update Time', 'other'=> 'date'], // 更新时间
            ];
        }
        return $this->filterTypes;
    }
}