<!--
 * @Author: micky
 * @Date: 2024-08-13 12:35:12
 * @LastEditors: micky
 * @LastEditTime: 2024-09-09 14:36:53
 * @FilePath: \chargingpile-uniapp\src\components\order-button\inspection-btn.vue
-->
<template>
    <view :class="pageType == 'card' ? 'order-btns-wrapper' : 'page-common-footer'">
        <template v-if="pageSource == '1'">
            <u-button
                shape="circle"
                type="primary"
                :plain="true"
                @click="closeOrder"
                v-if="
                    ifShow([
                        InspectWorkOrderStatusEnum.WAITING,
                        InspectWorkOrderStatusEnum.TOINSPECT
                    ])
                "
            >
                退单
            </u-button>
            <u-button
                shape="circle"
                type="primary"
                @click="popupShow = true"
                v-if="item?.orderStatus == WorkOrderStatusEnum.WAITING"
            >
                接单
            </u-button>
            <u-button
                shape="circle"
                type="primary"
                @click="startInspecting"
                v-if="ifShow([InspectionOrderStatusEnum.TOINSPECTING])"
            >
                开始巡检
            </u-button>
            <u-button
                shape="circle"
                type="primary"
                @click="faultDetection"
                v-if="ifShow([InspectionOrderStatusEnum.INSPECTING])"
            >
                故障检测
            </u-button>
        </template>
        <w-cancel-order ref="cancelPopup" :id="props?.item?.id" :type="activeTab" :pageSource="1" />
        <w-confirm-popup
            contentText="确认接单吗？"
            v-model="popupShow"
            @confirm="confirmTakeOrder"
        />
        <w-confirm-popup
            contentText="确认开始巡检吗？"
            v-model="inspectPopupShow"
            @confirm="confirmInspecting"
        />
    </view>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { apiStartRouting, apiRoutingOrderReceive } from '@/api/overhaul'
import {
    InspectionOrderStatusEnum,
    WorkOrderStatusEnum,
    InspectWorkOrderStatusEnum
} from '@/enums/businessEnums'
import { toast } from '@/utils/util'

const props = defineProps({
    item: Object,
    pageType: String,
    pageSource: String,
    activeTab: Number
})
const emits = defineEmits(['refresh'])
const popupShow = ref(false)
const inspectPopupShow = ref(false)
const cancelPopup = ref()

const ifShow = (arr: any[]) => {
    return arr.find(i => i == props.item?.orderStatus) != null ? true : false
}

// 退单
const closeOrder = () => {
    cancelPopup.value.showPopup()
}
// 确认接单
const confirmTakeOrder = async () => {
    await apiRoutingOrderReceive({
        id: props.item?.id
    })
    toast('接单成功')
    emits('refresh')
}

// 开始巡检
const startInspecting = () => {
    inspectPopupShow.value = true
}
// 确认开始巡检
const confirmInspecting = async () => {
    await apiStartRouting({ id: props.item?.id })
    toast('操作成功！')
    emits('refresh')
}
// 故障检测
const faultDetection = () => {
    uni.navigateTo({
        url: `/bundle/pages/inspection/fault-detection?id=${props.item?.id}`
    })
}
</script>
<style scoped lang="scss">
.btns {
    @apply flex justify-center items-center w-full my-2;
    view {
        @apply text-primary flex-1 text-center leading-[30px];
        &:last-child {
            @apply border-none;
        }
        &._border {
            @apply border-r border-border;
        }
    }
}
</style>
