<!--
 * @Author: micky
 * @Date: 2024-08-13 19:19:02
 * @LastEditors: micky
 * @LastEditTime: 2024-10-28 18:02:23
 * @FilePath: \chargingpile-uniapp\src\bundle\pages\repair\step\fill-receipt.vue
-->
<template>
    <w-page-common btnText="提交" @submit="handleSubmit">
        <TCard>
            <view class="c-form-item-wrapper">
                <view class="whole-row-label">故障现象</view>
                <w-fault-select
                    :type="FaultTypeEnum.PHENOMENON"
                    v-model="formData.faultPhenomenonId"
                    placeholder="请选择故障现象"
                />
            </view>
            <view class="c-form-item-wrapper">
                <view class="whole-row-label required">故障原因</view>
                <w-fault-select
                    :type="FaultTypeEnum.PROBLEM"
                    v-model="formData.faultCauseId"
                    placeholder="请选择故障原因"
                />
            </view>
            <view class="c-form-item-wrapper">
                <view class="whole-row-label required">损坏原因</view>
                <w-fault-select
                    :type="FaultTypeEnum.CAUSEOFDAMAGE"
                    v-model="formData.sourceOfDamageId"
                    placeholder="请选择损坏原因"
                />
            </view>
            <view class="c-form-item-wrapper">
                <view class="whole-row-label required">运维建议</view>
                <w-fault-select
                    :type="FaultTypeEnum.SUGGESTION"
                    v-model="formData.operationsSuggestId"
                    placeholder="请选择运维建议"
                />
            </view>
            <view class="spartparts px-4" v-show="detail?.sparePartList?.length > 0">
                <view class="c-input-field-item">
                    <view class="label">配件清单</view>
                    <view class="flex items-center text-primary" @click="choose">
                        <u-icon name="plus" color="#0e66fb" size="15"></u-icon>
                        <text class="ml-1" @click="addItem">添加明细</text>
                    </view>
                </view>
                <replaceParts
                    ref="partsListRef"
                    :sparepartsList="detail.sparePartList"
                    v-model="spareList"
                    :initialSparesList="initialSparesList"
                />
            </view>
        </TCard>
    </w-page-common>
</template>

<script setup lang="ts">
import { FaultTypeEnum } from '@/enums/businessEnums'
import { ref, unref } from 'vue'
import { onLoad } from '@dcloudio/uni-app'
import { apiRepairDetail, apiCommonStep } from '@/api/repair'
import { toast } from '@/utils/util'
import replaceParts from '../components/replace-parts.vue'

const detail = ref<any>({})
const formData = ref<any>({
    faultCauseId: '',
    faultPhenomenonId: '',
    sourceOfDamageId: '',
    operationsSuggestId: '',
    id: ''
})
const spareList = ref<PartsListItem[]>([])
const partsListRef = ref()
const initialSparesList = ref([])
const choose = () => {}
const addItem = () => {
    partsListRef.value.addItem()
}

const handleSubmit = async () => {
    const form = unref(formData)
    if (!form.faultCauseId) return toast('请选择故障原因')
    if (!form.sourceOfDamageId) return toast('请选择损坏原因')
    if (!form.operationsSuggestId) return toast('请选择运维建议')
    // if (unref(spareList).length === 0) return toast('请填写配件清单')
    const empty = unref(spareList).find(item => {
        return !item.repairMotherboard || !item.materialPositionNumber || !item.sparePartsId
    })
    if (empty) return toast('请填写完整的配件信息')
    const data = {
        ...unref(formData),
        repairStep: unref(detail).repairNextStep,
        sparePartUseParams: unref(spareList).map(item => {
            return {
                id: item.sparePartsId,
                useNumber: item.count,
                materialPositionNumber: item.materialPositionNumber,
                repairMotherboard: item.repairMotherboard
            }
        })
    }
    await apiCommonStep(data)
    toast('操作成功')
    uni.navigateBack({ delta: 1 })
}

const getDetail = async () => {
    const res = await apiRepairDetail({
        id: unref(formData).id
    })
    detail.value = res
    initialSparesList.value = uni.$u.deepClone(res.sparePartList)
    if (res.repairOrderAuditListVo) {
        const {
            faultCauseId,
            faultPhenomenonId,
            sourceOfDamageId,
            operationsSuggestId,
            partsUseList
        } = res.repairOrderAuditListVo
        formData.value.faultCauseId = faultCauseId
        formData.value.faultPhenomenonId = faultPhenomenonId
        formData.value.sourceOfDamageId = sourceOfDamageId
        formData.value.operationsSuggestId = operationsSuggestId
        if (partsUseList) {
            partsUseList.map(item => {
                partsListRef.value.addItem(item)
            })
        }
    }
}

onLoad(options => {
    formData.value.id = options.id as string
    getDetail()
})
</script>
