
<template>
    <base-dialog :title="prop.title" width="60%" :visible="prop.visible" :autoHeight="false" @open="handleOpen"
        @handleConfirm="handleConfirm" @close="handleClose">
        <div class="body">
            <el-form ref="formRef" :model="prop.form" label-width="120px" :rules="rule">
                <el-form-item label="类型名称" prop="name">
                    <el-input v-model="prop.form.name" placeholder="类型名称" />
                </el-form-item>
                <el-form-item label="需要处理的属性">
                    <el-input v-model="prop.form.fieldIncludeData" placeholder="需要处理的属性" />
                </el-form-item>
                <el-form-item label="需要排除的属性">
                    <el-input v-model="prop.form.fieldExcludeData" placeholder="需要排除的属性" />
                </el-form-item>
                <el-form-item label="说明" prop="desc">
                    <el-input  v-model="prop.form.desc" placeholder="说明" />
                </el-form-item>
            </el-form>
        </div>
    </base-dialog>
</template>
   
<script setup>
import { ref, watch, getCurrentInstance } from 'vue'
import { addDeviceType, updateDeviceType } from '@/api/big/deviceType'

const prop = defineProps({
    visible: {
        type: Boolean,
        default: false,
    },
    title: {
        type: String,
        default: '提示',
    },
    type: {
        type: String,
        default: 'add',
    },
    form: {
        type: Object,
        default: {
            name:String,
            fieldIncludeData:String,
            fieldExcludeData:String,
            desc:String
        },
    }
})
const formRef = ref(null)
const rule = {
    name: [
        { required: true, message: '请输入类型名称', trigger: 'blur' },
    ],
    // fieldIncludeData: [
    //     { required: true, message: '请输入需要处理的属性', trigger: 'blur' },
    // ],
    // fieldExcludeData: [
    //     { required: true, message: '请输入需要排除的属性', trigger: 'blur' },
    // ],
    desc: [
        { required: true, message: '请输入说明', trigger: 'blur' },
    ],
}
const { proxy: ctx } = getCurrentInstance()

const handleClose = () => {
    emit('closeDialog')
    emit('refreshTable')
}
// 弹窗弹出
const handleOpen = () => {
    // getAddFormOptions()
}


// 刷新表格事件
const emit = defineEmits(['refreshTable', 'closeDialog'])
// 添加/修改
async function addDeductionInfo() {

    if (!formRef.value) return
    let isValid = false
    await formRef.value.validate((valid, fields) => {
        if (valid) {
            isValid = true
            console.log('submit!')
        } else {
            console.log('error submit!', fields)
            isValid = false
        }
    })
    if (!isValid) return
    //增加
    if (prop.type == 'add') {
        let { code, msg } = await addDeviceType(prop.form)
        ctx.$message({
            type: code === 200 ? 'success' : 'warning',
            message: code === 200 ? '添加成功' : '添加失败',
        })
        if (code === 200) {
            // 刷新表格
            emit('refreshTable')
            emit('closeDialog')
        }
    } else { //修改
        let { code, msg } = await updateDeviceType(prop.form)
        ctx.$message({
            type: code === 200 ? 'success' : 'warning',
            message: code === 200 ? '修改成功' : '修改失败',
        })
        if (code === 200) {
            // 刷新表格
            emit('refreshTable')
            emit('closeDialog')
        }
    }


}
// 点击确定
const handleConfirm = () => {
    addDeductionInfo()
}



</script>
   
<style lang="scss" scoped>
.body {
    height: 100%;
}

.img-preview {
    max-width: 100%;
    max-height: 100%;
    display: block;
    margin: 0 auto;
    padding-top: 10px;
}
</style>