<template>
  <div class="form-wrap">
    <el-form
      class="search-box"
      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"
          multiple
          clearable>
          <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 saleChannels"
            :key="index"
            :label="item"
            :value="item"
          >
          </el-option>
        </el-select>
      </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="table-box">
      <!-- <div class="block-title">
        统计结果
      </div> -->
      <el-table
        border
        stripe
        v-loading="loading"
        :data="tableData"
        :row-class-name="totalRowClassName"
        :span-method="objectSpanMethod">
        <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="checkNum"
          label="检票人数">
        </el-table-column>
        <el-table-column
          prop="checkPrice"
          label="检票金额">
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>
<script>
import { getCheckerVerifyStatistics } from '@/api/queryReport'
import { totalRowClassName } from '@/utils'
import moment from 'moment'
import { getSaleChannelList } from '@/api/order'
// 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
    },
    ticketTypeList () {
      return this.$store.state.app.ticketTypeList
    },
    userScenic () {
      return this.$store.state.app.account.managerScenicMatrix ? this.$store.state.app.account.managerScenicMatrix : this.$store.state.app.scenicList
    }
  },
  components: {
  },
  data () {
    return {
      groupKeys: [{ value: 'tagName', label: '票种标签' }, { value: 'otaSourceName', label: '分销商' }],
      form: {
        checkTimeBegin: new Date(moment().startOf('day').subtract(7, 'day').format('YYYY-MM-DD HH:mm:ss')),
        checkTimeEnd: new Date(moment().endOf('day').format('YYYY-MM-DD HH:mm:ss')),
        ticketTypeIdList: [], //
        sourceList: [],
        pageNum: 1,
        pageSize: 10
      },
      selectKeys: [],
      finalGroup: [],
      tableIndex: 1,
      OrderIndexArr: [],
      tableData: [],
      deviceList: [],
      saleChannels: [],
      // ticketTypeList: [],
      loading: false
    }
  },
  created () {
    this.getCheckStatistic()
    this.getSaleChannelList()
  },
  mounted () {
  },
  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()
    },
    getSaleChannelList () {
      getSaleChannelList({ pageSize: -1, pageNum: 1 }).then((res) => {
        this.saleChannels = res?.data || []
      })
    },
    // 获取表格数据
    getCheckStatistic () {
      this.loading = true
      this.tableIndex++
      this.finalGroup = []
      this.finalGroup = this.groupKeys.filter(i => {
        return this.selectKeys.includes(i.value)
      })

      if (this.form.checkTimeBegin) {
        this.form.checkTimeBegin = moment(this.form.checkTimeBegin).format('YYYY-MM-DD HH:mm:ss')
      } else {
        this.form.checkTimeBegin = ''
      }

      if (this.form.checkTimeEnd) {
        this.form.checkTimeEnd = moment(this.form.checkTimeEnd).format('YYYY-MM-DD HH:mm:ss')
      } else {
        this.form.checkTimeEnd = ''
      }

      if (!this.form.ticketTypeIdList) {
        this.form.ticketTypeIdList = []
      }

      getCheckerVerifyStatistics(this.form).then(res => {
        this.tableData = res.data
      }).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.$confirm(`是否要导出明细数据`, '确认提示', {
        confirmButtonText: '是',
        cancelButtonText: '否',
        type: 'warning'
      }).then(() => {
        this.handleExport({
          ...this.form,
          export: true,
          exportDetail: true
        })
      }).catch(() => {
        this.handleExport({
          ...this.form,
          export: true,
          exportDetail: false
        })
      })
    },
    handleExport (params) {
      getCheckerVerifyStatistics(params).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>
