<template>
  <el-dialog
    v-model="dialogFormVisible"
    :title="title"
    width="500px"
    @close="close"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="按钮名称" prop="pid">
        <el-cascader
          v-model="form.pid"
          :options="buttonData"
          :props="{
            value: 'id',
            label: 'label',
          }"
          @change="handleChange"
        ></el-cascader>
      </el-form-item>
      <el-form-item label="按钮名称" prop="name">
        <el-input
          v-model.trim="form.name"
          size="small"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="权限标识" prop="perms">
        <el-input
          v-model.trim="form.perms"
          size="small"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input
          v-model.trim="form.remark"
          size="small"
          autocomplete="off"
        ></el-input>
      </el-form-item>
    </el-form>
    <div class="dialog-footer">
      <el-button type="primary" size="small" @click="save">确 定</el-button>
      <el-button size="small" @click="close">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { doEdit, doAdd, buttonTree } from '@/api/program';
import { getTypeCategoryTree } from '@/api/knowledge';
export default {
  data() {
    return {
      form: {
        pid: 0,
        id: '',
        name: '',
        perms: '',
        remark: '',
      },
      buttonData: [],
      rules: {
        name: [{ required: true, trigger: 'blur', message: '请输入按钮名称' }],
        perms: [{ required: true, trigger: 'blur', message: '请输入权限标识' }],
      },
      title: '',
      dialogFormVisible: false,
    };
  },
  created() {},
  methods: {
    handleCountryChange(value) {
      if (value) {
        const len = value.length - 1;
        this.form.parentId = value[len];
      } else {
        this.form.parentId = 0;
      }
    },
    showEdit(row) {
      if (!row) {
        this.title = '添加按钮';
      } else {
        this.title = '编辑按钮';
        this.form = Object.assign({}, row);
      }
      this.dialogFormVisible = true;
      this.buttonTree();
    },
    async buttonTree() {
      const { data } = await buttonTree();
      this.buttonData = data;
    },
    handleChange() {
      this.form.pid = this.form.pid[this.form.pid.length - 1];
    },
    close() {
      this.$refs['form'].resetFields();
      this.form = this.$options.data().form;
      this.dialogFormVisible = false;
      this.$emit('fetch-data');
    },
    save() {
      this.$refs['form'].validate(async valid => {
        if (valid) {
          if (this.form.id) {
            const { msg } = await doEdit(this.form);
            this.$notify({
              title: msg,
              type: 'success',
            });
          } else {
            const { msg } = await doAdd(this.form);
            this.$notify({
              title: msg,
              type: 'success',
            });
          }
          this.$refs['form'].resetFields();
          this.dialogFormVisible = false;
          this.$parent.fetchData();
        } else {
          return false;
        }
      });
    },
  },
};
</script>
<style>
.select {
  width: 100%;
}
</style>
