<template>
    <el-dialog class="cn-operate-dialog" width="500px" v-model.trim="dialogVisible" :title="title">
        <el-scrollbar>
            <el-form :inline="false" :model="form" ref="formRef" label-width="auto" :rules="rules">
                <el-form-item label="字典名称:" prop="name">
                    <el-input maxlength="32" show-word-limit 
                        
                        
                        v-model.trim="form.name"
                        placeholder="请输入字典名称"
                    ></el-input>
                </el-form-item>

                <el-form-item label="编码:" prop="code" class="top">
                    <el-input maxlength="32" show-word-limit 
                        
                        
                        v-model.trim="form.code"
                        placeholder="请输入字典编码"
                    ></el-input>
                </el-form-item>
                <el-form-item label="排序:" prop="sort" class="top">
                    <el-input maxlength="32" show-word-limit 
                        
                        
                        v-model.number="form.sort"
                        placeholder="请输入排序"
                    ></el-input>
                </el-form-item>
                <el-form-item label="描述:" class="top">
                    <el-input maxlength="300" show-word-limit 
                        
                        :autosize="{ minRows: 2, maxRows: 4 }"
                        type="textarea"
                        
                        v-model.trim="form.remark"
                        placeholder="请输入字典描述"
                    ></el-input>
                </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 { ElMessage } from 'element-plus'
import { useAdminInfo } from '@/stores/adminInfo'
import { dicAdd, dicUpdate } from '@/api/system-boot/dic'

const adminInfo = useAdminInfo()
const tableStore = inject('tableStore') as TableStore
// do not use same name with ref
const form = reactive({
    name: '',
    code: '',
    remark: '',
    sort: 100,
    type: 0,
    pid: 0,
    id: ''
})
const formRef = ref()
const rules = {
    name: [{ required: true, message: '请选择名称', trigger: 'blur' }],
    sort: [{ required: true, message: '请选择排序', trigger: 'blur' }],
    code: [{ required: true, message: '请选择编码', trigger: 'blur' }]
}
const dialogVisible = ref(false)
const title = ref('新增菜单')
const open = (text: string, data?: anyObj) => {
    formRef.value?.resetFields()
    title.value = text
    dialogVisible.value = true
    if (data) {
        for (let key in form) {
            form[key] = data[key]
        }
    } else {
        for (let key in form) {
            form[key] = ''
        }
        form.sort = 100
        form.pid = 0
        form.type = 0
    }
}
const submit = async () => {
    formRef.value.validate(async (valid: any) => {
        if (valid) {
            if (form.id) {
                await dicUpdate(form)
            } else {
                await dicAdd(form)
            }
            ElMessage.success('保存成功')
            tableStore.index()
            dialogVisible.value = false
        }
    })
}

defineExpose({ open })
</script>
