<template>
  <a-modal
    :visible="value"
    title="发布广告"
    @ok="handleOk"
    width="60%"
    @cancel="handleCancel"
  >
    <a-form :form="form">
      <a-form-item
        v-bind="formItemLayout"
        label="包含广告位"
      >
        <a-tag
          v-for="(item, index) in versionInfo.positions"
          :key="index"
          color="blue"
        >
          {{ item.name }}
        </a-tag>
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<script>
import { addVersion, publishAdv } from '@/api/adv'
export default {
  props: {
    versionInfo: {
      type: Object,
      default: () => {}
    },
    value: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      form: this.$form.createForm(this),
      formItemLayout: {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 18 }
        }
      }
    }
  },
  methods: {
    handleOk (e) {
      e.preventDefault()
      this.form.validateFieldsAndScroll((err, values) => {
        if (!err) {
          const params = { ...values, ...this.versionInfo, ...{ name: '', description: '' } }
          addVersion(params).then(res => {
            publishAdv(res.extraInfo.id, []).then(() => {
              this.$message.success('发布成功')
              this.handleCancel()
            })
          })
        }
      })
    },
    handleCancel () {
      this.$emit('input', false)
      this.form.resetFields()
    }
  }
}
</script>

<style scoped>
.ant-form-item{
  width: 50%; display: inline-block;
}
.color-block {
  display: flex;
}
.color-label {
  margin-left: 15px;
  font-size: 16px;
  font-weight: bold;
}
.time-block {
  display: flex;
  align-items: center;
}
</style>
