<template>
    <app-view class="g-detail">
        <template #header>
            <app-navbar title="商品详情" />
        </template>
        <Banner :data-list="detailBanners" />
        <div class="g-detail__buy">
            <div class="topic">
                <div class="topic-left">
                    <span class="price-text">发行价</span>
                    <span class="price-unit">￥</span>
                    <span class="price-integer">{{ detail.refprice?.toFixed(2) }}</span>
                </div>
                <div class="topic-right">
                    <span>开始：{{ detail.starttime }}</span>
                    <span>结束：{{ detail.endtime }}</span>
                </div>
            </div>
            <div class="title">
                <Tag type="danger" round>摇号中签</Tag>
                <span>{{ detail.sellname }}</span>
                <span>{{ detail.goodscode }}/{{ detail.goodsname }}</span>
            </div>
            <div class="qty">
                <span>总量：{{ detail.presaleqty }}</span>
            </div>
            <div class="info">
                <ul>
                    <li>
                        <span>预售定金</span>
                        <span>{{ earnest }}</span>
                    </li>
                    <li>
                        <span>转让定金</span>
                        <span>{{ parsePercent(detail.transferdepositratio) }}</span>
                    </li>
                </ul>
            </div>
        </div>
        <div class="g-detail__desc">
            <template v-for="(url, index) in detailImages" :key="index">
                <img :src="url" alt="" />
            </template>
        </div>
        <template #footer>
            <div class="g-detail__footer" v-if="detail.presalestatus === 2">
                <div class="price">
                    <span class="price-text">发行价：</span>
                    <span class="price-unit">￥</span>
                    <span class="price-integer">{{ detail.refprice?.toFixed(2) }}</span>
                </div>
                <div class="submit">
                    <span class="submit-button danger" @click="openComponent('delisting')">我要认购</span>
                </div>
            </div>
            <component ref="componentRef" :is="componentMap.get(componentId)" v-bind="{ detail }" @closed="closeComponent"
                v-if="componentId" />
        </template>
    </app-view>
</template>

<script lang="ts" setup>
import { computed, defineAsyncComponent } from 'vue'
import { Tag } from 'vant'
import { getFileUrl, parsePercent } from '@/filters'
import { useComponent } from '@/hooks/component'
import { useNavigation } from '@mobile/router/navigation'
import Banner from '@mobile/components/base/banner/index.vue'

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

const { getParamString } = useNavigation()
const { componentRef, componentId, openComponent, closeComponent } = useComponent()
const qs = getParamString('item')
const detail: Model.PresaleAuctionsRsp = JSON.parse(qs.toString() || '{}')

// 商品banner
const detailBanners = computed(() => {
    const bannerpicurl = detail.bannerpicurl ?? ''
    return bannerpicurl?.split(',').map((path) => path)
})

// 预售定金
const earnest = computed(() => {
    switch (detail.presaledepositalgorithm) {
        case 1:
            return parsePercent(detail.presaledepositvalue)
        case 2:
            return detail.presaledepositvalue.toFixed(2)
        default:
            return '0.0'
    }
})

// 商品图片列表
const detailImages = computed(() => {
    const pictureurls = detail.pictureurls ?? ''
    return pictureurls.split(',').map((path) => getFileUrl(path))
})
</script>