<template>
  <a-modal
    centered
    class="subsidyEdit-modal"
    :footer="null"
    :maskClosable="false"
    title="编辑"
    v-model="isShow"
    @cancel="isShow=false"
    :width="500">
    <a-spin :spinning="spinning" tip="Loading...">
      <div class="subsidyEdit-form">
        <span class="subsidyRequired">*</span>抵扣金额<a-input-number
          id="subsidyEdit-subsidyAmount"
          v-model.trim="subsidyAmount"
          style="width: 40%;margin:0 10px;"
          :min="0.01"
          :max="99999999"
          :precision="2"
          placeholder="请输入"/>元
      </div>
      <div class="btn-cont">
        <a-button id="subsidyEdit-modal-cancel" @click="isShow = false" style="margin-right: 15px;">取消</a-button>
        <a-button type="primary" id="subsidyEdit-modal-save" @click="handleSave">保存</a-button>
      </div>
    </a-spin>
  </a-modal>
</template>

<script>
// 接口
import { subsidizedProductEdit } from '@/api/shopProductSubsidy'
export default {
  name: 'SubsidyEditModal',
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    itemSn: {// 抵扣产品sn
      type: Array,
      default: () => {
        return []
      }
    },
    itemAmount: {// 抵扣金额
      type: [Number, String],
      default: ''
    }
  },
  data () {
    return {
      spinning: false,
      isShow: this.openModal, //  是否打开弹框
      subsidyAmount: undefined// 抵扣金额
    }
  },
  methods: {
    //  保存
    handleSave () {
      const _this = this
      if (!_this.subsidyAmount) {
        _this.$message.warning('请输入抵扣金额！')
        return
      }
      _this.spinning = true
      subsidizedProductEdit({ subsidyAmount: _this.subsidyAmount, productSns: _this.itemSn }).then(res => {
        if (res.status == 200) {
          if (res.data) {
            _this.getErrorInfo(res.data)
          } else {
            _this.$message.success(res.message)
            _this.$emit('ok')
            setTimeout(function () {
              _this.isShow = false
            }, 300)
          }
        }
        _this.spinning = false
      })
    },
    // 报错弹窗
    getErrorInfo (infoTip) {
      const _this = this
      this.$info({
        title: '提示',
        content: infoTip,
        onOk () {
          _this.$emit('ok')
          console.log('我知道了')
        }
      })
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
      } else {
        // 单行编辑时，抵扣金额数据回显
        this.subsidyAmount = this.itemAmount || undefined
      }
    }
  }
}
</script>
<style lang="less" scoped>
  .subsidyEdit-modal{
    /deep/.ant-modal-body {
    	padding: 40px 40px 24px;
    }
    .subsidyEdit-form{
      text-align: center;
    }
    .subsidyRequired{
      margin-right: 5px;
      color:#ED1c24;
    }
    .btn-cont {
    	text-align: center;
    	margin: 50px 0 10px;
    }
  }
</style>
