<?php

namespace backendApi\modules\v1\models\lists\atlas;

use common\helpers\Cache;
use common\helpers\user\Info;
use common\libs\dataList\DataListInterface;
use common\models\EmployLevel;
use common\models\Period;
use common\models\UserRelation;
use Yii;

class RelationList extends \common\libs\dataList\DataList implements DataListInterface {
    /**
     * 列表名称
     * @return string
     */
    public function getListName() {
        return '开拓网络列表';
    }

    /**
     * 列表筛选到的数据
     * @throws \yii\base\Exception
     */
    public function dataHandle() {
        $userId = $this->others['userId'];
        $deep = $this->others['deep'];
        $periodNum = $this->others['periodNum'];
        $period = Period::instance();
        $mainUserInfo = Info::baseInfo($userId, $periodNum);
        $decLevelConfig = Cache::getDecLevelConfig();
        $empLevelConfig = Cache::getEmpLevelConfig();
        $param = [];
        if ( $this->pageSize ) $param['pageSize'] = $this->pageSize;
        if ( $this->page !== null ) $param['page'] = $this->page;
        $this->listData = UserRelation::getChildrenInDeepFromPeriodWithPage($userId, $deep, $periodNum, $param);
        foreach($this->listData['list'] as $key=>$value){
            $userBaseInfo = Info::baseInfo($value['USER_ID'], $periodNum);
            $this->listData['list'][$key]['LIST_PERIOD_NUM'] = $periodNum?$periodNum:$period->getNowPeriodNum();
            $this->listData['list'][$key]['COUNT_DEEP'] = (int)($userBaseInfo['RELATION_DEEP']);
            $this->listData['list'][$key]['USER_NAME'] = $userBaseInfo['USER_NAME'];
            $this->listData['list'][$key]['SEE_USER_NAME'] = $userBaseInfo['USER_NAME'];
            $this->listData['list'][$key]['REAL_NAME'] = $userBaseInfo['REAL_NAME'];
            $this->listData['list'][$key]['PERIOD_AT'] = $userBaseInfo['PERIOD_AT'];
            $this->listData['list'][$key]['DEC_LV_NAME'] = $decLevelConfig[$userBaseInfo['DEC_LV']]['LEVEL_NAME'];
            $this->listData['list'][$key]['EMP_LV_NAME'] = isset($empLevelConfig[$userBaseInfo['EMP_LV']]) ? $empLevelConfig[$userBaseInfo['EMP_LV']]['LEVEL_NAME'] : $empLevelConfig[EmployLevel::getDefaultLevelId()]['LEVEL_NAME'];
        }
        $this->listData['periodNum'] = $periodNum;
        $this->listData['listTopDeep'] = $mainUserInfo['RELATION_DEEP'];
    }

    /**
     * 要展示和导出的所有字段
     * @return array
     */
    public function getColumn() {
        if (!$this->columns) {
            $this->columns = [
                'USER_ID' => null, // 这种传输方式主要是用于索引，因为过滤后的字段可能没有这种ID，但是一些功能的操作还需要用这种ID去关联，例如前台会员列表中的勾选批量状态管理，这里需要的就是USER_ID
                'SEE_USER_NAME' => null,
                'COUNT_DEEP' => [
                    'header' => Yii::t('ctx', 'numberOfLayers'), // 层数
                    'headerOther' => ['width' => '140'],
                ],
                'USER_NAME' => [
                    'header' => Yii::t('ctx', 'recommendedMemberNo'), // 推荐会员编号
                    'headerOther' => ['width' => '150'],
                ],
                'REAL_NAME' => [
                    'header' => Yii::t('ctx', 'nameRecommendedMember'), // 推荐会员姓名
//                    'headerOther' => [
//                        'width' => '120',
//                    ],
                ],
                'DEC_LV_NAME' => [
                    'header' => Yii::t('ctx', 'recommendedMemberLevel'), // 推荐会员级别
                    'headerOther' => [
                        'width' => '210',
                    ],
                ],
                'EMP_LV_NAME' => [
                    'header' => Yii::t('ctx', 'highestDirector'), // 推荐会员聘级
                    'headerOther' => [
                        'width' => '210',
                    ],
                ],
                'PERIOD_AT' => [
                    'header' => Yii::t('ctx', 'joiningPeriod'), // 加入期数
                    'headerOther' => ['width' => '110'],
                ],
            ];
        }
        return $this->columns;
    }

    /**
     * 前台用于筛选的类型集合
     * @return mixed
     */
    public function getFilterTypes() {
        if (!$this->filterTypes) {
            $this->filterTypes = [
                'userName' => ['isUserTable' => false, 'name' => Yii::t('ctx', 'modelListAtlasUserName')], // 会员编号
                'deep' => ['isUserTable' => false, 'name' => Yii::t('ctx', 'modelListAtlasDepth')], // 深度
                'periodNum' => ['isUserTable' => false, 'name' => Yii::t('ctx', 'numberOfPeriods')], // 期数
            ];
        }
        return $this->filterTypes;
    }
}