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

    /**
     * 列表筛选到的数据
     * @throws \yii\base\Exception
     */
    public function dataHandle()
    {
        $yearMonth = $this->others['yearMonth'];
//        $periodNum = $this->others['periodNum'];
        $this->listData = PerfOrder::lists($this->condition, $this->params, [
            'select' => 'PO.PERIOD_NUM,PO.USER_ID,U.USER_NAME,U.REAL_NAME,PO.LAST_STATUS,PO.LAST_DEC_LV,PO.LAST_EMP_LV,PO.DEC_TYPE,PO.DEC_AMOUNT,PO.PV,PO.CALC_MONTH',
            'from' => PerfOrder::tableName() . ' AS PO',
            'join' => [
                ['LEFT JOIN', User::tableName() . ' AS U', 'PO.USER_ID=U.ID'],
            ],
            'yearMonth' => $yearMonth,
            'orderBy' => 'U.ID ASC',
            'page' => $this->page,
            'pageSize' => $this->pageSize,
        ]);
        $decConfig = Cache::getDecLevelConfig();
        $empConfig = Cache::getEmpLevelConfig();
        if ($this->listData['list']) {
            foreach ($this->listData['list'] as $key => $value) {
                $this->listData['list'][$key]['LAST_DEC_LV_NAME'] = $decConfig[$value['LAST_DEC_LV']]['LEVEL_NAME'] ?? '';
                $this->listData['list'][$key]['LAST_EMP_LV_NAME'] = $empConfig[$value['LAST_EMP_LV']]['LEVEL_NAME'] ?? '';
                $this->listData['list'][$key]['LAST_STATUS_NAME'] = Tool::paramConvert(Yii::$app->params['userStatus'])[$value['LAST_STATUS']]['label'] ?? '';
            }
        }
    }

    /**
     * 要展示和导出的所有字段
     * @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']
                    ],
                ],
                'LAST_STATUS_NAME' => [
                    'header' => '结算时会员状态',
                    'headerOther' => [
                        'width' => '150',
                    ],
                ],
                'LAST_DEC_LV_NAME' => [
                    'header' => '结算时会员级别',
                    'headerOther' => [
                        'width' => '120',
                    ],
                ],
                'LAST_EMP_LV_NAME' => [
                    'header' => '结算时会员聘级',
                    'headerOther' => [
                        'width' => '140',
                    ],
                ],
//                'LOCATION' => [
//                    'header' => '市场',
//                    'headerOther' => [
//                        'width' => '100',
//                    ],
//                ],
                'DEC_TYPE' => [
                    'header' => '报单类型',
                    'headerOther' => [
                        'width' => '110',
                    ],
                    'value' => function($row) {
                        $decType = [
                            'ZC'=> '首购单',
                            'YH'=> '优惠单',
                            'ZG'=> '增购单',
                            'LS'=> '零售单',
                            'FX'=> '复销单',
                        ];
                        return $decType[$row['DEC_TYPE']];
                    },
                ],
                'DEC_AMOUNT' => [
                    'header' => '报单金额',
                    'value' => function($row) {
                        return (new Price([
                            'value' => $row['DEC_AMOUNT'],
                        ]))->result();
                    },
                    'headerOther' => [
                        'width' => '150',
                        'prop'=>'DEC_AMOUNT',
                    ],
                ],
                'PV' => [
                    'header' => '报单BV',
                    'value' => function($row) {
                        return (new Price([
                            'value' => $row['PV'],
                        ]))->result();
                    },
                    'headerOther' => [
                        'width' => '150',
                        'prop'=>'PV',
                    ],
                ],
                'CALC_MONTH' => [
                    'header' => '结算月',
                    'headerOther' => [
                        'width' => '110',
                    ],
                ],
            ];
        }
        return $this->columns;
    }

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