<?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\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 TraceUpQyList extends \common\libs\dataList\DataList implements DataListInterface
{
    /**
     * 列表名称
     * @return string
     */
    public function getListName(){
        return '团队奖向上追溯';
    }

    /**
     * 列表筛选到的数据
     * @throws \yii\base\Exception
     */
    public function dataHandle()
    {
        $userId = $this->others['userId'];
        $periodNum = $this->others['periodNum'];
        $data = UserNetwork::getAllParentFromPeriodWithPage($userId, $periodNum);
        if (isset($data['list']) && $data['list']) {
            $decTotal = CalcCache::nowPeriodPerf($userId, $periodNum);
            foreach ($data['list'] as $key => $value) {
                $perfPeriod = PerfPeriod::findOneAsArray('USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM',[':USER_ID'=>$value['USER_ID'],':PERIOD_NUM'=>$periodNum]);
                $data['list'][$key]['PERIOD_NUM'] = $perfPeriod['PERIOD_NUM'];
                $data['list'][$key]['DEC_TOTAL'] = $decTotal['PV_PCS_ZC'] + $decTotal['PV_PCS_ZG'] +$decTotal['PV_LS_TOUCH'];
                $data['list'][$key]['USER_NAME'] = Info::getUserNameByUserId($value['USER_ID']);
                $data['list'][$key]['REAL_NAME'] = Info::getUserRealNameByUserId($value['USER_ID']);
                $data['list'][$key]['LAST_DEC_LV_NAME'] = Cache::getDecLevelConfig()[$perfPeriod['LAST_DEC_LV']]['LEVEL_NAME'] ?? '';
                $data['list'][$key]['LAST_EMP_LV_NAME'] = Cache::getEmpLevelConfig()[$perfPeriod['LAST_EMP_LV']]['LEVEL_NAME'] ?? '';
                $data['list'][$key]['LAST_STATUS_NAME'] = Tool::paramConvert(Yii::$app->params['userStatus'])[$perfPeriod['LAST_STATUS']]['label'] ?? '';
                $data['list'][$key]['PV_1L_TOUCH'] = $perfPeriod['PV_1L_TOUCH'];
                $data['list'][$key]['PV_2L_TOUCH'] = $perfPeriod['PV_2L_TOUCH'];
                $data['list'][$key]['PV_3L_TOUCH'] = $perfPeriod['PV_3L_TOUCH'];
                $data['list'][$key]['PV_4L_TOUCH'] = $perfPeriod['PV_4L_TOUCH'];
                $data['list'][$key]['PV_5L_TOUCH'] = $perfPeriod['PV_5L_TOUCH'];
                $data['list'][$key]['SURPLUS_1L'] = $perfPeriod['SURPLUS_1L'];
                $data['list'][$key]['SURPLUS_2L'] = $perfPeriod['SURPLUS_2L'];
                $data['list'][$key]['SURPLUS_3L'] = $perfPeriod['SURPLUS_3L'];
                $data['list'][$key]['SURPLUS_4L'] = $perfPeriod['SURPLUS_4L'];
                $data['list'][$key]['SURPLUS_5L'] = $perfPeriod['SURPLUS_5L'];
                $data['list'][$key]['CALC_MONTH'] = $perfPeriod['CALC_MONTH'];
                //区域业绩
                $pervSurplusPerf = CalcCache::surplusPerf($value['USER_ID'], $periodNum);
                $data['list'][$key]['QY_1L'] = $data['list'][$key]['PV_1L_TOUCH'] + $pervSurplusPerf['SURPLUS_1L'];
                $data['list'][$key]['QY_2L'] = $data['list'][$key]['PV_2L_TOUCH'] + $pervSurplusPerf['SURPLUS_2L'];
                $data['list'][$key]['QY_3L'] = $data['list'][$key]['PV_3L_TOUCH'] + $pervSurplusPerf['SURPLUS_3L'];
                $data['list'][$key]['QY_4L'] = $data['list'][$key]['PV_4L_TOUCH'] + $pervSurplusPerf['SURPLUS_4L'];
                $data['list'][$key]['QY_5L'] = $data['list'][$key]['PV_5L_TOUCH'] + $pervSurplusPerf['SURPLUS_5L'];
                unset($bonus,$bigLocation);
            }
        } else {
            $data['list'] = [];
        }
        $this->listData = $data;
    }

    /**
     * 要展示和导出的所有字段
     * @return array
     */
    public function getColumn(){
        if(!$this->columns){
            $this->columns = [
                'PERIOD_NUM' => [
                    'header' => '期数',
                    'headerOther' => [
                        'width' => '150',
                    ],
                ],
                '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']
                    ],
                ],
                'LOCATION' => [
                    'header' => '市场',
                    'headerOther' => [
                        'width' => '100',
                    ],
                ],
                'LAST_STATUS_NAME' => [
                    'header' => '结算时状态',
                    'headerOther' => [
                        'width' => '110',
                    ],
                ],
                'LAST_DEC_LV_NAME' => [
                    'header' => '结算时会员级别',
                    'headerOther' => [
                        'width' => '120',
                    ],
                ],
                'LAST_EMP_LV_NAME' => [
                    'header' => '结算时会员聘级',
                    'headerOther' => [
                        'width' => '140',
                    ],
                ],
                'DEC_TOTAL' => [
                    'header' => '报单总积分',
                    'headerOther' => [
                        'width' => '110',
                    ],
                    'value' => function($row) {
                        return (new Price([
                            'value' => $row['DEC_TOTAL'],
                        ]))->result();
                    },
                ],
                'PV_1L_TOUCH' => [
                    'header' => '一市场新增业绩',
                    'headerOther' => [
                        'width' => '150',
                    ],
                    'value' => function($row) {
                        return (new Price([
                            'value' => $row['PV_1L_TOUCH'],
                        ]))->result();
                    },
                    'valueOther' => [
                        'tag'=>['type'=>'info', 'size' => 'small', 'class'=>'no-border'],
                    ]
                ],
                'QY_1L' => [
                    'header' => '一市场区域业绩',
                    'headerOther' => [
                        'width' => '150',
                    ],
                    'value' => function($row) {
                        return (new Price([
                            'value' => $row['QY_1L'],
                        ]))->result();
                    },
                    'valueOther' => [
                        'tag'=>['type'=>'info', 'size' => 'small', 'class'=>'no-border'],
                    ]
                ],
                'SURPLUS_1L' => [
                    'header' => '一市场结余业绩',
                    'headerOther' => [
                        'width' => '150',
                    ],
                    'value' => function($row) {
                        return (new Price([
                            'value' => $row['SURPLUS_1L'],
                        ]))->result();
                    },
                    'valueOther' => [
                        'tag'=>['type'=>'info', 'size' => 'small', 'class'=>'no-border'],
                    ]
                ],
                'PV_2L_TOUCH' => [
                    'header' => '二市场新增业绩',
                    'headerOther' => [
                        'width' => '150',
                    ],
                    'value' => function($row) {
                        return (new Price([
                            'value' => $row['PV_2L_TOUCH'],
                        ]))->result();
                    },
                    'valueOther' => [
                        'tag'=>['type'=>'info', 'size' => 'small', 'class'=>'no-border'],
                    ]
                ],
                'QY_2L' => [
                    'header' => '二市场区域业绩',
                    'headerOther' => [
                        'width' => '150',
                    ],
                    'value' => function($row) {
                        return (new Price([
                            'value' => $row['QY_2L'],
                        ]))->result();
                    },
                    'valueOther' => [
                        'tag'=>['type'=>'info', 'size' => 'small', 'class'=>'no-border'],
                    ]
                ],
                'SURPLUS_2L' => [
                    'header' => '二市场结余业绩',
                    'headerOther' => [
                        'width' => '150',
                    ],
                    'value' => function($row) {
                        return (new Price([
                            'value' => $row['SURPLUS_2L'],
                        ]))->result();
                    },
                    'valueOther' => [
                        'tag'=>['type'=>'info', 'size' => 'small', 'class'=>'no-border'],
                    ]
                ],
                'PV_3L_TOUCH' => [
                    'header' => '三市场新增业绩',
                    'headerOther' => [
                        'width' => '150',
                    ],
                    'value' => function($row) {
                        return (new Price([
                            'value' => $row['PV_3L_TOUCH'],
                        ]))->result();
                    },
                    'valueOther' => [
                        'tag'=>['type'=>'info', 'size' => 'small', 'class'=>'no-border'],
                    ]
                ],
                'QY_3L' => [
                    'header' => '三市场区域业绩',
                    'headerOther' => [
                        'width' => '150',
                    ],
                    'value' => function($row) {
                        return (new Price([
                            'value' => $row['QY_3L'],
                        ]))->result();
                    },
                    'valueOther' => [
                        'tag'=>['type'=>'info', 'size' => 'small', 'class'=>'no-border'],
                    ]
                ],
                'SURPLUS_3L' => [
                    'header' => '三市场结余业绩',
                    'headerOther' => [
                        'width' => '150',
                    ],
                    'value' => function($row) {
                        return (new Price([
                            'value' => $row['SURPLUS_3L'],
                        ]))->result();
                    },
                    'valueOther' => [
                        'tag'=>['type'=>'info', '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;
    }

    /**
     * 校验大区
     * @param $big
     * @param $loc
     * @param $num
     * @return bool
     */
    private function _chkBig($big, $loc, $num){
        return $loc == $num && $big != $loc;
    }
}