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

use backendApi\modules\v1\models\Admin;
use common\helpers\bonus\CalcCache;
use common\helpers\Cache;
use common\helpers\Tool;
use common\helpers\user\Info;
use common\libs\dataList\column\Price;
use common\libs\dataList\column\YesNo;
use common\libs\dataList\DataListInterface;
use common\models\CalcBonus;
use common\models\CalcBonusGL;
use common\models\PerfOrder;
use common\models\PerfPeriod;
use common\models\Period;
use common\models\Region;
use common\models\UserBind;
use common\models\UserBonus;
use common\models\UserInfo;
use common\libs\dataList\column\DateTime;
use common\models\UserNetwork;
use common\models\UserSystem;
use common\models\Withdraw;
use Yii;

class TraceDownGlList extends \common\libs\dataList\DataList implements DataListInterface
{
    /**
     * 列表名称
     * @return string
     */
    public function getListName(){
        return '管理奖向下追溯';
    }

    /**
     * 列表筛选到的数据
     * @throws \yii\base\Exception
     */
    public function dataHandle()
    {
        $yearMonth = $this->others['yearMonth'];
        $data = CalcBonusGL::lists($this->condition, $this->params, [
            'select' => 'CGL.*',
            'from' => CalcBonusGL::tableName() . ' AS CGL',
            'join' => [
                ['LEFT JOIN', UserInfo::tableName() . ' AS UI', 'CGL.USER_ID=UI.USER_ID'],
            ],
            'orderBy' => 'CGL.CREATED_AT ASC, CGL.ID ASC',
            'yearMonth' => $yearMonth,
            'page' => $this->page,
            'pageSize' => $this->pageSize,
        ]);
        if ($data['list']) {
            foreach ($data['list'] as $key => $value) {
                $userInfo = Info::baseInfoZh($value['FROM_USER_ID']);
                $data['list'][$key] = array_merge($userInfo, $value);
                $data['list'][$key]['LAST_DEC_LV_NAME'] = Cache::getDecLevelConfig()[$value['LAST_FROM_DEC_LV']]['LEVEL_NAME'] ?? '';
                $data['list'][$key]['LAST_EMP_LV_NAME'] = Cache::getEmpLevelConfig()[$value['LAST_FROM_EMP_LV']]['LEVEL_NAME'] ?? '';
                $data['list'][$key]['LAST_STATUS_NAME'] = Tool::paramConvert(Yii::$app->params['userStatus'])[$value['LAST_FROM_STATUS']]['label'] ?? '';
            }
        }
        $this->listData = $data;

    }

    /**
     * 要展示和导出的所有字段
     * @return array
     */
    public function getColumn(){
        if(!$this->columns){
            $this->columns = [
                'PERIOD_NUM' => [
                    'header' => '结算期数',
                ],
                'USER_NAME' => [
                    'header' => '提供者编号',
                    'headerOther' => [
                        'width' => '150',
                    ],
                    'valueOther' => [
                        'tag'=>['type'=>'info', 'size' => 'small', 'class'=>'no-border']
                    ],
                ],
                'REAL_NAME' => [
                    'header' => '提供者姓名',
                    'headerOther' => [
                        'width' => '120',
                    ],
                    'valueOther' => [
                        'tag'=>['type'=>'success', 'size' => 'small', 'class'=>'no-border']
                    ],
                ],
                'LAST_STATUS_NAME' => [
                    'header' => '结算时状态',
                    'headerOther' => [
                        'width' => '110',
                    ],
                ],
                'LAST_DEC_LV_NAME' => [
                    'header' => '结算时会员级别',
                    'headerOther' => [
                        'width' => '120',
                    ],
                ],
                'LAST_EMP_LV_NAME' => [
                    'header' => '结算时会员聘级',
                    'headerOther' => [
                        'width' => '140',
                    ],
                ],
                'AMOUNT' => [
                    'header' => '管理奖金额',
                    'headerOther' => [
                        'width' => '150',
                    ],
                    'value' => function($row) {
                        return (new Price([
                            'value' => $row['AMOUNT'],
                        ]))->result();
                    },
                    'valueOther' => function($row){
                        return [
                            'tag'=>['type'=>'danger', 'size' => 'small', 'class'=>'no-border'],
                        ];
                    },
                ],
                'ORI_BONUS' => [
                    'header' => '原金额',
                    'headerOther' => [
                        'width' => '150',
                    ],
                    'value' => function($row) {
                        return (new Price([
                            'value' => $row['ORI_BONUS'],
                        ]))->result();
                    },
                    'valueOther' => function($row){
                        return [
                            'tag'=>['type'=>'danger', 'size' => 'small', 'class'=>'no-border'],
                        ];
                    },
                ],
                'RECONSUME_POINTS' => [
                    'header' => '复消积分',
                    'headerOther' => [
                        'width' => '150',
                    ],
                    'value' => function($row) {
                        return (new Price([
                            'value' => $row['RECONSUME_POINTS'],
                        ]))->result();
                    },
                    'valueOther' => function($row){
                        return [
                            'tag'=>['type'=>'danger', 'size' => 'small', 'class'=>'no-border'],
                        ];
                    },
                ],
                'MANAGE_TAX' => [
                    'header' => '管理费',
                    'headerOther' => [
                        'width' => '150',
                    ],
                    'value' => function($row) {
                        return (new Price([
                            'value' => $row['MANAGE_TAX'],
                        ]))->result();
                    },
                    'valueOther' => function($row){
                        return [
                            'tag'=>['type'=>'danger', 'size' => 'small', 'class'=>'no-border'],
                        ];
                    },
                ],
                'CALC_MONTH' => [
                    'header' => '结算月',
                    'headerOther' => [
                        'width' => '110',
                    ],
                ],
            ];
        }
        return $this->columns;
    }

    /**
     * 前台用于筛选的类型集合
     * @return mixed
     */
    public function getFilterTypes()
    {
        if(!$this->filterTypes){
            $this->filterTypes = [];
        }
        return $this->filterTypes;
    }

}