<template>
  <div class="form-wrap">
    <el-form
      class="searchBox"
      ref="form"
      :model="form"
      :inline="true"
      label-width="100px">
      <div class="block-title">
        查询条件
      </div>
      <el-form-item
        label="省份"
        prop="keyWords5">
        <el-select
          v-model="form.keyWords5"
          clearable
          filterable>
          <el-option
            value=""
            label="全部"></el-option>
          <el-option
            v-for="item in provinceList"
            :key="item"
            :label="item"
            :value="item">
          </el-option>
        </el-select>
      </el-form-item>

      <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="keyWords1">
        <el-select
          v-model="form.keyWords1"
          placeholder="请选择">
          <el-option
            v-for="item in dimension"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="景点选择"
        prop="keyWords2"
      >
        <el-select
          v-model="form.keyWords2"
          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>
      <div class="btn-wrap">
        <el-button
          @click="reportExport">
          导出
        </el-button>
        <el-button
          @click="reset">
          重置
        </el-button>
        <el-button
          :disabled="loading"
          type="primary"
          @click="getSaleStatistic">
          搜索
        </el-button>
      </div>
    </el-form>

    <div class="tableBox">
      <div class="block-title">
        统计信息
      </div>
      <el-table
        border
        stripe
        v-loading="loading"
        :data="tableData">
        <el-table-column
          label="日期">
          <template slot-scope="prop">
            {{ prop.row.check_time ==='总计'?'总计':prop.row.check_time }}
          </template>
        </el-table-column>

        <el-table-column
          prop="check_count"
          label="数量">
        </el-table-column>
      </el-table>
      <!-- <el-pagination
        background
        :current-page.sync="form.pageNum"
        @current-change="getSaleStatistic()"
        layout="total, prev, pager, next"
        :total="total"
      >
      </el-pagination> -->
    </div>
  </div>
</template>

<script>
import { getFreeTicketStatistics } from '@/api/order.js'
import { exportFreeTicketStatistics } from '@/api/queryReport'
import moment from 'moment'
import { getProvinceList } from '@/api/visitor'

export default {
  components: {
  },
  data () {
    return {
      total: 0,
      form: {
        startDate: new Date(moment().startOf('day').valueOf()),
        endDate: new Date(moment().endOf('day').valueOf()),
        keyWords1: 'hour',
        keyWords2: '',
        keyWords5: ''
        // pageNum: 1,
        // pageSize: 10
      },
      tableData: [],
      provinceList: [],
      loading: false,
      dimension: [
        { label: '按小时', value: 'hour' },
        { label: '按天', value: 'month' },
        { label: '按月', value: 'year ' }

      ]
    }
  },
  computed: {
    scenicList () {
      return this.$store.state.app.scenicList
    }
  },
  created () {
    const searchParams = JSON.parse(sessionStorage.getItem('统计表-免票统计-の查询条件'))
    if (searchParams) {
    // 如果存在查询条件，则设置到组件的数据中
      this.form = searchParams // 记住上次搜索条件
    }
    this.getSaleStatistic()
    this.getProvinceList()
  },
  methods: {
    moment,
    reset () {
      this.$refs.form.resetFields()
    },
    reportExport () {
      let params = JSON.parse(JSON.stringify(this.form))
      exportFreeTicketStatistics(params).then(() => {
        this.$message.success('导出成功。')
        this.goToDownload()
      }, () => {
        this.$message.success('导出失败。')
      })
    },

    getSaleStatistic () {
      this.loading = true
      let params = JSON.parse(JSON.stringify(this.form))
      return getFreeTicketStatistics(params).then(res => {
        this.tableData = res
        this.total = res.length
      }).finally(() => {
        this.loading = false
        sessionStorage.setItem('统计表-免票统计-の查询条件', JSON.stringify(this.form)) // 下次记住
      })
    },

    getProvinceList () {
      getProvinceList().then((res) => {
        this.provinceList = res?.data
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
