<!--
 * @Author: micky
 * @Date: 2024-10-06 20:42:13
 * @LastEditors: micky
 * @LastEditTime: 2024-11-22 17:23:06
 * @FilePath: \chargingpile-uniapp\src\bundle\pages\repair\my-apply\material.vue
-->
<template>
    <z-paging ref="paging" v-model="dataList" @query="queryList" :fixed="false" height="100%">
        <block v-for="(item, index) in dataList" :key="`${index} + 'unique'`">
            <w-order-card>
                <template #title>
                    <view class="flex justify-between relative">
                        <view>单据编号：{{ item.orderNo }}</view>
                        <view
                            :style="{ color: materialApplyStatusColor[item.auditStatus] }"
                            class="text-sm"
                        >
                            {{ materialApplyStatusMap[item.auditStatus] }}
                        </view>
                    </view>
                </template>
                <template #content>
                    <view class="order-content-wrapper">
                        <view>
                            <text>模块代码</text>
                            <text>{{ item.moduleCode }}</text>
                        </view>
                        <view>
                            <text>申请配件</text>
                            <text>{{ getSpareParts(item) }}</text>
                        </view>
                        <view>
                            <text>申请时间</text>
                            <text>{{ item.applyForTime }}</text>
                        </view>
                        <view v-if="item.status == MaterialApplyStatusEnum.NOPASS">
                            <text>不通过原因</text>
                            <text>{{ item.createTime }}</text>
                        </view>
                    </view>
                </template>
                <template #btns>
                    <view class="order-btns-wrapper">
                        <template
                            v-if="
                                item?.auditStatus == MaterialApplyStatusEnum.NOPASS ||
                                item?.auditStatus == MaterialApplyStatusEnum.CANCELED
                            "
                        >
                            <!-- <u-button
                                shape="circle"
                                type="primary"
                                class="gray-btn"
                                @click="applyAgain(item.repairOrderId)"
                            >
                                重新申请
                            </u-button> -->
                        </template>
                        <template
                            v-else-if="
                                item?.auditStatus != MaterialApplyStatusEnum.COMPLETED &&
                                item?.auditStatus != MaterialApplyStatusEnum.CANCELED
                            "
                        >
                            <!-- <u-button
                                shape="circle"
                                type="primary"
                                :plain="true"
                                @click="cancelMaterial(item)"
                            >
                                取消领料
                            </u-button> -->
                            <u-button shape="circle" type="primary" @click="signCheck(item)">
                                确认签名
                            </u-button>
                        </template>
                    </view>
                </template>
            </w-order-card>
        </block>
        <w-cancel-material ref="cancelPopup" :id="activeId" :pageSource="1" @refresh="refresh" />
    </z-paging>
</template>

<script setup lang="ts">
import { computed, ref } from 'vue'
import { apiReturnStashList } from '@/api/repair'
import {
    MaterialApplyStatusEnum,
    materialApplyStatusMap,
    materialApplyStatusColor
} from '@/enums/businessEnums'
import { useZPaging } from '@/hooks/useZPaging'
import cache from '@/utils/cache'
import { USER_INFO } from '@/enums/cacheEnums'
import { toast } from '@/utils/util'

const userInfo = cache.get(USER_INFO)
const dataList = ref<any>([])
const activeId = ref('')
const cancelPopup = ref()
const params = ref({
    applicantId: userInfo.id,
    reviewRequest: 1
})
const { paging, queryList, refresh } = useZPaging(params.value, apiReturnStashList)

const getSpareParts = computed(() => item => {
    if (item.orderSparePartListVoList && item.orderSparePartListVoList?.length > 0) {
        let text = ''
        item.orderSparePartListVoList.map(item => {
            text += `${item.sparePartsName} - ${item.specificationsModel} x ${item.count}；`
        })
        return text
    }
    return '无'
})
const toPage = (id: number) => {
    uni.navigateTo({
        url: `/bundle/pages/repair/order-detail?id=${id}&pageSource=1`
    })
}
// 取消领料
const cancelMaterial = (item: any) => {
    activeId.value = item.repairOrderId
    cancelPopup.value.showPopup()
}
// 确认签名
const signCheck = (item: any) => {
    if (item.auditStatus != MaterialApplyStatusEnum.TOCONFIRM)
        return toast(
            `当前审核状态为【${materialApplyStatusMap[item.auditStatus!]}】,不允许确认签名`
        )
    uni.navigateTo({
        url: `/bundle/pages/repair/step/sign?id=${item?.repairOrderId}`
    })
}
// 重新申请
// const applyAgain = (id: number) => {
//     uni.navigateTo({
//         url: `/bundle/pages/repair/step/step-check?id=${id}&type=2`
//     })
// }

const setParams = payload => {
    params.value = {
        ...params.value,
        ...payload
    }
    refresh(params.value)
}

defineExpose({
    setParams
})
</script>
<style scoped lang="scss">
.order-content-wrapper view text {
    color: #7c7e82 !important;
}
</style>
