<template>
  <div class="table-box">
    <el-table
      border
      stripe
      :data="freeCount"
      :row-class-name="totalRowClassName"
    >
      <el-table-column
        label="今日免费通行人数数据"
        label-class-name="table-title"
      >
        <el-table-column
          prop="type"
          label="票/卡">
        </el-table-column>
        <el-table-column
          prop="ticketTypeName"
          label="类别">
        </el-table-column>
        <el-table-column
          prop="count"
          label="次数">
        </el-table-column>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import moment from 'moment'
import { totalRowClassName } from '@/utils'
import { getCheckStatisticsInTicketCount, getFreeTicketStatistics } from '@/api/order.js'
import { getMemberCheckStatistics } from '@/api/member'

export default {
  data () {
    return {
      totalRowClassName,
      freeTicketCount: [], // 免费票通行人数
      freeMemberCount: [], // 免费卡通行人数
      freePriceCount: [], // 免票
      startDate: moment().startOf('day').format('YYYY-MM-DD HH:mm:ss'),
      endDate: moment().endOf('day').format('YYYY-MM-DD HH:mm:ss')
    }
  },
  computed: {
    freeCount () {
      let sum = (this.freeTicketCount[this.freeTicketCount.length - 1] ? this.freeTicketCount[this.freeTicketCount.length - 1].count : 0) + (this.freeMemberCount[this.freeMemberCount.length - 1] ? this.freeMemberCount[this.freeMemberCount.length - 1].count : 0) + (this.freePriceCount[this.freePriceCount.length - 1] ? this.freePriceCount[this.freePriceCount.length - 1].count : 0)
      let obj = {
        type: '总计',
        ticketTypeName: '',
        count: sum
      }
      let freeCount = this.freeTicketCount.length ? this.freeTicketCount.concat(this.freeMemberCount) : this.freeMemberCount
      freeCount = freeCount.concat(this.freePriceCount)
      freeCount.push(obj)
      freeCount = freeCount.filter(i => !(i.ticketTypeName !== '小计' && i.price === '小计'))
      return freeCount
    }
  },
  created () {
    this.getFreeMemberCheckStatistics()
    this.getCheckStatisticsInTicketCount()
    this.getSaleStatistic()
  },
  methods: {
    getFreeMemberCheckStatistics (params) {
      getMemberCheckStatistics({
        keyWords4: 4,
        startDate: this.startDate,
        endDate: this.endDate
      }).then(res => {
        let obj = res.data[0]
        let keys = res.memberTypeList
        this.freeMemberCount = keys.map(i => {
          return {
            'type': '卡',
            'ticketTypeName': i,
            'count': obj[i]
          }
        })
      })
    },
    getSaleStatistic () {
      return getFreeTicketStatistics({
        startDate: moment().startOf('day').format('YYYY-MM-DD HH:mm:ss'),
        endDate: moment().endOf('day').format('YYYY-MM-DD HH:mm:ss')
      }).then(res => {
        this.tableData = res
        this.freePriceCount = res.map(i => {
          return {
            'type': '免票',
            'ticketTypeName': '免票',
            'count': Number(i.checkNums),
            ...i
          }
        })
      })
    },
    getCheckStatisticsInTicketCount () {
      getCheckStatisticsInTicketCount({
        keyWords4: 0,
        keyWords5: 0,
        startDate: this.startDate,
        endDate: this.endDate
      }).then(res => {
        this.freeTicketCount = res.data.map(i => {
          return {
            'type': '门票',
            ...i
          }
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
