<template>
    <el-dialog draggable width="500px" v-model.trim="dialogVisible" :title="title">
        <el-form :model="form" class="form-one" label-width="auto" ref="formRef" :rules="rules">
            <el-form-item label="版本:" prop="edDataId">
                <el-select v-model.trim="form.edDataId" placeholder="请选择版本" @change="selectChange">
                    <el-option v-for="item in devList" :disabled="version == item.versionNo"
                        :label="`${item.devTypeName}_${item.versionNo}_${item.versionAgreement}_${item.versionDate}`"
                        :value="item.id"></el-option>
                </el-select>
            </el-form-item>

            <el-form-item label="描述:">
                <el-input v-model.trim="versionDesc" disabled type="textarea" :rows="3"></el-input>
            </el-form-item>
        </el-form>

        <template #footer>
            <span class="dialog-footer">
                <el-button @click="dialogVisible = false">取消</el-button>
                <el-button type="primary" @click="submit" :loading="loading">确认</el-button>
            </span>
        </template>
    </el-dialog>
</template>

<script lang="ts" setup>
import { ref, inject, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { findByDevTypeId, upgrade } from '@/api/system-boot/dictTree'
const formRef = ref()
const dialogVisible = ref(false)
const title = ref('程序版本')
const devList: any = ref([])
const versionDesc = ref('')
const version = ref('')
// 表单数据（新增了版本类型、版本描述字段）
const form = reactive<any>({
    devId: [],
    edDataId: '' // 新增：下拉选择版本
})
const loading = ref(false)
// 校验规则（新增两个字段的必填校验）
const rules = {
    // 新增校验
    edDataId: [{ required: true, message: '请选择版本', trigger: 'change' }]
}

// 打开弹窗
const open = (data: any) => {

    loading.value = false
    version.value = data.version
    form.devId = []
    form.edDataId = ''
    versionDesc.value = ''

    getFindByDev(data.devModel)
    formRef.value?.resetFields()
    dialogVisible.value = true
    form.devId = data.id
}
// 获取版本
const getFindByDev = (devModel: string) => {
    findByDevTypeId({
        devType: devModel, //'45afe3ad9970785090cd7da27f489c37',
        pageNum: 1,
        pageSize: 10000
    }).then((res: any) => {
        devList.value = res.data.records
        if (devList.value.length == 0) {
            ElMessage.warning('该设备没有可升级的版本!')
            return
        } else if (devList.value.length == 1 && devList.value[0].versionNo == version.value) {
            ElMessage.warning('该设备没有可升级的版本!')
        }

    })
}
// 提交
const submit = () => {
    formRef.value.validate(async (valid: boolean) => {
        if (valid) {
            try {
                ElMessage.info('升级中，请稍等...')
                loading.value = true
                upgrade({
                    devId: form.devId.join(),
                    edDataId: form.edDataId
                })
                    .then(res => {
                        ElMessage.success('升级成功')
                        loading.value = false
                        dialogVisible.value = false
                    })
                    .catch(error => {
                        loading.value = false
                    })
            } catch (error) {
                loading.value = false
                ElMessage.error('升级失败')
            }
        }
    })
}
const selectChange = e => {
    devList.value.forEach(item => {
        if (item.id === e) {
            versionDesc.value = item.description
        }
    })
}

defineExpose({ open })
</script>
