<!--模型分类的新增编辑弹出框-->
<template>
    <el-dialog draggable class="cn-operate-dialog" v-model="machineVisible" :title="title" width="40%">
        <el-scrollbar>
            <el-form :inline="false" :model="form" label-width="120px" :rules="rules" ref="formRef">
                <el-form-item label="分类名称" prop="name">
                    <el-input v-model="form.name" placeholder="请输入分类名" clearable maxlength="32" show-word-limit/>
                </el-form-item>
                <el-form-item label="分类标识" prop="code">
                    <el-input v-model="form.code" placeholder="请输入分类标识" clearable maxlength="32" show-word-limit />
                </el-form-item>
                <el-form-item label="分类描述" prop="description">
                    <el-input
                        v-model="form.description"
                        placeholder="请输入分类描述"
                        clearable
                        type="textarea"
                    ></el-input>
                </el-form-item>
                <el-form-item label="排序">
                    <el-input-number style="width: 100%" v-model="form.sort" :min="0" placeholder="请输入排序" />
                </el-form-item>
            </el-form>
        </el-scrollbar>
        <template #footer>
            <span class="dialog-footer">
                <el-button @click="machineVisible = false">取消</el-button>
                <el-button type="primary" @click="submit">确认</el-button>
            </span>
        </template>
    </el-dialog>
</template>
<script lang="ts" setup>
import { ref, reactive, inject } from 'vue'
import { ElMessage } from 'element-plus'
import TableStore from '@/utils/tableStore'
import { addCategory, updateCategory } from '@/api/bpm-boot/category'

const tableStore = inject('tableStore') as TableStore
const machineVisible = ref(false)
const title = ref('')
const formRef = ref()

// 注意不要和表单ref的命名冲突
const form = reactive({
    id: '',
    name: '',
    code: '',
    description: '',
    sort: 100
})

//form表单校验规则
const rules = {
    name: [{ required: true, message: '分类名不能为空', trigger: 'blur' }],
    code: [{ required: true, message: '分类标识不能为空', trigger: 'blur' }]
}

const open = (text: string, data?: any) => {
    title.value = text
    if (data) {
        // 表单赋值
        for (let key in form) {
            form[key] = data[key]
        }
    } else {
        resetForm()
        // 在此处恢复默认表单
        for (let key in form) {
            form[key] = ''
        }
        form.sort=100
    }
    machineVisible.value = true
}

//重置表单内容
const resetForm = () => {
    if (formRef.value) {
        formRef.value.resetFields()
    }
}

/**
 * 提交用户表单数据
 */
const submit = () => {
    formRef.value.validate(async (valid: any) => {
        if (valid) {
            if (form.id) {
                await updateCategory(form)
            } else {
                await addCategory(form)
            }
            ElMessage.success('保存成功')
            tableStore.index()
            machineVisible.value = false
        }
    })
}

defineExpose({ open })
</script>

<style scoped>
.el-upload-list__item {
    transition: none !important;
}

.el-select {
    min-width: 180px;
}
</style>
