<template>
    <app-view>
        <template #header>
            <app-navbar title="掉期挂牌大厅" >
                <template #right>
                    <div class="button-more" @click="onListing">
                        <span>挂牌</span>
                    </div>
                </template>
            </app-navbar>
        </template>
        <Tabs v-model:active="tabIndex">
            <Tab title="卖大厅" />
            <Tab title="买大厅" />
        </Tabs>
        <div class="trade">
                <div class="trade-section sell" v-if="dataList.length">
                    <app-list :columns="columns" :data-list="dataList">
                        <template #username="{ row }">
                            <span >{{ row.userid }}</span>
                            <span >{{ row.username }}</span>
                        </template>
                        <template #operate="{ row }">
                            <Button size="small" type="primary" round @click="onDelisting(row, tabIndex)">摘牌</Button>
                        </template>
                    </app-list>
                </div>
            </div>
        <component ref="componentRef" :is="componentMap.get(componentId)" v-bind="{ selectedRow, tabIndex }" @closed="closeComponent"
            v-if="componentId" />
    </app-view>
</template>

<script lang="ts" setup>
import { shallowRef, defineAsyncComponent } from 'vue'
import { Tab, Tabs, Button } from 'vant'
import { useRequest } from '@/hooks/request'
import { useNavigation } from '@/hooks/navigation'
import { useComponent } from '@/hooks/component'
import AppList from '@mobile/components/base/list/index.vue'
import { queryTjmdTradeOrderDetail } from '@/services/api/swap'

const componentMap = new Map<string, unknown>([
    ['delisting', defineAsyncComponent(() => import('./components/delisting/Index.vue'))],
    ['listing', defineAsyncComponent(() => import('./components/delisting/Index.vue'))],
])

const { getQueryStringToNumber } = useNavigation()
const { componentRef, componentId, openComponent, closeComponent } = useComponent()
const goodsId = getQueryStringToNumber('id')
const tabIndex = shallowRef(0)
const selectedRow = shallowRef<Model.TjmdTradeOrderDetailRsp>()
const error = shallowRef(false)
const dataList = shallowRef<Model.TjmdTradeOrderDetailRsp[]>([])

const { pageIndex } = useRequest(queryTjmdTradeOrderDetail, {
    params: {
        goodsid: goodsId,
        buyorsell: tabIndex.value
    },
    onSuccess: (res) => {
        if (pageIndex.value === 1) {
            dataList.value = []
        }
        dataList.value.push(...res.data)
    },
    onError: () => {
        error.value = true
    }
})

const columns: Model.TableColumn[] = [
    { prop: 'orderprice', label: '价格' },
    { prop: 'orderqty', label: '数量' },
    { prop: 'username', label: '挂牌方' },
    { prop: 'operate', label: '摘牌' },
]

const onDelisting = (row: Model.TjmdTradeOrderDetailRsp, value: number) => {
    tabIndex.value = value
    selectedRow.value = row
    showComponent('delisting')
}

const onListing = () => {
    showComponent('listing')
}

const showComponent = (componentName: string) => {
    openComponent(componentName)
}

</script>