<?php

namespace backendApi\modules\v1\models\lists\bonus;

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\Countries;
use common\models\DealType;
use common\models\FlowBonus;
use common\libs\dataList\column\DateTime;
use common\models\User;
use Yii;

class FlowBonusList extends \common\libs\dataList\DataList implements DataListInterface {
    /**
     * 列表名称
     * @return string
     */
    public function getListName() {
        return '奖金流水';
    }

    /**
     * 列表筛选到的数据
     * @throws \yii\base\Exception
     */
    public function dataHandle() {
        $this->listData = FlowBonus::lists($this->condition, $this->params, [
            'select' => 'FB.*,U.USER_NAME,U.REAL_NAME,U.IS_DEC, C.NAME AS COUNTRY',
            'orderBy' => 'FB.CREATED_AT DESC,FB.SORT DESC,FB.AUTO_SORT DESC,FB.ID DESC',
            'from' => FlowBonus::tableName() . ' AS FB',
            'join' => [
                ['INNER JOIN', User::tableName() . ' AS U', 'FB.USER_ID=U.ID'],
                ['INNER JOIN', Countries::tableName() . ' AS C', 'U.COUNTRY_ID=C.ID'],
            ],
            'page' => $this->page,
            'pageSize' => $this->pageSize,
        ]);
        foreach ($this->listData['list'] as $key => $value) {
            $this->listData['list'][$key]['DEAL_TYPE_NAME'] = DealType::getAllTypesForShow()[$value['DEAL_TYPE_ID']]['TYPE_NAME'] ?? '';
            $this->listData['list'][$key]['LAST_DEC_LV_NAME'] = Cache::getDecLevelConfig()[$value['LAST_DEC_LV']]['LEVEL_NAME'] ?? '';
            $this->listData['list'][$key]['LAST_EMP_LV_NAME'] = Cache::getEmpLevelConfig()[$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 = [
                'ID' => null,
                'CREATED_AT' => [
                    'header' => Yii::t('ctx', 'createAt'), // 创建时间
                    'value' => function ($row) {
                        return (new DateTime([
                            'value' => $row['CREATED_AT'],
                        ]))->result();
                    },
                    'headerOther' => ['width' => '170'],
                ],
                'PERIOD_NUM' => [
                    'header' => Yii::t('ctx', 'periodNumber'), // 期数
                    'headerOther' => [
                        'width' => '120',
                    ],
                ],
                'CALC_MONTH' => [
                    'header' => Yii::t('ctx', 'bonusMonth'), // 结算月
                    'headerOther' => [
                        'width' => '120',
                    ],
                ],
                'DEAL_TYPE_NAME' => [
                    'header' => Yii::t('ctx', 'transactionType'), // 交易类型
                    'headerOther' => [
                        'width' => '150',
                    ],
                ],
                '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' => '120',
                    ],
                    'valueOther' => [
                        'tag' => ['type' => 'success', 'size' => 'small', 'class' => 'no-border']
                    ],
                ],
                'COUNTRY' => [
                    'header' => Yii::t('ctx', 'country'),
                ],
                'LAST_DEC_LV_NAME' => [
                    'header' => Yii::t('ctx', 'memberLevel'), // 会员级别
                    'headerOther' => [
                        'width' => '110',
                    ],
                    'valueOther' => [
                        'tag' => ['type' => 'warning', 'size' => 'small', 'class' => 'no-border']
                    ],
                ],
                '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']
                        ];
                    },
                ],
                'BEFORE_BALANCE' => [
                    'header' => Yii::t('ctx', 'balanceBeforeTransaction'), // 交易前余额
                    'value' => function ($row) {
                        return (new Price([
                            'value' => $row['TOTAL'] - $row['AMOUNT'],
                        ]))->result();
                    },
                    'headerOther' => [
                        'width' => '200',
                    ],
                ],
                'AMOUNT' => [
                    'header' => Yii::t('ctx', 'debitCredit'), // 交易额
                    'value' => function ($row) {
                        $result = (new Price([
                            'value' => $row['AMOUNT'],
                        ]))->result();
                        return $row['IS_INCR'] > 0 ? '+' . $result : $result;
                    },
                    'headerOther' => [
                        'width' => '150',
                        'prop' => 'BONUS',
                    ],
                    'valueOther' => function ($row) {
                        if ($row['IS_INCR'] > 0) {
                            $type = 'success';
                        } elseif ($row['IS_INCR'] == 0) {
                            $type = 'danger';
                        } else {
                            $type = 'info';
                        }
                        return [
                            'tag' => ['type' => $type, 'size' => 'small']
                        ];
                    },
                ],
                'TOTAL' => [
                    'header' => Yii::t('ctx', 'newBalance'), // 交易后余额
                    'value' => function ($row) {
                        return (new Price([
                            'value' => $row['TOTAL'],
                        ]))->result();
                    },
                    'headerOther' => [
                        'width' => '150',
                    ],
                ],
                'ADMIN_NAME' => [
                    'header' => Yii::t('ctx', 'operator'), // 操作人
                    'headerOther' => ['width' => '110'],
                ],
                'REMARK' => [
                    'header' => Yii::t('ctx', 'remarks'), // 备注
                    'headerOther' => ['width' => '250'],
                ],
                'TRANSFER_SN' => [
                    'header' => Yii::t('ctx', 'serialNumber'), // 流水号
                    'headerOther' => ['width' => '250'],
                ],
            ];
        }
        return $this->columns;
    }

    /**
     * 前台用于筛选的类型集合
     * @return mixed
     */
    public function getFilterTypes() {
        if (!$this->filterTypes) {
            $this->filterTypes = [
                'CREATED_AT' => ['isUserTable' => false, 'name' => Yii::t('ctx', 'createAt'), 'other' => 'date'], // 创建时间
                'PERIOD_NUM' => ['isUserTable' => false, 'name' => Yii::t('ctx', 'periodNumber')], // 期数
                'CALC_MONTH' => ['isUserTable' => false, 'name' => Yii::t('ctx', 'bonusMonth'), 'other' => 'month'], // 结算月
                'DEAL_TYPE_NAME' => ['isUserTable' => false, 'name' => Yii::t('ctx', 'transactionType'), 'other' => 'dealTypes'], // 交易类型
                'USER_NAME' => ['isUserTable' => false, 'name' => Yii::t('ctx', 'memberCode')], // 会员编号
                'REAL_NAME' => ['isUserTable' => false, 'name' => Yii::t('ctx', 'memberName')], // 会员姓名
                'LAST_DEC_LV_NAME' => ['isUserTable' => false, 'name' => Yii::t('ctx', 'memberLevel'), 'other' => 'decLevel'], // 会员等级
                'LAST_EMP_LV_NAME' => ['isUserTable' => false, 'name' => Yii::t('ctx', 'rank'), 'other' => 'empLevel'], // 聘级
                'IS_DEC' => ['isUserTable' => false, 'name' => Yii::t('ctx', 'whetherStockist'), 'other' => 'yesOrNo'], // 是否报单中心
                'AMOUNT' => ['isUserTable' => false, 'name' => Yii::t('ctx', 'debitCredit')], // 交易额
                'TOTAL' => ['isUserTable' => false, 'name' => Yii::t('ctx', 'newBalance')], // 交易后余额
                'ADMIN_NAME' => ['isUserTable' => false, 'name' => Yii::t('ctx', 'operator')], // 操作人
                'REMARK' => ['isUserTable' => false, 'name' => Yii::t('ctx', 'remarks')], // 备注
                'TRANSFER_SN' => ['isUserTable' => false, 'name' => Yii::t('ctx', 'serialNumber')], // 流水号
            ];
        }
        return $this->filterTypes;
    }
}
