import TouristList from './TouristList'
import { randomString } from '@/utils'

export default {
  data () {
    return {
      touristList: []
    }
  },
  components: {
    TouristList
  },
  computed: {
    category () {
      return this.$route.meta.category || 'ticket'
    }
  },
  methods: {
    showTouristList (record) {
      this.currentRecord = record
      this.formData = this.$parent.$parent.$refs.formInfo.form
      this.touristList = record.tickets
      this.touristList.forEach(v => {
        v.idKey = randomString()
      })
      if (!this.currentRecord.currentTicket) {
        this.currentRecord.currentTicket = this.currentRecord.tickets[0]
      }
      this.$refs.touristList.show()
    },
    updateTourist (data) {
      const { id: ticketTypeId, playDateBegin, playDateEnd, isDiscount, price, batchConfigId } = this.currentRecord.currentTicket
      this.currentRecord.tickets = data.map(item => {
        const data = {
          ...item,
          playDateBegin,
          playDateEnd,
          isDiscount,
          price,
          ticketTypeId
        }

        batchConfigId && (data.batchConfigId = batchConfigId)

        return data
      })

      this.currentRecord.count = data.length
      this.currentRecord.total = this.$NP.times(this.currentRecord.price, this.currentRecord.count)
    },
    handleConfirm () {
      if (this.category === 'batch') {
        if (this.tickets.some(i => !i.batchConfigId)) {
          return this.$message.error('请选择场次')
        }
      }
      this.formData = this.$parent.$parent.$refs.formInfo.form
      this.formData.remark = this.remark
      this.formData.pictureRemarks = this.pictureRemarks

      if (this.formData.checkWay === 3) {
        if (this.tickets.some(i => !i.guestIdentify)) {
          return this.$message.error('检票方式为身份证入园，门票列表缺少身份证，请检查。')
        }
      }

      if (this.formData.checkWay === 4) {
        if (this.tickets.some(i => !i.face)) {
          return this.$message.error('检票方式为人脸入园，门票列表缺少人脸照片，请检查。')
        }
      }

      if (this.formData.checkWay === 5) {
        if (this.tickets.some(i => !i.ticketNo)) {
          return this.$message.error('检票方式为预印票，门票列表缺少票号，请检查。')
        }
      }

      this.$refs.confirm.show()
    }
  }
}
