<?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\User;
use common\models\UserBind;
use common\models\UserBonus;
use common\models\UserInfo;
use common\libs\dataList\column\DateTime;
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',
            'from' => UserBonus::tableName() . ' AS UB',
            'join' => [
//                ['LEFT JOIN', UserInfo::tableName() . ' AS UI', 'UI.USER_ID=UB.USER_ID'],
                ['LEFT 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']
            ],
            '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' => 'Member Code', // 会员编号
                    'headerOther' => [
                        'width' => '150',
                    ],
                    'valueOther' => [
                        'tag'=>['type'=>'info', 'size' => 'small', 'class'=>'no-border']
                    ],
                ],
                'REAL_NAME' => [
                    'header' => 'Member Name', // 会员姓名
                    'headerOther' => [
                        'width' => '120',
                    ],
                    'valueOther' => [
                        'tag'=>['type'=>'success', 'size' => 'small', 'class'=>'no-border']
                    ],
                ],
                'IS_DEC' => [
                    'header' => 'Whether Stockist', // 是否报单中心
                    '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' => 'Member Bonus', // 会员奖金余额
                    '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' => 'Member Ecoin', // 会员余额
                    'value' => function($row) {
                        return (new Price([
                            'value' => $row['CASH'],
                        ]))->result();
                    },
                    'headerOther' => [
                        'width' => '150',
                        'prop'=>'BONUS',
                    ],
                    'valueOther' => [
                        'tag'=>['type'=>'danger', 'size' => 'small', 'class'=>'no-border'],
                    ],
                ],
                'TOURISM_POINTS' => [
                    'header' => 'Travel Incentive', // 旅游积分
                    'value' => function($row) {
                        return (new Price([
                            'value' => $row['TOURISM_POINTS'],
                        ]))->result();
                    },
                    'headerOther' => [
                        'width' => '150',
                        'prop'=>'TOURISM_POINTS',
                    ],
                    'valueOther' => [
                        'tag'=>['type'=>'danger', 'size' => 'small', 'class'=>'no-border'],
                    ],
                ],
                'GARAGE_POINTS' => [
                    'header' => 'Car Incentive', // 车积分
                    'value' => function($row) {
                        return (new Price([
                            'value' => $row['GARAGE_POINTS'],
                        ]))->result();
                    },
                    'headerOther' => [
                        'width' => '150',
                        'prop'=>'GARAGE_POINTS',
                    ],
                    'valueOther' => [
                        'tag'=>['type'=>'danger', 'size' => 'small', 'class'=>'no-border'],
                    ],
                ],
                'VILLA_POINTS' => [
                    'header' => 'Villa Incentive', // 房积分
                    'value' => function($row) {
                        return (new Price([
                            'value' => $row['VILLA_POINTS'],
                        ]))->result();
                    },
                    'headerOther' => [
                        'width' => '150',
                        'prop'=>'VILLA_POINTS',
                    ],
                    'valueOther' => [
                        'tag'=>['type'=>'danger', 'size' => 'small', 'class'=>'no-border'],
                    ],
                ],
                'PERIOD_AT' => [
                    'header' => 'Joining Period', // 加入期数
                    'headerOther' => ['width' => '110'],
                ],
                'MOBILE' => [
                    'header' => 'Phone Number', // 手机号码
                    'value' => function($row) {
                        return "\t".$row['MOBILE'];
                    },
                    'headerOther' => ['width' => '120'],
                ],
                'TEL' => [
                    'header' => 'Alternate Phone Number', // 备用手机号码
                    'value' => function($row) {
                        return "\t".$row['TEL'];
                    },
                    'headerOther' => ['width' => '180'],
                ],
            ];
        }
        return $this->columns;
    }

    /**
     * 前台用于筛选的类型集合
     * @return mixed
     */
    public function getFilterTypes()
    {
        if(!$this->filterTypes){
            $this->filterTypes = [
                'USER_NAME'=> ['isUserTable'=> false, 'name'=> 'Member Code'], // 会员编号
                'REAL_NAME'=> ['isUserTable'=> false, 'name'=> 'Member Name'], // 会员姓名
                'IS_DEC'=> ['isUserTable'=> false, 'name'=> 'Whether Stockist', 'other'=> 'yesOrNo'], // 是否报单中心
                'BONUS'=> ['isUserTable'=> false, 'name'=> 'Member Bonus'], // 会员奖金余额
                'CASH'=> ['isUserTable'=> false, 'name'=> 'Member Ecoin'], // 会员余额
                'PERIOD_AT'=> ['isUserTable'=> false, 'name'=> 'Joining Period'], // 加入期数
                'MOBILE'=> ['isUserTable'=> false, 'name'=> 'Phone Number'], // 手机号码
                'TEL'=> ['isUserTable'=> false, 'name'=> 'Alternate Phone Number'], // 备用手机号码
            ];
        }
        return $this->filterTypes;
    }
}