<template>
  <el-dialog
    v-model="dialogFormVisible"
    :title="title"
    width="1500px"
    @close="close"
  >
    <el-form ref="form" :model="form" label-width="100px" label-position="left">
      <el-form-item label="消息类型">
        <el-select
          v-model="form.msgType"
          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"
          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 @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
    <select-data ref="select" @getMaterialId="getMaterialId"></select-data>
  </el-dialog>
</template>

<script>
import selectData from './selectData';
import {
  getMaterialMsgType,
  getMaterialType,
  materialSaveOrUpdate,
} 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: false,
      msgTypeData: [],
      typeData: [],
      isMaterial: false,
      isText: false,
    };
  },
  created() {},
  methods: {
    showEdit(row) {
      if (!row) {
        this.title = '添加';
      } else {
        this.title = '编辑';
        const form = Object.assign({}, row);
        this.form = form;
      }
      this.dialogFormVisible = true;
      this.projectId = sessionStorage.getItem('projectId');
      this.getMaterialTypeData();
      this.getMaterialMsgTypeData();
    },
    changeMsgType() {
      this.$refs['form'].resetFields();
      if (this.form.msgType == '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) {
          this.form.textContent = delHtmlTag(this.form.textContent);
          const { msg } = await materialSaveOrUpdate(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>
