<?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\Countries;
use common\models\DealType;
use common\models\forms\ReconsumeForm;
use common\models\PerfAudit;
use common\models\ReconsumeAudit;
use common\models\ReconsumePool;
use common\models\DecRole;
use common\models\Transfer;
use common\models\User;
use common\models\UserInfo;
use common\models\UserSystem;
use common\models\YearHighestEmpLv;
use Yii;

class TransferList extends \common\libs\dataList\DataList implements DataListInterface {
    /**
     * 列表名称
     * @return string
     */
    public function getListName() {
        return '转账记录列表';
    }

    /**
     * 列表筛选到的数据
     * @throws \yii\base\Exception
     */
    public function dataHandle() {
        $this->listData = Transfer::lists($this->condition, $this->params, [
            'select' => 'BA.*, C.NAME AS COUNTRY',
            'orderBy' => 'BA.CREATED_AT DESC, BA.ID DESC',
            'from' => Transfer::tableName() . ' AS BA',
            'join' => [
                ['INNER JOIN', User::tableName() . ' AS U', 'BA.OUT_UID=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]['LAST_OUT_DEC_LV_NAME'] = Cache::getDecLevelConfig()[$value['LAST_OUT_DEC_LV']]['LEVEL_NAME'] ?? '';;
            $this->listData['list'][$key]['LAST_IN_DEC_LV_NAME'] = Cache::getDecLevelConfig()[$value['LAST_IN_DEC_LV']]['LEVEL_NAME'] ?? '';
        }
    }

    /**
     * 要展示和导出的所有字段
     * @return array
     */
    public function getColumn() {
        if (!$this->columns) {
            $this->columns = [
                'ID' => null, // 这种传输方式主要是用于索引，因为过滤后的字段可能没有这种ID，但是一些功能的操作还需要用这种ID去关联，例如前台会员列表中的勾选批量状态管理，这里需要的就是USER_ID
                'TRANSFER_SN' => [
                    'header' => Yii::t('ctx', 'modelListFinanceTranserListTransferSn'), // 转账记录流水号
                    'headerOther' => [
                        'width' => '250',
                    ],
                    'valueOther' => function ($row) {
                        return [
                            'tag' => ['type' => 'warning', 'size' => 'small', 'class' => 'no-border']
                        ];
                    },
                ],
                'LAST_OUT_USER_NAME' => [
                    'header' => Yii::t('ctx', 'modelListFinanceTranserListLastOutUserName'), // 转出会员编号
                    'headerOther' => ['width' => '150'],
                ],
                'LAST_OUT_REAL_NAME' => [
                    'header' => Yii::t('ctx', 'modelListFinanceTranserListLastOutRealName'), // 转出会员姓名
                    'headerOther' => [
                        'width' => '120',
                    ],
                ],
                'COUNTRY' => [
                    'header' => Yii::t('ctx', 'country'),
                ],
                'LAST_OUT_DEC_LV_NAME' => [
                    'header' => Yii::t('ctx', 'modelListFinanceTranserListLastOutDecLvName'), // 转出主体会员级别
                    'headerOther' => [
                        'width' => '150',
                    ],
                ],
                'OUT_WALLET' => [
                    'header' => Yii::t('ctx', 'modelListFinanceTranserListOutWallet'), //转出账户
                    'value' => function ($row) {
                        return Transfer::WALLET_NAME[$row['OUT_WALLET']];
                    },
                    'headerOther' => [
                        'width' => '150',
                    ],
                ],
                'LAST_IN_USER_NAME' => [
                    'header' => Yii::t('ctx', 'modelListFinanceTranserListLastInUserName'), // 转入会员编号
                    'headerOther' => ['width' => '150'],
                ],
                'LAST_IN_REAL_NAME' => [
                    'header' => Yii::t('ctx', 'modelListFinanceTranserListLastInRealName'), // 转入会员姓名
                    'headerOther' => [
                        'width' => '120',
                    ],
                ],
                'LAST_IN_DEC_LV_NAME' => [
                    'header' => Yii::t('ctx', 'modelListFinanceTranserListLastInDecLvName'),//转入主体级别
                    'headerOther' => [
                        'width' => '150',
                    ],
                ],
                'IN_WALLET' => [
                    'header' => Yii::t('ctx', 'modelListFinanceTranserListInWallet'),//转入账户
                    'value' => function ($row) {
                        return Transfer::WALLET_NAME[$row['IN_WALLET']];
                    },
                    'headerOther' => [
                        'width' => '150',
                    ],
                ],
                'ORI_AMOUNT' => [
                    'header' => Yii::t('ctx', 'modelListFinanceTranserListOriAmount'),//转账金额
                    'value' => function ($row) {
                        return Tool::formatPrice($row['ORI_AMOUNT']);
                    },
                    'headerOther' => [
                        'width' => '150',
                    ],
                ],
                'FEE' => [
                    'header' => Yii::t('ctx', 'modelListFinanceTranserListFee'),//手续费
                    'value' => function ($row) {
                        return Tool::formatPrice($row['FEE']);
                    },
                    'headerOther' => [
                        'width' => '150',
                    ],
                ],
                'AMOUNT' => [
                    'header' => Yii::t('ctx', 'modelListFinanceTranserListAmount'),//实际转入金额
                    'value' => function ($row) {
                        return Tool::formatPrice($row['AMOUNT']);
                    },
                    'headerOther' => [
                        'width' => '150',
                    ],
                    'valueOther' => function ($row) {
                        return [
                            'tag' => ['type' => 'danger', 'size' => 'small', 'class' => 'no-border']
                        ];
                    },
                ],
                'CREATED_AT' => [
                    'header' => Yii::t('ctx', 'modelListFinanceTranserListCreatedAt'),//转账时间
                    'value' => function ($row) {
                        return (new DateTime([
                            'value' => $row['CREATED_AT'],
                        ]))->result();
                    },
                    'headerOther' => ['width' => '190'],
                ],
                'PERIOD_NUM' => [
                    'header' => Yii::t('ctx', 'modelListFinanceTranserListPeriodNum'),//转账期数
                    'headerOther' => [
                        'width' => '150',
                    ],
                ],
                'REMARK' => [
                    'header' => Yii::t('ctx', 'modelListFinanceBalanceAuditListRemark'), // 备注
                    'headerOther' => [
                        'width' => '200',
                    ],
                ],
            ];
        }
        return $this->columns;
    }

    /**
     * 前台用于筛选的类型集合
     * @return mixed
     */
    public function getFilterTypes() {
        if (!$this->filterTypes) {
            $this->filterTypes = [
                'TRANSFER_SN' => ['isUserTable' => false, 'name' => Yii::t('ctx', 'modelListFinanceTranserListTransferSn')],
                'LAST_OUT_USER_NAME' => ['isUserTable' => false, 'name' => Yii::t('ctx', 'modelListFinanceTranserListLastOutUserName')],
                'LAST_OUT_REAL_NAME' => ['isUserTable' => false, 'name' => Yii::t('ctx', 'modelListFinanceTranserListLastOutRealName')],
                'LAST_OUT_DEC_LV_NAME' => ['isUserTable' => false, 'name' => Yii::t('ctx', 'modelListFinanceTranserListLastOutDecLvName'), 'other' => 'decLevel'],
//                'OUT_WALLET' => ['isUserTable' => false, 'name' => Yii::t('ctx', 'modelListFinanceTranserListOutWallet'), 'other' => 'select', 'selectData' => [['id' => 'bonus', 'name' => '会员账户'], ['id' => 'cash', 'name' => '现金钱包']]],
                'LAST_IN_USER_NAME' => ['isUserTable' => false, 'name' => Yii::t('ctx', 'modelListFinanceTranserListLastInUserName')],
                'LAST_IN_REAL_NAME' => ['isUserTable' => false, 'name' => Yii::t('ctx', 'modelListFinanceTranserListLastInRealName')],
                'LAST_IN_DEC_LV_NAME' => ['isUserTable' => false, 'name' => Yii::t('ctx', 'modelListFinanceTranserListLastInDecLvName'), 'other' => 'decLevel'],
//                'IN_WALLET' => ['isUserTable' => false, 'name' => Yii::t('ctx', 'modelListFinanceTranserListInWallet'), 'other' => 'select', 'selectData' => [['id' => 'bonus', 'name' => '会员账户'], ['id' => 'cash', 'name' => '现金钱包']]],
                'ORI_AMOUNT' => ['isUserTable' => false, 'name' => Yii::t('ctx', 'modelListFinanceTranserListOriAmount')],
                'FEE' => ['isUserTable' => false, 'name' => Yii::t('ctx', 'modelListFinanceTranserListFee')],
                'AMOUNT' => ['isUserTable' => false, 'name' => Yii::t('ctx', 'modelListFinanceTranserListAmount')],
                'CREATED_AT' => ['isUserTable' => false, 'name' => Yii::t('ctx', 'modelListFinanceTranserListCreatedAt'), 'other' => 'date'],
                'PERIOD_NUM' => ['isUserTable' => false, 'name' => Yii::t('ctx', 'modelListFinanceTranserListPeriodNum')],
                'REMARK' => ['isUserTable' => false, 'name' => Yii::t('ctx', 'modelListFinanceBalanceAuditListRemark')],
                //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;
    }
}
