<template>
    <el-dialog draggable class="cn-operate-dialog" v-model="dialogVisible" :title="title">
        <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" maxlength="32" show-word-limit @input="handleInput" clearable></el-input>
                </el-form-item>
                <el-form-item label="编码" class="top" prop="code">
                    <el-input v-model="form.code" maxlength="32" show-word-limit clearable></el-input>
                </el-form-item>

                <el-form-item label="排序" class="top" prop="sort">
                    <el-input-number v-model="form.sort" :min="0" />
                </el-form-item>
                <el-form-item label="开启等级" class="top">
                    <el-select v-model="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="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 v-model="form.remark" type="textarea" :rows="2"></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 { dictTypeAdd, dictTypeUpdate } from '@/api/system-boot/dictType'

const adminInfo = useAdminInfo()
const tableStore = inject('tableStore') as TableStore
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) => {
    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) {
            if (form.id) {
                await dictTypeUpdate(form)
            } else {
                await dictTypeAdd(form)
            }
            ElMessage.success('保存成功')
            tableStore.index()
            dialogVisible.value = false
        }
    })
}

const handleInput = (value: string) => {
    form.name = value.replace(/\s/g, '')
    //form.name = value.replace(/[^\d]/g, '')
    
}

defineExpose({ open })
</script>
