<template>
    <app-view class="swap-detail">
        <template #header>
            <app-navbar :title="item.goodscode + '/' + item.goodsname">
                <template #right>
                    <div class="button-more" @click="onListing">
                        <span>挂牌</span>
                    </div>
                </template>
                <template #footer>
                    <app-quote :goodsCode="item.refgoodscode" style="margin-bottom: .2rem;" />
                </template>
            </app-navbar>
        </template>
        <app-pull-refresh ref="pullRefreshRef" v-model:loading="loading" v-model:error="error" v-model:pageIndex="pageIndex"
            :page-count="pageCount" @refresh="onRefresh">
            <Tabs v-model:active="tabIndex" @click="onTabChange">
                <Tab title="买大厅" :name="BuyOrSell.Buy" />
                <Tab title="卖大厅" :name="BuyOrSell.Sell" />
            </Tabs>
            <div class="trade-section sell" v-if="dataList.length">
                <app-list :columns="columns" :data-list="dataList">
                    <template #username="{ row }">
                        <span>{{ row.userid }}/{{ row.username }}</span>
                    </template>
                    <template #operate="{ row }">
                        <Button size="small" type="primary" round @click="onDelisting(row)">
                            {{ tabIndex === BuyOrSell.Buy ? '卖出' : '买入' }}
                        </Button>
                    </template>
                </app-list>
            </div>
        </app-pull-refresh>
        <component ref="componentRef" :is="componentMap.get(componentId)" v-bind="{ selectedRow, tabIndex, item }"
            @closed="closeComponent" v-if="componentId" />
    </app-view>
</template>

<script lang="ts" setup>
import { shallowRef, defineAsyncComponent } from 'vue'
import { Tab, Tabs, Button, showToast } from 'vant'
import { useRequest } from '@/hooks/request'
import { useNavigation } from '@mobile/router/navigation'
import { useComponent } from '@/hooks/component'
import { BuyOrSell } from '@/constants/order'
import { queryTjmdTradeOrderDetail } from '@/services/api/swap'
import { useLoginStore } from '@/stores'
import AppPullRefresh from '@mobile/components/base/pull-refresh/index.vue'
import AppList from '@mobile/components/base/list/index.vue'
import AppQuote from '@mobile/components/modules/quote/price/index.vue'

const componentMap = new Map<string, unknown>([
    ['delisting', defineAsyncComponent(() => import('./components/delisting/Index.vue'))],
    ['listing', defineAsyncComponent(() => import('./components/listing/Index.vue'))],
])
const pullRefreshRef = shallowRef()
const loginStore = useLoginStore()
const { getGlobalUrlParams } = useNavigation()
const item: Model.QuoteGoodsListRsp = getGlobalUrlParams()
const tabIndex = shallowRef(0)
const selectedRow = shallowRef<Model.TjmdTradeOrderDetailRsp>()
const error = shallowRef(false)
const dataList = shallowRef<Model.TjmdTradeOrderDetailRsp[]>([])

const { componentRef, componentId, openComponent, closeComponent } = useComponent(() => onTabChange())

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

const onRefresh = () => {
    run({
        buyorsell: tabIndex.value,
    })
}

const onTabChange = () => {
    /// 重置为1
    pageIndex.value = 1
    onRefresh()
}

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

const onDelisting = (row: Model.TjmdTradeOrderDetailRsp) => {
    selectedRow.value = row
    /// 不能与自己成交
    if (row.userid === loginStore.userId) {
        showToast('不能与自己成交')
        return
    }
    openComponent('delisting')
}

const onListing = () => {
    openComponent('listing')
}
</script>