<template>
  <el-card>
    <div
      slot="header"
      class="card-title">
      <span class="top-title">最近售票</span>
      <div class="opt-btns">
        <el-button
          @click="getRecentOrders(true)">
          刷新
        </el-button>
        <el-button
          @click="getMoreOrders">
          查看更多
        </el-button>
      </div>
    </div>

    <el-table
      border
      :data="tableData">
      <el-table-column
        prop="orderNo"
        label="订单号"
        width="220">
      </el-table-column>
      <el-table-column
        prop="orderCategory"
        label="订单类型">
        <template slot-scope="scope">
          <span v-if="scope.row.orderCategory === 'ticket'">景区门票</span>
          <span v-else-if="scope.row.orderCategory === 'batch'">场次售票</span>
          <span v-else-if="scope.row.orderCategory === 'member'">年卡次卡</span>
          <span v-else>{{ scope.row.orderCategory }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="firstTicketTypeName"
        label="票种">
      </el-table-column>
      <el-table-column
        prop="payChannel"
        label="支付渠道">
      </el-table-column>
      <el-table-column
        prop="payTime"
        label="售票时间">
      </el-table-column>
      <el-table-column
        prop="ticketNum"
        label="票数">
      </el-table-column>
      <el-table-column
        prop="price"
        label="总价">
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page.sync="pageNum"
      :page-size="pageSize"
      @current-change="getRecentOrders()"
      layout="total, prev, pager, next"
      :total="total"
    >
    </el-pagination>
  </el-card>
</template>

<script>
import { apiOrderList } from '@/api/order'
import mixin from './mixin'
import orderItemMixin from '../common/orderItemMixin'
import moment from 'moment'
import { EventBus } from '@/utils/eventBus'

export default {
  mixins: [mixin, orderItemMixin],
  props: {
    value: {
      type: Array,
      default: () => []
    },
    currentTicket: {
      type: Object,
      default: () => null
    }
  },
  computed: {
    category () {
      return this.$route.meta.category || 'ticket'
    }
  },
  created () {
    this.getRecentOrders(true)
  },
  data () {
    return {
      tableData: [],
      total: 0,
      pageSize: 10,
      pageNum: 1,
      loading: false
    }
  },
  methods: {
    getRecentOrders (goFirst = false) {
      if (goFirst) {
        this.pageNum = 1
      }
      const { pageSize, pageNum, category } = this
      const params = {
        pageNum,
        pageSize,
        orderCategory: category,
        createTimeBegin: moment().subtract(7, 'days').format('YYYY-MM-DD HH:mm:ss'),
        createTimeEnd: moment().format('YYYY-MM-DD HH:mm:ss')
      }
      this.loading = true
      apiOrderList(params).then(res => {
        this.total = res.data.total
        this.tableData = res.data.records
      }).finally(() => {
        this.loading = false
      })
    },
    getMoreOrders () {
      this.$router.push({ name: 'salesQuery' })

      EventBus.$emit('activeTag', '/queryReport/salesQuery')
    }
  }
}
</script>

<style lang="scss" scoped>
.item {
  margin-left: 20px;
}

.price-info {
  padding: 10px 0;
  text-align: right;
  background: #fff;
}

.btn-wrap {
  display: flex;
  width: 100%;
  margin-bottom: 10px;
  font-size: 14px;
  justify-content: space-between;
  align-items: center;

  .el-input {
    display: inline-block;
    width: 100px;
  }
}
</style>
