<!--
 * @Author: micky
 * @Date: 2024-09-07 20:48:32
 * @LastEditors: micky
 * @LastEditTime: 2024-11-05 19:02:07
 * @FilePath: \chargingpile-uniapp\src\bundle\pages\repair\step\dismantle.vue
-->
<template>
    <w-page-common
        btnText=""
        @submit="handleSubmit"
        :btnDisabled="btnDisabled"
        :needPadding="false"
    >
        <view class="bg-[#fffbe8] text-error px-3 py-1 text-base mb-3">
            拆机过程中发现内部没贴或者有贴部件码，请通知管理确认是否返修。
        </view>
        <TCard title="基本信息" :cardStyle="{ margin: '24rpx' }">
            <view class="c-form-item-wrapper py-[12rpx]">
                <view class="item">
                    <view>工单编号</view>
                    <view>
                        {{ detailForm.orderNo }}
                    </view>
                </view>
                <view class="item">
                    <view>模块代码</view>
                    <view>
                        {{ detailForm.moduleCode }}
                    </view>
                </view>
                <view class="item">
                    <view>模块型号</view>
                    <view>
                        {{ detailForm.moduleNumber }}
                    </view>
                </view>
                <view class="item">
                    <view>功率</view>
                    <view>
                        {{ parseModulePower(detailForm.modulePower!) }}
                    </view>
                </view>
            </view>
        </TCard>
        <TCard :cardStyle="{ margin: '15px 24rpx' }">
            <w-spareParts-info
                :sparePartList="sparePartList"
                showIcon
                @iconClick="iconClick"
                :nameInput="true"
            >
                <template #right>
                    <view class="text-primary text-base flex items-center" @click="addParts">
                        <u-icon name="plus" size="28rpx" color="paimary"></u-icon>
                        <view class="ml-1">添加部件</view>
                    </view>
                </template>
            </w-spareParts-info>
        </TCard>
        <TCard :cardStyle="{ margin: '0 24rpx' }">
            <TFeedBack v-model:remark="remark" v-model:imageList="fileList" />
        </TCard>
        <view class="px-3">
            <u-button
                text="提交"
                color="#2759FF"
                shape="circle"
                @click="handleSubmit"
                class="flex-1"
                size="middle"
            ></u-button>
        </view>
    </w-page-common>
</template>

<script setup lang="ts">
import { ref, unref } from 'vue'
import { toast } from '@/utils/util'
import { SPAREPARTS } from '@/enums/cacheEnums'
import cache from '@/utils/cache'
import { onLoad, onUnload } from '@dcloudio/uni-app'
import { apiDismantle, apiRepairDetail } from '@/api/repair'
import { useUploadValidate } from '@/hooks/useUploadValidate'
import { useModulePower } from '@/hooks/useLockFn'

interface SparePartsItem {
    id: number
    partCode: string
    partName: string
}
const pages = getCurrentPages() // 当前页面
const detailForm = ref<Partial<RepairItem>>({})
const orderId = ref('')
const sparePartList = ref<SparePartsItem[]>([])
const code = ref<string | undefined>('')
const { handleUploadFile, btnDisabled } = useUploadValidate()
const { parseModulePower } = useModulePower()
const iconClick = (index: number) => {
    sparePartList.value.splice(index, 1)
}

const addParts = () => {
    cache.set(SPAREPARTS, unref(sparePartList))

    const exist = pages.find(item => item.route == 'bundle/pages/repair/dismantle')
    if (exist) {
        uni.redirectTo({
            url: `/bundle/pages/scan/scan?type=6&id=${unref(detailForm).id}`
        })
    } else {
        uni.navigateTo({
            url: `/bundle/pages/scan/scan?type=6&id=${unref(detailForm).id}`
        })
    }
}

const remark = ref('')
const fileList: any = ref({
    repairImage: []
})

const handleSubmit = () => {
    if (unref(sparePartList)?.length === 0) return toast('请扫描录入部件')
    if (unref(fileList)?.repairImage?.length === 0) return toast('请上传图片')
    handleUploadFile(async (files: Record<string, string>) => {
        const params = {
            id: unref(orderId),
            orderPartCodeList: unref(sparePartList).map(item => {
                return {
                    partCode: item.partCode,
                    partName: item.partName
                }
            }),
            repairOrderStepRemark: unref(remark),
            repairOrderStepImg: unref(files).repairImage?.toString(),
            repairStep: unref(detailForm).repairNextStep
        }

        await apiDismantle(params).finally(() => {
            btnDisabled.value = false
        })
        toast('操作成功')
        setTimeout(() => {
            uni.reLaunch({
                url: '/pages/repair/task/index'
            })
        }, 300)
    }, unref(fileList))
}
const getDetail = async () => {
    const res = await apiRepairDetail({ id: unref(orderId) })
    detailForm.value = res
    if (res.orderPartCodeList?.length > 0 && cache.get(SPAREPARTS).length == 0) {
        sparePartList.value = res.orderPartCodeList
    }
}
onLoad(options => {
    orderId.value = options.id as string
    code.value = options.code
    if (options.code) {
        const cacheData = cache.get(SPAREPARTS) || []
        const arr = [...cacheData]
        if (cacheData?.findIndex(item => item.partCode == options.code) > -1) {
            sparePartList.value = arr
            toast(`扫描的部件编码【${options.code}】已存在`)
        } else {
            arr.push({
                id: cacheData.length + 1,
                partCode: code.value
            })
            sparePartList.value = arr
        }
    }
    getDetail()
})

// onUnload(() => {
//     uni.reLaunch({ url: '/pages/repair/task/index' })
// })
</script>
<style scoped lang="scss">
page {
    height: 100vh;
}
</style>
