<template>
    <app-view>
        <template #header>
            <app-navbar title="掉期挂牌大厅" />
        </template>
        <Tabs v-model:active="tabIndex" @change="tabChange">
            <Tab title="卖大厅">
            </Tab>
            <Tab title="买大厅">
            </Tab>
        </Tabs>
        <div class="trade">
                <div class="trade-section sell" v-if="dataList.length">
                    <app-list :columns="columns" :data-list="dataList">
                        <template #operate="{ row }">
                            <Button size="small" round @click="delisting(row)">摘牌</Button>
                        </template>
                    </app-list>
                </div>
            </div>
        <component ref="componentRef" :is="components.delisting" v-bind="{ selectedRow }" @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 components = {
    delisting: defineAsyncComponent(() => import('./components/delisting/Index.vue')),
    listing: defineAsyncComponent(() => import('./components/listing/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 showModal = shallowRef(true)

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: 'fixedprice', label: '价格' },
    { prop: 'orderqty', label: '数量' },
    { prop: 'username', label: '挂牌方' },
]

const tabChange = (index: number) => {
    console.log('切换列表', index)
}

const delisting = (row: Model.TjmdTradeOrderDetailRsp) => {
    selectedRow.value = row
    openComponent('delisting')
}
</script>