<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="keyList1">
        <el-select
          v-model="form.keyList1"
          clearable
          multiple
          filterable
          placeholder="请选择"
        >
          <el-option
            v-for="item in ticketTypeList"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="票种归类标签"
        prop="keyList4">
        <el-select
          v-model="form.keyList4"
          multiple
          filterable
          clearable>
          <el-option-group
            v-for="group in ticketTagList"
            :key="group.name"
            :label="group.name">
            <el-option
              v-for="item in group.children"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-option-group>
        </el-select>
      </el-form-item>
      <el-form-item
        label="时间"
        prop="startDate"
      >
        <el-date-picker
          v-model="form.startDate"
          type="month"
          placeholder="选择日期时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item
        label="支付方式"
        prop="keyList6">
        <el-select
          v-model="form.keyList6"
          filterable
          multiple
          clearable>
          <el-option
            v-for="item in payChannelOptions"
            :key="item"
            :label="item"
            :value="item">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item
        label="销售渠道"
        prop="keyList5"
      >
        <el-select
          v-model="form.keyList5"
          placeholder="请选择"
          multiple
          filterable
          clearable
        >
          <el-option
            v-for="(item,idx) in saleChannel"
            :key="idx"
            :label="item"
            :value="item"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="景点"
        prop="keyWords4"
      >
        <el-select
          v-model="form.keyWords4"
          placeholder="请选择">
          <el-option
            value=""
            label="全部">
          </el-option>
          <el-option
            v-for="item in scenicList"
            :key="item.id"
            :value="item.id"
            :label="item.name">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="售票点"
        prop="keyList8"
      >
        <el-select
          v-model="form.keyList8"
          multiple>
          <el-option
            v-for="item in ticketSpotList"
            :key="item.id"
            :value="item.id"
            :label="item.name"></el-option>
        </el-select>
      </el-form-item>
      <div class="btn-wrap">
        <el-button
          @click="reset">
          重置
        </el-button>
        <el-button
          :disabled="loading"
          type="primary"
          @click="getComparedAnalysis">
          搜索
        </el-button>
        <el-button
          type="primary"
          @click="exportOrderComparedAnalysis">
          导出
        </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
          v-for="(item,index) in tableRow"
          :key="index"
          :prop="String(index)"
          :label="item">
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
import { getComparedAnalysis, exportOrderComparedAnalysis } from '@/api/order.js'
import { exportFreeTicketStatistics } from '@/api/queryReport'
import { getTicketTypeList } from '@/api/ticketType'
import { getTicketSpotList } from '@/api/ticketSpot'
import { getTicketTypeTagList } from '@/api/ticketTypeTag'

import moment from 'moment'

export default {
  components: {
  },
  data () {
    return {
      total: 0,
      form: {
        startDate: new Date(moment().format('YYYY-MM')),
        keyWords4: '', // 景点
        keyList1: [], // 票种
        keyList8: [], // 售票点
        keyList6: [], // 支付方式
        keyList5: [], // 销售渠道
        keyList4: [], // 票种标签
        pageNum: 0,
        pageSize: -1
      },
      tableData: [],
      tableRow: [], // 列表头部数据
      ticketTypeList: [], // 票种列表
      ticketSpotList: [], // 售票点列表
      ticketTagList: [], // 票种标签列表
      loading: false,
      dimension: [
        { label: '按小时', value: 'hour' },
        { label: '按天', value: 'month' },
        { label: '按月', value: 'year ' }

      ]
    }
  },
  computed: {
    scenicList () {
      return this.$store.state.app.scenicList
    },
    payChannelOptions () {
      return this.$store.getters.payChannelOptions
    },
    saleChannel () {
      return this.$store.state.app.saleChannel
    }
  },
  created () {
    const searchParams = JSON.parse(sessionStorage.getItem('统计表-门票同比分析表-查询条件'))
    if (searchParams) {
    // 如果存在查询条件，则设置到组件的数据中
      this.form = searchParams // 记住上次搜索条件
    }
    this.getComparedAnalysis()
    this.getTicketTypeList()
    this.getTicketSpotList()
    this.getTicketTagList()
  },
  methods: {
    moment,
    reset () {
      this.$refs.form.resetFields()
    },
    // 获取票种标签列表
    getTicketTagList () {
      getTicketTypeTagList({}).then(res => {
        this.ticketTagList = res.children
      })
    },
    // 获取票种列表
    getTicketTypeList () {
      return getTicketTypeList({
        pageNum: 1,
        pageSize: 999
      }).then(res => {
        this.ticketTypeList = res.list
      })
    },
    // 获取售票点
    getTicketSpotList () {
      return getTicketSpotList({
        pageNum: 1,
        pageSize: 999
      }).then(res => {
        this.ticketSpotList = res.list
      })
    },
    reportExport () {
      let params = JSON.parse(JSON.stringify(this.form))
      exportFreeTicketStatistics(params).then(() => {
        this.$message.success('导出成功。')
        this.goToDownload()
      }, () => {
        this.$message.success('导出失败。')
      })
    },
    exportOrderComparedAnalysis () {
      exportOrderComparedAnalysis(this.form).then(res => {
        this.$message.success('导出成功。')
        this.goToDownload()
      })
    },

    getComparedAnalysis () {
      this.loading = true
      let params = JSON.parse(JSON.stringify(this.form))
      return getComparedAnalysis(params).then(res => {
        this.tableRow = res.shift()
        this.tableData = res.map(i => {
          return { ...i }
        })
      }).finally(() => {
        this.loading = false
        sessionStorage.setItem('统计表-门票同比分析表-查询条件', JSON.stringify(this.form)) // 下次记住
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
