import { TradeMode } from "@/common/constants/enumCommon"
import { initData } from "@/common/methods"
import { getQuoteDayInfoByCode } from "@/services/bus/goods"
import { getMarketByTradeMode } from "@/services/bus/market"
import { queryDeliveryRelation } from "@/services/go/delivery"
import { QueryDeliveryRelationRsp } from "@/services/go/delivery/interface"
import { QueryQuoteDayRsp } from "@/services/go/quote/interface"
import { ref } from "vue"

// 获取交个商品
const allDeliverGoods = ref<QueryDeliveryRelationRsp[]>([])
// 行情商品
const goodsList = ref<QueryQuoteDayRsp[]>([])
type deliverytype = 1 | 2 | 3 | 4
// 1和3是订单交易 挂牌转让
// 2和4是基差点价
export function handleDeliveryRelation(param: deliverytype[]) {
    // 查询交割商品
    const deliverGoods = ref<any[]>([])
    function queryAction() {
        const marketid = getMarketByTradeMode(TradeMode.ListingAndSelection)!.marketid
        queryDeliveryRelation({ marketid }).then(res => {
            allDeliverGoods.value = res
            deliverGoods.value = getDeliverGoods()
            res.forEach(el => {
                // 找到行情数据
                const quote = getQuoteDayInfoByCode(el.goodscode);
                if (quote) {
                    Object.assign(quote, { goodsname: el.goodsname, deliverytype: el.deliverytype })
                    goodsList.value.push(quote)
                }
            })
        })
    }
    function getDeliverGoods() {
        return allDeliverGoods.value.filter(e => param.includes(e.deliverytype as deliverytype))
    }
    initData(() => {
        if (allDeliverGoods.value.length === 0) {
            // 从服务交割商品
            queryAction()
        } else {
            // 从缓存中获取交割商品
            deliverGoods.value = getDeliverGoods()
        }
    })
    // 更新交割商品
    function updateGeliverGoods() {
        queryAction()
    }
    // 获取对应的行情
    function getQuoteList() {
        return goodsList.value.filter(e => param.includes((e as unknown as any).deliverytype))
    }
    // 通过goodscode 查找交割商品
    function findDeilverGoodsByGoodsCode(goodscode: string) {
        return allDeliverGoods.value.find(e => e.goodscode === goodscode)!
    }
    return { deliverGoods, updateGeliverGoods, getQuoteList, findDeilverGoodsByGoodsCode }
}

export const columnsList = [
    { title: '序号', key: 'index', width: 80 },
    { title: '代码', key: 'goodscode' },
    { title: '名称', key: 'goodsname' },
    { 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: 'preclose' },
    { title: '昨结价', key: 'presettle' },
    { title: '最低价', key: 'lowest' },
    { title: '最高价', key: 'highest' },
    // { title: '振幅', key: 'vibration' }, // (最高价 - 最低价 ) / 最新价  * 100 %
    // { title: '总量', key: 'totalvolume' },
    // { title: '现量', key: 'lastvolume' },
    // { title: '持仓量', key: 'holdvolume' },
    // { title: '日增', key: 'holdincrement' },
    // { title: '金额', key: 'totalturnover' },
];

export enum ComponentType {
    chart,
    marketContent,
    tradeDetail,
}