<template>
  <div class="form-wrap">
    <el-form
      class="search-box"
      ref="form"
      :model="form"
      :inline="true"
      label-width="100px">
      <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
          default-time="23:59:59"
          v-model="form.endDate"
          type="datetime"
          placeholder="选择日期时间">
        </el-date-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="售票员"
        prop="keyWords3">
        <el-select v-model="form.keyWords3">
          <el-option
            label="全部"
            value=""></el-option>
          <el-option
            v-for="item in salesList"
            :key="item.id"
            :label="item.nickName"
            :value="item.nickName">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="景点"
        prop="keyWords4">
        <el-select v-model="form.keyWords4">
          <el-option
            value=""
            label="全部"></el-option>
          <el-option
            v-for="item in userScenic"
            :key="item.id"
            :value="item.id"
            :label="item.name">
          </el-option>
        </el-select>
      </el-form-item>

      <template>
        <el-form-item
          label="游玩开始时间"
          prop="keyWords5">
          <el-date-picker
            v-model="form.keyWords5"
            type="datetime"
            placeholder="选择日期时间">
          </el-date-picker>
        </el-form-item>

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

        <el-form-item
          label="销售渠道"
          prop="keyWords1"
        >
          <el-select
            v-model="form.keyWords1"
            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>
      </template>

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

    <div class="table-box">
      <el-table
        border
        stripe
        v-loading="loading"
        :data="tableData2"
        :row-class-name="totalRowClassName">
        <el-table-column
          width="100"
          prop="otaSourceName"
          label="销售渠道">
        </el-table-column>
        <el-table-column
          prop="sum_price"
          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="cancelPrice"
          label="退票金额">
        </el-table-column>
        <el-table-column
          prop="sum_cancel"
          label="退票张数">
        </el-table-column>
        <el-table-column
          prop="cancel_people"
          label="退票人数">
        </el-table-column>
        <el-table-column
          prop="actual_price"
          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>
    </div>
  </div>
</template>

<script>
import { getScenic, getTicketTypeList } from '@/api/ticketType'
import { apiOrder, getOrderStatisticsBySaleChannel, getOrderStatisticsByManager } from '@/api/order.js'
import { exportOrderStatistics } from '@/api/queryReport'
import { totalRowClassName } from '@/utils'
import moment from 'moment'
export default {
  computed: {
    salesList () {
      return this.$store.state.user.salesList
    },
    saleChannel () {
      return this.$store.state.app.saleChannel
    },
    userScenic () {
      return this.$store.state.app.account.managerScenicMatrix ? this.$store.state.app.account.managerScenicMatrix : this.scenicList
    }
  },
  data () {
    return {
      ticketTypeList: [],
      oldTicketTypeOptions: [[]],
      scenicList: [],
      form: {
        startDate: new Date(moment().startOf('day').format('YYYY-MM-DD HH:mm:ss')),
        endDate: new Date(moment().endOf('day').format('YYYY-MM-DD HH:mm:ss')),
        keyWords1: null, // 销售渠道
        keyWords2: '', // 票种id
        keyWords3: '', // 售票员id
        keyWords4: '', // 景点
        keywords5: ''
      },
      tableData1: [],
      tableData2: [],
      tableData3: [],
      loading: false
    }
  },
  created () {
    this.getTicketTypeList()
    this.getScenicList()
    this.getSaleStatistic()
  },
  methods: {
    totalRowClassName,
    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
    },
    reset () {
      this.$refs.form.resetFields()
    },
    reportExport () {
      exportOrderStatistics(this.form).then(() => {
        this.$message.success('导出成功。')
        this.goToDownload()
      }, () => {
        this.$message.success('导出失败。')
      })
    },
    // 获取票种列表
    getTicketTypeList () {
      return getTicketTypeList({
        pageNum: 1,
        pageSize: 999
      }).then(res => {
        this.ticketTypeList = res.list
      })
    },
    // 获取景点列表
    getScenicList () {
      return getScenic().then(res => {
        this.scenicList = res?.data.children.map(item => item.data)
      })
    },
    // 获取表格数据
    getSaleStatistic () {
      this.loading = true
      Promise.all([
        this.getSaleStatistic2()
      ]).finally(() => {
        this.loading = false
      })
    },
    getSaleStatistic1 () {
      return apiOrder('getOrderStatistics', this.form).then(res => {
        this.tableData1 = res.data
      })
    },
    getSaleStatistic2 () {
      return getOrderStatisticsBySaleChannel(this.form).then(res => {
        this.tableData2 = res.data
      })
    },
    getSaleStatistic3 () {
      return getOrderStatisticsByManager(this.form).then(res => {
        this.tableData3 = res.data
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
