<template>
  <a-modal
    centered
    class="editActiveEndTime-basicInfo-modal"
    :footer="null"
    :maskClosable="false"
    title="促销时间变更"
    v-model="isShow"
    @cancel="isShow=false"
    :width="500">
    <a-spin :spinning="spinning" tip="Loading...">
      <a-form-model
        id="editActiveEndTime-basicInfo-form"
        ref="ruleForm"
        :model="form"
        :rules="rules"
        :label-col="formItemLayout.labelCol"
        :wrapper-col="formItemLayout.wrapperCol" >
        <a-form-model-item label="" prop="name">
          <div style="margin-left:40px;">确定要变更{{ form.name }}的促销时间吗?</div>
        </a-form-model-item>
        <a-form-model-item label="原结束日期" prop="time">
          <span>{{ form.timeEnd }}</span>
        </a-form-model-item>
        <a-form-model-item label="变更后结束日期" prop="newTimeEnd">
          <a-date-picker style="width:90%" v-model="form.newTimeEnd" @change="onChange" :format="dateFormat" :disabled-date="disabledDate"/>
        </a-form-model-item>
      </a-form-model>
      <div class="btn-cont">
        <a-button id="editActiveEndTime-basicInfo-modal-back" @click="isShow = false">取消</a-button>
        <a-button style="margin-left: 15px;" type="primary" id="editActiveEndTime-modal-save" @click="handleSave">确定</a-button>
      </div>
    </a-spin>
  </a-modal>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import moment from 'moment'
import { promotionDateModify } from '@/api/promotion'
export default {
  name: 'EditActiveEndTimeModal',
  mixins: [commonMixin],
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    } },
  data () {
    return {
      isShow: this.openModal, //  是否打开弹框
      spinning: false,
      formItemLayout: {
        labelCol: { span: 6 },
        wrapperCol: { span: 16 }
      },
      form: {
        name: '',
        timeStart: '',
        timeEnd: '',
        newTimeEnd: ''
      },
      dateFormat: 'YYYY-MM-DD',
      rules: {
        newTimeEnd: [
          { required: true, message: '请选择变更的结束时间', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    onChange (date, dateString) {
      this.form.newTimeEnd = dateString + ' 23:59:59'
    },
    //  保存
    handleSave () {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          promotionDateModify({ promotionSn: _this.form.sn, promotionDateStart: _this.form.timeStart, promotionDateEnd: _this.form.newTimeEnd }).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              setTimeout(() => {
                _this.isShow = false
                _this.$emit('ok', res.data)
                _this.spinning = false
              }, 1000)
            } else {
              _this.spinning = false
            }
          })
        } else {
          return false
        }
      })
    },
    // 获取显示内容
    setData (obj) {
      this.form = { ...this.form, ...obj }
    },
    // 不可选日期
    disabledDate (date, dateStrings) {
      return date && date.valueOf('day') < moment().subtract(1, 'day') // 今天以后，包含今天
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.form = {
          name: '',
          timeStart: '',
          timeEnd: '',
          newTimeEnd: ''
        }
      }
    }
  }
}
</script>

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

</style>
