<template>
  <a-modal
    centered
    class="edit-card-modal"
    :footer="null"
    :maskClosable="false"
    title="编辑礼品卡"
    v-model="isShow"
    @cancle="isShow=false"
    :width="600">
    <a-form-model
      id="edit-card-form"
      :model="formData"
      :rules="rules"
      ref="ruleForm"
      :label-col="formItemLayout.labelCol"
      :wrapper-col="formItemLayout.wrapperCol"
      @submit="handleSubmit"
      v-if="detailsData">
      <a-form-model-item label="创建时间"> {{ detailsData.createDate }} </a-form-model-item>
      <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="礼品卡内容"> {{ detailsData.content }} </a-form-model-item>
      <a-form-model-item label="实体卡编号" prop="optionalSequence">
        <a-input id="edit-card-optionalSequence" :maxLength="30" v-model="formData.optionalSequence" placeholder="请输入实体卡编号(最多30个字符)" allowClear />
      </a-form-model-item>
      <a-form-model-item label="备注" prop="remarks">
        <a-textarea id="edit-card-remarks" :maxLength="500" v-model="formData.remarks" placeholder="请输入备注(最多500个字符)" allowClear />
      </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="edit-card-submit" style="margin-right: 15px">保存</a-button>
        <a-button @click="isShow=false" style="margin-left: 15px" id="edit-card-close">取消</a-button>
      </a-form-model-item>
    </a-form-model>
  </a-modal>
</template>

<script>
import { mapGetters } from 'vuex'
import { giftCardDetail, giftCardEdit } from '@/api/giftCard'
export default {
  name: 'EditCardModal',
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    cardId: {
      type: [Number, String],
      default: ''
    }
  },
  computed: {
    ...mapGetters(['userInfo'])
  },
  data () {
    return {
      isShow: this.openModal, //  是否打开弹框
      formItemLayout: {
        labelCol: { span: 4 },
        wrapperCol: { span: 16 }
      },
      formData: {
        id: this.cardId,
        optionalSequence: '', //  实体卡编号
        remarks: '' //  备注
      },
      rules: {},
      detailsData: null //  详情
    }
  },
  methods: {
    //  获取详情
    getDetails () {
      giftCardDetail({ id: this.cardId }).then(res => {
        if (res.status == 200) {
          this.formData.id = res.data.id
          this.formData.optionalSequence = res.data.optionalSequence || ''
          this.formData.remarks = res.data.remarks || ''
          this.detailsData = res.data
        } else {
          this.detailsData = null
        }
      })
    },
    //  物流信息  保存
    handleSubmit (e) {
      e.preventDefault()
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          giftCardEdit(_this.formData).then(res => {
            if (res.status == 200) {
              _this.isShow = false
              this.$emit('refresh')
            }
          })
        } 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)
      }
    },
    cardId (newValue, oldValue) {
      if (this.isShow && newValue) {
        this.getDetails()
      }
    }
  }
}
</script>

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