<?php

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

use backendApi\modules\v1\models\Admin;
use common\helpers\Audit;
use common\helpers\Cache;
use common\helpers\Tool;
use common\helpers\user\Info;
use common\helpers\user\Reconsume;
use common\libs\dataList\column\DateTime;
use common\libs\dataList\column\YesNo;
use common\libs\dataList\DataListInterface;
use common\models\BalanceAudit;
use common\models\DealType;
use common\models\forms\ReconsumeForm;
use common\models\ReconsumeAudit;
use common\models\ReconsumePool;
use common\models\DecRole;
use common\models\User;
use common\models\UserInfo;
use common\models\YearHighestEmpLv;
use Yii;

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

    /**
     * 列表筛选到的数据
     * @throws \yii\base\Exception
     */
    public function dataHandle() {
        $this->listData = BalanceAudit::lists($this->condition, $this->params, [
            'select' => 'BA.*,U.USER_NAME,U.REAL_NAME,ADMC.ADMIN_NAME CREATE_ADMIN_NAME,ADMU.ADMIN_NAME AUDIT_ADMIN_NAME',
            'orderBy' => 'BA.CREATED_AT DESC, BA.ID DESC',
            'from' => BalanceAudit::tableName() . ' AS BA',
            'join' => [
                ['LEFT JOIN', User::tableName() . ' AS U', 'BA.USER_ID=U.ID'],
                ['LEFT JOIN', Admin::tableName() . ' AS ADMC', 'BA.UPDATE_ADMIN_ID=ADMC.ID'],
                ['LEFT JOIN', Admin::tableName() . ' AS ADMU', 'BA.AUDIT_ADMIN_ID=ADMU.ID'],
            ],
            'page' => $this->page,
            'pageSize' => $this->pageSize,
        ]);
        $auditStatus = array_column(\Yii::$app->params['auditStatus'], null, 'value');
        $dealTypes = DealType::getAllTypesForShow();
        foreach ($this->listData['list'] as $key => $value) {
//            $userInfo = Info::baseInfoZh($value['USER_ID']);
//            $this->listData['list'][$key]['USER_NAME'] = $userInfo['USER_NAME'];
//            $this->listData['list'][$key]['REAL_NAME'] = $userInfo['REAL_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_DEC_ROLE_NAME'] = DecRole::getRoleNameId($value['LAST_DEC_ROLE_ID']);
            $this->listData['list'][$key]['TYPE_NAME'] = BalanceAudit::TYPE[$value['TYPE']]['label'];
            $this->listData['list'][$key]['DEAL_TYPE_NAME'] = $dealTypes[$value['DEAL_TYPE']]['TYPE_NAME'];
            $this->listData['list'][$key]['AUDIT_STATUS_NAME'] = $auditStatus[$value['AUDIT_STATUS']]['label'];
        }
        $this->listData['dealTypes'] = $dealTypes;
    }

    /**
     * 要展示和导出的所有字段
     * @return array
     */
    public function getColumn() {
        if (!$this->columns) {
            $this->columns = [
                'ID' => null, // 这种传输方式主要是用于索引，因为过滤后的字段可能没有这种ID，但是一些功能的操作还需要用这种ID去关联，例如前台会员列表中的勾选批量状态管理，这里需要的就是USER_ID
                'AUDIT_STATUS' => null,
                'USER_NAME' => [
                    'header' => '会员编号',
                    'headerOther' => ['width' => '150'],
                ],
                'REAL_NAME' => [
                    'header' => '会员姓名',
                    'headerOther' => [
                        'width' => '120',
                    ],
                ],
                'LAST_DEC_LV_NAME' => [
                    'header' => '会员级别',
                    'headerOther' => [
                        'width' => '110',
                    ],
                ],
                'LAST_DEC_ROLE_NAME' => [
                    'header' => '报单中心级别',
                    'headerOther' => [
                        'width' => '110',
                    ],
                ],
                'LAST_EMP_LV_NAME' => [
                    'header' => '聘级',
                    'headerOther' => [
                        'width' => '130',
                    ],
                ],
                'TYPE_NAME' => [
                    'header' => '账户类型',
                    'headerOther' => [
                        'width' => '140',
                    ],
                    'valueOther' => function ($row) {
                        return [
                            'tag' => ['type' => 'warning', 'size' => 'small', 'class' => 'no-border']
                        ];
                    },
                ],
                'DEAL_TYPE_NAME' => [
                    'header' => '交易类型',
                    'headerOther' => [
                        'width' => '200',
                    ],
                    'valueOther' => function ($row) {
                        return [
                            'tag' => ['type' => 'warning', 'size' => 'small', 'class' => 'no-border']
                        ];
                    },
                ],
                'AMOUNT' => [
                    'header' => '调整金额',
                    'headerOther' => [
                        'width' => '150',
                    ],
                    'value' => function ($row) {
                        return Tool::formatPrice($row['AMOUNT']);
                    },
                    'valueOther' => function ($row) {
                        return [
                            'tag' => ['type' => 'danger', 'size' => 'small', 'class' => 'no-border']
                        ];
                    },
                ],
                'CREATE_REMARK' => [
                    'header' => '创建备注',
                    'headerOther' => [
                        'width' => '180',
                    ],
                ],
                'REMARK_IS_SHOW' => [
                    'header' => '前台是否显示创建备注',
                    'value' => function ($row) {
                        return (new YesNo([
                            'value' => $row['REMARK_IS_SHOW'],
                        ]))->result();
                    },
                    'headerOther' => [
                        'width' => '160',
                    ],
                ],
                'AUDIT_STATUS_NAME' => [
                    'header' => '审核状态',
                    'headerOther' => [
                        'width' => '100',
                    ],
                    'valueOther' => function ($row) {
                        return [
                            'tag' => ['type' => Tool::statusType($row['AUDIT_STATUS']), 'size' => 'small']
                        ];
                    },
                ],
                'CREATE_ADMIN_NAME' => [
                    'header' => '申请人',
                    'headerOther' => [
                        'width' => '150',
                    ],
                ],
                'CREATED_AT' => [
                    'header' => '申请时间',
                    'value' => function ($row) {
                        return (new DateTime([
                            'value' => $row['CREATED_AT'],
                        ]))->result();
                    },
                    'headerOther' => ['width' => '190'],
                ],
                'AUDIT_ADMIN_NAME' => [
                    'header' => '审核人',
                    'headerOther' => [
                        'width' => '150',
                    ],
                ],
                'AUDITED_AT' => [
                    'header' => '审核时间',
                    'value' => function ($row) {
                        return (new DateTime([
                            'value' => $row['AUDITED_AT'],
                        ]))->result();
                    },
                    'headerOther' => ['width' => '190'],
                ],
                'AUDIT_REMARK' => [
                    'header' => '审核备注',
                    'headerOther' => [
                        'width' => '180',
                    ],
                ],
            ];
        }
        return $this->columns;
    }

    /**
     * 前台用于筛选的类型集合
     * @return mixed
     */
    public function getFilterTypes() {
        if (!$this->filterTypes) {
            $this->filterTypes = [
                'USER_NAME' => ['isUserTable' => false, 'name' => '会员编号'],
                'REAL_NAME' => ['isUserTable' => false, 'name' => '会员姓名'],
                'LAST_DEC_LV_NAME' => ['isUserTable' => false, 'name' => '会员级别', 'other' => 'decLevel'],
                'LAST_DEC_ROLE_NAME' => ['isUserTable' => false, 'name' => '报单中心级别', 'other' => 'decRole'],
                'LAST_EMP_LV_NAME' => ['isUserTable' => false, 'name' => '聘级', 'other' => 'empLevel'],
                'TYPE_NAME' => ['isUserTable' => false, 'name' => '账户类型', 'other' => 'select', 'selectData' => Tool::formatFilter(BalanceAudit::TYPE, 'name', 'label')],
                'DEAL_TYPE_NAME' => ['isUserTable' => false, 'name' => '交易类型', 'other' => 'select', 'selectData' => Tool::formatFilter(DealType::getAllTypesForShow(), 'ID', 'TYPE_NAME')],
                'AMOUNT' => ['isUserTable' => false, 'name' => '调整金额'],
                'CREATE_REMARK' => ['isUserTable' => false, 'name' => '备注'],
                'REMARK_IS_SHOW' => ['isUserTable' => false, 'name' => '前台是否显示备注', 'other' => 'yesOrNo'],
                'CREATE_ADMIN_NAME' => ['isUserTable' => false, 'name' => '申请人'],
                'CREATED_AT' => ['isUserTable' => false, 'name' => '申请时间', 'other' => 'date'],
                'AUDIT_ADMIN_NAME' => ['isUserTable' => false, 'name' => '审核人'],
                'AUDITED_AT' => ['isUserTable' => false, 'name' => '审核时间', 'other' => 'date'],
            ];
        }
        return $this->filterTypes;
    }
}