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

use common\helpers\Cache;
use common\helpers\Tool;
use common\helpers\user\Info;
use common\libs\dataList\DataListInterface;
use common\models\CalcBonus;
use common\models\Countries;
use common\models\DeclarationLevel;
use common\models\EliteLevel;
use common\models\EmployLevel;
use common\models\Period;
use common\models\User;
use common\models\UserInfo;
use Yii;

class PeriodBonusList extends \common\libs\dataList\DataList implements DataListInterface
{
    /**
     * 列表名称
     * @return string
     */
    public function getListName(){
        return '奖金列表';
    }

    /**
     * 列表筛选到的数据
     * @throws \yii\base\Exception
     */
    public function dataHandle()
    {
        $yearMonth = $this->others['yearMonth'];
        $this->listData = CalcBonus::lists($this->condition, $this->params, [
            'select'=>'CB.*,UI.CON_UID,UI.REC_UID,PO.CALC_YEAR,PO.CALC_MONTH, C.NAME AS COUNTRY',
            'from' => CalcBonus::tableName().' AS CB',
            'join' => [
                ['INNER JOIN', UserInfo::tableName() . ' AS UI', 'CB.USER_ID=UI.USER_ID'],
                ['INNER JOIN', Period::tableName() . ' AS PO', 'PO.PERIOD_NUM=CB.PERIOD_NUM'],
                ['INNER JOIN', User::tableName() . ' AS CU', 'UI.USER_ID=CU.ID'],
                ['INNER JOIN', Countries::tableName() . ' AS C', 'CU.COUNTRY_ID=C.ID'],
            ],
            'yearMonth' => $yearMonth,
            'orderBy' => 'CB.CREATED_AT DESC, CB.ID DESC',
            'page' => $this->page,
            'pageSize' => $this->pageSize,
        ]);

        if ($this->listData['list']) {
            foreach ($this->listData['list'] as $key => $value) {
                $this->listData['list'][$key]['LAST_DEC_LV_NAME'] = Cache::getDecLevelConfig()[$value['LAST_DEC_LV']]['LEVEL_NAME'] ?? Cache::getDecLevelConfig()[DeclarationLevel::getDefaultLevelId()]['LEVEL_NAME'];
                $this->listData['list'][$key]['LAST_EMP_LV_NAME'] = Cache::getEmpLevelConfig()[$value['LAST_EMP_LV']]['LEVEL_NAME'] ?? Cache::getEmpLevelConfig()[EmployLevel::getDefaultLevelId()]['LEVEL_NAME'];
                $this->listData['list'][$key]['LAST_ELITE_LV_NAME'] = Cache::getEliteLevelConfig()[$value['LAST_ELITE_LV']]['LEVEL_NAME'] ?? Cache::getEliteLevelConfig()[EliteLevel::getDefaultLevelId()]['LEVEL_NAME'];
                $this->listData['list'][$key]['LAST_STATUS_NAME'] = Tool::paramConvert(Yii::$app->params['userStatus'])[$value['LAST_STATUS']]['label'];
                $this->listData['list'][$key]['LAST_REC_USER_NAME'] = Info::getUserNameByUserId($value['REC_UID']);
                $this->listData['list'][$key]['LAST_REC_REAL_NAME'] = Info::getUserRealNameByUserId($value['REC_UID']);
                $this->listData['list'][$key]['BONUS_YEAR_MONTH'] = $value['CALC_YEAR'] . '-' . ($value['CALC_MONTH'] >= 10 ? $value['CALC_MONTH'] : '0' . $value['CALC_MONTH']);
            }
        }
    }

    /**
     * 要展示和导出的所有字段
     * @return array
     */
    public function getColumn(){
        if(!$this->columns){
            $this->columns = [
                'ID' => null,
                'PERIOD_NUM' => [
                    'header' => \Yii::t('ctx', 'payCycle'), // 结算期数
                    'headerOther' => [
                        'width' => '130',
                    ],
                    'valueOther' => [
                        'tag'=>['type'=>'', 'size' => 'small', 'class'=>'no-border'],
                    ],
                ],
                'BONUS_YEAR_MONTH' => [
                    'header' => Yii::t('ctx', 'bonusMonth'), // 结算月
                    'headerOther' => [
                        'width' => '110',
                    ],
                ],
                'LAST_USER_NAME' => [
                    'header' => \Yii::t('ctx', 'memberCode'), // 会员编号
                    'headerOther' => [
                        'width' => '150',
                    ],
                    'valueOther' => [
                        'tag'=>['type'=>'info', 'size' => 'small', 'class'=>'no-border']
                    ],
                ],
                'LAST_REAL_NAME' => [
                    'header' => \Yii::t('ctx', 'memberName'), // 会员姓名
                    'headerOther' => [
                        'width' => '120',
                    ],
                    'valueOther' => [
                        'tag'=>['type'=>'success', 'size' => 'small', 'class'=>'no-border']
                    ],
                ],
                'COUNTRY' => [
                    'header' => Yii::t('ctx', 'country'),
                ],
                'LAST_DEC_LV_NAME' => [
                    'header' => \Yii::t('ctx', 'PCMemberLevel'), // 结算时会员级别
                    'headerOther' => [
                        'width' => '140',
                    ],
                ],
                'LAST_EMP_LV_NAME' => [
                    'header' => \Yii::t('ctx', 'latestDirector'), // 结算时会员最新管理星级
                    'headerOther' => [
                        'width' => '140',
                    ],
                ],
                'LAST_ELITE_LV_NAME' => [
                    'header' => \Yii::t('ctx', 'latestElite'), // 结算时会员最新ELite
                    'headerOther' => [
                        'width' => '140',
                    ],
                ],
                'LAST_REC_USER_NAME' => [
                    'header' => Yii::t('ctx', 'sponsorNo'), // 开拓者编号
                    'headerOther' => [
                        'width' => '150',
                    ],
                ],
                'BONUS_PB' => [
                    'header' => Yii::t('ctx', 'performanceBonus'), // 绩效奖
                    'headerOther' => [
                        'width' => '150',
                    ],
                ],
                'BONUS_TOTAL' => [
                    'header' => Yii::t('ctx', 'totalBonus'), // 总奖金
                    'headerOther' => [
                        'width' => '120',
                    ],
                ],
                'BONUS_E' => [
                    'header' => Yii::t('ctx', 'eliteBonus'), // Elite奖金
                    'headerOther' => [
                        'width' => '120',
                    ],
                ],
                'BONUS_PE' => [
                    'header' => Yii::t('ctx', 'proEliteBonus'), // Pro_Elite奖金
                    'headerOther' => [
                        'width' => '120',
                    ],
                ],
                'BONUS_SE' => [
                    'header' => Yii::t('ctx', 'superEliteBonus'), // Super_Elite奖金
                    'headerOther' => [
                        'width' => '150',
                    ],
                ],
                'BONUS_LB' => [
                    'header' => Yii::t('ctx', 'leaderShipBonus'), // LeaderShip奖金
                    'headerOther' => [
                        'width' => '150',
                    ],
                ],
                'BONUS_ST' => [
                    'header' => Yii::t('ctx', 'stockistBonus'), // 店铺奖
                    'headerOther' => [
                        'width' => '120',
                    ],
                ],
                'BONUS_WB' => [
                    'header' => Yii::t('ctx', 'welcomeBonus'), // 迎新奖
                    'headerOther' => [
                        'width' => '130',
                    ],
                ],
                'BONUS_CAR' => [
                    'header' => Yii::t('ctx', 'carBonus'), // 车奖
                    'headerOther' => [
                        'width' => '120',
                    ],
                ],
            ];
        }
        return $this->columns;
    }

    /**
     * 前台用于筛选的类型集合
     * @return mixed
     */
    public function getFilterTypes()
    {
        if(!$this->filterTypes){
            $this->filterTypes = [
                'LAST_USER_NAME'=> ['isUserTable'=> false, 'name'=> Yii::t('ctx', 'memberCode')], // 会员编号
                'LAST_REAL_NAME'=> ['isUserTable'=> false, 'name'=> Yii::t('ctx', 'memberName')], // 会员姓名
                'LAST_DEC_LV_NAME'=> ['isUserTable'=> false, 'name'=> Yii::t('ctx', 'PCMemberLevel'), 'other'=> 'decLevel'], // 结算时会员级别
                'LAST_EMP_LV_NAME'=> ['isUserTable'=> false, 'name'=> Yii::t('ctx', 'latestDirector'), 'other'=> 'empLevel'], // 结算时会员管理星级
                'LAST_ELITE_LV_NAME'=> ['isUserTable'=> false, 'name'=> Yii::t('ctx', 'latestElite'), 'other'=> 'elite'], // 结算时会员Elite
                'CALC_MONTH'=> ['isUserTable'=> false, 'name'=> Yii::t('ctx', 'bonusMonth')], // 结算月
            ];
        }
        return $this->filterTypes;
    }
}
