<template>
  <div class="manage-input">
    <el-form ref="form" :model="form" label-width="100px" label-position="left">
      <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">
        <el-button type="primary" size="small" @click="showData()">
          选择素材
        </el-button>
      </el-form-item>
    </el-form>
    <div class="dialog-footer">
      <el-button type="primary" size="small" @click="save">确 定</el-button>
    </div>
    <select-data ref="select" @getMaterialId="getMaterialId"></select-data>
  </div>
</template>

<script>
import selectData from './selectData';
import {
  getMaterialMsgType,
  getMaterialType,
  defaultReplySaveOrUpdate,
  getDefaultReplyType,
} from '@/api/wechat';

import ClassicEditor from '@/common/ckeditor/ckeditor';
import '@/common/ckeditor/zh';
import { knowledgeEditorConfig } from '@/common/ckeditor.config.js';
export default {
  components: {
    selectData,
  },
  data() {
    return {
      form: {
        id: '',
        mediaId: '',
        msgType: '',
        textContent: '',
        materialType: '',
      },
      clearable: true,
      title: '',
      editor: ClassicEditor,
      editorConfig: knowledgeEditorConfig,
      dialogFormVisible: true,
      msgTypeData: [],
      typeData: [],
      isMaterial: false,
      isText: false,
    };
  },
  methods: {
    defaultIndex() {
      this.getSubscribeReplyData();
    },
    async getSubscribeReplyData() {
      const { data } = await getDefaultReplyType();
      this.form = data;
      this.materialOrText(this.form.msgType);
      this.getMaterialTypeData();
      this.getMaterialMsgTypeData();
    },
    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;
      }
    },
    showData() {
      this.$refs['select'].selectData();
    },
    async getMaterialMsgTypeData() {
      const { data } = await getMaterialMsgType();
      this.msgTypeData = 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');
    },
    getMaterialId(media_id) {
      this.form.mediaId = media_id;
    },
    save() {
      this.$refs['form'].validate(async valid => {
        if (valid) {
          delete this.form.image;
          delete this.form.voice;
          delete this.form.video;
          delete this.form.news;
          const { msg } = await defaultReplySaveOrUpdate(this.form);
          this.$notify({
            title: msg,
            type: 'success',
          });
          this.getSubscribeReplyData();
        } 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;
    }
  }
}
.manage-input {
  padding: 20px;
}
</style>
