<!-- 我的订单-采购订单 -->
<template>
    <app-view class="order-list-purchase">
        <app-pull-refresh ref="pullRefreshRef" class="order-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>
                            <span>{{ item.enddatemonth }}</span>
                            <span>{{ parsePercent(item.depositrate) }}</span>
                            <span>{{ item.tradeqty }}{{ getGoodsUnitName(item.unitid) }}</span>
                        </div>
                        <div class="right">
                            <span>{{ item.thjorderstatusdisplay }}</span>
                        </div>
                    </div>
                    <div class="section-item__content">
                        <table cellspacing="0" cellpadding="0">
                            <tr>
                                <th>预付款</th>
                                <td>{{ handleNumberValue(item.payeddeposit) }}</td>
                                <th>参考价</th>
                                <td>{{ handleNumberValue(item.tradeprice) }}</td>
                                <th>到期日</th>
                                <td>{{ item.enddate }}</td>
                            </tr>
                            <tr>
                                <th>购买价</th>
                                <td>{{ handleNumberValue(item.transferprice) }}</td>
                                <th>实际价</th>
                                <td>{{ handleNumberValue(item.lastprice) }}</td>
                                <th>尾款</th>
                                <td>{{ handleNumberValue(item.remainamount) }}</td>
                            </tr>
                        </table>
                    </div>
                    <div class="section-item__btnbar">
                        <Button size="small" @click="onClick(item)" round>详情</Button>
                    </div>
                </li>
            </ul>
        </app-pull-refresh>
    </app-view>
</template>

<script lang="ts" setup>
import { shallowRef, onActivated } from 'vue'
import { Button } from 'vant'
import { useNavigation } from '@/hooks/navigation'
import { handleNumberValue, parsePercent } from '@/filters'
import { getGoodsUnitName } from '@/constants/unit'
import { useRequest } from '@/hooks/request'
import { queryTHJPurchaseTradeDetail } from '@/services/api/order'
import AppPullRefresh from '@mobile/components/base/pull-refresh/index.vue'

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

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

const onClick = (item: Model.THJPurchaseTradeDetailRsp) => {
    router.push({ name: 'purchase-trade-detail', params: { item: JSON.stringify(item) } })
}

onActivated(() => {
    pullRefreshRef.value?.refresh()
})
</script>