<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="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>
      <div class="btn-wrap">
        <el-button
          @click="reset">
          重置
        </el-button>
        <el-button
          :disabled="loading"
          type="primary"
          @click="getStatistic">
          搜索
        </el-button>
        <el-button
          type="primary"
          @click="reportExport">
          导出
        </el-button>
      </div>
    </el-form>

    <div class="table-box">
      <!-- <div class="block-title">
        统计信息
      </div> -->
      <el-table
        border
        stripe
        v-loading="loading"
        :data="tableData"
        :span-method="arraySpanMethod"
        :row-class-name="totalRowClassName"
        :cell-class-name="handleCellStyle"
        :header-cell-class-name="handleCellStyle"
      >
        <el-table-column
          prop="otaSourceName"
          label="渠道">
        </el-table-column>
        <el-table-column
          prop="ticketTypeId"
          label="票编号">
        </el-table-column>
        <el-table-column
          prop="ticketTypeName"
          label="票名称">
        </el-table-column>
        <el-table-column
          prop="unit_price"
          label="单价">
        </el-table-column>
        <el-table-column
          prop="sum_count"
          label="售票张数">
        </el-table-column>
        <el-table-column
          prop="sum_cancel"
          label="退票张数">
        </el-table-column>
        <el-table-column
          prop="sum_price"
          label="售票金额">
        </el-table-column>
        <el-table-column
          prop="cancelPrice"
          label="退票金额">
        </el-table-column>
        <el-table-column
          prop="cash_price"
          label="现金金额">
        </el-table-column>
        <el-table-column
          prop="bank_price"
          label="银行金额">
        </el-table-column>
        <el-table-column
          prop="actual_price"
          label="金额小计">
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
import { getDailyOrderStatistics, exportDailyOrderStatistics } from '@/api/queryReport'
import moment from 'moment'

export default {
  components: {
  },
  data () {
    return {
      form: {
        startDate: new Date(moment().startOf('day').valueOf()),
        endDate: new Date(moment().endOf('day').valueOf())
      },
      tableData: [],
      loading: false
    }
  },
  computed: {
  },
  created () {
    this.getStatistic()
  },
  watch: {
    'loading' (val) {
      const keys = Object.keys(this)

      keys.forEach(i => {
        if (i.indexOf('colunmSpan') > -1) {
          delete this[i]
        }
      })
    }
  },
  methods: {
    moment,
    reset () {
      this.$refs.form.resetFields()
    },
    reportExport () {
      exportDailyOrderStatistics(this.form).then(() => {
        this.$message.success('导出成功。')

        this.goToDownload()
      }, () => {
        this.$message.success('导出失败。')
      })
    },
    getStatistic () {
      this.loading = true
      let params = JSON.parse(JSON.stringify(this.form))
      this.tableData = []
      return getDailyOrderStatistics(params).then(res => {
        this.tableData = res.data
      }).finally(() => {
        this.loading = false
      })
    },
    arraySpanMethod ({ row, column, rowIndex, columnIndex }) {
      if (columnIndex < 1) {
        if (row[column.property] === '小计') {
          return [1, 1]
        }

        if (!this[`colunmSpan${columnIndex}`]) {
          this[`colunmSpan${columnIndex}`] = this.getSpanArrayByRepeat(this.tableData, column.property)
        }

        return this.getRowSpanByRepeat(this[`colunmSpan${columnIndex}`], rowIndex)
      }

      return [1, 1]
    },
    getRowSpanByRepeat (spanArray, rowIndex) {
      let span = spanArray.find(i => {
        return rowIndex >= i[0] && rowIndex <= i[1]
      })
      if (!span || (span[1] === span[0])) {
        return [1, 1]
      }

      if (rowIndex === span[0]) {
        return [span[1] - span[0] + 1, 1]
      } else {
        return [0, 0]
      }
    },
    getSpanArrayByRepeat (data, tag) {
      let indexArray = []
      data.forEach((item, index) => {
        if (index > 0) {
          if (item[tag] !== data[index - 1][tag]) {
            indexArray.push(index)
          }
        }
      })
      if (!indexArray.length) {
        indexArray = [data.length]
      }

      let rowSpanArray = indexArray.map((i, index) => {
        if (index === 0) {
          return [0, i - 1]
        } else {
          return [indexArray[index - 1], i - 1]
        }
      })
      if (indexArray.length > 0) {
        rowSpanArray.push([indexArray[indexArray.length - 1], data.length - 1])
      }
      return rowSpanArray
    },
    handleCellStyle ({ row, column, rowIndex, columnIndex }) {
      if (columnIndex === 3) {
        return 'boldBorder'
      }
      if (columnIndex === 7) {
        return 'boldBorder'
      }
    },
    totalRowClassName ({ row, rowIndex }) {
      if (row.otaSourceName === '小计') {
        return 'table-total-row'
      }
      if (row.ticketTypeName === '小计') {
        return 'table-total-row'
      }
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-table--border {
  .boldBorder {
    border-right-width: 5px;
  }
}
</style>
