import { BtnListType } from "@/common/components/btnList/interface"
import { TradeMode } from "@/common/constants/enumCommon"
import { initData } from "@/common/methods"
import { queryTableList } from "@/common/setup/table"
import { getMarketIdsByTradeMode } from "@/services/bus/market"
import { getUserAccountType } from "@/services/bus/user"
import { queryQuoteGoodsList } from "@/services/go/Tjmd"
import { QueryQuoteGoodsListReq, QueryQuoteGoodsListRsp } from "@/services/go/Tjmd/interface"
import { ref } from "vue"

const refGoods = ref<QueryQuoteGoodsListRsp[]>([])

export function findItemGoods(goodscode: string) {
    return refGoods.value.find(el => el.goodscode === goodscode)!
}

const { loading, queryTable } = queryTableList<QueryQuoteGoodsListRsp>();

export function getSwapList() {
    // 组装 参数
    const marketids = getMarketIdsByTradeMode(TradeMode.DiaoQi)
    const param: QueryQuoteGoodsListReq = {
        usertype: getUserAccountType(),
    }
    if (marketids) {
        param.marketids = marketids
    }
    return queryTable(queryQuoteGoodsList, param)
}

// 获取 商品掉期 商品列表
export const useSwapList = () => {
    // 行情商品
    const goodsList = ref<QueryQuoteGoodsListRsp[]>([])
    initData(() => {
        // 开始查询 商品掉期
        getSwapList().then(res => {
            refGoods.value = res
            goodsList.value = res
        })
    })
    // 获取标记商品列表
    function getRefGoodsList(): string[] {
        const result = new Set<string>(['全部标的合约'])
        refGoods.value.forEach(el => {
            result.add(el.refgoodsname)
        })
        return [...result]
    }
    // 过滤标的合约
    function filterGoodsList(value: string) {
        if (value === '全部标的合约') {
            goodsList.value = refGoods.value
        } else {
            goodsList.value = refGoods.value.filter(goods => goods.refgoodsname === value)
        }
    }

    return { loading, goodsList, getRefGoodsList, filterGoodsList, refGoods }
}

export const columnsList = [
    { title: '序号', key: 'index', width: 80 },
    { title: '代码', key: 'goodscode' },
    { title: '名称', key: 'goodsname' },

    { title: '标的合约', key: 'refgoodsname' },

    // { title: '买价', key: 'bid' },
    // { title: '买量', key: 'bidvolume' },
    // { title: '卖价', key: 'ask' },
    // { title: '卖量', key: 'askvolume' },

    { title: '当前价', key: 'last' },
    { title: '涨跌', key: 'change' }, // 最新价 - 昨结价
    { title: '幅度', key: 'amplitude' }, // （最新价 - 昨结价） / 100 %
    { title: '开盘', key: 'opened' },
    { title: '最高', key: 'highest' },
    { title: '最低', key: 'lowest' },

    { title: '结算', key: 'settle' },

    { title: '昨结算', key: 'presettle' },

    // { title: '振幅', key: 'vibration' }, // (最高价 - 最低价 ) / 最新价  * 100 %
    // { title: '总量', key: 'totalvolume' },
    // { title: '现量', key: 'lastvolume' },
    // { title: '持仓量', key: 'holdvolume' },
    // { title: '日增', key: 'holdincrement' },
    // { title: '金额', key: 'totalturnover' },
];

// 获取按钮列表
export function getBtnList(isBuyAndSell: boolean) {
    const btnList: BtnListType[] = [
        { lable: '挂牌', code: 'PostBuying', className: 'operBtn' },
        // { lable: '详情', code: 'Detail', className: 'btnDeafault' },
    ];
    if (isBuyAndSell) {
        btnList.push({ lable: '买卖大厅', code: 'BuyAndSell', className: 'btnDeafault' })
    }
    return btnList
}