<template>
  <div>
    <el-dialog v-model="dialogFormVisible" :title="props.formData.title" :before-close="closeDialog">
      <el-form :model="props.formData" ref="form" label-position="top" :rules="rules" label-width="100px">
        <el-form-item label="菜单名称" prop="menuName">
          <el-input v-model="props.formData.menuName" />
        </el-form-item>
        <el-form-item label="菜单类型" prop="menuType">
          <el-radio-group v-model="props.formData.menuType">
            <el-radio label="1">菜单组</el-radio>
            <el-radio label="2">菜单</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="菜单地址" prop="menuUrl">
          <el-input v-model="props.formData.menuUrl" />
        </el-form-item>
        <el-form-item label="菜单编码" prop="menuCode">
          <el-input v-model="props.formData.menuCode" />
        </el-form-item>
        <el-form-item label="是否展示" prop="showMode">
          <el-radio-group v-model="props.formData.showMode">
            <el-radio label="1">不展示</el-radio>
            <el-radio label="2">展示</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="props.formData.bz" :rows="3" type="textarea" />
        </el-form-item>
      </el-form>

      <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" @click="saveData"> <el-icon>
              <DocumentChecked />
            </el-icon>保存 </el-button>
          <el-button @click="closeDialog"> <el-icon>
              <DocumentDelete />
            </el-icon>关闭</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, getCurrentInstance } from "vue";
import { addSysMenu, editSysMenu } from "@/api/menu";
import { ElMessage } from "element-plus";
const { proxy } = getCurrentInstance();
const rules = {
  menuName: [{ required: true, message: "请输入菜单名称", trigger: "blur" }],
  menuType: [{ required: true, message: "请选择菜单类型", trigger: "blur" }],
  menuUrl: [{ required: true, message: "请输入菜单地址", trigger: "blur" }],
  menuCode: [{ required: true, message: "请输入菜单编码", trigger: "blur" }],
  showMode: [{ required: true, message: "请选择是否展示", trigger: "blur" }]
};
const dialogFormVisible = ref(false);
const form = ref(null);
const emits = defineEmits(["saveSuccess"]);
// 接收子组件传来的
const props = defineProps({
  formData: {
    type: Object,
    default: {}
  }
});
const closeDialog = () => {
  if (!props.isShow) {
    proxy.$modal.confirm('离开会丢失页面中修改的内容，确认离开吗？')
      .then(() => {
        dialogFormVisible.value = false
      })
      .catch(() => {
        // catch error
      })
  } else {
    dialogFormVisible.value = false
  }

};
// 保存数据
const saveData = () => {
  form.value.validate((valid) => {
    if (valid) {
      if (!props.formData.id) {
        addSysMenu(props.formData).then((res) => {
          if (res) {
            ElMessage.success("保存成功");
            emits("saveSuccess");
          }
        });
      } else {
        editSysMenu(props.formData).then((res) => {
          if (res) {
            ElMessage.success("修改成功");
            emits("saveSuccess");
          }
        });
      }
    }
  });
};
defineExpose({
  dialogFormVisible
});
</script>

<style lang="scss" scoped>
.el-form {
  display: block;

  .el-form-item {
    width: 100%;
  }
}
</style>
