<?php

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

use backendApi\modules\v1\models\Admin;
use common\helpers\Cache;
use common\helpers\Tool;
use common\libs\dataList\column\DateTime;
use common\libs\dataList\column\YesNo;
use common\libs\dataList\DataListInterface;
use common\models\BalanceAudit;
use common\models\Countries;
use common\models\DealType;
use common\models\User;
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, C.NAME AS COUNTRY',
            'orderBy' => 'BA.CREATED_AT DESC, BA.ID DESC',
            'from' => BalanceAudit::tableName() . ' AS BA',
            'join' => [
                ['INNER 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'],
                ['INNER JOIN', Countries::tableName() . ' AS C', 'U.COUNTRY_ID=C.ID'],
            ],
            'page' => $this->page,
            'pageSize' => $this->pageSize,
        ]);
        $auditStatus = array_column(Tool::paramConvert(\Yii::$app->params['auditStatus']), null, 'value');
        $dealTypes = DealType::getAllTypesForShow();
        foreach ($this->listData['list'] as $key => $value) {
            $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'] = Cache::getDecRoleConfig()[$value['LAST_DEC_ROLE_ID']]['ROLE_NAME'] ?? '';
            $this->listData['list'][$key]['TYPE_NAME'] = BalanceAudit::getBalanceAuditType()[$value['TYPE']]['label'] ?? 'Others';
            $this->listData['list'][$key]['DEAL_TYPE_NAME'] = $dealTypes[$value['DEAL_TYPE']]['TYPE_NAME'] ?? 'Others';
            $this->listData['list'][$key]['AUDIT_STATUS_NAME'] = $auditStatus[$value['AUDIT_STATUS']]['label'] ?? 'Others';
        }
        $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' => Yii::t('ctx', 'modelListFinanceBalanceAuditListUserName'), // 会员编号
                    'headerOther' => ['width' => '150'],
                ],
                'REAL_NAME' => [
                    'header' => Yii::t('ctx', 'modelListFinanceBalanceAuditListRealName'), //会员姓名
                    'headerOther' => [
                        'width' => '120',
                    ],
                ],
                'COUNTRY' => [
                    'header' => Yii::t('ctx', 'country'),
                ],
                'LAST_DEC_LV_NAME' => [
                    'header' => Yii::t('ctx', 'modelListFinanceBalanceAuditListMemberLevel'), // 会员级别
                    'headerOther' => [
                        'width' => '110',
                    ],
                ],
                'LAST_DEC_ROLE_NAME' => [
                    'header' => Yii::t('ctx', 'modelListFinanceBalanceAuditListStockistLevel'), // 报单中心级别
                    'headerOther' => [
                        'width' => '170',
                    ],
                ],
                'LAST_EMP_LV_NAME' => [
                    'header' => Yii::t('ctx', 'modelListFinanceBalanceAuditListEmpLevel'),//聘级
                    'headerOther' => [
                        'width' => '130',
                    ],
                ],
                'TYPE_NAME' => [
                    'header' => Yii::t('ctx', 'modelListFinanceBalanceAuditListTypeName'), // 账户类型
                    'headerOther' => [
                        'width' => '140',
                    ],
                    'valueOther' => function ($row) {
                        return [
                            'tag' => ['type' => 'warning', 'size' => 'small', 'class' => 'no-border']
                        ];
                    },
                ],
                'DEAL_TYPE_NAME' => [
                    'header' => Yii::t('ctx', 'modelListFinanceBalanceAuditListDealType'), // 交易类型
                    'headerOther' => [
                        'width' => '200',
                    ],
                    'valueOther' => function ($row) {
                        return [
                            'tag' => ['type' => 'warning', 'size' => 'small', 'class' => 'no-border']
                        ];
                    },
                ],
                'AMOUNT' => [
                    'header' => Yii::t('ctx', 'modelListFinanceBalanceAuditListAmount'), // 调整金额
                    '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' => Yii::t('ctx', 'modelListFinanceBalanceAuditListRemark'), // 备注
                    'headerOther' => [
                        'width' => '180',
                    ],
                ],
                'REMARK_IS_SHOW' => [
                    'header' => Yii::t('ctx', 'modelListFinanceBalanceAuditListRemarkIsShow'), // 前台是否显示备注
                    'value' => function ($row) {
                        return (new YesNo([
                            'value' => $row['REMARK_IS_SHOW'],
                        ]))->result();
                    },
                    'headerOther' => [
                        'width' => '160',
                    ],
                ],
                'AUDIT_STATUS_NAME' => [
                    'header' => Yii::t('ctx', 'modelListFinanceBalanceAuditListAuditStatusName'), // 审核状态
                    'headerOther' => [
                        'width' => '100',
                    ],
                    'valueOther' => function ($row) {
                        return [
                            'tag' => ['type' => Tool::statusType($row['AUDIT_STATUS']), 'size' => 'small']
                        ];
                    },
                ],
                'CREATE_ADMIN_NAME' => [
                    'header' => Yii::t('ctx', 'modelListFinanceBalanceAuditListApplicant'), // 申请人
                    'headerOther' => [
                        'width' => '150',
                    ],
                ],
                'CREATED_AT' => [
                    'header' => Yii::t('ctx', 'modelListFinanceBalanceAuditListCreatedAt'), // 申请时间
                    'value' => function ($row) {
                        return (new DateTime([
                            'value' => $row['CREATED_AT'],
                        ]))->result();
                    },
                    'headerOther' => ['width' => '190'],
                ],
                'AUDIT_ADMIN_NAME' => [
                    'header' => Yii::t('ctx', 'modelListFinanceBalanceAuditListAuditName'), // 审核人
                    'headerOther' => [
                        'width' => '150',
                    ],
                ],
                'AUDITED_AT' => [
                    'header' => Yii::t('ctx', 'modelListFinanceBalanceAuditListAuditTime'), // 审核时间
                    'value' => function ($row) {
                        return (new DateTime([
                            'value' => $row['AUDITED_AT'],
                        ]))->result();
                    },
                    'headerOther' => ['width' => '190'],
                ],
            ];
        }
        return $this->columns;
    }

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

                //4047 (2026/3/24 16:54 Ryan)
                'COUNTRY' => [
                    'name' => Yii::t('ctx', 'country'),
                    'other'=> 'select',
                    'selectData'=> self::getCountry()
                ],
            ];
        }
        return $this->filterTypes;
    }

    public function getCountry()
    {
        $countries = Countries::getFromCache();

        $data = [];
        foreach ($countries as $country) {
            $data[] = [
                'id' => $country['ID'],
                'name' => $country['NAME'],
            ];
        }

        return $data;
    }
}