<?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\HighestEmpLevelLog;
use common\models\User;
use common\libs\dataList\column\DateTime;
use Yii;

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

    /**
     * 列表筛选到的数据
     */
    public function dataHandle()
    {
        $this->listData = HighestEmpLevelLog::lists($this->condition, $this->params, [
            'select' => 'LL.*, CU.USER_NAME, ADM.ADMIN_NAME',
            'orderBy' => 'LL.CREATED_AT DESC, LL.ID DESC',
            'from' => HighestEmpLevelLog::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() {
        $empLevelConfig = Cache::getEmpLevelConfig();
        if (!$this->columns) {
            $this->columns = [
                'ID' => null,
                'USER_ID' => null,
                'USER_NAME' => [
                    'header' => '会员编号',
                ],
                'FROM_HIGHEST_EMP_LV_NAME' => [
                    'header' => '调整前聘级',
                    'value' => function($row) use($empLevelConfig) {
                        return $empLevelConfig[$row['FROM_ID']]['LEVEL_NAME'] ?? '';
                    },
                    'valueOther' => [
                        'tag'=>['type'=>'warning', 'size' => 'small', 'class'=>'no-border']
                    ],
                ],
                'TO_HIGHEST_EMP_LV_NAME' => [
                    'header' => '调整后聘级',
                    'value' => function($row) use($empLevelConfig) {
                        return $empLevelConfig[$row['TO_ID']]['LEVEL_NAME'] ?? '';
                    },
                    'valueOther' => [
                        'tag'=>['type'=>'warning', 'size' => 'small', 'class'=>'no-border']
                    ],
                ],
                'ADMIN_NAME' => [
                    'header' => '操作管理员',
                ],
                'CREATED_AT' => [
                    'header' => '创建时间',
                    'value' => function($row) {
                        return (new DateTime([
                            'value' => $row['CREATED_AT'],
                        ]))->result();
                    },
                ],
            ];
        }
        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;
    }
}