<template>
  <el-dialog
    class="modify-privilege"
    :visible.sync="modalIsShow"
    :title="title"
    top="10vh"
    width="600px"
    :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
            v-model.trim="modalForm.name"
            placeholder="请输入菜单名称"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item prop="url" label="URL地址：">
          <el-input
            v-model.trim="modalForm.url"
            placeholder="请输入URL地址"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="上级菜单：">
          <el-input 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 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 @click="cancel">取消</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: "",
  sequence: 1,
  type: "MENU",
  remark: "",
};

export default {
  name: "modify-privilege",
  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,
            max: 30,
            message: "菜单名称不能超过30",
            trigger: "change",
          },
        ],
        url: [
          {
            required: true,
            pattern: /^[0-9a-zA-Z/_-]{3,200}$/,
            message:
              "URL地址只能由字母、数字、斜线、短横线和下划线组成，长度在3-200之间",
            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;

      if (!this.isEdit) datas.id = data;

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