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

use backendApi\modules\v1\models\Admin;
use common\helpers\Cache;
use common\libs\dataList\DataListInterface;
use common\models\DecLevelLog;
use common\models\User;
use common\models\UserInfo;
use common\libs\dataList\column\DateTime;
use common\models\UserMove;
use Yii;

class DecLevelList extends \common\libs\dataList\DataList implements DataListInterface
{
    /**
     * 列表名称
     * @return string
     */
    public function getListName(){
        return '会员级别调整列表';
    }

    /**
     * 列表筛选到的数据
     */
    public function dataHandle()
    {
        $this->listData = DecLevelLog::lists($this->condition, $this->params, [
            'select' => 'LL.*,
                CU.USER_NAME,ADM.ADMIN_NAME',
            'orderBy' => 'LL.CREATED_AT DESC, LL.ID DESC',
            'from' => DecLevelLog::tableName() . ' AS LL',
            'join' => [
                ['LEFT JOIN', Admin::tableName() . ' AS ADM', 'LL.ADMIN_ID=ADM.ID'],
                ['LEFT JOIN', User::tableName() . ' AS CU', 'LL.USER_ID=CU.ID'],
            ],
            'page' => $this->page,
            'pageSize' => $this->pageSize,
        ]);
    }

    /**
     * 要展示和导出的所有字段
     * @return array
     */
    public function getColumn(){
        $decLevelConfig = Cache::getDecLevelConfig();
        if(!$this->columns){
            $this->columns = [
                'ID' => null,
                'USER_ID' => null,
                'USER_NAME' => [
                    'header' => '会员编号',
                    'headerOther' => ['width' => '150'],
                ],
                'FROM_DEC_LV_NAME' => [
                    'header' => '修改前级别',
//                    'headerOther' => [
//                        'width' => '120',
//                    ],
                    'value' => function($row) use($decLevelConfig) {
                        return $decLevelConfig[$row['FROM_ID']]['LEVEL_NAME'];
                    },
                    'valueOther' => [
                        'tag'=>['type'=>'warning', 'size' => 'small', 'class'=>'no-border']
                    ],
                ],
                'TO_DEC_LV_NAME' => [
                    'header' => '修改后级别',
//                    'headerOther' => [
//                        'width' => '130',
//                    ],
                    'value' => function($row) use($decLevelConfig) {
                        return isset($decLevelConfig[$row['TO_ID']])?$decLevelConfig[$row['TO_ID']]['LEVEL_NAME']:'';
                    },
                    'valueOther' => [
                        'tag'=>['type'=>'warning', 'size' => 'small', 'class'=>'no-border']
                    ],
                ],
                'ADMIN_NAME' => [
                    'header' => '操作管理员',
//                    'headerOther' => ['width' => '120'],
                ],
                'CREATED_AT' => [
                    'header' => '创建时间',
                    'value' => function($row) {
                        return (new DateTime([
                            'value' => $row['CREATED_AT'],
                        ]))->result();
                    },
//                    'headerOther' => ['width' => '170'],
                ],
            ];
        }
        return $this->columns;
    }

    /**
     * 前台用于筛选的类型集合
     * @return mixed
     */
    public function getFilterTypes()
    {
        if(!$this->filterTypes){
            $this->filterTypes = [
                'USER_NAME'=> ['isUserTable' => false,'name'=> '会员编号'],
                'CREATED_AT' => ['isUserTable' => false, 'name' => '创建时间', 'other' => 'date'],
                'ADMIN_NAME' => ['isUserTable' => false, 'name' => '操作管理员'],
            ];
        }
        return $this->filterTypes;
    }
}