<template>
    <app-view class="hiswrorder-order" flex>
        <template #header>
            <app-navbar title="供求委托-历史" />
        </template>
        <Cell title="委托日期" :value="date" @click="show = true" is-link></Cell>
        <app-pull-refresh ref="pullRefreshRef" class="order-list__wrapper" v-model:loading="loading"
            v-model:pageIndex="pageIndex" v-model:error="error" :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 class="right">
                            <span>{{ getWRTradeOrderStatusName(item.wrtradeorderstatus) }}</span>
                        </div>
                    </div>
                    <div class="section-item__content">
                        <table cellspacing="0" cellpadding="0">
                            <tr>
                                <th>挂牌数量</th>
                                <td>{{ item.orderqty }}</td>
                                <th>挂牌价格</th>
                                <td>{{ item.fixedprice }}</td>
                                <th>成交数量</th>
                                <td>{{ item.tradeqty }}</td>
                            </tr>
                            <tr>
                                <th>仓库</th>
                                <td colspan="5">{{ handleNoneValue(item.warehousename) }}</td>
                            </tr>
                        </table>
                    </div>
                    <div class="section-item__btnbar">
                        <Button size="small" @click="onClick(item)" round>详情</Button>
                    </div>
                </li>
            </ul>
        </app-pull-refresh>
        <Calendar :show="show" type="range" :max-date="new Date()" :min-date="moment().subtract(1, 'years').toDate()"
            @close="onClose" @confirm="onConfirm" />
    </app-view>
</template>

<script lang="ts" setup>
import { shallowRef, ref, reactive } from 'vue'
import { Button, Calendar, Cell } from 'vant'
import { formatDate, handleNoneValue } from '@/filters'
import { useRequest } from '@/hooks/request'
import { queryWrOrderDetail } from '@/services/api/order'
import { getWRTradeOrderStatusName } from '@/constants/order'
import AppPullRefresh from '@mobile/components/base/pull-refresh/index.vue'
import { useNavigation } from '@/hooks/navigation'
import moment from 'moment'

const { router } = useNavigation()
const dataList = shallowRef<Model.WrOrderDetailRsp[]>([])
const error = shallowRef(false)
/// 是否显示日历
const show = shallowRef(false)
/// 显示日期
const date = ref('')
/// 下拉刷新
const pullRefreshRef = shallowRef()

const formData = reactive<Model.WrOrderDetailReq>({
    pagesize: 20,
    haswr: 1,
})

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

const onRefresh = () => {
    run(formData)
}

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

const onClose = () => {
    show.value = false
}

const onConfirm = (values: Date[]) => {
    const [start, end] = values;
    show.value = false
    formData.begindate = formatDate(start.toString(), 'YYYYMMDD')
    formData.enddate = formatDate(end.toString(), 'YYYYMMDD')
    date.value = formData.begindate + '-' + formData.enddate
    /// 查询
    pullRefreshRef.value?.refresh()
}
</script>