<template>
  <a-modal
    centered
    class="edit-remark-modal"
    :footer="null"
    :maskClosable="false"
    title="编辑备注"
    v-model="isShow"
    @cancel="isShow = false"
    :width="450">
    <a-spin :spinning="spinning" tip="Loading...">
      <a-form-model
        id="edit-remark-form"
        ref="ruleForm"
        :model="form"
        :rules="rules"
        :label-col="formItemLayout.labelCol"
        :wrapper-col="formItemLayout.wrapperCol"
      >
        <a-form-model-item label="备注" prop="remarks">
          <a-textarea
            :autosize="{minRows: 5, maxRows: 5}"
            id="edit-remark-remarks"
            :maxLength="100"
            v-model="form.remarks"
            placeholder="请输入备注(最多100个字符)"
            allowClear />
        </a-form-model-item>
      </a-form-model>
      <div class="btn-cont">
        <a-button id="edit-remark-modal-back" @click="isShow = false" style="margin-right: 15px;">取消</a-button>
        <a-button type="primary" id="edit-remark-modal-save" @click="handleSave">保存</a-button>
      </div>
    </a-spin>
  </a-modal>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
// 接口
import { onlinePayUpdateRemark } from '@/api/sales'
export default {
  name: 'EditRemarkModal',
  mixins: [commonMixin],
  props: {
    openModal: {
      //  弹框显示状态
      type: Boolean,
      default: false
    },
    itemSn: {
      type: String,
      default: ''
    },
    mainContent: {
      type: String,
      default: ''
    }
  },
  data () {
    return {
      spinning: false,
      isShow: this.openModal, //  是否打开弹框
      formItemLayout: {
        labelCol: { span: 3 },
        wrapperCol: { span: 20 }
      },
      form: {
        remarks: ''// 备注
      },
      rules: {
        remarks: [{ required: true, message: '请输入备注', trigger: 'blur' }]
      }
    }
  },
  methods: {
    //  保存
    handleSave () {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const form = JSON.parse(JSON.stringify(_this.form))
          form.salesBillSn = _this.itemSn
          _this.spinning = true
          onlinePayUpdateRemark(form).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              setTimeout(() => {
                _this.$emit('ok', res.data)
              }, 1000)
              _this.isShow = false
            }
            _this.spinning = false
          })
        } else {
          return false
        }
      })
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.$refs.ruleForm.resetFields()
      } else {
        if (this.mainContent && this.mainContent.length) {
          this.form.remarks = this.mainContent
        }
      }
    }
  }
}
</script>

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