<template>
  <div>
    <el-dialog
      v-dialogDrag
      :visible.sync="showModal"
      :close-on-click-modal="false"
      width="700px"
      :title="modalTitle"
    >
      <div>
        <Form
          id="edit-form"
          ref="formObj"
          :model="formObj"
          :rules="ruleValidate"
          :label-width="80"
          inline
        >
          <Form-item label="标题" prop="title" style="width: 100%">
            <input name="type" type="hidden" value="0">
            <el-input v-model="formObj.title" name="title" placeholder="请输入标题" />
          </Form-item>
          <Form-item label="内容" prop="content" style="width: 100%">
            <editor ref="editorModal" v-model="formObj.content" @on-result-change="_editorData" />
          </Form-item>
        </Form>
      </div>
      <div slot="footer">
        <modal-footer ref="footerModal" :footer="footerList" @on-result-change="_footerResult" />
      </div>
    </el-dialog>
  </div>
</template>
<script>
/**
 * 添加编辑系统消息表
 */
import { message } from '../../../api'
import editor from './editor'

export default {
  components: {
    editor
  },
  data() {
    return {
      id: '',
      modalTitle: '',
      formObj: {
        type: '',
        title: '',
        content: ''
      },
      ruleValidate: {
        type: [
          {
            required: true,
            message: '类型不能为空',
            trigger: 'change',
            type: 'number'
          }
        ],
        title: [{ required: true, message: '标题不能为空', trigger: 'blur' }],
        content: [{ required: true, message: '内容不能为空', trigger: 'blur' }]
      },
      showModal: false,
      footerList: [
        { id: '', name: '取消', type: '' },
        { id: '', name: '保存', type: 'primary' }
      ]
    }
  },
  methods: {
    // modal
    _footerResult(name) {
      switch (name) {
        case '取消':
          this._cancel()
          break
        case '保存':
          this._ok()
          break
      }
    },
    _hideLoading() {
      this.$refs.footerModal._hideLoading()
    },
    _resultChange(result, msg) {
      if (result) {
        this.showModal = false
        this.$message.success(msg)
        this.$emit('on-result-change')
      }
      this._hideLoading()
    },
    _ok() {
      this.$refs.formObj.validate(valid => {
        if (valid) {
          const data = this.$serialize('edit-form')
          data.content = this.formObj.content
          this._save(data)
        } else {
          this.$message.error('表单验证失败!')
          this._hideLoading()
        }
      })
    },
    _save: async function(data) {
      if (this.$string(this.id).isEmpty()) {
        const result = await message.add(data)
        this._resultChange(result, '添加成功!')
      } else {
        const result = await message.edit({
          id: this.formObj.id,
          obj: data
        })
        this._resultChange(result, '编辑成功!')
      }
    },
    _cancel() {
      this.showModal = false
    },
    _open(formObj) {
      this.showModal = true
      this.$nextTick(() => {
        this.$refs.formObj.resetFields()
        this._hideLoading()
        if (this.$string(formObj).isEmpty()) {
          this.id = ''
          this.modalTitle = '添加通知'
        } else {
          this.id = formObj.id
          this.formObj = formObj
          this.modalTitle = '编辑通知'
        }
        this.$refs.editorModal._open(this.formObj.content) // 打开editor
      })
    },
    _editorData(msg) {
      this.formObj.content = msg
    }
  }
}
</script>
