<template>
  <el-dialog
    v-model="dialogFormVisible"
    :title="title"
    width="500px"
    @close="close"
  >
    <el-form ref="form" :model="form" label-width="100px" label-position="left">
      <el-form-item label="关键词" prop="keyword">
        <el-input
          v-model="form.keyword"
          size="small"
          placeholder="关键词"
        ></el-input>
      </el-form-item>
      <el-form-item label="匹配类型">
        <el-select v-model="form.matchType" filterable placeholder="匹配类型">
          <el-option
            v-for="item in matchTypeData"
            :key="item.dataCode"
            :label="item.dataValue"
            :value="item.dataCode"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="消息类型">
        <el-select
          v-model="form.msgType"
          size="small"
          filterable
          placeholder="消息类型"
          @change="changeMsgType"
        >
          <el-option
            v-for="item in msgTypeData"
            :key="item.dataCode"
            :label="item.dataValue"
            :value="item.dataCode"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item v-if="isMaterial" label="素材类型" prop="materialType">
        <el-select
          v-model="form.materialType"
          size="small"
          filterable
          placeholder="素材类型"
        >
          <el-option
            v-for="item in typeData"
            :key="item.dataCode"
            :label="item.dataValue"
            :value="item.dataCode"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item v-if="isText" label="文本内容" prop="textContent">
        <ckeditor
          v-model="form.textContent"
          size="small"
          :editor="editor"
          :config="editorConfig"
        ></ckeditor>
      </el-form-item>
      <el-form-item v-if="isMaterial" label="选择素材" prop="mediaId">
        <p>已选择素材：{{ form.mediaId }}</p>
        <el-button type="primary" size="mini" @click="showData()">
          选择素材
        </el-button>
      </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>
    <select-data ref="select" @getMaterialId="getMaterialId"></select-data>
  </el-dialog>
</template>

<script>
import {
  keywordReplyAdd,
  keywordReplyEdit,
  getMatchType,
  getMaterialMsgType,
  getMaterialType,
} from '@/api/wechat';
import selectData from '../follow/selectData';
import ClassicEditor from '@/common/ckeditor/ckeditor';
import '@/common/ckeditor/zh';
import { knowledgeEditorConfig } from '@/common/ckeditor.config.js';
import { delHtmlTag } from '@/utils/common';
export default {
  components: {
    selectData,
  },
  data() {
    return {
      form: {
        id: '',
        keyword: '',
        matchType: '',
        msgType: '',
        textContent: '',
        materialType: '',
        mediaId: '',
      },
      clearable: true,
      title: '',
      dialogFormVisible: false,
      isText: false,
      isMaterial: false,
      matchTypeData: [],
      msgTypeData: [],
      typeData: [],
      editor: ClassicEditor,
      editorConfig: knowledgeEditorConfig,
    };
  },
  created() {},
  methods: {
    showEdit(row) {
      if (!row) {
        this.title = '添加关键词回复';
      } else {
        this.title = '编辑关键词回复';
        const form = Object.assign({}, row);
        this.form.id = form.id;
        this.form.keyword = form.keyword;
        if (form.matchType != '' && form.matchType != null) {
          this.form.matchType = form.matchType.dataCode;
        }
        if (form.msgType != '' && form.msgType != null) {
          this.form.msgType = form.msgType.dataCode;
          this.MaterialOrText(this.form.msgType);
        }
        if (form.materialType != null && form.materialType != '') {
          this.form.materialType = form.materialType.dataCode;
        }
        this.form.textContent = form.textContent;
      }
      this.dialogFormVisible = true;
      this.getMaterialMsgTypeData();
      this.getMaterialTypeData();
      this.getMatchTypeData();
    },
    getMaterialId(media_id) {
      this.form.mediaId = media_id;
    },
    showData() {
      this.$refs['select'].selectData();
    },
    changeMsgType() {
      this.$refs['form'].resetFields();
      this.MaterialOrText(this.form.msgType);
    },
    MaterialOrText(type) {
      if (type == 'material') {
        this.isMaterial = true;
        this.isText = false;
      } else {
        this.isMaterial = false;
        this.isText = true;
      }
    },
    async getMaterialMsgTypeData() {
      const { data } = await getMaterialMsgType();
      this.msgTypeData = data;
    },
    async getMatchTypeData() {
      const { data } = await getMatchType();
      this.matchTypeData = data;
    },
    async getMaterialTypeData() {
      const { data } = await getMaterialType();
      this.typeData = 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) {
            this.form.textContent = delHtmlTag(this.form.textContent);
            const { msg } = await keywordReplyEdit(this.form);
            this.$notify({
              title: msg,
              type: 'success',
            });
          } else {
            delete this.form.id;
            const { msg } = await keywordReplyAdd(this.form);
            this.$notify({
              title: msg,
              type: 'success',
            });
          }
          this.$refs['form'].resetFields();
          this.$parent.fetchData();
          this.dialogFormVisible = false;
        } else {
          return false;
        }
      });
    },
  },
};
</script>
<style lang="scss">
.el-dialog {
  .el-dialog__header {
    border-bottom: 1px solid #e3e3e3 !important;
    padding: 15px 20px;
  }
  .dialog-footer {
    text-align: right;
    .el button {
      height: 36px !important;
      min-height: 36px !important;
      line-height: 366px !important;
    }
  }
}
</style>
