<template>
  <div class="form-wrap">
    <el-form
      class="search-box"
      ref="form"
      :model="form"
      :inline="true"
    >
      <div class="block-title">
        查询条件
      </div>
      <el-form-item
        label="起始时间"
        prop="cancelTimeBegin">
        <el-date-picker
          v-model="form.cancelTimeBegin"
          type="datetime"
          placeholder="选择日期时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item
        label="结束时间"
        prop="cancelTimeEnd">
        <el-date-picker
          default-time="23:59:59"
          v-model="form.cancelTimeEnd"
          type="datetime"
          placeholder="选择日期时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item
        label="票种"
        prop="ticketTypeIdList">
        <el-select
          v-model="form.ticketTypeIdList"
          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="otaSourceNameList">
        <el-select
          v-model="form.otaSourceNameList"
          multiple
          clearable
          filterable>
          <el-option
            v-for="item in otaSourceList"
            :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="table-box">
      <!-- <div class="block-title">
        统计信息
      </div> -->
      <el-table
        border
        stripe
        v-loading="loading"
        :data="tableData"
        :row-class-name="totalRowClassName">
        <el-table-column
          prop="otaSourceName"
          label="销售渠道">
        </el-table-column>
        <el-table-column
          prop="ticketTypeName"
          label="票种">
        </el-table-column>
        <el-table-column
          prop="unitPrice"
          label="单价">
        </el-table-column>
        <el-table-column
          prop="cancelCount"
          label="退票数量">
        </el-table-column>
        <el-table-column
          prop="cancelNum"
          label="退票人数">
        </el-table-column>
        <el-table-column
          prop="cancelPrice"
          label="退票金额">
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

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

export default {
  name: 'refundStatistics',
  data () {
    return {
      form: {
        cancelTimeBegin: new Date(moment().format('YYYY-MM-DD') + ' 00:00:00'),
        cancelTimeEnd: new Date(moment().format('YYYY-MM-DD') + ' 23:59:59'),
        ticketTypeIdList: [], // 票种
        otaSourceNameList: []
      },
      tableData: [],
      otaSourceList: [],
      loading: false
    }
  },
  computed: {
    ticketTypeList () {
      const { ticketTypeList } = this.$store.state.app
      const { orderCategory } = this.form
      return orderCategory ? ticketTypeList.filter(item => item.category === orderCategory) : ticketTypeList
    },
    payChannelOptions () {
      return this.$store.getters.payChannelOptions
    }
  },
  created () {
    this.getSaleChannelList()
    this.getList()
  },
  methods: {
    totalRowClassName,
    getSaleChannelList () {
      getSaleChannelList().then(res => {
        this.otaSourceList = res.data
      })
    },
    reset () {
      this.$refs.form.resetFields()
    },
    setTime () {
      if (this.form.cancelTimeBegin) {
        this.form.cancelTimeBegin = moment(this.form.cancelTimeBegin).format('YYYY-MM-DD HH:mm:ss')
      } else {
        this.form.cancelTimeBegin = ''
      }

      if (this.form.cancelTimeEnd) {
        this.form.cancelTimeEnd = moment(this.form.cancelTimeEnd).format('YYYY-MM-DD HH:mm:ss')
      } else {
        this.form.cancelTimeEnd = ''
      }
    },
    reportExport () {
      this.form.export = true
      this.setTime()
      getCancelOrderStatistics(this.form).then(() => {
        this.$message.success('导出成功。')
        this.goToDownload()
      }, () => {
        this.$message.success('导出失败。')
      })
    },
    getList () {
      this.loading = true
      this.setTime()
      getCancelOrderStatistics(this.form).then(res => {
        this.tableData = res.data
      }).finally(() => {
        this.loading = false
      })
    }
  }
}
</script>
