<template>
  <a-modal
    centered
    class="auditRejectModal-modal"
    :footer="null"
    :maskClosable="false"
    :closable="false"
    title="备注原因"
    v-model="isShow"
    @cancel="isShow=false"
    :width="800">
    <div>
      <a-spin :spinning="spinning" tip="Loading...">
        <a-form-model
          id="auditRejectModal-form"
          ref="ruleForm"
          :model="form"
          :rules="rules"
          :label-col="formItemLayout.labelCol"
          :wrapper-col="formItemLayout.wrapperCol"
        >
          <a-form-model-item label="备货单号">
            {{ (itemData && itemData.dispatchBillNo) || '--' }}
          </a-form-model-item>
          <a-form-model-item label="原因" prop="remarks">
            <a-input
              id="auditRejectModal-remarks"
              :maxLength="100"
              type="textarea"
              v-model.trim="form.remarks"
              ref="remarks"
              placeholder="请输入原因(最多100个字符)"
              allowClear />
          </a-form-model-item>
        </a-form-model>
        <div class="btn-cont">
          <a-button type="primary" id="auditRejectModal-save" @click="handleSave">提交</a-button>
          <a-button id="auditRejectModal-cancel" @click="isShow = false" style="margin-left: 15px;">取消</a-button>
        </div>
      </a-spin>
    </div>
  </a-modal>
</template>

<script>
export default {
  name: 'AuditRejectModal',
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    spinning: {
      type: Boolean,
      default: false
    },
    itemData: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data () {
    return {
      isShow: this.openModal, //  是否打开弹框
      formItemLayout: {
        labelCol: { span: 4 },
        wrapperCol: { span: 17 }
      },
      form: {
        remarks: ''
      },
      rules: {
        remarks: [
          { required: false, message: '请输入原因', trigger: 'blur' }
        ]
      },
      detailData: null //  数据详情
    }
  },
  methods: {
    // 保存
    handleSave () {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          _this.$emit('ok', _this.form.remarks)
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.$refs.ruleForm.resetFields()
      } else {
        // 默认首项获取焦点
        const _this = this
        setTimeout(() => {
          if (_this.$refs['remarks']) {
            _this.$refs['remarks'].focus()
          }
        }, 300)
      }
    }
  }
}
</script>

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