<template>
  <a-modal
    centered
    id="editActiveEndTime-basicInfo-modal"
    class="editActiveEndTime-basicInfo-modal"
    :footer="null"
    :maskClosable="false"
    title="时间变更"
    v-model="isShow"
    @cancel="isShow=false"
    :width="500">
    <a-spin :spinning="spinning" tip="Loading...">
      <div style="text-align: center;margin-bottom: 20px;font-weight: bold;">确定要变更{{ form.name }}的促销时间吗?</div>
      <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="time">
          <span>{{ form.timeEnd }}</span>
        </a-form-model-item>
        <a-form-model-item label="变更后结束日期" prop="newTimeEnd">
          <a-date-picker
            id="editActiveEndTime-basicInfo-newTimeEnd"
            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-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 {
      spinning: false,
      isShow: this.openModal, //  是否打开弹框
      // form表单 label布局
      formItemLayout: {
        labelCol: { span: 6 },
        wrapperCol: { span: 16 }
      },
      form: {
        name: '', // 促销规则名称
        timeStart: '', // 开始时间
        timeEnd: '', // 结束时间
        newTimeEnd: undefined// 新变更时间
      },
      dateFormat: 'YYYY-MM-DD', // 时间显示格式
      // 表单规则验证
      rules: {
        newTimeEnd: [
          { required: true, message: '请选择变更的结束时间', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    // 选择变更时间
    onChange (date, dateString) {
      if (date) {
        this.form.newTimeEnd = dateString + ' 23:59:59'
      } else {
        this.form.newTimeEnd = undefined
      }
    },
    //  保存
    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: undefined
        }
        this.$refs.ruleForm.resetFields()
      }
    }
  }
}
</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>
