<template>
  <div class="form-wrap">
    <el-form
      class="searchBox"
      ref="form"
      :model="form"
      :inline="true">
      <div class="block-title">
        查询条件
      </div>
      <el-form-item
        label="起始时间"
        prop="checkTimeBegin">
        <el-date-picker
          v-model="form.checkTimeBegin"
          type="datetime"
          placeholder="选择日期时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item
        label="截止时间"
        prop="checkTimeEnd">
        <el-date-picker
          default-time="23:59:59"
          v-model="form.checkTimeEnd"
          type="datetime"
          placeholder="选择日期时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item
        label="票种"
        prop="ticketTypeIdList">
        <el-select v-model="form.ticketTypeIdList">
          <el-option
            value=""
            label="全部"></el-option>
          <el-option
            v-for="item in ticketTypeList"
            :key="item.id"
            :value="item.id"
            :label="item.name">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item
        label="销售渠道"
        prop="sourceList">
        <el-select
          v-model="form.sourceList"
          multiple
          clearable>
          <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="keyWords7"
      >
        <el-select
          v-model="form.keyWords7"
          placeholder="请选择">
          <el-option
            value=""
            label="全部">
          </el-option>
          <el-option
            :value="1"
            label="散客">
          </el-option>
          <el-option
            :value="2"
            label="团体">
          </el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item
        label="是否按分销商分组"
        style="white-space:nowrap">
        <el-switch
          v-model="isShow_otaName"
          active-color="#13ce66"
          inactive-color="#ff4949">
        </el-switch>
      </el-form-item> -->

      <!-- <el-form-item
        label="分组筛选"
        style="width:100%">
        <el-checkbox-group
          v-model="selectKeys"
          :max="4">
          <draggable :list="groupKeys">
            <el-checkbox
              :key="item.value"
              :label="item.value"
              v-for="(item) in groupKeys"
            >
              {{ item.label }}
            </el-checkbox>
          </draggable>
        </el-checkbox-group>
        （可拖拽排序）
      </el-form-item> -->

      <div
        class="btn-wrap"
        style="margin-bottom:20px">
        <el-button
          @click="reportExport">
          导出
        </el-button>
        <el-button
          @click="reset">
          重置
        </el-button>
        <el-button
          :disabled="loading"
          type="primary"
          @click="getCheckStatistic">
          搜索
        </el-button>
      </div>
    </el-form>

    <div class="tableBox">
      <div class="block-title">
        统计结果
      </div>
      <el-table
        border
        stripe
        v-loading="loading"
        :data="tableData"
        :row-class-name="totalRowClassName">
        <!-- <el-table-column
          v-for="(item,index) in finalGroup"
          :key="index"
          :prop="item.prop||item.value"
          :label="item.label">
        </el-table-column> -->
        <!-- <el-table-column
          v-if="tableData.find(v=>v.otaName)"
          prop="otaName"
          label="分销商">
        </el-table-column>
        <el-table-column
          v-if="tableData.find(v=>v.tagName)"
          prop="tagName"
          label="票种标签">
        </el-table-column> -->
        <el-table-column
          prop="otaSourceName"
          label="订单渠道">
        </el-table-column>
        <el-table-column
          prop="ticketTypeName"
          label="票种">
        </el-table-column>
        <el-table-column
          prop="unitPrice"
          label="单价">
        </el-table-column>
        <el-table-column
          prop="checkCount"
          label="检票数量">
        </el-table-column>
        <el-table-column
          prop="检票人数"
          label="人数">
        </el-table-column>
        <el-table-column
          prop="checkPrice"
          label="检票金额">
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>
<script>
import { exportCheckStatisticsInTicketCount } from '@/api/queryReport'
import { getTicketTypeList } from '@/api/ticketType'
import { totalRowClassName } from '@/utils'
import { apiOrder } from '@/api/order.js'
// import ReaderInput from '@/components/ReaderInput'
// import draggable from 'vuedraggable'
// import moment from 'moment'
export default {
  name: 'checkStatistic',
  computed: {
    scenicName () {
      return this.$localStore.get('scenicName') || this.$store.state.user.scenicName
    },
    saleChannel () {
      return this.$store.state.app.saleChannel
    },
    scenicList () {
      return this.$store.state.app.scenicList
    },
    userScenic () {
      return this.$store.state.app.account.manager_scenic_matrix ? this.$store.state.app.account.manager_scenic_matrix : this.$store.state.app.scenicList
    }
  },
  components: {
  },
  data () {
    return {
      groupKeys: [{ value: 'tagName', label: '票种标签' }, { value: 'otaName', label: '分销商' }],
      ticketTypeList: [],
      form: {
        export: false,
        checkTimeBegin: '',
        checkTimeEnd: '',
        ticketTypeIdList: '', //
        sourceList: [],
        pageNum: 1,
        pageSize: 10
      },
      selectKeys: [],
      finalGroup: [],
      tableIndex: 1,
      OrderIndexArr: [],
      isShow_otaName: false,
      tableData: [],
      loading: false
    }
  },
  created () {
    this.getTicketTypeList()
    this.getCheckStatistic()
  },
  mounted () {
    if (this.scenicName !== '铭投山庄') return
    setTimeout(() => {
      this.form.keyList2 = this.userScenic.map(v => v.id)
      this.getCheckStatistic()
    }, 1000)
  },
  watch: {
    isShow_otaName (newName, oldName) {
      if (newName) {
        this.form.keyGroupBy = ['otaName']
        this.getCheckStatistic()
      }
    }
  },
  methods: {
    hideClear () {
      if (['铭投山庄'].includes(this.scenicName)) {
        if (this.form.keyList2.length === 1) {
          return 'hideClear'
        } else {
          return ''
        }
      } else {
        return ''
      }
    },
    disabled (item) {
      if (['铭投山庄'].includes(this.scenicName)) {
        if (this.form.keyList2.length > 1) {
          return false
        } else if (this.form.keyList2.length === 1) {
          if (this.form.keyList2.includes(item.id)) {
            return true
          } else {
            return false
          }
        }
      } else {
        return false
      }
    },
    totalRowClassName,
    reset () {
      this.$refs.form.resetFields()
    },
    // 获取票种列表
    getTicketTypeList () {
      return getTicketTypeList({
        pageNum: 1,
        pageSize: 999
      }).then(res => {
        this.ticketTypeList = res.list
      })
    },
    // 获取表格数据
    getCheckStatistic () {
      this.loading = true
      this.tableIndex++
      let groupArray = []
      let orderIndexArr = []
      this.finalGroup = []
      this.finalGroup = this.groupKeys.filter(i => {
        return this.selectKeys.includes(i.value)
      })
      // this.form.keyGroupBy = this.finalGroup.map(i => i.value)
      apiOrder('orderCheckStatistics', this.form).then(res => {
        console.log(res, '111')
        this.tableData = res.data

        this.finalGroup.forEach(i => {
          groupArray.push({})
        })
        groupArray.forEach((group, index) => {
          const keyName = this.finalGroup[index].prop || this.finalGroup[index].value

          this.tableData.forEach((el, index) => {
            if (el[keyName] === '小计') {
              this.index++
            }

            !group[el[keyName] + this.index] && (group[el[keyName] + this.index] = [])
            group[el[keyName] + this.index].push(index)
          })

          const arr = []
          for (let k in group) {
            if (group[k].length > 1) {
              arr.push(group[k])
            }
          }
          orderIndexArr.push(arr)
        })

        this.OrderIndexArr = orderIndexArr
      }).finally(() => {
        this.loading = false
      })
    },
    objectSpanMethod ({ row, column, rowIndex, columnIndex }) {
      if (columnIndex < this.finalGroup.length) {
        if (!(this.OrderIndexArr[columnIndex] && this.OrderIndexArr[columnIndex].length)) return

        for (let i = 0; i < this.OrderIndexArr[columnIndex].length; i++) {
          let element = this.OrderIndexArr[columnIndex][i]
          for (let j = 0; j < element.length; j++) {
            let item = element[j]
            if (rowIndex === item) {
              if (j === 0) {
                return {
                  rowspan: element.length,
                  colspan: 1
                }
              } else if (j !== 0) {
                return {
                  rowspan: 0,
                  colspan: 0
                }
              }
            }
          }
        }
      }
    },
    reportExport () {
      this.form.export = true
      exportCheckStatisticsInTicketCount(this.form).then(() => {
        this.$message.success('导出成功。')
        this.goToDownload()
      }, () => {
        this.$message.success('导出失败。')
      })
    }
  }
}
</script>

<style scoped lang="scss">
.hideClear{
  ::v-deep .el-tag.el-tag--info .el-tag__close{
    display: none;
  }
}
</style>
