<template>
  <el-dialog
    v-model="dialogFormVisible"
    :title="title"
    width="800px"
    @close="close"
  >
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="100px"
      label-position="left"
    >
      <el-form-item label="标题" prop="title">
        <el-input
          v-model.trim="form.title"
          size="small"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="分类" prop="category">
        <el-select
          v-model="form.category"
          size="small"
          filterable
          placeholder="请选择分类"
        >
          <el-option
            v-for="item in dictData"
            :key="item.dataCode"
            :label="item.dataValue"
            :value="item.dataCode"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="简介" prop="introduction">
        <ckeditor
          v-model="form.introduction"
          size="small"
          :editor="editor"
          :config="editorConfig"
        ></ckeditor>
      </el-form-item>
      <el-form-item label="详情" prop="detail">
        <ckeditor
          v-model="form.detail"
          size="small"
          :editor="editor"
          :config="editorConfig"
        ></ckeditor>
      </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 { doAdd, doEdit, getCategoryDict } from '@/api/knowledge';
import ClassicEditor from '@/common/ckeditor/ckeditor';
import '@/common/ckeditor/zh';
import { knowledgeEditorConfig } from '@/common/ckeditor.config.js';

export default {
  data() {
    return {
      form: {
        id: '',
        title: '',
        introduction: '',
        detail: '',
        category: '',
      },
      rules: {
        title: [{ required: true, trigger: 'blur', message: '请输入标题' }],
        category: [
          { required: true, trigger: 'change', message: '请选择分类' },
        ],
        introduction: [
          { required: true, trigger: 'blur', message: '请输入简介' },
        ],
        detail: [{ required: true, trigger: 'blur', message: '请输入详情' }],
      },
      dictData: [],
      roleData: [],
      clearable: true,
      roleId: '',
      parentData: [],
      title: '',
      dialogFormVisible: false,
      editor: ClassicEditor,
      editorConfig: knowledgeEditorConfig,
    };
  },
  created() {},
  methods: {
    showEdit(row) {
      if (!row) {
        this.title = '添加知识库';
      } else {
        this.title = '编辑知识库';
        const data = Object.assign({}, row);
        this.form.category = data.category.dataCode;
        this.form.id = data.id;
        this.form.title = data.title;
        this.form.introduction = data.introduction;
        this.form.detail = data.detail;
      }
      this.dialogFormVisible = true;
      this.getCategoryDict();
    },
    async getCategoryDict() {
      const { data } = await getCategoryDict();
      this.dictData = data;
    },
    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 {
            delete this.form.id;
            const { msg } = await doAdd(this.form);
            this.$notify({
              title: msg,
              type: 'success',
            });
          }
          this.$refs['form'].resetFields();
          this.$parent.fetchData();
          this.dialogFormVisible = false;
        } else {
          return false;
        }
      });
    },
  },
};
</script>
