<template>
    <el-dialog width='500px' v-model.trim='dialogVisible' :title='title'>
        <el-scrollbar>
            <el-form :inline='false' :model='form' label-width='auto' class="form-one" :rules='rules' ref='formRef'>
                <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='编码' class='top' prop='code'>
                    <el-input maxlength="32" show-word-limit    v-model.trim='form.code' placeholder="请输入字典编码"></el-input>
                </el-form-item>

               
                <el-form-item label='开启等级' class='top'>
                    <el-select v-model.trim='form.openLevel' placeholder='选择开启等级'>
                        <el-option v-for='item in OpenLevel' :key='item.value' :label='item.label' :value='item.value'>
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label='开启算法' class='top'>
                    <el-select v-model.trim='form.openDescribe' placeholder='选择开启算法'>
                        <el-option v-for='item in OpenDescribe' :key='item.value' :label='item.label'
                            :value='item.value'>
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label='字典描述' class='top'>
                    <el-input  show-word-limit  maxlength="300"  v-model.trim='form.remark' placeholder='请输入字典描述'
                        type='textarea' :rows='2'></el-input>
                </el-form-item>
                <el-form-item label='排序' class='top' prop='sort'>
                    <el-input-number v-model.trim="form.sort" :min="0"  style="width: 100%;"/>
                </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' :loading="loading">确定</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 { dictTypeAdd, dictTypeUpdate } from '@/api/system-boot/dictType'

const adminInfo = useAdminInfo()
const tableStore = inject('tableStore') as TableStore
const loading = ref(false)
const OpenLevel = [
    { value: 0, label: '不开启' },
    { value: 1, label: '开启' }
]
const OpenDescribe = [
    { value: 0, label: '不开启' },
    { value: 1, label: '开启' }
]
const formRef = ref()
const form = reactive({
    openLevel: 0,
    openDescribe: 0,
    remark: '',
    name: '',
    code: '',
    sort: 100,
    id: ''
})
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.openLevel = 0
        form.openDescribe = 0
        form.sort = 100
    }
}
const submit = async () => {
    formRef.value.validate(async (valid: boolean) => {
        if (valid) {
            loading.value = true
            if (form.id) {
                await dictTypeUpdate(form)
            } else {
                await dictTypeAdd(form)
            }
            ElMessage.success('保存成功')
            tableStore.index()
            dialogVisible.value = false
            loading.value = false
        }
    })
}

defineExpose({ open })
</script>
