<template>
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="100">
        <el-form-item label="类型" prop="type">
            <el-radio-group v-model="formData.type" :disabled="type === 'edit'">
                <el-radio label="菜单" border>菜单</el-radio>
                <el-radio label="按钮" border>按钮</el-radio>
            </el-radio-group>
        </el-form-item>
        <el-form-item label="上级菜单" prop="fatherMenu">
            <el-select v-model="formData.fatherMenu" placeholder="请选择上级菜单" clearable>
                <el-option v-for="item in fatherMenuOption" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
        </el-form-item>
        <el-form-item label="菜单名称" prop="menuName">
            <el-input v-model="formData.menuName" placeholder="请输入菜单名称"></el-input>
        </el-form-item>
        <el-form-item label="图标" prop="icon">
            <i :class="formData.icon" style="font-size: 26px;margin: 0 10px;"></i>
            <el-button @click="isIconDialog = true">选择图标</el-button>
        </el-form-item>
        <el-form-item label="路由" prop="route">
            <el-input v-model="formData.route" placeholder="请输入路由"></el-input>
        </el-form-item>
        <el-form-item label="排序" prop="order">
            <el-input class="number-input" v-model="formData.order" type="number" input-style="text-align: center;" @change="numberInputChange">
                <template #prepend>
                    <el-button text icon="Minus" :disabled="formData.order <= 1" @click="formData.order--" />
                </template>
                <template #append>
                    <el-button text icon="Plus" @click="formData.order++" />
                </template>
            </el-input>
        </el-form-item>
        <el-form-item label="是否显示" prop="isShow">
            <el-radio-group v-model="formData.isShow">
                <el-radio :label="true" border>是</el-radio>
                <el-radio :label="false" border>否</el-radio>
            </el-radio-group>
        </el-form-item>

        <div style="text-align: right;padding-top: 0;">
            <el-button class="f18" @click="close">取消</el-button>
            <el-button class="f18" type="primary" @click="saveData">确认</el-button>
        </div>
    </el-form>

    <el-dialog v-model="isIconDialog" title="选择图标" top="20vh" width="25%">
        <div class="iconfont-box">
            <div class="iconfont-item" v-for="item in iconfont.glyphs" :key="item.font_class" @click="iconClick(item)">
                <i :class="'icon-' + item.font_class"></i>
            </div>
        </div>
    </el-dialog>
</template>

<script lang='ts' setup>
import { onMounted, reactive, ref, toRefs, watch } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import iconfont from "@/assets/font/iconfont.json";

const emit = defineEmits(['close'])

const props = defineProps({
    type: String
})

const fatherMenuOption = [
    { label: '权限管理', value: '权限管理' },
    { label: '患者管理', value: '患者管理' },
]
const rules = reactive({
    menuName: [
        { required: true, message: '请输入菜单名称', trigger: ['blur', 'change'] },
    ],
    route: [
        { required: true, message: '请输入路由地址', trigger: ['blur', 'change'] },
    ]
})

const formRef = ref()
const isIconDialog = ref(false)
const formData = ref({} as any)


onMounted(() => {
    resetData()
    console.log(iconfont.glyphs)
})

defineExpose({
    formData,
    resetData,
})
function close() {
    emit('close')
}
function resetData() {
    formRef.value.resetFields()
    formData.value = {
        id: '',
        type: '菜单',
        fatherMenu: '',
        menuName: '',
        icon: '',
        route: '',
        order: 1,
        isShow: true
    }
}

const numberInputChange = (e: any) => {
    if(!Number(e)) {
        formData.value.order = 1
    }
}
const iconClick = (e: any) => {
    formData.value.icon = 'icon-' + e.font_class
    isIconDialog.value = false
}
const saveData = async () => {
    await formRef.value.validate((valid: any, fields: any) => {
        if (valid) {
            ElMessage.success('保存成功！')
            close()
        } else {
            console.log('error submit!', fields)
        }
    })
}
</script>

<style lang='scss' scoped>
:deep(.el-form-item) {
    .el-form-item__label {
        display: block;
        // text-align: justify;
        // text-align-last: justify;
        padding: 0 10px 0 20px;

        &:before {
            display: none;
        }
    }
}
.iconfont-box {
    display: flex;
    flex-wrap: wrap;
    max-height: 300px;
    .iconfont-item {
        font-size: 30px;
        padding: 10px;
        &:hover {
            background: rgba($main-color, .1);
        }
        &:focus, &:active {
            background: rgba($main-color, .2);
            color: $main-color;
        }
    }
}
</style>
