<!--
 * @Author: micky
 * @Date: 2024-09-08 19:23:35
 * @LastEditors: micky
 * @LastEditTime: 2024-10-29 18:05:26
 * @FilePath: \chargingpile-uniapp\src\bundle\pages\repair\step\step-common.vue
-->
<template>
    <w-page-common
        btnText="提交"
        @submit="handleSubmit"
        :btnDisabled="btnDisabled"
        :needPadding="false"
    >
        <view
            class="bg-[#fffbe8] text-error px-3 py-1 text-base"
            v-if="detail?.repairNextStep == RepairStepEnum.INSTORAGE"
        >
            请将维修好的模块放到指定区域后，再进行良品入库操作
        </view>
        <view class="px-[24rpx] pt-[20rpx]">
            <TCard v-if="showIfPass">
                <view class="c-input-field-item px-[12px]">
                    <text class="label">是否通过</text>
                    <u-switch
                        v-model="formData.isPass"
                        :activeValue="YesOrNoEnum.YES"
                        :inactiveValue="YesOrNoEnum.NO"
                    ></u-switch>
                </view>
                <view
                    class="text-error px-3 py-2 text-base"
                    v-if="formData.isPass == YesOrNoEnum.NO"
                >
                    测试不通过，稍后可以进入工单详情->工单步骤更新相应步骤的数据
                </view>
            </TCard>
            <TCard :cardStyle="showIfPass ? { marginTop: '15px' } : {}">
                <TFeedBack v-model:remark="formData.remark" v-model:imageList="fileList" />
            </TCard>
        </view>
    </w-page-common>
</template>

<script setup lang="ts">
import { computed, ref, unref } from 'vue'
import { toast } from '@/utils/util'
import { apiRepairDetail, apiCommonStep } from '@/api/repair'
import { onLoad } from '@dcloudio/uni-app'
import { YesOrNoEnum, RepairStepEnum, repairStepMap } from '@/enums/businessEnums'
import { useUploadValidate } from '@/hooks/useUploadValidate'

const detail = ref<Partial<RepairItem>>({})
const orderId = ref('')
const formData = ref({
    remark: '',
    isPass: YesOrNoEnum.YES,
    id: ''
})
// 是否是更新
const isUpdate = ref('')
const stepId = ref('')
// 需要特殊处理，不是直接拿接口步骤的
const setStep = ref('')
const fileList: any = ref({
    repairImage: []
})
const { handleUploadFile, btnDisabled } = useUploadValidate()

const showIfPass = computed(() => {
    return (
        detail?.value.repairNextStep == RepairStepEnum.FEATURE ||
        detail?.value?.repairNextStep == RepairStepEnum.AGING
    )
})

const handleSubmit = () => {
    const form = unref(formData)
    if (unref(fileList)?.repairImage?.length === 0) return toast('请上传图片')
    handleUploadFile(async files => {
        const params: any = {
            id: unref(orderId),
            repairOrderStepRemark: form.remark,
            repairOrderStepImg: unref(files).repairImage?.toString(),
            repairStep: unref(detail).repairNextStep
        }
        params.changeStepOrNot = isUpdate.value
        if (stepId.value) params.repairOrderStepDataId = stepId.value
        if (unref(detail).repairNextStep == RepairStepEnum.FEATURE) {
            params.functionalTestPassed = form.isPass
        } else if (unref(detail).repairNextStep == RepairStepEnum.AGING) {
            params.agingTestPassed = form.isPass
        }

        await apiCommonStep(params).finally(() => {
            btnDisabled.value = false
        })
        toast('操作成功')
        setTimeout(() => {
            uni.navigateBack()
        }, 300)
    }, unref(fileList))
}

// const isContinue = () => {
//     if (!useJudgeApprentice()) return false
//     const { repairNextStep } = detail.value

//     return [
//         RepairStepEnum.BEFOREREPAIR,
//         RepairStepEnum.ONREPAIRING,
//         RepairStepEnum.AFTERREPAIR
//     ].includes(+repairNextStep)
// }

const getDetail = async () => {
    const res = await apiRepairDetail({ id: unref(orderId) })
    // 路由跳转过来携带步骤，那么需要特殊处理
    if (setStep.value) {
        res.repairNextStep = setStep.value
    }
    const obj = res.repairOrderStepDataList.find(item => item.repairStep == res.repairNextStep)
    if (obj) {
        formData.value.remark = obj.remark
        if (obj.img) {
            fileList.value['repairImage'] = obj.img?.split(',')?.map(img => {
                return {
                    url: img
                }
            })
        }
        if (obj.auditStatus != null) formData.value.isPass = obj.auditStatus
    }

    detail.value = res

    uni.setNavigationBarTitle({ title: repairStepMap[res.repairNextStep] })
}

onLoad(options => {
    orderId.value = options.id as string
    if (options.step != 'undefined') setStep.value = options.step!
    if (options.isUpdate == 'true') isUpdate.value = YesOrNoEnum.NO
    else isUpdate.value = YesOrNoEnum.YES
    if (options.stepId != 'undefined') stepId.value = options.stepId
    getDetail()
})
</script>
<style scoped lang="scss"></style>
