<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">
        <p>已选择素材：{{ form.mediaId }}</p>
        <el-button type="primary" size="mini" @click="showData()">
          选择素材
        </el-button>
      </el-form-item>
    </el-form>
    <div class="dialog-footer" style="text-align: right">
      <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,
  materialSaveOrUpdate,
  getSubscribeReply,
} from '@/api/wechat';
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: '',
        mediaId: '',
        msgType: '',
        textContent: '',
        materialType: '',
      },
      clearable: true,
      title: '',
      editor: ClassicEditor,
      editorConfig: knowledgeEditorConfig,
      dialogFormVisible: true,
      msgTypeData: [],
      typeData: [],
      isMaterial: false,
      isText: false,
    };
  },
  created() {
    this.getSubscribeReplyData();
  },
  methods: {
    followIndex() {
      this.getSubscribeReplyData();
    },
    async getSubscribeReplyData() {
      const { data } = await getSubscribeReply();
      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;
          // this.form.textContent = delHtmlTag(this.form.textContent);
          this.form.textContent =
            '面对全球普遍存在乡村、景区、民宿、营地、服务区等分散性场景，每天产生大量污水随意排放，受限厂网建设投资大和运营成本高等诸多现实因素，难以收集到集中式污水厂处理的严峻挑战。力鼎深知，污水问题不仅影响水环境的改善，更关乎人类的卫生需求和健康保障。我们有志成为全球领先的分散式场景污水处理解决方案服务商，通过科技创新和技术升级，为各类分散场景实现污水高效解决方案，为人类创造一个更加清洁、健康、宜居的生活环境。同时，我们也将积极履行社会责任，与各方携手合作，共同推动分散式污水处理行业的健康发展，为构建美好世界贡献力量。' +
            '\n' +
            '清道夫：https://mp.weixin.qq.com/s/Xdgzc05iGKKIoIo7uXcHSg' +
            '\n' +
            '全球合伙人招募：https://jinshuju.net/f/Ja8lek' +
            '\n' +
            '产品体验：http://deepdragon.lidinghb.com/';
          const { msg } = await materialSaveOrUpdate(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>
