<template>
    <view :class="pageType == 'card' ? 'order-btns-wrapper' : 'page-common-footer'">
        <template v-if="showButton">
            <template
                v-if="
                    item?.repairNextStep != null &&
                    item.scrapOrNot != YesOrNoEnum.YES &&
                    item.returning != YesOrNoEnum.YES &&
                    item.feedingOrNot != YesOrNoEnum.YES
                "
            >
                <u-button
                    shape="circle"
                    type="primary"
                    @click="item.func(item.params)"
                    v-for="(item, index) in btnMap[item?.repairNextStep]"
                    :key="index"
                    :class="item.className"
                >
                    {{ item.btnText }}
                </u-button>
            </template>
            <!-- 下个步骤是签名 & 不在退仓状态  或 当前为补料状态 -->
            <template
                v-if="
                    (item?.repairNextStep == RepairStepEnum.SIGN &&
                        item.returning != YesOrNoEnum.YES) ||
                    item.feedingOrNot == YesOrNoEnum.YES
                "
            >
                <!-- <u-button
                    shape="circle"
                    :plain="true"
                    type="primary"
                    @click="cancelMaterial"
                    v-if="item.feedingOrNot != YesOrNoEnum.YES"
                >
                    取消领料
                </u-button> -->
                <u-button shape="circle" type="primary" @click="signCheck">确认签名</u-button>
            </template>
        </template>
        <w-cancel-material
            ref="cancelPopup"
            :id="props?.item?.id"
            :pageSource="1"
            @refresh="emits('refresh')"
        />
    </view>
</template>

<script setup lang="ts">
import { RepairStepEnum, RoleIdEnum, YesOrNoEnum } from '@/enums/businessEnums'
import { computed, ref } from 'vue'
import { useRoleActiveId } from '@/hooks/useRoleData'
import { toast } from '@/utils/util'

const props = defineProps<{
    item: Partial<RepairItem>
    pageType: string
    pageSource: string
}>()
const emits = defineEmits(['refresh'])
const { activeRoleId } = useRoleActiveId()
const cancelPopup = ref()
const btnMap: any = {
    [RepairStepEnum.DISMANTLE]: [
        {
            btnText: '拆机',
            func: dismantle
        }
    ],
    [RepairStepEnum.CLEAN]: [
        {
            btnText: '除尘',
            func: toCommonStep
        }
    ],
    [RepairStepEnum.FAULTCHECK]: [
        {
            btnText: '故障排查',
            func: toCheckStep,
            params: 1
        }
    ],
    // [RepairStepEnum.SIGN]: [
    //     {
    //         btnText: '确认签名',
    //         func: signCheck
    //     }
    // ],
    [RepairStepEnum.SPAREPARTS]: [
        {
            btnText: '重领配件',
            func: toCheckStep,
            params: 2
        }
    ],
    [RepairStepEnum.BEFOREREPAIR]: [
        {
            btnText: '上传维修前图片',
            func: toCommonStep
        }
    ],
    [RepairStepEnum.ONREPAIRING]: [
        {
            btnText: '上传维修中图片',
            func: toCommonStep
        }
    ],
    [RepairStepEnum.AFTERREPAIR]: [
        {
            btnText: '上传维修后图片',
            func: toCommonStep
        }
    ],
    [RepairStepEnum.FEATURE]: [
        {
            btnText: '功能测试',
            func: toCommonStep
        }
    ],
    [RepairStepEnum.DCPFC]: [
        {
            btnText: 'DC&PFC组装',
            func: toPfcStep
        }
    ],
    [RepairStepEnum.ANTIPAINTGLUE]: [
        {
            btnText: '补强三防漆和点胶',
            func: toCommonStep
        }
    ],
    [RepairStepEnum.MODULEASSEMBLY]: [
        {
            btnText: '模块装配',
            func: toCommonStep
        }
    ],

    [RepairStepEnum.AGING]: [
        {
            btnText: '老化测试',
            func: toCommonStep
        }
    ],
    [RepairStepEnum.FILLIN]: [
        {
            btnText: '填写维修信息',
            func: fillIn
        }
    ],
    [RepairStepEnum.INSTORAGE]: [
        {
            btnText: '良品入库',
            func: toCommonStep
        }
    ]
}

const showButton = computed(() => {
    return activeRoleId.value == RoleIdEnum.REPAIR && props.item.isReassignment == null
})

function dismantle() {
    uni.navigateTo({
        url: `/bundle/pages/repair/step/dismantle?id=${props.item?.id}`
    })
}
// 确认签名
function signCheck() {
    // if (props.item.auditStatus != MaterialApplyStatusEnum.TOCONFIRM)
    //     return toast(
    //         `当前审核状态为【${materialApplyStatusMap[props.item.auditStatus!]}】,不允许确认签名`
    //     )
    uni.navigateTo({
        url: `/bundle/pages/repair/step/sign?id=${props.item?.id}`
    })
}
// 取消领料
function cancelMaterial() {
    cancelPopup.value.showPopup()
}

function toCommonStep(step: any) {
    uni.navigateTo({
        url: `/bundle/pages/repair/step/step-common?id=${props.item?.id}&step=${step}`
    })
}

function toCheckStep(type: number) {
    uni.navigateTo({
        url: `/bundle/pages/repair/step/step-check?id=${props.item?.id}&type=${type}&clientId=${props.item.clientId}`
    })
}

// DC&PFC组装
function toPfcStep() {
    uni.navigateTo({
        url: `/bundle/pages/repair/step/step-pfc?id=${props.item?.id}`
    })
}

// 填写回单
function fillIn() {
    uni.navigateTo({
        url: `/bundle/pages/repair/step/fill-receipt?id=${props.item?.id}`
    })
}
</script>
<style scoped lang="scss">
:deep(button) {
    padding: 10rpx 20rpx;
}
.status {
    font-size: 28rpx;
    color: $red-1;
}
</style>
