<template>
  <a-modal
    centered
    class="add-card-modal"
    :footer="null"
    :maskClosable="false"
    title="新增礼品卡"
    v-model="isShow"
    @cancle="isShow=false"
    :width="600">
    <a-form-model
      id="add-card-form"
      :model="formData"
      :rules="rules"
      ref="ruleForm"
      :label-col="formItemLayout.labelCol"
      :wrapper-col="formItemLayout.wrapperCol"
      @submit="handleSubmit">
      <a-form-model-item label="创建人">
        <p class="creater">{{ userInfo.userNameCN }}</p><p class="telephone">{{ userInfo.mobile }}</p>
      </a-form-model-item>
      <a-form-model-item label="礼品卡内容" prop="goldNum">
        <a-input-number
          id="add-card-goldNum"
          v-model="formData.goldNum"
          :min="1"
          :max="999999"
          :precision="0"
          style="width: 90%;margin-right: 5px;"
          placeholder="请输入礼品卡内容(1~999999)"
          allowClear /><span>乐豆</span>
      </a-form-model-item>
      <a-form-model-item label="礼品卡数量" prop="createNum">
        <a-input-number
          id="add-card-createNum"
          v-model="formData.createNum"
          :min="1"
          :max="1000"
          :precision="0"
          style="width: 90%;margin-right: 5px;"
          placeholder="请输入礼品卡数量(1~1000)"
          allowClear /><span>张</span>
      </a-form-model-item>
      <a-form-model-item :wrapper-col="{ span: 12, offset: 6 }" style="text-align: center;">
        <a-button type="primary" html-type="submit" id="add-card-submit" style="margin-right: 15px">保存</a-button>
        <a-button @click="isShow=false" style="margin-left: 15px" id="add-card-close">取消</a-button>
      </a-form-model-item>
    </a-form-model>
  </a-modal>
</template>

<script>
import { giftCardAdd } from '@/api/giftCard'
import { mapGetters } from 'vuex'
export default {
  name: 'AddCardModal',
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    }
  },
  computed: {
    ...mapGetters(['userInfo'])
  },
  data () {
    return {
      isShow: this.openModal, //  是否打开弹框
      formItemLayout: {
        labelCol: { span: 4 },
        wrapperCol: { span: 16 }
      },
      formData: {
        goldNum: null, //  礼品卡内容
        createNum: 1 //  礼品卡数量
      },
      rules: {
        goldNum: [
          { required: true, message: '请输入礼品卡内容', trigger: 'blur' }
        ],
        createNum: [
          { required: true, message: '请输入礼品卡数量', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    //  保存
    handleSubmit (e) {
      e.preventDefault()
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          giftCardAdd(_this.formData).then(res => {
            if (res.status == 200) {
              _this.isShow = false
              this.$emit('refresh', true)
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
      } else {
        const _this = this
        setTimeout(() => {
          _this.$refs.ruleForm.resetFields()
        }, 100)
      }
    }
  }
}
</script>

<style lang="less" scoped>
  .add-card-modal{
    .creater{
      display: inline-block;
      margin-right: 15px;
    }
    .telephone{
      display: inline-block;
    }
  }
</style>
