<template>
    <el-dialog class="cn-operate-dialog" width="500px" v-model.trim="dialogVisible" :title="title">
        <el-scrollbar>
            <el-form :inline="false" :model="form" label-width="auto" ref="formRef" class="form-one" :rules="rules">
                <el-form-item label="上级菜单">
                    <el-cascader
                        v-model.trim="form.pid"
                        :options="tableStore.table.data"
                        :props="cascaderProps"
                        clearable
                        style="width: 100%"
                    />
                </el-form-item>
                <el-form-item label="菜单名称" prop="name">
                    <el-input maxlength="32" show-word-limit    v-model.trim="form.name" placeholder="请输入菜单名称" />
                </el-form-item>
                <el-form-item label="图标">
                    <IconSelector v-model.trim="form.icon" placeholder="请选择图标" />
                </el-form-item>
                <el-form-item label="菜单路由" prop="path">
                    <el-input   v-model.trim="form.path" placeholder="请输入菜单名称" />
                </el-form-item>
                <el-form-item label="组件路径" prop="routeName">
                    <el-input 
                        v-model.trim="form.routeName"
                        placeholder="请输入组件路径，如/src/views/dashboard/index.vue"
                    />
                </el-form-item>
                <el-form-item label="排序" prop="sort">
                    <el-input-number v-model.number="form.sort" style="width: 100%;" :min="0" />
                </el-form-item>
                <el-form-item label="菜单描述">
                    <el-input  show-word-limit 
                        maxlength="300"
                        
                        v-model.trim="form.remark"
                        :rows="2"
                        type="textarea"
                        placeholder="请输入描述"
                    />
                </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 IconSelector from '@/components/baInput/components/iconSelector.vue'
import { updateMenu, addMenu } from '@/api/systerm'
import { ElMessage } from 'element-plus'
defineOptions({
    name: 'auth/menu/popupMenu'
})
const formRef = ref()
const loading = ref(false)
const emits = defineEmits<{
    (e: 'init'): void
}>()
const tableStore = inject('tableStore') as TableStore
const cascaderProps = {
    label: 'title',
    value: 'id',
    checkStrictly: true,
    emitPath: false
}
const form: any = reactive({
    code: '',
    icon: '',
    id: '',
    name: '',
    path: '',
    pid: '0',
    remark: '',
    routeName: '',
    sort: 100,
    type: 0
})

const dialogVisible = ref(false)
const title = ref('新增菜单')
const rules = {
    name: [{ required: true, message: '请输入菜单名称', trigger: 'blur' }],
    path: [{ required: true, message: '请输入菜单路由', trigger: 'blur' }],
    icon: [{ required: true, message: '请选择图标', trigger: 'blur' }],
    routeName: [{ required: true, message: '请输入组件路径', trigger: 'blur' }],
    sort: [{ required: true, message: '请输入排序', trigger: 'blur' }]
}
const open = (text: string, data: anyObj) => {
    formRef.value?.resetFields()
    title.value = text
    // 重置表单
    for (let key in form) {
        form[key] = ''
    }
    form.pid = data.pid || '0'
    form.sort = 100
    form.type = 0

    if (data.id) {
        for (let key in form) {
            form[key] = data[key] ? data[key] : data[key] === 0 ? 0 : ''
        }
        form.path = data.routePath || ''
        form.name = data.title || ''
    }
    dialogVisible.value = true
}
const submit = async () => {
    formRef.value.validate(async valid => {
        if (valid) {
            loading.value = true
            if (form.id) {
                form.pid = form.pid || '0'
                await updateMenu(form).then(() => {
                    ElMessage.success('编辑成功!')
                }).finally(() => {
                    loading.value = false
                })
            } else {
                form.code = 'menu'
                form.pid = form.pid || '0'
                let obj = JSON.parse(JSON.stringify(form))
                delete obj.id

                await addMenu(obj).then(() => {
                    ElMessage.success('新增成功!')
                }).finally(() => {
                    loading.value = false
                })
            }
            emits('init')
            dialogVisible.value = false
        }
    })
}

defineExpose({ open })
</script>
