<template>
    <el-dialog draggable width="400px" v-model="dialogVisible" :title="title">
        <el-form :inline="false" :model="form" label-width="auto" class="form-one" :rules="rules" ref="formRef">
            <el-form-item label="型号名称" prop="name">
                <el-input
                    v-model.trim="form.name"
                    clearable
                    maxlength="32"
                    show-word-limit
                    placeholder="请输入型号名称"
                />
            </el-form-item>
            <el-form-item label="关联icd" prop="icdId">
                <el-select v-model="form.icdId" placeholder="选择关联icd" filterable>
                    <el-option v-for="item in options" filterable :key="item.id" :label="item.name" :value="item.id" />
                </el-select>
            </el-form-item>
        </el-form>
        <template #footer>
            <span class="dialog-footer">
                <el-button @click="dialogVisible = false">取消</el-button>
                <el-button type="primary" @click="submit">确认</el-button>
            </span>
        </template>
    </el-dialog>
</template>
<script lang="ts" setup>
import { ref, inject, onMounted } from 'vue'
import { reactive } from 'vue'
import { getIcdList } from '@/api/device-boot/icd'
import { addDevType, updateDevType } from '@/api/device-boot/modelManage'
import { UploadInstance, UploadProps, UploadRawFile, ElMessage, genFileId } from 'element-plus'
const emit = defineEmits(['submit'])
const dialogVisible = ref(false)
const title = ref('')
const formRef = ref()
const form = reactive<anyObj>({
    name: '',
    icdId: '',
    id:''
})
const options: any = ref([])
const rules = {
    icdId: [{ required: true, message: '请选择关联icd', trigger: 'change' }],
    name: [{ required: true, message: '请输入型号名称', trigger: 'blur' }]
}

const open = (text: string, data?: anyObj) => {
    getIcdList().then(res => {
        options.value = res.data
    })
    form.icdId = data?.icdId
    form.name = data?.name
    form.id = data?.id
    title.value = text
    dialogVisible.value = true
}

onMounted(() => {})
const submit = () => {
    formRef.value.validate(async (valid: boolean) => {
        if (valid) {
            if (title.value == '新增终端型号') {
                addDevType(form).then(res => {
                    ElMessage.success('新增成功')
                    dialogVisible.value = false
                    emit('submit')
                })
            } else {
                updateDevType(form).then(res => {
                    ElMessage.success('编辑成功')
                    dialogVisible.value = false
                    emit('submit')
                })
            }
        }
    })
}

defineExpose({ open })
</script>
