<template>
  <div class="form-wrap">
    <el-form
      class="searchBox"
      ref="form"
      :model="form"
      :inline="true"
      label-width="110px">
      <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="keyWords1">
        <el-select
          v-model="form.keyWords1"
          clearable
          placeholder="请选择">
          <el-option
            label="全部"
            value=""></el-option>
          <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="keyWords2">
        <el-input v-model="form.keyWords2"></el-input>
      </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 payChannelOptions"
            :key="item"
            :label="item"
            :value="item">
          </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="getList">
          搜索
        </el-button>
      </div>
    </el-form>

    <div class="tableBox">
      <div class="block-title">
        统计信息
      </div>
      <el-table
        border
        stripe
        v-loading="loading"
        :data="tableData"
        :row-class-name="totalRowClassName">
        <el-table-column
          type="index"
          label="#">
        </el-table-column>
        <el-table-column
          prop="ticket_type_name"
          label="票种">
        </el-table-column>
        <el-table-column
          prop="cancel_count"
          label="退票数量">
        </el-table-column>
        <el-table-column
          prop="cancel_price"
          label="退票金额">
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
import { exportCancelOrderStatistics } from '@/api/queryReport'
import { getCancelOrderStatistics } from '@/api/order'
import { getTicketTypeList } from '@/api/ticketType'
import { totalRowClassName } from '@/utils'
import moment from 'moment'

export default {
  name: 'refundStatistics',
  data () {
    return {
      ticketTypeList: [],
      form: {
        'startDate': new Date(moment().format('YYYY-MM-DD') + ' 00:00:00'),
        'endDate': new Date(moment().format('YYYY-MM-DD') + ' 23:59:59'),
        'keyWords1': '', // 票种
        'keyWords2': '', // 操作人
        'keyWords3': ''
      },
      tableData: [],
      loading: false
    }
  },
  computed: {
    payChannelOptions () {
      return this.$store.getters.payChannelOptions
    }
  },
  created () {
    const searchParams = JSON.parse(sessionStorage.getItem('统计表-退票统计-の查询条件'))
    if (searchParams) {
    // 如果存在查询条件，则设置到组件的数据中
      this.form = searchParams // 记住上次搜索条件
    }
    this.getTicketTypeList()
    this.getList()
  },
  methods: {
    totalRowClassName,
    reset () {
      this.$refs.form.resetFields()
    },
    reportExport () {
      exportCancelOrderStatistics(this.form).then(() => {
        this.$message.success('导出成功。')
        this.goToDownload()
      }, () => {
        this.$message.success('导出失败。')
      })
    },
    // 获取票种列表
    getTicketTypeList () {
      return getTicketTypeList({
        pageNum: 1,
        pageSize: 999
      }).then(res => {
        this.ticketTypeList = res.list
      })
    },
    getList () {
      this.loading = true
      getCancelOrderStatistics(this.form).then(res => {
        this.tableData = res.data
      }).finally(() => {
        this.loading = false
        sessionStorage.setItem('统计表-退票统计-の查询条件', JSON.stringify(this.form)) // 下次记住
      })
    }
  }
}
</script>
