<template>
    <el-dialog class="cn-operate-dialog" v-model="dialogVisible" :title="title">
        <el-scrollbar>
            <el-form :inline="false" :model="form" label-width="120px">
                <el-form-item label="名称">
                    <el-input v-model="form.name" placeholder="请输入名称" />
                </el-form-item>
                <el-form-item label="编码">
                    <el-input v-model="form.code" placeholder="请输入编码" />
                </el-form-item>
                <el-form-item label="备注">
                    <el-input v-model="form.remark" placeholder="请输入备注" />
                </el-form-item>
                <el-form-item label="排序">
                    <el-input-number v-model="form.sort" :min="0" />
                </el-form-item>
            </el-form>
        </el-scrollbar>

        <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 } from 'vue'
import { reactive } from 'vue'
import TableStore from '@/utils/tableStore'
import { addDictTree, updateStatistical } from '@/api/system-boot/dictTree'
import { ElMessage } from 'element-plus'

const emits = defineEmits<{
    (e: 'over'): void
}>()
defineOptions({
    name: 'govern/setting/statisticalType/add'
})
const tableStore = inject('tableStore') as TableStore
const form = reactive({
    name: '',
    code: '',
    remark: '',
    sort: 100,
    pid: '',
    id: ''
})

const dialogVisible = ref(false)
const title = ref('新增菜单')
const open = (text: string, data?: anyObj) => {
    title.value = text
    for (let key in form) {
        form[key] = data ? data[key] : ''
    }
    dialogVisible.value = true
}
const submit = async () => {
    // 非空校验
    for (let key in form) {
        if (key != 'pid' && key != 'id' && key != 'sort') {
            if (!form[key]) {
                ElMessage.warning('请填写完整信息')
                return
            }
        }
    }
    if (form.id) {
        await updateStatistical(form).then(res => {
            ElMessage.success('编辑成功')
        })
    } else {
        if (!form.pid) {
            form.pid = tableStore.table.params.pid
        }
        await addDictTree(form).then(res => {
            ElMessage.success('新增成功')
        })
    }
    emits('over')
    dialogVisible.value = false
}

defineExpose({ open })
</script>
