<template>
  <div class="form-wrap">
    <el-form
      class="search-box"
      ref="form"
      :model="form"
      :inline="true"
      label-width="120px">
      <div class="block-title">
        查询条件
      </div>
      <el-form-item
        label="游玩开始日期"
        prop="keyWords1">
        <el-date-picker
          default-time="00:00:00"
          v-model="form.keyWords1"
          type="datetime"
          placeholder="选择日期时间">
        </el-date-picker>
      </el-form-item>

      <el-form-item
        label="游玩结束时间"
        prop="keyWords2">
        <el-date-picker
          default-time="23:59:59"
          v-model="form.keyWords2"
          type="datetime"
          placeholder="选择日期时间">
        </el-date-picker>
      </el-form-item>

      <el-form-item
        label="场次名称"
        prop="keyWords3">
        <el-select v-model="form.keyWords3">
          <el-option
            value=""
            label="全部">
          </el-option>
          <el-option
            v-for="item in batchList"
            :key="item.id"
            :label="item.name"
            :value="item.name">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item
        label="场次状态"
        prop="keyWords4">
        <el-select
          v-model="form.keyWords4"
          placeholder="">
          <el-option
            v-for="item in statusOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item
        label="场次开始时间"
        prop="keyWords5"
      >
        <el-time-picker
          value-format="HH:mm:ss"
          v-model="form.keyWords5"
          placeholder="任意时间点">
        </el-time-picker>
      </el-form-item>

      <el-form-item
        label="场次截止时间"
        prop="keyWords6"
      >
        <el-time-picker
          value-format="HH:mm:ss"
          v-model="form.keyWords6"
          placeholder="任意时间点">
        </el-time-picker>
      </el-form-item>

      <el-form-item
        label="绑定的票种列表"
        prop="keyList1"
      >
        <el-select
          v-model="form.keyList1"
          multiple
          collapse-tags
          clearable
          placeholder="请选择"
          @change="selectAll">
          <el-option
            :value="0"
            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="是否已预检">
        <el-select v-model="form.keyWords7">
          <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="keyWords8">
        <el-select
          v-model="form.keyWords8"
          clearable
          placeholder="请选择">
          <el-option
            label="全部"
            value=""></el-option>
          <el-option
            v-for="item in ticketStatusDic"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </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,index) in saleChannel"
            :key="index"
            :label="item"
            :value="item"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <div class="btn-wrap">
        <el-button
          @click="reset"
        >
          重置
        </el-button>
        <el-button
          :disabled="loading"
          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
        border
        stripe
        v-loading="loading"
        :data="tableData">
        <el-table-column
          prop="batchConfigName"
          label="场次名称"
        >
        </el-table-column>

        <el-table-column
          prop="sum_count"
          label="已售"
        >
        </el-table-column>
        <el-table-column
          prop="sum_people"
          label="已售人数"
        >
        </el-table-column>

        <el-table-column
          prop="cancel_count"
          label="已退"
        >
        </el-table-column>
        <el-table-column
          prop="cancel_people"
          label="已退人数"
        >
        </el-table-column>

        <el-table-column
          prop="actual_count"
          label="实售"
        >
        </el-table-column>
        <el-table-column
          prop="actual_people"
          label="实售人数"
        >
        </el-table-column>

        <el-table-column
          prop="query_count"
          label="预检"
        >
        </el-table-column>
        <el-table-column
          prop="query_people"
          label="预检人数"
        >
        </el-table-column>

        <el-table-column
          prop="not_query_count"
          label="未预检"
        >
        </el-table-column>
        <el-table-column
          prop="not_query_people"
          label="未预检人数"
        >
        </el-table-column>

        <el-table-column
          prop="check_count"
          label="核检"
        >
        </el-table-column>
        <el-table-column
          prop="check_people"
          label="核检人数"
        >
        </el-table-column>

        <el-table-column
          prop="not_check_count"
          label="未核检"
        >
        </el-table-column>
        <el-table-column
          prop="not_check_people"
          label="未核检人数"
        >
        </el-table-column>

        <el-table-column
          prop="query_not_check_count"
          label="预检未核检"
        >
        </el-table-column>
        <el-table-column
          prop="query_not_check_people"
          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 { getTicketSaleByBatch, exportTicketSaleByBatch } from '@/api/queryReport'
import { getTicketTypeList } from '@/api/ticketType'
import { ticketStatusDic } from '@/assets/staticData'

import moment from 'moment'
const statusOptions = [
  {
    label: '全部',
    value: ''
  },
  {
    label: '开启',
    value: 1
  },
  {
    label: '关闭',
    value: 0
  }
]

export default {
  computed: {
    batchList () {
      return this.$store.state.app.batchList
    },
    saleChannel () {
      return this.$store.state.app.saleChannel
    }
  },
  data () {
    return {
      ticketStatusDic,
      statusOptions,
      ticketTypeList: [],
      oldTicketTypeOptions: [[]],
      form: {
        keyWords1: new Date(moment().startOf('day')), // 游玩开始
        keyWords2: new Date(moment().endOf('day')), // 游玩截止
        keyWords3: '', // 场次名称
        keyWords4: '', // 场次状态
        keyWords5: '00:00:00', // 场次开始时间
        keyWords6: '23:59:59', // 场次结束时间
        keyWords7: '', // 是否预检
        keyWords8: '', // 票状态
        keyList1: [],
        keyList2: [],
        pageNum: 1,
        pageSize: 10
      },
      accountList: [],
      tableData: [],
      total: 0,
      loading: false
    }
  },
  async created () {
    this.getTicketTypeList()
    const searchParams = JSON.parse(sessionStorage.getItem('统计表-场次售票统计-查询条件'))
    if (searchParams) {
    // 如果存在查询条件，则设置到组件的数据中
      this.form = searchParams // 记住上次搜索条件
    }
    this.getList()
  },
  methods: {
    reset () {
      this.$refs.form.resetFields()
    },
    selectAll (val) {
      let allValues = this.ticketTypeList.map(i => i.id)

      allValues.unshift(0) // 添加全部
      // 用来储存上一次的值，可以进行对比
      const oldVal = this.oldTicketTypeOptions.length === 1 ? [] : this.oldTicketTypeOptions[1]

      // 若是全部选择
      if (val.includes(0)) this.form.keyList1 = allValues

      // 取消全部选中  上次有 当前没有 表示取消全选
      if (oldVal.includes(0) && !val.includes(0)) this.form.keyList1 = []

      // 点击非全部选中  需要排除全部选中 以及 当前点击的选项
      // 新老数据都有全部选中
      if (oldVal.includes(0) && val.includes(0)) {
        const index = val.indexOf(0)
        val.splice(index, 1) // 排除全选选项
        this.form.keyList1 = val
      }

      // 全选未选 但是其他选项全部选上 则全选选上 上次和当前 都没有全选
      if (!oldVal.includes(0) && !val.includes(0)) {
        if (val.length === allValues.length - 1) this.form.keyList1 = [0].concat(val)
      }

      // 储存当前最后的结果 作为下次的老数据
      this.oldTicketTypeOptions[1] = this.form.keyList1
    },
    // 获取票种列表
    getTicketTypeList () {
      return getTicketTypeList({
        pageNum: 1,
        pageSize: 999
      }).then(res => {
        this.ticketTypeList = res.list
      })
    },
    handleSizeChange (size) {
      this.form.pageSize = size
      this.getList()
    },
    getList () {
      this.loading = true
      getTicketSaleByBatch(this.form).then(res => {
        this.tableData = res?.data
      }).finally(() => {
        this.loading = false
      })
      sessionStorage.setItem('统计表-场次售票统计-查询条件', JSON.stringify(this.form)) // 下次记住
    },
    exportStatist () {
      let params = JSON.parse(JSON.stringify(this.form))

      params.keyWords1 = params.keyWords1 ? moment(params.keyWords1).format('YYYY-MM-DD HH:mm:ss') : ''
      params.keyWords2 = params.keyWords2 ? moment(params.keyWords2).format('YYYY-MM-DD HH:mm:ss') : ''

      exportTicketSaleByBatch(params).then(res => {
        this.$message.success('导出成功。')
        this.goToDownload()
      })
    }
  }
}
</script>

<style scoped>

</style>
