<template>
  <a-modal
    centered
    class="handleManage-modal"
    :footer="null"
    :maskClosable="false"
    title="处理"
    v-model="isShow"
    @cancel="isShow=false"
    :width="800">
    <!-- 表单 -->
    <div>
      <a-spin :spinning="spinning" tip="Loading...">
        <a-form-model
          id="handleManage-form"
          ref="ruleForm"
          :model="form"
          :rules="rules"
          :label-col="formItemLayout.labelCol"
          :wrapper-col="formItemLayout.wrapperCol"
        >
          <a-form-model-item label="业务名称">
            {{ nowData && nowData.bizName||'--' }}
          </a-form-model-item>
          <a-form-model-item label="业务编号">
            {{ nowData && nowData.bizNo||'--' }}
          </a-form-model-item>
          <a-form-model-item label="处理状态" prop="disposeType">
            <v-select
              code="DATA_CHECK_RESULT_DISPOSE_TYPE"
              id="handleManage-disposeType"
              placeholder="请选择处理状态"
              v-model="form.disposeType"
            ></v-select>
          </a-form-model-item>
          <a-form-model-item label="处理描述">
            <a-textarea
              id="handleManage-name"
              :maxLength="100"
              v-model.trim="form.disposeDesc"
              @change="filterEmpty"
              placeholder="请输入处理描述(最多100个字符)"
              allowClear />
          </a-form-model-item>
        </a-form-model>
        <div class="btn-cont">
          <a-button id="handleManage-cancel" @click="isShow = false" style="margin-right: 15px;">取消</a-button>
          <a-button type="primary" id="handleManage-save" @click="handleSave">保存</a-button>
        </div>
      </a-spin>
    </div>
  </a-modal>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
// 组件
import { VSelect } from '@/components'
import moment from 'moment'
// 接口
import { datacheckModify } from '@/api/mqmsg'
export default {
  name: 'ValidEditModal',
  mixins: [commonMixin],
  components: { VSelect },
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    nowData: {
      type: Object,
      default: null
    }
  },
  data () {
    return {
      isShow: this.openModal, //  是否打开弹框
      spinning: false,
      // form 表单
      formItemLayout: {
        labelCol: { span: 6 },
        wrapperCol: { span: 16 }
      },
      // 提交参数
      form: {
        disposeDesc: '', // 处理描述
        disposeType: undefined, // 处理状态
        disposeDate: moment().format('YYYY-MM-DD HH:mm:ss'), // 处理时间
        id: '' // 数据校验ID
      },
      // 表单验证规则
      rules: {
        disposeType: [
          { required: true, message: '请选择处理状态', trigger: 'change' }
        ],
        disposeDesc: [
          { required: true, message: '请输入处理描述', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    // 处理描述 去空格
    filterEmpty () {
      let str = this.form.name
      str = str.replace(/\ +/g, '')
      str = str.replace(/[ ]/g, '')
      str = str.replace(/[\r\n]/g, '')
      this.form.name = str
    },
    //  保存
    handleSave () {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const form = _this.form
          _this.spinning = true
          datacheckModify(form).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$emit('close', 1)
              _this.spinning = false
            } else {
              _this.spinning = false
            }
          })
        } else {
          return false
        }
      })
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close', 0)
        this.$refs.ruleForm.resetFields()
      } else {
        if (this.nowData && this.nowData.id) { //  编辑
          this.form.disposeType = this.nowData && this.nowData.disposeType ? this.nowData.disposeType : ''
          this.form.disposeDesc = this.nowData && this.nowData.disposeDesc ? this.nowData.disposeDesc : ''
          this.form.id = this.nowData && this.nowData.id ? this.nowData.id : ''
        }
      }
    }
  }
}
</script>

<style lang="less">
  .handleManage-modal{
    .ant-modal-body {
      padding: 40px 40px 24px;
    }
    .btn-cont {
      text-align: center;
      margin: 35px 0 10px;
    }
  }
</style>
