<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="categoryName">
        <el-input
          v-model.trim="form.categoryName"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="上级分类" prop="parentId">
        <el-select v-model="form.parentId" placeholder="请选择上级分类">
          <el-option
            v-for="item in categoryData"
            :key="item.categoryId"
            :label="item.categoryName"
            :value="item.categoryId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="排序" prop="orderNum">
        <el-input v-model.trim="form.orderNum" 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 { getDeptList, doDeptAdd, doDeptEdit } from '@/api/dept';
import { doTypeAdd, doTypeEdit, getTypeList } from '@/api/knowledge';

export default {
  data() {
    return {
      form: {
        categoryId: '',
        parentId: '',
        categoryName: '',
        orderNum: '',
      },
      rules: {
        categoryName: [
          { required: true, trigger: 'blur', message: '请输入分类名称' },
        ],
      },
      clearable: true,
      categoryData: [],
      title: '',
      dialogFormVisible: false,
    };
  },
  created() {},
  methods: {
    async getTypeList() {
      const { data } = await getTypeList();
      data.push({ categoryId: 0, categoryName: '顶级分类' });
      this.categoryData = data;
    },
    categoryEdit(row) {
      this.getTypeList();
      if (!row) {
        this.title = '添加分类';
      } else {
        this.title = '编辑分类';
        this.form.categoryId = row.id;
        this.form.parentId = row.parentId;
        this.form.categoryName = row.label;
        this.form.orderNum = row.weight;
      }
      this.dialogFormVisible = true;
    },
    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.categoryId) {
            const { msg } = await doTypeEdit(this.form);
            this.$notify({
              title: msg,
              type: 'success',
            });
          } else {
            delete this.form.categoryId;
            const { msg } = await doTypeAdd(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>
