<template>
  <!-- 网络取票 -->
  <div class="form-wrap">
    <el-form
      class="search-box"
      ref="form"
      :model="form"
      :inline="true"
      label-width="100px">
      <div class="block-title">
        查询条件
      </div>
      <el-form-item
        label="手机号"
        prop="keyWords3">
        <el-input v-model="form.keyWords3"></el-input>
      </el-form-item>
      <el-form-item
        label="取票码"
        prop="keyWords15">
        <el-input
          v-model="form.keyWords15"
          clearable></el-input>
      </el-form-item>
      <el-form-item
        label="身份证"
        prop="keyWords4">
        <ReaderInput
          type="ID"
          v-model="form.keyWords4"></ReaderInput>
      </el-form-item>
      <el-form-item
        label="起始时间"
        prop="startDate"
      >
        <el-date-picker
          v-model="form.startDate"
          type="datetime"
          placeholder="选择日期时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item
        label="截止时间"
        prop="endDate"
      >
        <el-date-picker
          default-time="23:59:59"
          v-model="form.endDate"
          type="datetime"
          placeholder="选择日期时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item
        label="游玩起始时间"
        prop="startDate1"
      >
        <el-date-picker
          v-model="form.startDate1"
          type="datetime"
          placeholder="选择日期时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item
        label="游玩截止时间"
        prop="endDate1"
      >
        <el-date-picker
          default-time="23:59:59"
          v-model="form.endDate1"
          type="datetime"
          placeholder="选择日期时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item
        label="订单号"
        prop="keyWords1">
        <el-input v-model="form.keyWords1"></el-input>
      </el-form-item>
      <el-form-item
        label="票号"
        prop="keyWords7">
        <QRReader v-model="form.keyWords7"></QRReader>
      </el-form-item>
      <el-form-item
        label="分销商"
        prop="keyWords8">
        <el-input v-model="form.keyWords8"></el-input>
      </el-form-item>
      <!-- <el-form-item
        label="企业码"
        prop="keyWords5">
        <el-input v-model="form.keyWords5"></el-input>
      </el-form-item> -->
      <el-form-item
        label="是否已取"
        prop="keyWords14">
        <el-select v-model="form.keyWords14">
          <el-option
            value=""
            label="全部"></el-option>
          <el-option
            :value="1"
            label="是"></el-option>
          <el-option
            :value="0"
            label="否"></el-option>
        </el-select>
      </el-form-item>
      <div class="btn-wrap">
        <el-button
          type="default"
          @click="reset">
          重置
        </el-button>
        <el-button
          type="primary"
          @click="getList">
          查询
        </el-button>
      </div>
    </el-form>

    <div class="table-box">
      <div class="block-title">
        查询结果
      </div>
      <el-table
        border
        stripe
        v-loading="loading"
        :data="tableData">
        <el-table-column
          width="180"
          prop="orderNo"
          label="订单号">
        </el-table-column>
        <el-table-column
          width="180"
          prop="ota_3rd_orderNo"
          label="渠道订单号">
        </el-table-column>
        <el-table-column
          prop="otaSourceName"
          label="分销商">
        </el-table-column>
        <el-table-column
          label="票种">
          <template slot-scope="scope">
            {{ scope.row.tickets[0].tname }}
          </template>
        </el-table-column>
        <el-table-column
          width="80"
          prop="tickets.length"
          label="票数量">
        </el-table-column>
        <el-table-column
          width="100"
          prop="buyerName"
          label="预订人">
        </el-table-column>
        <el-table-column
          width="120"
          prop="buyerPhone"
          label="预订手机">
        </el-table-column>
        <el-table-column
          label="证件号码">
          <template slot-scope="scope">
            {{ scope.row.buyerIdentifyType | paperType }}
          &nbsp;
            {{ scope.row.buyerIdentify }}
          </template>
        </el-table-column>
        <el-table-column
          width="150"
          label="售票时间">
          <template slot-scope="scope">
            {{ scope.row.createTime | formatTime }}
          </template>
        </el-table-column>
        <el-table-column
          width="80"
          label="总价">
          <template slot-scope="scope">
            {{ scope.row.tickets.length * scope.row.tickets[0].price }}
          </template>
        </el-table-column>
        <el-table-column
          width="100"
          label="状态">
          <template slot-scope="scope">
            <el-tag :type="scope.row.status | orderStatusColor">
              {{ scope.row.status | orderStatus }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          width="120"
          label="取单张门票">
          <template slot-scope="scope">
            <el-link
              type="primary"
              @click="$refs.ticketList.show(scope.row)">
              查看门票
            </el-link>
          </template>
        </el-table-column>
        <el-table-column
          width="100"
          label="操作">
          <template
            slot-scope="scope"
            v-if="showPrintOrder(scope.row)">
            <el-link
              v-if="canPrint(scope.row)"
              type="primary"
              size="mini"
              @click="printOrder(scope.row)">
              订单取票
            </el-link>
            <el-link
              type="danger"
              disabled
              v-else>
              已取票
            </el-link>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        v-if="total > 10"
        background
        :current-page.sync="form.pageNum"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="form.pageSize"
        @size-change="handleSizeChange"
        @current-change="getList()"
        layout="sizes,total, prev, pager, next"
        :total="total">
      </el-pagination>
    </div>

    <ElDialog
      title="订单取票"
      width="80%"
      v-model="orderDialogVisible">
      <PrintOrder
        :info="currentItem"
        :ticket-type="dialogTicketType"
        @updateList="getList"
      ></PrintOrder>
    </ElDialog>
    <!-- <ElDialog
      title="取票"
      width="700px"
      v-model="ticketDialogVisible">
      <PrintTicket
        :info="currentItem"
        :ticket-type="dialogTicketType"
        :order-info="dialogOrderInfo"
        @updateList="getList"
      ></PrintTicket>
    </ElDialog> -->

    <TicketList
      ref="ticketList"
      @updateList="getList"></TicketList>
  </div>
</template>

<script>
import { getOrderListForPrint } from '@/api/printOnlineTicket'
import { getTicketTypeList } from '@/api/ticketType'
import ElDialog from '@/components/Dialog'
import PrintOrder from './printOlTicket/printOrderDialog'
import ReaderInput from '@/components/ReaderInput'
import TicketList from './printOlTicket/TicketList'
import moment from 'moment'
// import TicketList from './TicketList'

export default {
  name: 'printOnlineTicket',
  components: {
    ElDialog,
    PrintOrder,
    ReaderInput,
    TicketList
  },
  data () {
    return {
      form: {
        startDate: new Date(moment().startOf('day').subtract(1, 'months').valueOf()),
        endDate: new Date(moment().endOf('day').valueOf()),
        startDate1: new Date(moment().startOf('day').valueOf()),
        endDate1: new Date(moment().endOf('day').valueOf()),
        keyWords1: '', // 订单号
        keyWords2: '', // 游客姓名
        keyWords3: '', // 游客手机
        keyWords4: '', // 游客身份证
        keyWords5: '', // 状态
        keyWords6: '', // 团散
        keyWords7: '', // 票号
        keyWords8: '', // 是否已取票
        keyWords9: '', // 取票码
        pageNum: 1,
        pageSize: 5
      },
      tableData: [],
      total: 0,
      orderDialogVisible: false,
      ticketDialogVisible: false,
      currentItem: {},
      loading: false,
      ticketTypeList: [],
      dialogTicketType: {},
      dialogOrderInfo: {}
    }
  },
  created () {
    this.getTicketTypeList()
    this.getList()
  },
  methods: {
    showPrintOrder (item) {
      return item.status === 'WAIT_USE' || item.status === 'PART_USE'
    },
    // 获取票种列表
    getTicketTypeList () {
      return getTicketTypeList({
        pageNum: 1,
        pageSize: 999
      }).then(res => {
        this.ticketTypeList = res.list
      })
    },
    reset () {
      this.$refs.form.resetFields()
    },
    handleSizeChange (val) {
      this.form.pageSize = val
      this.getList()
    },
    getList () {
      this.loading = true
      getOrderListForPrint(this.form).then(res => {
        this.total = res.total
        this.tableData = res.list
      }).finally(() => {
        this.loading = false
      })
    },
    canPrint (order) {
      return order.tickets.some(ticket => !ticket.isPrint)
    },
    printOrder (item) {
      let ticketType = this.ticketTypeList.find(type => type.id === item.tickets[0].ticketTypeId)
      this.dialogTicketType = ticketType
      this.orderDialogVisible = true
      this.currentItem = item
    },
    printTicket (order, item) {
      let ticketType = this.ticketTypeList.find(type => type.id === item.ticketTypeId)
      this.dialogTicketType = ticketType
      this.dialogOrderInfo = order
      this.ticketDialogVisible = true
      this.currentItem = item
    }
  }
}
</script>
