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

use common\helpers\Cache;
use common\helpers\user\Info;
use common\models\PerfMonth;
use common\models\PerfStandard;
use common\models\User as modelUser;
use common\libs\dataList\column\Price;
use common\libs\dataList\DataListInterface;
use common\libs\dataList\column\DateTime;
use common\models\UserInfo;
use Yii;

class PerfStandardList extends \common\libs\dataList\DataList implements DataListInterface
{
    /**
     * 列表名称
     * @return string
     */
    public function getListName(){
        return '达标业绩列表';
    }

    /**
     * 列表筛选到的数据
     * @throws \yii\base\Exception
     */
    public function dataHandle()
    {
        $data = PerfStandard::lists($this->condition, $this->params, [
            'select'=>'PS.*, U.USER_NAME, U.REAL_NAME,RU.USER_NAME REC_USER_NAME,RU.REAL_NAME REC_REAL_NAME,',
            'from' => PerfStandard::tableName().' AS PS',
            'join' => [
                ['LEFT JOIN', modelUser::tableName() . ' AS U', 'PS.USER_ID=U.ID'],
                ['LEFT JOIN', UserInfo::tableName() . ' AS UI', 'PS.USER_ID=UI.USER_ID'],
                ['LEFT JOIN', modelUser::tableName() . ' AS RU', 'UI.REC_UID=RU.ID'],
            ],
            'orderBy' => 'PS.ID DESC',
            'page' => $this->page,
            'pageSize' => $this->pageSize,
        ]);
        if ($data['list']) {
            foreach ($data['list'] as $key => $value) {
                $data['list'][$key]['LAST_DEC_LV_NAME'] = Cache::getDecLevelConfig()[$value['LAST_DEC_LV']]['LEVEL_NAME'] ?? '';
                $data['list'][$key]['LAST_EMP_LV_NAME'] = Cache::getEmpLevelConfig()[$value['LAST_EMP_LV']]['LEVEL_NAME'] ?? '';
            }
        }
        $this->listData = $data;
    }

    /**
     * 要展示和导出的所有字段
     * @return array
     */
    public function getColumn(){
        if(!$this->columns){
            $this->columns = [
                'CALC_MONTH' => [
                    'header' => '结算月',
                    'headerOther' => [
                        'width' => '100',
                    ],
                ],
                'USER_NAME' => [
                    'header' => '会员编号',
                    'headerOther' => [
                        'width' => '110',
                    ],
                ],
                'REAL_NAME' => [
                    'header' => '会员姓名',
                    'headerOther' => [
                        'width' => '150',
                    ],
                ],
                'REC_USER_NAME' => [
                    'header' => '推荐人编号',
                    'headerOther' => [
                        'width' => '110',
                    ],
                ],
                'REC_REAL_NAME' => [
                    'header' => '推荐人姓名',
                    'headerOther' => [
                        'width' => '150',
                    ],
                ],
                'LAST_DEC_LV_NAME' => [
                    'header' => '结算时会员级别',
                    'headerOther' => [
                        'width' => '150',
                    ],
                ],
                'LAST_EMP_LV_NAME' => [
                    'header' => '结算时会员聘级',
                    'headerOther' => [
                        'width' => '150',
                    ],
                ],
                'AMOUNT_PCS' => [
                    'header' => '个人销售额',
                    'headerOther' => [
                        'width' => '150',
                    ],
                    'value' => function($row) {
                        return (new Price([
                            'value' => $row['AMOUNT_PCS'],
                        ]))->result();
                    },
                ],
                'AMOUNT_PSS' => [
                    'header' => '团队销售额',
                    'headerOther' => [
                        'width' => '150',
                    ],
                    'value' => function($row) {
                        return (new Price([
                            'value' => $row['AMOUNT_PSS'],
                        ]))->result();
                    },
                ],
                '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 = [
                'CALC_MONTH'=> ['isUserTable'=> false, 'name'=> '结算月'],
                'USER_NAME'=> ['isUserTable'=> false, 'name'=> '会员编号'],
                'REC_USER_NAME'=> ['isUserTable'=> false, 'name'=> '推荐人编号'],
                'CREATED_AT'=> ['isUserTable'=> false, 'name'=> '创建时间', 'other'=> 'date'],
            ];
        }
        return $this->filterTypes;
    }
}