<template>
    <app-view class="wareorder-list-purchase">
        <app-pull-refresh class="wareorder-list__wrapper" v-model:error="error" v-model:pageIndex="pageIndex"
            :page-count="pageCount" @refresh="onRefresh">
            <ul class="section">
                <li class="section-item" v-for="(item, index) in dataList" :key="index">
                    <div class="section-item__titlebar">
                        <div class="left">
                            <span>{{ item.wrstandardname }}</span>
                        </div>
                    </div>
                    <div class="section-item__content">
                        <table cellspacing="0" cellpadding="0">
                            <tr>
                                <th>库存</th>
                                <td>{{ item.qty }}</td>
                                <th>冻结</th>
                                <td>{{ item.freezerqty }}</td>
                                <th>可用</th>
                                <td>{{ item.enableqty }}</td>
                            </tr>
                            <tr>
                                <th>仓库</th>
                                <td colspan="5">{{ item.warehousename }}</td>
                            </tr>
                        </table>
                    </div>
                    <div class="section-item__btnbar">
                        <Button size="small" round @click="listing(item)">挂牌</Button>
                        <Button size="small" round @click="pickup(item)">提货</Button>
                    </div>
                </li>
            </ul>
        </app-pull-refresh>
        <component ref="componentRef" v-bind="{ selectedRow }" :is="componentMap.get(componentId)"
            @closed="closeComponent" v-if="componentId" />
    </app-view>
</template>

<script lang="ts" setup>
import { shallowRef, defineAsyncComponent } from 'vue'
import { Button } from 'vant'
import { useQueryHoldLB } from '@/business/order'
import { useComponent } from '@/hooks/component'
import AppPullRefresh from '@mobile/components/base/pull-refresh/index.vue'

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

const { componentRef, componentId, openComponent, closeComponent } = useComponent()
const { pageIndex, pageCount, getHoldLB } = useQueryHoldLB()
const dataList = shallowRef<Model.HoldLBRsp[]>([])
const selectedRow = shallowRef()
const error = shallowRef(false)

const pickup = (item: Model.HoldLBRsp) => {
    selectedRow.value = item
    openComponent('pickup')
}

const listing = (item: Model.HoldLBRsp) => {
    selectedRow.value = item
    openComponent('listing')
}

const onRefresh = (finish: () => void) => {
    /// 查询未完成
    getHoldLB().then((res) => {
        if (pageIndex.value === 1) {
            dataList.value = []
        }
        dataList.value.push(...res)
    }).catch(() => {
        error.value = true
    }).finally(() => {
        finish()
    })
}
</script>