<template>
  <div
    class="table-box buyer-info"
    v-if="data">
    <el-form
      :inline="true"
      label-width="120"
      label-position="right"
      class="buyer-info">
      <el-form-item
        label="旅行社"
        v-if="data.groupIndividual === 2"
      >
        <span>{{ data.travelAgencyName }}</span>
      </el-form-item>
      <el-form-item :label="data.groupIndividual === 1 ? '预订人' : '导游'">
        {{ data.buyerName || '暂无' }}
      </el-form-item>
      <el-form-item label="手机号">
        {{ data.buyerPhone || '暂无' }}
      </el-form-item>
      <el-form-item label="证件类型">
        {{ data.buyerIdentifyType | paperType }}
      </el-form-item>
      <el-form-item label="证件号">
        {{ data.buyerIdentify || '暂无' }}
      </el-form-item>
      <br>
      <el-form-item
        label="票号搜索"
        style="width: 100%;"
      >
        <el-input v-model="filterText"></el-input>
      </el-form-item>
    </el-form>
    <el-checkbox
      style="float: right;"
      v-model="isStorage"
    >
      <span class="danger">使用预印票出票</span>
    </el-checkbox>
    <el-table
      border
      :data="tableData">
      <el-table-column
        width="80"
        label="序号"
        prop="id"
      >
        <template slot-scope="scope">
          {{ scope.row.id.toString().padStart(8,'0') }}
        </template>
      </el-table-column>
      <el-table-column
        prop="ticketNo"
        label="票号"
      >
      </el-table-column>

      <el-table-column
        v-if="isStorage"
        width="260px"
        column-key="storage"
        label="预印票票号">
        <template slot-scope="scope">
          <QRReader
            v-if="scope.row.status === 'WAIT_USE'"
            class="qr-input"
            v-model="scope.row.storage_ticket"></QRReader>
        </template>
      </el-table-column>

      <el-table-column
        prop="tname"
        label="票种"
      >
      </el-table-column>

      <template v-if="tableData[0]">
        <el-table-column
          v-if="tableData[0].performName"
          label="节目"
        >
          <template slot-scope="scope">
            {{ scope.row.performName }}
          </template>
        </el-table-column>

        <el-table-column
          v-if="tableData[0].batchConfigName"
          label="场次"
        >
          <template slot-scope="scope">
            {{ scope.row.batchConfigName }}
          </template>
        </el-table-column>

        <el-table-column
          v-if="tableData[0].seatRowTag"
          label="座位号"
        >
          <template slot-scope="scope">
            {{ `${scope.row.seatNameNew}${scope.row.seatRowTag }排${scope.row.seatName}座` }}
          </template>
        </el-table-column>
      </template>
      <el-table-column
        v-if="tableData[0].scheduleConfigName"
        prop="scheduleConfigName"
        label="班次"
      >
      </el-table-column>
      <el-table-column
        v-if="tableData[0].scheduleConfigName"
        label="班次内序号">
        <template
          slot-scope="scope"
          v-if="scope.row.scheduleConfigName">
          {{ scope.row.scheduleSerialNumber }}
        </template>
      </el-table-column>
      <el-table-column
        width="80"
        label="票价"
      >
        <template slot-scope="scope">
          {{ scope.row.price/scope.row.checkNum }}
        </template>
      </el-table-column>

      <el-table-column
        prop="guestName"
        label="游客姓名"
      >
      </el-table-column>

      <el-table-column
        prop="guestIdentify"
        label="身份证"
      >
      </el-table-column>

      <el-table-column
        width="100"
        prop="checkNum"
        label="检票人数"
      >
      </el-table-column>

      <el-table-column
        width="100"
        prop="payDateBegin"
        label="游玩时间"
      >
        <template slot-scope="scope">
          {{ scope.row.isNeedPlaytime?scope.row.payDateBegin:'无限制' }}
        </template>
      </el-table-column>

      <el-table-column
        width="80"
        label="状态"
      >
        <template slot-scope="scope">
          <OrderStatusTag :value="scope.row.status"></OrderStatusTag>
        </template>
      </el-table-column>

      <el-table-column
        width="80"
        label="小计"
        prop="price"
      >
      </el-table-column>

      <el-table-column
        width="80"
        label="已补打次数"
        prop="second_print_times"
      >
      </el-table-column>

      <el-table-column
        width="100"
        label="操作"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            v-if="showPrint(scope.row)"
            @click="print(scope.row)">
            打印
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="btn-wrap-center">
      <el-button
        type="primary"
        size="mini"
        v-if="(!ticket_used_can_print || ticket_used_can_print ==='1') && (data.status !== 'WAIT_PAY' &&data.status !== 'CLOSED')"
        @click="printAll">
        全部打印
      </el-button>
      <el-button
        type="primary"
        size="mini"
        v-if="(!ticket_used_can_print || ticket_used_can_print ==='1') && (data.status !== 'WAIT_PAY' &&data.status !== 'CLOSED')"
        @click="printSmall">
        打印小票
      </el-button>
    </div>
  </div>
</template>

<script>
import { secondPrintTicket } from '@/api/printModal'
import checkPermission from '@/utils/checkPermission'

export default {
  data () {
    return {
      projectName: process.env.VUE_APP_PROJECT,
      orderInfo: null,
      currentTicket: null,
      filterText: '',
      isStorage: false
    }
  },
  props: {
    data: {
      type: Object,
      default: null
    }
  },
  computed: {
    tableData () {
      if (this.data && this.data.tickets) {
        return this.data.tickets.filter(t =>
          !this.filterText || t.ticketNo.indexOf(this.filterText) !== -1
        )
      } else {
        return []
      }
    },
    ticket_used_can_print () {
      let ticket_used_can_print = this.$store.state.app.systemConfig.find(i => i.configKey === 'ticket_used_can_print')
      return ticket_used_can_print && ticket_used_can_print.configValue
    }
  },
  methods: {
    print (currentTicket) {
      if (this.isStorage && !currentTicket.storage_ticket) {
        return this.$message.error('预制门票票号不能为空')
      }
      let list = [currentTicket.ticketNo]
      let params = {
        ticketList: list,
        storageTicketList: this.isStorage ? [currentTicket.storage_ticket].filter(Boolean) : null
      }
      secondPrintTicket(params).then(() => {
        this.$print({ ...JSON.parse(JSON.stringify(this.data)), tickets: [currentTicket], type: 'html' })
        this.$message.success('操作成功')
        this.$parent.handleClose()
        this.$emit('update')
      })
    },
    printAll () {
      // “全部打印”，打印的是筛选后的内容
      let tickets = this.tableData
      let refundOutDate = checkPermission('ticket_sale:print_outofdate')
      let refundUsed = checkPermission('ticket_sale:print_checked')
      // 过滤已过期门票
      if (!refundOutDate) {
        tickets = tickets.filter(t => {
          return t.status !== 'OUTOFDATE'
        })
      }

      // 过滤已检票门票
      if (!refundUsed) {
        tickets = tickets.filter(t => {
          return t.status !== 'USED'
        })
      }
      tickets = tickets.filter(t => {
        return t.status !== 'CANCELED'
      })
      if (tickets.length === 0) {
        this.$message.error('无门票可以打印')
        return
      }

      if (this.isStorage && tickets.some(i => !i.storage_ticket)) {
        return this.$message.error('预制门票票号不能为空')
      }

      let data = { ...this.data, tickets }
      let list = tickets.map(t => t.ticketNo)
      let params = {
        ticketList: list,
        storageTicketList: this.isStorage ? tickets.map(i => i.storage_ticket) : null
      }
      secondPrintTicket(params).then(() => {
        this.$print(data)

        this.$message.success('操作成功')
        this.$parent.handleClose()
        this.$emit('update')
      })
    },
    showPrint (ticket) {
      if (this.data.status === 'WAIT_PAY' || this.data.status === 'CLOSED') {
        return false
      }
      if (ticket.status === 'CANCELED') {
        return false
      }
      // 打印过期票权限
      if (ticket.status === 'OUTOFDATE') {
        if (this.ticket_used_can_print && this.ticket_used_can_print === '0') {
          return false
        }
        return checkPermission('ticket_sale:print_outofdate')
      }

      // 打印已检票权限
      if (ticket.status === 'USED') {
        if (this.ticket_used_can_print && this.ticket_used_can_print === '0') {
          return false
        }
        return checkPermission('ticket_sale:print_checked')
      }

      return true
    },
    printSmall () {
      this.data.manager_name = this.data.manager
      this.$printSmallTicket(this.data)
    }
  }
}
</script>

<style scoped>

</style>
