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.$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 { currentTicket } = this.currentRecord
      const { id: ticketTypeId, playDateBegin, playDateEnd, isDiscount, batchConfigId } = currentTicket
      this.currentRecord.tickets = data.map(item => {
        const data = {
          ...item,
          playDateBegin,
          playDateEnd,
          isDiscount,
          ticketTypeId
        }

        batchConfigId && (data.batchConfigId = batchConfigId)

        return data
      })
      this.currentRecord.discountDescription = this.currentRecord.tickets.map((item) => item.discountDescription).filter(i => i).join('，')
      this.currentRecord.count = data.length
      this.currentRecord.total = this.$NP.times(this.currentRecord.price, this.currentRecord.count)
    },
    handleConfirm () {
      const currentBatch = this.$parent.$refs.formInfo.currentBatch

      if (this.category === 'batch') {
        console.log('当前选择的场次', this.currentTicket)
        console.log('当前选择的批次', currentBatch)

        if (!this.currentTicket?.id || !currentBatch?.batchConfigId) {
          return this.$message.error('请选择场次')
        }
      }
      this.formData = this.$parent.$refs.formInfo.form
      this.formData.batchConfigId = currentBatch?.batchConfigId || ''
      this.currentTicket.batchConfigId = currentBatch?.batchConfigId || ''
      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()
    }
  }
}
