<template>
  <a-modal width="600px" :visible="visible" title="门票改签" @ok="handleOk" @cancel="handleCancel">
    <a-form-model ref="form" :model="form">
      <a-form-model-item v-bind="layout" label="日期">
        <a-date-picker :disabledDate="disabledDate" v-model="form.date"></a-date-picker>
      </a-form-model-item>

      <a-form-model-item v-bind="layout" label="场次" v-if="ticket.batch_type_id">
        <a-select v-model="form.id">
          <a-select-option v-for="(item) in bindBatches" :key="item.id">
            {{ item.name }}
          </a-select-option>
        </a-select>
      </a-form-model-item>
    </a-form-model>
  </a-modal>
</template>

<script>
import moment from 'moment'
import { getTicketBatch } from '@/api/ticketSetting/ticket'
import { changeticket } from '@/api/orderSetting/order'
export default {
  data () {
    return {
      visible: false,
      bindBatches: [],
      form: {
        date: '',
        id: ''
      },
      layout: {
        labelCol: { span: 6 },
        wrapperCol: { span: 14 }
      }
    }
  },
  methods: {
    show (order, ticket) {
      this.visible = true
      this.order = order
      this.ticket = ticket
      this.form.date = moment(ticket.play_date_begin)
      this.getTicketBatch(ticket.ticket_type_id)
    },
    disabledDate (current) {
      return current < moment().startOf('day')
    },
    handleOk () {
      const { guest_identify, guest_name, guest_phone, price } = this.ticket

      const tickets = [{
        guest_identify,
        guest_name,
        guest_phone,
        price,
        batch_type_id: this.form.id,
        origin_ticket_id: this.ticket.id,
        play_date_begin: moment(this.form.date).format('YYYY-MM-DD'),
        ticket_type_id: this.ticket.ticket_type_id
      }]

      const _this = this

      this.$confirm({
        content: '确定改签吗？',
        onOk () {
          changeticket(_this.order.id, tickets).then(res => {
            _this.$message.success('修改成功')
            _this.$emit('update')
            _this.visible = false
          })
        }
      })
    },
    getTicketBatch (id) {
      this.loading = true

      getTicketBatch(id).then(res => {
        this.bindBatches = res.extraInfo.list
        this.form.id = this.bindBatches.length && this.bindBatches[0].id
      }).finally(() => {
        this.loading = false
      })
    },
    handleCancel () {
      this.visible = false
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
