import { BuyOrSell } from '@/common/constants/enumCommon';
import { EnumRouterName } from '@/common/constants/enumRouterName';
import { ComposeTableParam, handleComposeTable } from '@/common/export/commonTable';
import { queryTableList } from '@/common/setup/table';
import { queryQueryOrderQuoteMyq } from '@/services/go/wrtrade';
import { QueryOrderQuoteMyqQsp, QueryOrderQuoteMyqQsq } from '@/services/go/wrtrade/interface';


export function handleColumns() {
    const columns = [
        { title: '品种', key: 'deliverygoodsname', align: 'center', width: 140 },
        { title: '商品', key: 'wrtypename', align: 'center', width: 400 },
        { title: '仓库', key: 'warehousename', align: 'center', width: 200 },
        { title: '价格', key: 'fixedprice', align: 'center', width: 120 },
        { title: '议价', key: 'canbargain', align: 'center', width: 80 },
        { title: '数量', key: 'orderqty', align: 'center' },
        { title: '整单', key: 'canpart', align: 'center', width: 80 },
        { title: '挂牌有效期', key: 'validtime', align: 'center', width: 120 },
        { title: '履约保证金', key: 'marginvalue', align: 'center', width: 120 },
        { title: '履约方式', key: 'templatename', align: 'center', width: 120 },
        { title: '挂牌方', key: 'username', align: 'center', width: 120 },
    ];
    function getBuyOrSellColumns(type: BuyOrSell) {
        const title = type === BuyOrSell.buy ? '买' : '卖'
        return [
            { title, dataIndex: 'index', key: 'index', width: 80, align: 'center' },
            ...columns
        ]
    }
    return { getBuyOrSellColumns }
}

// 买 仓单贸易 贸易圈数据
export function getBuyData() {
    // 表头
    const { getBuyOrSellColumns } = handleColumns()
    const culums = getBuyOrSellColumns(BuyOrSell.buy)
    const buyColumns = culums.map(el => {
        const slots = { customRender: el.key }
        return { ...el, slots, dataIndex: el.key }
    })

    // 表格列表数据
    const { loading, tableList, queryTable } = queryTableList<QueryOrderQuoteMyqQsp>();
    // 获取列表数据
    const queryTableAction = () => {
        const param: QueryOrderQuoteMyqQsq = {
            buyorsell: BuyOrSell.buy,
            haswr: 1,
        };
        queryTable(queryQueryOrderQuoteMyq, param);
    };
    // 表格通用逻辑
    const param: ComposeTableParam = {
        queryFn: queryTableAction,
        menuType: EnumRouterName.warehouse_receipt_trade_blocs,
        tableFilterKey: [],
        isDetail: false,
    };
    const {
        contextMenu, openContext, closeContext, // 右键
        expandedRowKeys, selectedRow, Rowclick, // 表格折腾面板数据与单击、双击事件
        componentId, closeComponent, openComponent,  // 控制异步组件
        firstBtn, secondBtn, // 表格按钮
    } = handleComposeTable<QueryOrderQuoteMyqQsp>(param)

    return {
        buyContextMenu: contextMenu,
        buyOpenContext: openContext,
        buyCloseContext: closeContext,
        buyColumns,
        buyExpandedRowKeys: expandedRowKeys,
        buySelectedRow: selectedRow,
        buyRowclick: Rowclick,
        buyComponentId: componentId,
        buyCloseComponent: closeComponent,
        buyOpenComponent: openComponent,
        buyTableList: tableList,
        loading
    }
}
