<template>
  <div class="form-wrap">
    <el-form
      class="search-box"
      ref="form"
      :model="form"
      :inline="true"
      label-width="110px">
      <div class="block-title">
        查询条件
      </div>
      <el-form-item
        label="订单号"
        prop="keyWords1">
        <el-input v-model="form.keyWords1"></el-input>
      </el-form-item>
      <el-form-item
        label="票号"
        prop="keyWords2">
        <QRReader v-model="form.keyWords2"></QRReader>
      </el-form-item>
      <el-form-item
        label="游客姓名"
        prop="keyWords6">
        <el-input v-model="form.keyWords6"></el-input>
      </el-form-item>
      <el-form-item
        label="游客手机"
        prop="keyWords5">
        <el-input v-model="form.keyWords5"></el-input>
      </el-form-item>
      <el-form-item
        label="游客证件号"
        prop="keyWords4">
        <ReaderInput
          type="ID"
          v-model="form.keyWords4"></ReaderInput>
      </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="keyWords8">
        <el-select
          v-model="form.keyWords8"
          clearable
          placeholder="请选择">
          <el-option
            label="全部"
            value=""></el-option>
          <el-option
            v-for="item in ticketStatusDic"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="票种"
        prop="keyWords3">
        <el-select
          v-model="form.keyWords3"
          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>

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

    <div class="table-box">
      <!-- <div class="block-title">
        统计信息
      </div> -->
      <el-table
        border
        stripe
        class="table"
        v-loading="loading"
        :data="tableData"
        :row-class-name="totalRowClassName">
        <el-table-column
          type="index"
          label="#">
        </el-table-column>
        <el-table-column
          prop="ticketTypeName"
          label="票种">
        </el-table-column>
        <el-table-column
          prop="price"
          label="票价">
        </el-table-column>
        <el-table-column
          prop="sum_people"
          label="改签人数">
        </el-table-column>
        <el-table-column
          prop="sum_count"
          label="改签张数">
        </el-table-column>
        <el-table-column
          prop="sum_price"
          label="改签金额">
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
import { exportModifyOrderStatistics } from '@/api/queryReport'
import { getTicketTypeList } from '@/api/ticketType'
import { ticketStatusDic } from '@/assets/staticData'
import { totalRowClassName } from '@/utils'
import { apiOrder } from '@/api/order'

import ReaderInput from '@/components/ReaderInput'

import moment from 'moment'

export default {
  name: 'rescheduleStatistics',
  components: {
    ReaderInput
  },
  data () {
    return {
      ticketStatusDic: ticketStatusDic,
      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': '', // 票种id
        'keyWords4': '', // 游客证件号
        'keyWords5': '', // 游客手机号
        'keyWords6': '', // 游客姓名
        'keyWords8': '' // 票状态
      },
      tableData: [],
      loading: false
    }
  },
  created () {
    const searchParams = JSON.parse(sessionStorage.getItem('统计表-改签统计-查询条件'))
    if (searchParams) {
    // 如果存在查询条件，则设置到组件的数据中
      this.form = searchParams // 记住上次搜索条件
    }
    this.getTicketTypeList()
    this.getList()
  },
  methods: {
    totalRowClassName,
    reset () {
      this.$refs.form.resetFields()
    },
    // 获取票种列表
    getTicketTypeList () {
      return getTicketTypeList({
        pageNum: 1,
        pageSize: 999
      }).then(res => {
        this.ticketTypeList = res.list
      })
    },
    getList () {
      this.loading = true
      apiOrder('getModifyOrderStatistics', this.form).then(res => {
        this.tableData = res.data
      }).finally(() => {
        this.loading = false
        sessionStorage.setItem('统计表-改签统计-查询条件', JSON.stringify(this.form)) // 下次记住
      })
    },
    reportExport () {
      exportModifyOrderStatistics(this.form).then(() => {
        this.$message.success('导出成功。')
        this.goToDownload()
      }, () => {
        this.$message.success('导出失败。')
      })
    }
  }
}
</script>
