<template>
    <app-view class="wareorder-list-purchase">
        <app-pull-refresh class="wareorder-list__wrapper" v-model:loading="loading" v-model:error="error"
            v-model:pageIndex="pageIndex" :page-count="pageCount" @refresh="run">
            <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 class="right">
                            <span>{{ getApplyStatusName(item.applystatus) }}</span>
                        </div>
                    </div>
                    <div class="section-item__content">
                        <table cellspacing="0" cellpadding="0">
                            <tr>
                                <th>数量</th>
                                <td>{{ item.qty }}</td>
                                <th>类型</th>
                                <td>{{ item.appointmentmodeldisplay }}</td>
                                <th>申请时间</th>
                                <td>{{ item.applytime }}</td>
                            </tr>
                            <tr>
                                <th>仓库</th>
                                <td colspan="5">{{ item.warehousename }}</td>
                            </tr>
                        </table>
                    </div>
                    <div class="section-item__btnbar">
                        <Button size="small" round @click="onClick(item)">详情</Button>
                    </div>
                </li>
            </ul>
        </app-pull-refresh>
    </app-view>
</template>

<script lang="ts" setup>
import { shallowRef } from 'vue'
import { Button } from 'vant'
import { useNavigation } from '@/hooks/navigation'
import { useRequest } from '@/hooks/request'
import { queryWrOutInApply } from '@/services/api/order'
import AppPullRefresh from '@mobile/components/base/pull-refresh/index.vue'
import { getApplyStatusName } from "@/constants/order"

const { router } = useNavigation()
const dataList = shallowRef<Model.WrOutInApplyRsp[]>([])
const error = shallowRef(false)

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

const onClick = (item: Model.WrOutInApplyRsp) => {
    router.push({ name: 'wroutinapply-detail', params: { item: JSON.stringify(item) } })
}
</script>