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

use backendApi\modules\v1\models\Admin;
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\Countries;
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\UserPerformance;
use common\models\UserWallet;
use common\models\Withdraw;
use Yii;

class BalanceList extends \common\libs\dataList\DataList implements DataListInterface
{
    /**
     * 列表名称
     * @return string
     */
    public function getListName(){
        return '会员奖金余额列表';
    }

    /**
     * 列表筛选到的数据
     * @throws \yii\base\Exception
     */
    public function dataHandle()
    {
        $this->condition .= ' AND UI.STATUS=1 AND I.LAST_LOGIN_AT>=1620748800';
        $this->listData = UserBonus::lists($this->condition, $this->params, [
            'select' => 'UB.*,UI.*,UW.CASH, UP.AMOUNTS AS PRP, C.NAME AS COUNTRY',
            'from' => UserBonus::tableName() . ' AS UB',
            'join' => [
                ['INNER JOIN', User::tableName() . ' AS UI', 'UI.ID=UB.USER_ID'],
                ['LEFT JOIN', UserWallet::tableName() . ' AS UW', 'UW.USER_ID=UB.USER_ID'],
                ['LEFT JOIN', UserInfo::tableName() . ' AS I', 'I.USER_ID=UB.USER_ID'],
                ['LEFT JOIN', "(SELECT USER_ID,SUM(AMOUNTS) AS AMOUNTS FROM AR_USER_PERFORMANCE WHERE STATUS_ID<30 GROUP BY USER_ID) UP", 'UI.ID=UP.USER_ID'],
                ['INNER JOIN', Countries::tableName() . ' AS C', 'UI.COUNTRY_ID=C.ID'],
            ],
            'orderBy' => 'UB.CREATED_AT ASC, UB.ID ASC',
            'page' => $this->page,
            'pageSize' => $this->pageSize,
        ]);
    }

    /**
     * 要展示和导出的所有字段
     * @return array
     */
    public function getColumn(){
        if(!$this->columns){
            $this->columns = [
                'USER_ID' => null, // 这种传输方式主要是用于索引，因为过滤后的字段可能没有这种ID，但是一些功能的操作还需要用这种ID去关联，例如前台会员列表中的勾选批量状态管理，这里需要的就是USER_ID
                'USER_NAME' => [
                    'header' => \Yii::t('ctx', 'memberCode'), // 会员编号
                    'headerOther' => [
                        'width' => '150',
                    ],
                    'valueOther' => [
                        'tag'=>['type'=>'info', 'size' => 'small', 'class'=>'no-border']
                    ],
                ],
                'REAL_NAME' => [
                    'header' => \Yii::t('ctx', 'memberName'), // 会员姓名
                    'headerOther' => [
                        'width' => '250',
                    ],
                    'valueOther' => [
                        'tag'=>['type'=>'success', 'size' => 'small', 'class'=>'no-border']
                    ],
                ],
                'COUNTRY' => [
                    'header' => Yii::t('ctx', 'country'),
                ],
                'IS_DEC' => [
                    'header' => \Yii::t('ctx', 'whetherStockist'), // 是否报单中心
                    'value' => function($row) {
                        return (new YesNo([
                            'value' => $row['IS_DEC'],
                        ]))->result();
                    },
                    'headerOther' => function($row) {
                        return [
                            'width' => '200',
                        ];
                    },
                    'valueOther' => function($row) {
                        return [
                            'tag'=>['type'=>(isset($row['IS_DEC']) && $row['IS_DEC'] )? 'success' : 'info', 'size' => 'small']
                        ];
                    },
                ],
                'BONUS' => [
                    'header' => \Yii::t('ctx', 'memberBonus'), // 会员奖金余额
                    'value' => function($row) {
                        return (new Price([
                            'value' => $row['BONUS'],
                        ]))->result();
                    },
                    'headerOther' => [
                        'width' => '150',
                        'prop'=>'BONUS',
                    ],
                    'valueOther' => [
                        'tag'=>['type'=>'danger', 'size' => 'small', 'class'=>'no-border'],
                    ],
                ],
                'CASH' => [
                    'header' => \Yii::t('ctx', 'memberEcoin'), // 会员余额
                    'value' => function($row) {
                        return (new Price([
                            'value' => $row['CASH'],
                        ]))->result();
                    },
                    'headerOther' => [
                        'width' => '150',
                        'prop'=>'BONUS',
                    ],
                    'valueOther' => [
                        'tag'=>['type'=>'danger', 'size' => 'small', 'class'=>'no-border'],
                    ],
                ],
                'USER_PERFORMANCE_BONUS' => [
                    'header' => \Yii::t('ctx', 'userPerformance'), // 绩效奖金
                    'value' => function($row) {
                        return (new Price([
                            'value' => $row['PRP'],
                        ]))->result();
                    },
                    'headerOther' => [
                        'width' => '150',
                        'prop'=>'USER_PERFORMANCE_BONUS',
                    ],
                    'valueOther' => [
                        'tag'=>['type'=>'danger', 'size' => 'small', 'class'=>'no-border'],
                    ],
                ],
            ];
        }
        return $this->columns;
    }

    /**
     * 前台用于筛选的类型集合
     * @return mixed
     */
    public function getFilterTypes()
    {
        if(!$this->filterTypes){
            $this->filterTypes = [
                'USER_NAME'=> ['isUserTable'=> false, 'name'=> \Yii::t('ctx', 'memberCode')], // 会员编号
                'REAL_NAME'=> ['isUserTable'=> false, 'name'=> \Yii::t('ctx', 'memberName')], // 会员姓名
                'IS_DEC'=> ['isUserTable'=> false, 'name'=> \Yii::t('ctx', 'whetherStockist'), 'other'=> 'yesOrNo'], // 是否报单中心
            ];
        }
        return $this->filterTypes;
    }
}
