import { BtnListType } from "@/common/components/btnList/interface";
import { UserType } from "@/common/constants/enumCommon";
import { getAccountTypeList, getCanUseMoney } from "@/services/bus/account";
import { getGoodsByCode, getQuoteDayInfoByCodeFindPrice } from "@/services/bus/goods";
import { getUserAccountType } from "@/services/bus/user";
import { QueryTjmdTradeOrderDetailRsp } from "@/services/go/Tjmd/interface";
import { UnwrapRef } from "vue";
import { RefGoodsList } from "../interface";

// 获取按钮列表
export function useBuyOrSellBtnList() {
    const btnList: BtnListType[] = [
        { lable: '摘牌', code: 'Listing', className: 'operBtn' },
    ];
    return btnList
}

// true： 普通交易商；false: 机构
export const useUserType = () => {
    return getUserAccountType() === UserType.investment
}

// 判断是否是 机构
export const isInstitution = () => {
    return getUserAccountType() === UserType.institution
}
// 判断是否是 投资者
export const isInvestment = () => {
    return getUserAccountType() === UserType.investment
}

// 交易账号
export function useBlocksAccount<T extends { accountid: number | undefined }>(formState: UnwrapRef<T>) {
    // 资金账号
    const accountList = getAccountTypeList([2]);
    if (accountList.length) {
        formState.accountid = accountList[0].accountid;
    }
    // 资金账号
    function getSelectedAccount() {
        return accountList.find((e) => e.accountid === formState.accountid);
    }
    // 可以资金
    function canUseMoney() {
        return getCanUseMoney(getSelectedAccount()!)
    }
    // 处理内部资金账号
    function handleSelectedAccount() {
        const item = getSelectedAccount()!;
        const list = getAccountTypeList([2]);
        return item.taaccounttype === 2 ? item : list[0];
    }
    return { handleSelectedAccount, canUseMoney, getSelectedAccount, accountList };
}

// 获取买卖大厅的价格，如果是浮动价的时候，取行情最新价格价基差
export function getBuyOrSellMarketPrice(record: QueryTjmdTradeOrderDetailRsp, selectedRow: RefGoodsList) {
    let result = record.orderprice
    // 浮动价
    if (record.pricemode === 3) {
        const price = getQuoteDayInfoByCodeFindPrice(selectedRow.refgoodscode);
        const goods = getGoodsByCode(selectedRow.refgoodscode)?.decimalplace || 2
        if (price && price !== '--') {
            // 浮动价
            result = +(+price + record.marketmaxsub).toFixed(goods);
        }
    }
    return result
}

// 价格排序
// true: 升序，false: 倒序
export function sortPirce(arr: QueryTjmdTradeOrderDetailRsp[], selectedRow: RefGoodsList, isUp = true) {
    return arr.sort((a, b) => {
        return isUp ? getBuyOrSellMarketPrice(a, selectedRow) - getBuyOrSellMarketPrice(b, selectedRow) : getBuyOrSellMarketPrice(b, selectedRow) - getBuyOrSellMarketPrice(a, selectedRow)
    })
}

// 是否有浮动价
export function hasFloat(arr: QueryTjmdTradeOrderDetailRsp[]) {
    return arr.find(e => e.pricemode === 3) ? true : false
}