<template>
    <app-view class="goods-detail g-form">
        <template #header>
            <app-navbar :title="quote ? quote.goodscode + '/' + quote.goodsname : '挂牌大厅'" />
        </template>
        <component :is="Price" v-bind="{ goodsCode }" />
        <component :is="Chart" v-bind="{ goodsCode }" />
        <component :is="Forex" v-bind="{ goodsCode }" :show-more="showMore" @more-click="onMoreClick" />
        <component :is="Tik" v-bind="{ goodsCode }" />
        <template #footer>
            <div class="g-form__footer">
                <Button block type="danger" square @click="onListing(BuyOrSell.Buy)">买入</Button>
                <Button block type="primary" square @click="onListing(BuyOrSell.Sell)">卖出</Button>
            </div>
            <component ref="componentRef" :is="componentMap.get(componentId)" v-bind="{ goodsCode, buyOrSell }"
                @closed="closeComponent" v-if="componentId" />
        </template>
    </app-view>
</template>

<script lang="ts" setup>
import { shallowRef, defineAsyncComponent, computed } from 'vue'
import { Button } from 'vant'
import { useNavigation } from '@mobile/router/navigation'
import { useComponent } from '@/hooks/component'
import { BuyOrSell } from '@/constants/order'
import { useFuturesStore } from '@/stores'

defineProps({
    showMore: {
        type: Boolean,
        default: true
    }
})

const Price = defineAsyncComponent(() => import('@mobile/components/modules/quote/price/index.vue'))
const Chart = defineAsyncComponent(() => import('@mobile/components/modules/quote/chart/index.vue'))
const Forex = defineAsyncComponent(() => import('@mobile/components/modules/quote/forex/index.vue'))
const Tik = defineAsyncComponent(() => import('@mobile/components/modules/quote/tik/index.vue'))

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

const { router, getQueryStringToNumber } = useNavigation()
const futuresStore = useFuturesStore()
const goodsid = getQueryStringToNumber('goodsid')
const quote = futuresStore.getGoodsQuote(goodsid)
const buyOrSell = shallowRef<BuyOrSell>()

const goodsCode = computed(() => quote.value?.goodscode ?? '')

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

const onListing = (type: BuyOrSell) => {
    buyOrSell.value = type
    openComponent('listing')
}

const onMoreClick = (buyorsell: BuyOrSell) => {
    router.push({
        name: 'goods-trade',
        query: {
            goodsid,
            buyorsell,
        }
    })
}
</script>