<template>
  <el-dialog
    :visible.sync="dialog"
    :title="isAdd ? '新增菜单' : '编辑菜单'"
    append-to-body
    width="600px"
    @close="close"
    :close-on-click-modal="false"
  >
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      size="small"
      label-width="80px"
    >
      <el-form-item label="菜单图标">
        <el-popover
          placement="bottom-start"
          width="460"
          trigger="click"
          @show="$refs['iconSelect'].reset()"
        >
          <IconSelect ref="iconSelect" @selected="selected" />
          <el-input
            slot="reference"
            v-model="form.icon"
            style="width: 460px;"
            placeholder="点击选择图标"
            readonly
          >
            <svg-icon
              v-if="form.icon"
              slot="prefix"
              :icon-class="form.icon"
              class="el-input__icon"
              style="height: 32px;width: 16px;"
            />
            <i v-else slot="prefix" class="el-icon-search el-input__icon" />
            <span v-show="form.icon" slot="suffix" @click="btnClearable">
              <i
                class="el-icon-close"
                style="margin-left: 5px;cursor: pointer;"
              ></i>
            </span>
          </el-input>
        </el-popover>
      </el-form-item>
      <el-form-item label="菜单名称" prop="name">
        <el-input
          v-model="form.name"
          placeholder="名称"
          style="width: 460px;"
        />
      </el-form-item>
      <el-form-item label="菜单排序" prop="sort">
        <el-input-number
          v-model.number="form.sort"
          :min="1"
          :max="999"
          controls-position="right"
          style="width: 460px;"
        />
      </el-form-item>
      <el-form-item label="内部菜单" prop="iframe">
        <el-radio v-model="form.iframe" :label="0">是</el-radio>
        <el-radio v-model="form.iframe" :label="1">否</el-radio>
      </el-form-item>
      <el-form-item label="链接地址">
        <el-input
          v-model="form.path"
          placeholder="菜单路径"
          style="width: 460px;"
        />
      </el-form-item>
      <el-form-item v-if="form.iframe === 0" label="组件路径">
        <el-input
          v-model="form.component"
          placeholder="菜单路径"
          style="width: 460px;"
        />
      </el-form-item>
      <el-form-item label="上级类目">
        <treeselect
          v-model="form.pid"
          :normalizer="normalizer"
          :options="menus"
          style="width: 460px;"
          placeholder="选择上级类目"
        />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="doSubmit"
        >确认</el-button
      >
    </div>
  </el-dialog>
</template>

<script>
import { mapGetters } from "vuex";
import { add, edit, getMenusTree } from "@/api/menu";
import Treeselect from "@riophae/vue-treeselect";
import IconSelect from "@/components/IconSelect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  components: { Treeselect, IconSelect },
  props: {
    isAdd: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      normalizer(node) {
        return {
          id: node.id,
          label: node.name,
          children:
            node.children && node.children.length > 0 ? node.children : 0
        };
      },
      loading: false,
      dialog: false,
      menus: [],
      form: {
        name: "",
        sort: 999,
        path: "",
        component: "",
        iframe: 0,
        pid: 0,
        icon: ""
      },
      rules: {
        name: [{ required: true, message: "请输入名称", trigger: "blur" }],
        sort: [
          {
            required: true,
            message: "请输入序号",
            trigger: "blur",
            type: "number"
          }
        ],
        iframe: [{ required: true, message: "请选择菜单类型", trigger: "blur" }]
      }
    };
  },
  computed: {
    ...mapGetters(["user"])
  },
  methods: {
    close() {
      this.resetForm();
    },
    cancel() {
      this.resetForm();
    },
    doSubmit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.pid === undefined) {
            this.$message({
              message: "上级类目不能为空",
              type: "error"
            });
            return false;
          }
          if (this.form.pid === this.form.id) {
            this.$message({
              message: "不能选择当前菜单作为上级类目",
              type: "error"
            });
            return false;
          }
          if (this.form.name.trim() === "") {
            this.$message({
              message: "菜单名不能为空",
              type: "error"
            });
            return false;
          }
          this.loading = true;
          if (this.isAdd) {
            this.doAdd();
          } else this.doEdit();
        } else {
          return false;
        }
      });
    },
    doAdd() {
      add(this.form)
        .then(res => {
          if (res.code === 0) {
            this.resetForm();
            this.$notify({
              title: "添加成功",
              type: "success",
              duration: 2500
            });
            this.$parent.init();
          } else {
            this.$notify({
              title: res.message,
              type: "error",
              duration: 2500
            });
          }
          this.loading = false;
        })
        .catch(err => {
          this.loading = false;
          console.log(err.response.data.message);
        });
    },
    doEdit() {
      edit(this.form)
        .then(res => {
          if (res.code === 0) {
            this.resetForm();
            this.$notify({
              title: "修改成功",
              type: "success",
              duration: 2500
            });
            this.$parent.init();
          } else {
            this.$notify({
              title: res.message,
              type: "error",
              duration: 2500
            });
          }
          this.loading = false;
        })
        .catch(err => {
          this.loading = false;
          console.log(err.response.data.message);
        });
    },
    resetForm() {
      this.dialog = false;
      this.$refs["form"].resetFields();
      this.form = {
        name: "",
        sort: 999,
        path: "",
        component: "",
        iframe: 0,
        pid: 0,
        icon: ""
      };
    },
    selected(name) {
      this.form.icon = name;
    },
    getMenus() {
      getMenusTree({}).then(res => {
        this.menus = [];
        const menu = { id: 0, name: "顶级类目", children: [] };
        menu.children = res.data;
        this.menus.push(menu);
      });
    },
    btnClearable() {
      this.form.icon = "";
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
::v-deep .el-input-number .el-input__inner {
  text-align: left;
}
::v-deep .el-icon-close:before {
  content: "\E78D";
}
</style>
