<template>
  <el-dialog
    class="modify-menu"
    :visible.sync="modalIsShow"
    :title="title"
    top="10vh"
    width="500px"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    append-to-body
    @opened="visibleChange"
  >
    <div class="part-box part-box-pad part-box-border">
      <el-form
        ref="modalFormComp"
        :model="modalForm"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item prop="name" label="菜单名称：">
          <el-input
            style="width:100%;"
            v-model.trim="modalForm.name"
            placeholder="请输入菜单名称"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item prop="url" label="URL地址：">
          <el-input
            style="width:100%;"
            v-model.trim="modalForm.url"
            placeholder="请输入URL地址"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="上级菜单：">
          <el-input
            style="width:100%;"
            v-model.trim="modalForm.parentName"
            disabled
          ></el-input>
        </el-form-item>
        <el-form-item label="菜单类型：">
          <el-select
            v-model="modalForm.type"
            style="width: 100px;"
            placeholder="请选择"
          >
            <el-option
              v-for="(val, key) in PRIVILEGE_TYPE"
              :key="key"
              :value="key"
              :label="val"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注：">
          <el-input
            style="width:100%;"
            v-model.trim="modalForm.remark"
          ></el-input>
        </el-form-item>
      </el-form>
    </div>
    <div slot="footer">
      <el-button type="primary" :disabled="isSubmit" @click="submit"
        >确认</el-button
      >
      <el-button type="danger" @click="cancel" plain>取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { PRIVILEGE_TYPE } from "@/constants/enumerate";
import { updateMenu } from "../api";

const initModalForm = {
  id: null,
  name: "",
  url: "",
  parentId: null,
  parentName: "",
  sortNumber: 1,
  type: "M",
  remark: ""
};

export default {
  name: "modify-menu",
  props: {
    instance: {
      type: Object,
      default() {
        return {};
      }
    }
  },
  computed: {
    isEdit() {
      return !!this.instance.id;
    },
    title() {
      return (this.isEdit ? "编辑" : "新增") + "权限";
    }
  },
  data() {
    return {
      modalIsShow: false,
      isSubmit: false,
      modalForm: {},
      PRIVILEGE_TYPE,
      rules: {
        name: [
          {
            required: true,
            pattern: /^[\u4E00-\u9FA5/]{1,30}$/,
            message: "菜单名称只能输入汉字和斜线，长度不能超过30",
            trigger: "change"
          }
        ],
        url: [
          {
            required: true,
            pattern: /^[0-9a-zA-Z_-]{3,30}$/,
            message:
              "URL地址只能由字母、数字、短横线和下划线组成，长度在3-30之间",
            trigger: "change"
          }
        ]
      }
    };
  },
  methods: {
    initData(val) {
      this.modalForm = this.$objAssign(initModalForm, val);
    },
    visibleChange() {
      this.initData(this.instance);
      this.$nextTick(() => {
        this.$refs.modalFormComp.clearValidate();
      });
    },
    cancel() {
      this.modalIsShow = false;
    },
    open() {
      this.modalIsShow = true;
    },
    async submit() {
      const valid = await this.$refs.modalFormComp.validate().catch(() => {});
      if (!valid) return;

      if (this.isSubmit) return;
      this.isSubmit = true;
      const datas = { ...this.modalForm };
      const data = await updateMenu(datas).catch(() => {});
      this.isSubmit = false;
      if (!data) return;

      this.$emit("confirm", this.modalForm);
      this.cancel();
    }
  }
};
</script>
