<template>
  <div>
    <Modal v-model="showModal" v-drag :mask-closable="false" class="zIndex-1100">
      <p slot="header">
        {{ modalTitle }}
      </p>
      <div>
        <Form ref="formObj" :model="formObj" :rules="ruleValidate" :label-width="70" onsubmit="return false">
          <Form-item :label="modalTitle" prop="remark">
            <Input v-model="remark" :placeholder="placeholder" style="width:100%" />
          </Form-item>
        </Form>
      </div>
      <div slot="footer" class="btn-width">
        <modal-footer ref="footerModal" :footer="footerList" @on-result-change="_footerResult" />
      </div>
    </Modal>
  </div>
</template>
<script>
/**
 * 退回原因(全局)
 */

export default {
  components: {},
  data() {
    const validateRemark = (rule, value, callback) => {
      if (this.remark === '') {
        callback(new Error(this.message))
      } else {
        callback()
      }
    }
    return {
      modalTitle: '',
      showModal: false,
      formObj: {},
      remark: '',
      contractId: '',
      ruleValidate: {
        remark: [{ required: true, validator: validateRemark, trigger: 'blur' }]
      },
      placeholder: '',
      footerList: [
        { id: '', name: '取消', type: '' },
        { id: '', name: '确定', type: 'primary' }
      ],
      message: '内容不能为空'
    }
  },
  methods: {
    _open(title, id) {
      this.modalTitle = title
      this.contractId = id
      this.placeholder = '请输入' + title
      this.showModal = true
      this.remark = ''
      this.message = title + '不能为空'
      this.$refs.footerModal._hideLoading()
    },
    _footerResult(name) {
      switch (name) {
        case '取消':
          this._cancel()
          break
        case '确定':
          this._ok()
          break
      }
    },
    _cancel() {
      this.showModal = false
    },
    _ok() {
      this.$refs.formObj.validate(valid => {
        if (valid) {
          this.$emit(
            'on-result-change',
            this.remark,
            this.modalTitle,
            this.contractId
          )
          this.showModal = false
        } else {
          this.$refs.footerModal._hideLoading()
          this.$Message.error('表单验证失败!')
        }
      })
    }
  }
}
</script>
