<template>
  <a-modal
    centered
    class="rechargeEdit-modal"
    :footer="null"
    :maskClosable="false"
    :title="itemSn?'编辑':'新增'"
    v-model="isShow"
    @cancel="isShow=false"
    :width="750">
    <a-spin :spinning="spinning" tip="Loading...">
      <a-form-model
        id="rechargeEdit-form"
        ref="ruleForm"
        :model="form"
        :rules="rules"
        :label-col="formItemLayout.labelCol"
        :wrapper-col="formItemLayout.wrapperCol"
      >
        <a-form-model-item label="充值规则" prop="rechargeAmount">
          <div>
            充值<a-input-number
              id="rechargeEdit-rechargeAmount"
              v-model.trim="form.rechargeAmount"
              style="width: 35%;margin:0 10px;"
              :min="0.01"
              :max="999999"
              :precision="2"
              :disabled="itemSn?true:false"
              placeholder="请输入"/>元，送<a-input-number
                id="rechargeEdit-giveAmount"
                v-model.trim="form.giveAmount"
                style="width: 35%;margin:0 10px;"
                :min="0.01"
                :max="999999"
                :precision="2"
                :disabled="itemSn?true:false"
                placeholder="请输入"/>元抵扣
          </div>
        </a-form-model-item>
        <a-form-model-item label="备注">
          <a-textarea
            :autosize="{minRows: 5, maxRows: 5}"
            id="rechargeEdit-remarks"
            :maxLength="50"
            v-model="form.remarks"
            placeholder="请输入易码通显示的充值备注（最多50个字符）"
            allowClear />
        </a-form-model-item>
        <div class="tipWord">注意：请设置【抵扣产品】</div>
      </a-form-model>
      <div class="btn-cont">
        <a-button type="primary" id="rechargeEdit-save" @click="handleSave">保存</a-button>
        <a-button id="rechargeEdit-cancel" @click="isShow = false" style="margin-left: 15px;">取消</a-button>
      </div>
    </a-spin>
  </a-modal>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { rechargeRuleDetail, rechargeRuleCreate, rechargeRuleModify } from '@/api/rechargeRule'
export default {
  name: 'RechargeEditModal',
  mixins: [commonMixin],
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    itemSn: {
      type: [Number, String],
      default: ''
    }
  },
  data () {
    return {
      spinning: false,
      isShow: this.openModal, //  是否打开弹框
      formItemLayout: {// form表单中label设置
        labelCol: { span: 3 },
        wrapperCol: { span: 18 }
      },
      // 提交数据
      form: {
        rechargeAmount: undefined, // 充值金额
        giveAmount: undefined, // 赠送金额
        remarks: undefined// 备注
      },
      // 必填项判断
      rules: {
        rechargeAmount: [{ required: true, message: '请输入充值金额', trigger: 'blur' }]
      }
    }
  },
  methods: {
    //  保存
    handleSave () {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          if (!_this.form.giveAmount) {
            _this.$message.warning('请输入赠送金额')
            return
          }
          _this.spinning = true
          const ajaxName = _this.itemSn ? rechargeRuleModify : rechargeRuleCreate
          ajaxName(_this.form).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$emit('ok')
              setTimeout(function () {
                _this.isShow = false
              }, 300)
            }
            _this.spinning = false
          })
        } else {
          return false
        }
      })
    },
    // 获取详情
    getDetail () {
      const _this = this
      _this.spinning = true
      rechargeRuleDetail({ sn: _this.itemSn }).then(res => {
        if (res.status == 200) {
          _this.form = res.data
        }
        _this.spinning = false
      })
    },
    // 重置
    resetForm (obj) {
      this.form.rechargeAmount = undefined
      this.form.giveAmount = undefined
      this.form.remarks = undefined
      this.$refs.ruleForm.resetFields()
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.resetForm()
      }
    },
    itemSn (newValue, oldValue) {
      if (newValue) {
        this.getDetail()
      }
    }
  }
}
</script>
<style lang="less" scoped>
  .rechargeEdit-modal{
    /deep/.ant-modal-body {
    	padding: 40px 40px 24px;
    }
    .rechargeEdit-form{
      text-align: center;
    }
    .tipWord{
      margin:-10px 0  0 48px;
      color:gray;
    }
    .btn-cont {
    	text-align: center;
    	margin: 50px 0 10px;
    }
  }

</style>
