<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="timeBegin">
        <el-date-picker
          v-model="form.timeBegin"
          type="datetime"
          placeholder="选择日期时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item
        label="截止时间"
        prop="timeEnd">
        <el-date-picker
          default-time="23:59:59"
          v-model="form.timeEnd"
          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> -->
      <el-form-item
        label="分组筛选"
        style="width:100%">
        <el-checkbox-group
          v-model="selectKeys"
          :min="0"
          :max="4">
          <draggable :list="groupKeys">
            <el-checkbox
              :key="item.value"
              :label="item.value"
              v-for="(item) in groupKeys"
              :disabled="disabled(item)"
            >
              {{ item.label }}
            </el-checkbox>
          </draggable>
        </el-checkbox-group>
      </el-form-item>
      <div
        class="btn-wrap"
        style="margin-bottom:20px">
        <el-button
          @click="reset">
          重置
        </el-button>
        <el-button
          :disabled="loading"
          type="primary"
          @click="getCheckStatistic">
          搜索
        </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"
        :row-class-name="totalRowClassName"
        :span-method="objectSpanMethod">
        <el-table-column
          v-for="(item) in finalGroup"
          :key="item.value"
          :prop="item.prop || item.value"
          :label="item.label">
        </el-table-column>
        <el-table-column
          prop="statDate"
          label="日期">
        </el-table-column>
        <!-- <el-table-column
          prop="team"
          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="scenicName"
          label="子景点名称">
        </el-table-column>
        <el-table-column
          prop="scenicUnitPrice"
          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 { getOsCheckStatistic } 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'

const groupKeys = [
  { value: 'team', label: '团体' }
]

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,
      form: {

        timeBegin: new Date(moment().startOf('day').subtract(7, 'day').format('YYYY-MM-DD HH:mm:ss')),
        timeEnd: 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: {
    getList () {
      this.getCheckStatistic()
    },
    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.timeBegin) {
        this.form.timeBegin = moment(this.form.timeBegin).format('YYYY-MM-DD HH:mm:ss')
      } else {
        this.form.timeBegin = ''
      }

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

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

      this.finalGroup = this.groupKeys.filter(i => {
        return this.selectKeys.includes(i.value)
      })

      this.form.groupByList = this.finalGroup.map(i => i.value)

      getOsCheckStatistic(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) {
      getOsCheckStatistic(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>
