import { BuyOrSell } from '@/common/constants/enumCommon';
import { EnumRouterName } from '@/common/constants/enumRouterName';
import { queryResultLoadingAndInfo } from '@/common/methods/request/resultInfo';
import { TableKey } from '@/common/methods/table/interface';
import { getTableColumns, getTableEvent, queryTableList } from "@/common/setup/table";
import { queryFaProductDetail } from '@/services/go/wrtrade';
import { WrFAProductDetail } from "@/services/go/wrtrade/interface";
import { Ref, ref } from 'vue';


export function handleBuyAndSellList<T>(menuType: EnumRouterName, isDetail: boolean, tableName: keyof TableKey) {
    // 表格列表数据
    const { loading, tableList, queryTable } = queryTableList<T>();
    // 表头数据
    const { columns, registerColumn } = getTableColumns();
    // 表格事件
    const { expandedRowKeys, selectedRow, Rowclick } = getTableEvent<T>({});
    // 注册表头信息 过滤
    registerColumn(tableName, []);
    return {
        loading, tableList, columns, expandedRowKeys, selectedRow, Rowclick, queryTable
    }
}

// 根据 当前tab 的枚举，获取买大厅对应的 wrpricetype haswr tableKey
export function getBuyMarketParam(enumName: EnumRouterName) {
    let wrpricetype = 1, haswr = 0, tableKey: keyof TableKey = 'table_pcweb_spot_trade_warehouse_price_trading_hall_buy'
    switch (enumName) {
        case EnumRouterName.warehouse_receipt_trade_price:
            wrpricetype = 1;
            haswr = 1
            tableKey = 'table_pcweb_spot_trade_warehouse_price_trading_hall_buy'
            break
        case EnumRouterName.warehouse_receipt_trade_floating_price:
            wrpricetype = 2;
            haswr = 1
            tableKey = 'table_pcweb_spot_trade_warehouse_float_trading_hall_buy'
            break
        case EnumRouterName.warehouse_pre_sale_price:
            wrpricetype = 1;
            haswr = 0
            tableKey = 'table_pcweb_spot_trade_warehouse_presale_price_trading_hall_buy'
            break
        case EnumRouterName.warehouse_pre_sale_floating_price:
            wrpricetype = 2;
            haswr = 0
            tableKey = 'table_pcweb_spot_trade_warehouse_presale_float_trading_hall_buy'
            break
    }
    return { wrpricetype, haswr, tableKey }
}

// 根据 当前tab 的枚举，获取卖大厅对应的 wrpricetype haswr tableKey
export function getSellMarketParam(enumName: EnumRouterName) {
    let wrpricetype = 1, haswr = 0, tableKey: keyof TableKey = 'table_pcweb_spot_trade_warehouse_price_trading_hall_sale'
    switch (enumName) {
        case EnumRouterName.warehouse_receipt_trade_price:
            wrpricetype = 1;
            haswr = 1
            tableKey = 'table_pcweb_spot_trade_warehouse_price_trading_hall_sale'
            break
        case EnumRouterName.warehouse_receipt_trade_floating_price:
            wrpricetype = 2;
            haswr = 1
            tableKey = 'table_pcweb_spot_trade_warehouse_float_trading_hall_sale'
            break
        case EnumRouterName.warehouse_pre_sale_price:
            wrpricetype = 1;
            haswr = 0
            tableKey = 'table_pcweb_spot_trade_warehouse_presale_price_trading_hall_sale'
            break
        case EnumRouterName.warehouse_pre_sale_floating_price:
            wrpricetype = 2;
            haswr = 0
            tableKey = 'table_pcweb_spot_trade_warehouse_presale_float_trading_hall_sale'
            break
    }
    return { wrpricetype, haswr, tableKey }
}

export function handleIs(enumName: EnumRouterName, buyOrSell: BuyOrSell) {
    /**
     *
     * @returns 1 => 仓单贸易; 0 => 仓单预售
     */
    function isWR(): 0 | 1 {
        if (enumName === EnumRouterName.warehouse_receipt_trade_price || enumName === EnumRouterName.warehouse_receipt_trade_floating_price) {
            return 1
        } else {
            return 0
        }
    }
    /**
     *
     * @returns true => 买； false =>卖
     */
    function isBuy() {
        return buyOrSell === BuyOrSell.buy
    }
    /**
     *
     * @returns  true => 浮动价； false =>一口价
     */
    function isFloat() {
        return enumName === EnumRouterName.warehouse_receipt_trade_floating_price ||
            enumName === EnumRouterName.warehouse_pre_sale_floating_price
    }
    return { isWR, isBuy, isFloat }
}

const list = ref<WrFAProductDetail[]>([]);
// 获取融资摘牌
export function handleFinacing(loading: Ref<boolean>, id: number) {
    queryResultLoadingAndInfo(queryFaProductDetail, loading, { wrfactortypeid: id }).then((res) => {
        list.value = res;
        return res
    });
    function hasFinacing(): boolean {
        return list.value.length > 0
    }
    return { hasFinacing }
}

export function getFinacingList(): WrFAProductDetail[] {
    return list.value
}

// 商品实时价格
const goodsPrice = ref<string | number>('--')
export function setGoodsPrice(value: string | number) {
    goodsPrice.value = value
}
export function getGoodsPrice(): Ref<string | number> {
    return goodsPrice
}
