import { BtnListType } from '@/common/components/btnList/interface';
import { initData } from '@/common/methods';
import { queryResultLoadingAndInfo } from '@/common/methods/request/resultInfo';
import { QueryAccMgrTaaccount } from '@/services/go/ermcp/account';
import { ErmcpLoginUser, ErmcpLoginUserEx, ErmcpTaAccount, ErmcpTaAccountEx } from "@/services/go/ermcp/account/interface";
import { reactive, ref, Ref } from "vue";

/**
 * 获取账户状态
 * @param type
 * @returns
 */
export function getAccountStatus(type: number) {
    let result = '--'
    switch (type) {
        case 1:
            result = '正常'
            break;
        case 2:
            result = '待审核'
            break;
        case 3:
            result = '待复审'
            break;
        case 4:
            result = '正常'
            break;
        case 5:
            result = '审核拒绝'
            break;
        case 6:
            result = '停用'
            break;
        case 7:
            result = '注销'
            break;
    }
    return result
}

/**
 * 获取期货账户状态
 * @param type
 * @returns
 */
export function getTaAccountStatus(type: number) {
    let result = '--'
    switch (type) {
        case 1:
            result = '正常'
            break;
        case 2:
            result = '受限'
            break;
        case 3:
            result = '冻结（停用）'
            break;
        case 4:
            result = '禁止建仓（人工受限）'
            break;
        case 5:
            result = '禁止交易（人工冻结）'
            break;
        case 6:
            result = '待激活'
            break;
        case 7:
            result = '已删除（注销）'
            break;
    }
    return result
}

/**
 * 获取角色状态
 * @param type
 * @returns
 */
export function getRoleTypeName(type: number) {
    let result = '--'
    switch (type) {
        case 1:
            result = '正常'
            break;
        case 2:
            result = '停用'
            break;
    }
    return result
}

export function handleModalData<T extends object>() {
    // 弹窗选中的数据
    const selectedData = reactive({}) as T
    // 处理弹窗传递数据
    function openAction(item: T) {
        Object.assign(selectedData, item)
    }
    return { selectedData, openAction }
}

export function handleTableList(fn: Function, tableList: Ref<ErmcpLoginUserEx[]>) {
    fn().then(() => {
        tableList.value.forEach((el) => {
            // 过滤无效
            const arr = el.userlist.filter((item) => item.loginstatus !== 3);
            Object.assign(el, { userlist: arr });
        });
    });
}

// 处理根据状态显示对应按钮
export function handleBtnList(btnList: BtnListType[], item: ErmcpLoginUser) {
    switch (item.loginstatus) {
        case 1: // 正常
            return btnList.filter((e) => e.lable !== '解冻');
        case 2: // 冻结
            return btnList.filter((e) => e.lable === '解冻' || e.lable === '详情');
    }
};

export function handleBtnClickData() {
    const firstBtnData = ref<any>(null)
    const secondBtnData = ref<any>(null)
    const thirdBtnDat = ref<any>(null)
    function btnClick(...rest: any[]) {
        firstBtnData.value = rest[0];
        secondBtnData.value = rest[1];
        thirdBtnDat.value = rest[0];
    }
    return { firstBtnData, secondBtnData, thirdBtnDat, btnClick }
}


// 授权期货账户
const accountList = ref<ErmcpTaAccount[]>([]);
// 查询账户管理期货账号
export function useTaAccount() {
    const loading = ref<boolean>(false)
    function loadTaAccount() {
        queryResultLoadingAndInfo(QueryAccMgrTaaccount, loading).then((res) => {
            accountList.value.length = 0;
            res.forEach((el: ErmcpTaAccountEx) => {
                const { subacclist } = el;
                subacclist.forEach((e) => {
                    if (e.tradestatus === 1) {
                        //交易状态 - 1:正常
                        accountList.value.push(e);
                    }
                });
            });
        });
    }
    initData(() => {
        // 处理授权期货账户
        if (accountList.value.length === 0) {
            loadTaAccount()
        }
    })
    // 更新授权期货账户
    function updateTaAccount() {
        loadTaAccount()
    }
    // 过滤授权期货账户
    function filerTaAccount(id: number) {
        return accountList.value.filter((e) => e.relateduserid === id);
    }
    return { accountList, filerTaAccount, updateTaAccount }
}


// 处理 点击二级 按钮时候，一级 选中 的数据
export function useSelectedData(tableList: Ref<ErmcpLoginUserEx[]>, openComponent: Function) {
    // 一级 对应的选中数据
    const firstSelectedData = ref<ErmcpLoginUserEx>();
    // 二级按钮 打开，处理选中上一层数据
    function openSecondBtnAction(item: BtnListType, second: ErmcpLoginUser) {
        tableList.value.forEach((el) => {
            el.userlist.forEach((sub) => {
                if (second.userid === sub.userid) {
                    firstSelectedData.value = el;
                }
            });
        });
        openComponent(item, second);
    }
    return { firstSelectedData, openSecondBtnAction }
}