<template>
  <div class="form-wrap">
    <el-form
      class="search-box"
      ref="form"
      :model="form"
      :inline="true"
      label-width="140px"
    >
      <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
          v-model="form.endDate"
          type="datetime"
          default-time="23:59:59"
          placeholder="选择日期时间"
        >
        </el-date-picker>
      </el-form-item>

      <el-form-item
        label="交易类型"
        prop="keyWords1"
      >
        <el-select
          v-model="form.keyWords1"
          placeholder="请选择"
        >
          <el-option
            v-for="item in memberTradeTypeList"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item
        label="操作员"
        prop="keyWords2"
      >
        <el-input v-model="form.keyWords2"></el-input>
      </el-form-item>

      <el-form-item
        label="线上会员是否已取"
        prop="keyList1"
      >
        <el-select
          v-model="form.keyWords6"
          placeholder="请选择">
          <el-option
            label="全部"
            value=""></el-option>
          <el-option
            label="是"
            :value="1"
          >
          </el-option>
          <el-option
            label="否"
            :value="0"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item
        label="会员类型列表"
        prop="keyList1"
      >
        <el-select
          v-model="form.keyList1"
          multiple
          collapse-tags
          clearable
          placeholder="请选择">
          <el-option
            v-for="item in memberTypeList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item
        label="价格最小值"
        prop="keyWords4">
        <el-input v-model.number="form.keyWords4"></el-input>
      </el-form-item>

      <el-form-item
        label="价格最大值"
        prop="keyWords5">
        <el-input v-model.number="form.keyWords5"></el-input>
      </el-form-item>
      <el-form-item
        label="销售渠道"
        prop="keyWords7">
        <el-select
          v-model="form.keyWords7"
          placeholder="请选择">
          <el-option
            label="全部"
            :value="null"></el-option>
          <el-option
            v-for="(item,index) in saleChannel"
            :key="index"
            :label="item"
            :value="item"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="售票点"
        prop="keyList2"
      >
        <el-select
          v-model="form.keyList2"
          multiple>
          <el-option
            v-for="item in ticketSpotList"
            :key="item.id"
            :value="item.id"
            :label="item.name"></el-option>
        </el-select>
      </el-form-item>

      <div class="btn-wrap">
        <el-button
          @click="reset"
        >
          重置
        </el-button>
        <el-button
          type="primary"
          @click="getList"
        >
          搜索
        </el-button>
        <el-button
          type="primary"
          @click="exportStatist">
          导出
        </el-button>
      </div>
    </el-form>

    <div class="table-box">
      <div class="block-title">
        会员交易统计
      </div>
      <el-table
        stripe
        border
        :row-class-name="totalRowClassName"
        :span-method="arraySpanMethod"
        v-loading="loading"
        :data="tableData">
        <el-table-column
          prop="trade_type"
          label="交易类型"
          width="80"
        >
        </el-table-column>
        <el-table-column
          prop="payChannel"
          label="支付方式"
        >
        </el-table-column>
        <el-table-column
          prop="memberTypeName"
          label="会员类型"
        >
        </el-table-column>
        <el-table-column
          prop="price"
          label="价格"
        >
        </el-table-column>
        <el-table-column
          prop="number"
          label="数量"
        >
        </el-table-column>
        <el-table-column
          prop="total_money"
          label="总计"
        >
        </el-table-column>
      </el-table>
      <el-pagination
        background
        :current-page.sync="form.pageNum"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="form.pageSize"
        @size-change="handleSizeChange"
        @current-change="getList()"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { memberTradeTypeList } from '@/assets/staticData'
import { exportMemberTradeStatistics } from '@/api/queryReport'
import { getMemberType, getMemberTradeStatistics } from '@/api/member'
import { getTicketSpotList } from '@/api/ticketSpot'

import moment from 'moment'

export default {
  name: 'memberTradeStatistics',
  data () {
    return {
      comName: '',
      memberTypeList: [],
      ticketSpotList: [],
      memberTradeTypeList,
      form: {
        startDate: new Date(moment().format('YYYY-MM-DD') + ' 00:00:00'),
        endDate: new Date(moment().format('YYYY-MM-DD') + ' 23:59:59'),
        keyWords1: '', // 交易类型 1销售 2充值 3结算 4补卡
        keyWords2: '', // 创建人
        keyWords3: '', // 会员名字
        keyWords4: '', // 价格最小值
        keyWords5: '', // 价格最大值
        keyWords6: '', // 线上会员是否已取
        keyWords7: '', // 销售渠道
        keyList1: [],
        keyList2: [],
        orderByKeyWords: '',
        orderString: '',
        keyGroupBy: ['payChannel'],
        pageNum: 1,
        pageSize: 10
      },
      tableData: [],
      total: 0,
      loading: false
    }
  },
  filters: {
    tradeType (input) {
      switch (input) {
        case 1:
          return '销售'
        case 2:
          return '充值'
        case 3:
          return '结算'
        case 4:
          return '补卡'
        case 5:
          return '入库'
        case 6:
          return '激活'
      }
    }
  },
  computed: {
    saleChannel () {
      return this.$store.state.app.saleChannel
    }
  },
  mounted () {
    this.getTicketSpotList()
    this.getMemberTypeList().then(() => {
      this.getList()
    })
  },
  methods: {
    reset () {
      this.$refs.form.resetFields()
    },
    exportStatist () {
      exportMemberTradeStatistics(this.form).then(res => {
        this.$message.success('导出成功。')
        this.goToDownload()
      })
    },
    getTicketSpotList () {
      this.loading = true
      return getTicketSpotList({
        pageNum: 1,
        pageSize: 10000
      }).then(res => {
        this.ticketSpotList = res.list
      })
    },
    totalRowClassName ({ row, rowIndex }) {
      for (let key in row) {
        if (row[key] === '小计' || (row[key] + '').indexOf('总计') > -1) {
          return 'table-total-row'
        }
      }
    },
    arraySpanMethod ({ row, column, rowIndex, columnIndex }) {
      if (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 === 0) {
        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]
        }
      }
    },
    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.trade_type === '小计') {
          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
    },
    // 获取会员类型
    getMemberTypeList () {
      return getMemberType({
        pageNum: 1,
        pageSize: 10000
      }).then(res => {
        this.memberTypeList = res.list
      })
    },
    handleSizeChange (size) {
      this.form.pageSize = size
      this.getList()
    },
    // 获取列表
    getList () {
      this.loading = true
      getMemberTradeStatistics(this.form).then(res => {
        this.tableData = res.data
        // 设置需要合并单元格的数组
        this.getGroupArray()
      }).finally(() => {
        this.loading = false
      })
    }
  }
}
</script>

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

</style>
