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

use common\helpers\Cache;
use common\helpers\user\Info;
use common\models\CalcBonusBSDefault;
use common\models\Countries;
use common\models\PerfMonth;
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 PerfMonthList extends \common\libs\dataList\DataList implements DataListInterface
{
    /**
     * 列表名称
     * @return string
     */
    public function getListName(){
        return '月业绩列表';
    }

    /**
     * 列表筛选到的数据
     * @throws \yii\base\Exception
     */
    public function dataHandle()
    {
        $data = PerfMonth::lists($this->condition, $this->params, [
            'select'=>'PM.*, U.USER_NAME, U.REAL_NAME,RU.USER_NAME REC_USER_NAME,RU.REAL_NAME REC_REAL_NAME, C.NAME AS COUNTRY',
            'from' => PerfMonth::tableName().' AS PM',
            'join' => [
                ['INNER JOIN', modelUser::tableName() . ' AS U', 'PM.USER_ID=U.ID'],
                ['LEFT JOIN', UserInfo::tableName() . ' AS UI', 'PM.USER_ID=UI.USER_ID'],
                ['LEFT JOIN', modelUser::tableName() . ' AS RU', 'UI.REC_UID=RU.ID'],
                ['INNER JOIN', Countries::tableName() . ' AS C', 'U.COUNTRY_ID=C.ID'],
            ],
            'orderBy' => 'PM.CREATED_AT DESC, PM.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' => Yii::t('ctx', 'bonusMonth'), // 结算月
                    'headerOther' => [
                        'width' => '110',
                    ],
                ],
                'USER_NAME' => [
                    'header' => Yii::t('ctx', 'memberCode'), // 会员编号
                    'headerOther' => [
                        'width' => '120',
                    ],
                ],
                'REAL_NAME' => [
                    'header' => Yii::t('ctx', 'memberName'), // 会员姓名
                    'headerOther' => [
                        'width' => '150',
                    ],
                ],
                'COUNTRY' => [
                    'header' => Yii::t('ctx', 'country'),
                ],
                'REC_USER_NAME' => [
                    'header' => Yii::t('ctx', 'recommenderNo'), // 推荐人编号
                    'headerOther' => [
                        'width' => '140',
                    ],
                ],
                'REC_REAL_NAME' => [
                    'header' => Yii::t('ctx', 'recommenderName'), // 推荐人姓名
                    'headerOther' => [
                        'width' => '160',
                    ],
                ],
                'LAST_DEC_LV_NAME' => [
                    'header' =>  Yii::t('ctx', 'PCMemberLevel'), // 结算时会员级别
                    'headerOther' => [
                        'width' => '150',
                    ],
                ],
                'LAST_EMP_LV_NAME' => [
                    'header' => Yii::t('ctx', 'directorRank'), // 结算时会员聘级
                    'headerOther' => [
                        'width' => '150',
                    ],
                ],
                'PV_PCS' => [
                    'header' => Yii::t('ctx', 'personalPerformance'), // 个人业绩
                    'headerOther' => [
                        'width' => '170',
                    ],
                    'value' => function($row) {
                        return (new Price([
                            'value' => $row['PV_PCS'],
                        ]))->result();
                    },
                ],
//                'PV_PGS' => [
//                    'header' => Yii::t('ctx', 'directorPGSPerformance'), // 管理奖个人小组业绩
//                    'headerOther' => [
//                        'width' => '190',
//                    ],
//                    'value' => function($row) {
//                        return (new Price([
//                            'value' => $row['DIRECTOR_BONUS_PGS'],
//                        ]))->result();
//                    },
//                ],
//                'VIP_PV_1L_ZC' => [
//                    'header' => Yii::t('ctx', 'LNewMarketEntryPerformance'), // 一市场新增报单业绩
//                    'headerOther' => [
//                        'width' => '240',
//                    ],
//                    'value' => function($row) {
//                        return (new Price([
//                            'value' => $row['VIP_PV_1L_ZC'],
//                        ]))->result();
//                    },
//                ],
//                'VIP_PV_2L_ZC' => [
//                    'header' =>  Yii::t('ctx', 'RNewMarketEntryPerformance'), // 二市场新增报单业绩
//                    'headerOther' => [
//                        'width' => '240',
//                    ],
//                    'value' => function($row) {
//                        return (new Price([
//                            'value' => $row['VIP_PV_2L_ZC'],
//                        ]))->result();
//                    },
//                ],
//                'VIP_SURPLUS_1L_ZC' => [
//                    'header' => Yii::t('ctx', 'LMarketMonthlyRemainingDeclarationPerformance'), // 一市场月剩余报单业绩
//                    'headerOther' => [
//                        'width' => '370',
//                    ],
//                    'value' => function($row) {
//                        return (new Price([
//                            'value' => $row['VIP_SURPLUS_1L_ZC'],
//                        ]))->result();
//                    },
//                ],
//                'VIP_SURPLUS_2L_ZC' => [
//                    'header' =>  Yii::t('ctx', 'RMarketMonthlyRemainingDeclarationPerformance'), // 二市场月剩余报单业绩
//                    'headerOther' => [
//                        'width' => '370',
//                    ],
//                    'value' => function($row) {
//                        return (new Price([
//                            'value' => $row['VIP_SURPLUS_2L_ZC'],
//                        ]))->result();
//                    },
//                ],
                'PV_PSS' => [
                    'header' => Yii::t('ctx', 'monthlyNewTeamPerformance'), // 月新增团队业绩
                    'headerOther' => [
                        'width' => '230',
                    ],
                    'value' => function($row) {
                        return (new Price([
                            'value' => $row['PV_PSS'],
                        ]))->result();
                    },
                ],
                'PV_PSS_TOTAL' => [
                    'header' => Yii::t('ctx', 'monthlyCumulativeTeamPerformance'), // 月累计团队业绩
                    'headerOther' => [
                        'width' => '270',
                    ],
                    'value' => function($row) {
                        return (new Price([
                            'value' => $row['PV_PSS_TOTAL'],
                        ]))->result();
                    },
                ],
                'CREATED_AT' => [
                    'header' => Yii::t('ctx', 'createAt'), // 创建时间
                    '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'=>  Yii::t('ctx', 'bonusMonth')], // 结算月
                'USER_NAME'=> ['isUserTable'=> false, 'name'=> Yii::t('ctx', 'memberCode')], // 会员编号
                'REC_USER_NAME'=> ['isUserTable'=> false, 'name'=> Yii::t('ctx', 'recommenderNo')], // 推荐人编号
                'CREATED_AT'=> ['isUserTable'=> false, 'name'=> Yii::t('ctx', 'createAt'), 'other'=> 'date'], // 创建时间
            ];
        }
        return $this->filterTypes;
    }
}
