<template>
  <div>
    <el-dialog
      v-dialogDrag
      :visible.sync="showModal"
      width="700px"
      :title="modalTitle"
    >
      <div>
        <Form id="edit-form" ref="formObj" :model="formObj" :rules="ruleValidate" :label-width="80">
          <Form-item label="消息类型" prop="msgType" style="width: 100%">
            <el-select v-model="formObj.msgType" placeholder="请选择消息类型" clearable style="width: 100%;">
              <el-option v-for="item in msgTypeList" :key="item.value" :value="item.value" :label="item.display" />
            </el-select>
          </Form-item>
          <Form-item label="跳转链接" prop="link" style="width: 100%">
            <el-input v-model="formObj.link" type="textarea" name="link" placeholder="请输入跳转链接" />
          </Form-item>
        </Form>
      </div>
      <div slot="footer">
        <modal-footer ref="footerModal" :footer="footerList" @on-result-change="_footerResult" />
      </div>
    </el-dialog>
  </div>
</template>
<script>
/**
 * 添加编辑系统消息表
 */
import { resEnum, messageType } from '../../../api'

export default {
  data() {
    return {
      id: '',
      modalTitle: '',
      formObj: {
        msgType: '',
        link: ''
      },
      msgTypeList: [],
      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' }
      ]
    }
  },
  mounted() {
    this._getMsgType()
  },
  methods: {
    _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 = '编辑消息类型'
        }
      })
    },
    _getMsgType: async function() {
      const result = await resEnum.getByType('EnvMessageType')
      this.msgTypeList = result
    },
    // 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.msgType = this.formObj.msgType
          this._save(data)
        } else {
          this.$message.error('表单验证失败!')
          this._hideLoading()
        }
      })
    },
    _save: async function(data) {
      if (this.$string(this.id).isEmpty()) {
        const result = await messageType.add(data)
        this._resultChange(result, '添加成功!')
      } else {
        const result = await messageType.edit({
          id: this.formObj.id,
          obj: data
        })
        this._resultChange(result, '编辑成功!')
      }
    },
    _cancel() {
      this.showModal = false
    }
  }
}
</script>
