<template>
  <div class="form-wrap">
    <el-form
      class="search-box"
      ref="form"
      :model="form"
      :inline="true"
      label-width="70px"
    >
      <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="getOrderGrandTotalStatistics"
        >
          搜索
        </el-button>
        <el-button
          type="primary"
          @click="reportExport"
          :loading="exporting">
          导出
        </el-button>
      </div>
    </el-form>

    <div class="table-box">
      <div
        class="block-title"
        style="padding-top:10px">
        售票累计统计
        <Tip msg="包含所有渠道门票的销售累计数据。"></Tip>
      </div>
      <!--  -->
      <el-table
        border
        stripe
        :row-class-name="totalRowClassName"
        :span-method="arraySpanMethod"
        v-loading="loading"
        :data="tableData">
        <el-table-column
          prop="category_name"
          label="类别"
        >
        </el-table-column>
        <el-table-column
          prop="sale_channel_name"
          label="渠道名"
        >
        </el-table-column>
        <el-table-column
          prop="ticketTypeName"
          label="票种名称"
        >
        </el-table-column>
        <el-table-column
          prop="day_count"
          label="日售数量"
        >
        </el-table-column>
        <el-table-column
          prop="day_price"
          label="日售金额"
        >
        </el-table-column>
        <el-table-column
          prop="month_count"
          label="月售数量"
        >
        </el-table-column>
        <el-table-column
          prop="month_price"
          label="月售金额"
        >
        </el-table-column>
        <el-table-column
          prop="year_count"
          label="年售数量"
        >
        </el-table-column>
        <el-table-column
          prop="year_price"
          label="年售金额"
        >
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
import Tip from '@/components/Tip'
import { getOrderGrandTotalStatistics } from '@/api/order'
import { exportOrderGrandTotalStatistics } from '@/api/queryReport'
import moment from 'moment'

export default {
  name: 'orderTotalStatistics',
  data () {
    return {
      loading: false,
      exporting: false,
      tableData: [],
      form: {
        startDate: new Date(moment().format('YYYY-MM-DD') + ' 00:00:00'),
        endDate: new Date(moment().format('YYYY-MM-DD') + ' 23:59:59')
      }
    }
  },
  components: {
    Tip
  },
  created () {
    const searchParams = JSON.parse(sessionStorage.getItem('统计表-售票累计统计-查询条件'))
    if (searchParams) {
    // 如果存在查询条件，则设置到组件的数据中
      this.form = searchParams // 记住上次搜索条件
    }
    this.getOrderGrandTotalStatistics()
  },
  methods: {
    reset () {
      this.$refs.form.resetFields()
    },
    getOrderGrandTotalStatistics () {
      this.loading = true
      getOrderGrandTotalStatistics(this.form).then(res => {
        this.tableData = res.data
        this.getGroupArray()
        this.loading = false
        sessionStorage.setItem('统计表-售票累计统计-查询条件', JSON.stringify(this.form)) // 下次记住
      })
    },
    totalRowClassName ({ row, rowIndex }) {
      for (let key in row) {
        if (row[key] === '小计') {
          return 'table-total-row table-total-row2'
        }
        if (row[key] === '总计') {
          return 'table-total-row'
        }
      }
    },
    arraySpanMethod ({ row, column, rowIndex, columnIndex }) {
      if (rowIndex === this.tableData.length - 1) return [1, 1]
      if (columnIndex === 0 || columnIndex === 1) {
        let span = this.rowSpanArray.find(i => {
          return rowIndex >= i[0] && rowIndex <= i[1]
        })

        if (!span) {
          return [1, 1]
        }

        if (rowIndex === span[0]) {
          return [span[1] - span[0], 1]
        } else if (rowIndex === span[1]) {
          return [1, 1]
        } else {
          return [0, 0]
        }
      }

      if (columnIndex === 1) {
        let span = this.tradeTypeArray.find(i => {
          return rowIndex >= i[0] && rowIndex <= i[1]
        })

        if (!span) {
          return [1, 1]
        }

        if (rowIndex === span[0]) {
          return [span[1] - span[0] + 1, 1]
        } else {
          return [0, 0]
        }
      }
    },
    reportExport () {
      this.exporting = true
      exportOrderGrandTotalStatistics({}).then(() => {
        this.$message.success('导出成功。')

        this.goToDownload()
      }, () => {
        this.$message.success('导出失败。')
      }).finally(() => {
        this.exporting = false
      })
    },
    getGroupArray () {
      let tradeTypeIndex = []
      this.tableData.forEach((item, index) => {
        if (this.tableData[index - 1]) {
          if (item.trade_type !== this.tableData[index - 1].trade_type) {
            tradeTypeIndex.push(index)
          }
        }
      })

      let tradeTypeArray = tradeTypeIndex.map((i, index) => {
        if (index === 0) {
          return [0, i - 1]
        } else {
          return [tradeTypeIndex[index - 1], i - 1]
        }
      })

      this.tradeTypeArray = tradeTypeArray

      let totalIndex = this.tableData.map((item, index) => {
        if (item.ticketTypeName === '小计') {
          return index
        } else {
          return null
        }
      }).filter(Boolean)

      let rowSpanArray = totalIndex.map((i, index) => {
        if (index === 0) {
          return [0, i]
        } else {
          return [totalIndex[index - 1] + 1, i]
        }
      })
      this.rowSpanArray = rowSpanArray
    }
  }
}
</script>

<style lang="scss" scoped>
.form-wrap{
  ::v-deep .el-table tr td:first-child .cell, .el-table tr th:first-child .cell{
    padding-left:10px;
  }
}
</style>
