<template>
  <div class="form-wrap">
    <div class="searchBox">
      <el-form
        ref="form"
        :model="form"
        :inline="true"
        label-width="150px">
        <el-form-item
          label="起始时间"
          prop="timeBegin">
          <el-date-picker
            v-model="form.timeBegin"
            type="datetime"
            placeholder="选择日期时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item
          label="截止时间"
          prop="timeEnd">
          <el-date-picker
            v-model="form.timeEnd"
            type="datetime"
            placeholder="选择日期时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item
          label="分销商名称"
          prop="otaSourceNameList"
        >
          <el-select
            v-model="form.otaSourceNameList"
            multiple
            clearable
            placeholder="请选择"
            filterable>
            <el-option
              label="全部"
              value="">
            </el-option>
            <el-option
              v-for="item in otaList"
              :key="item.id"
              :label="item.otaSourceName"
              :value="item.otaSourceName">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="票种"
          prop="ticketTypeIdList">
          <el-select
            filterable
            multiple
            clearable
            v-model="form.ticketTypeIdList"
            placeholder="请选择">
            <el-option
              label="全部"
              value="">
            </el-option>
            <el-option
              v-for="item in ticketTypeList"
              :key="item.id"
              :label="item.name"
              :value="item.ticketTypeId">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div class="btn-wrap">
        <el-button @click="reset">重置</el-button>
        <el-button
          type="primary"
          @click="getData">搜索</el-button>
        <el-button
          :loading="exportLoading"
          type="primary"
          @click="handelExport">导出</el-button>
      </div>
    </div>

    <div class="tableBox">
      <div class="block-title">
        查询信息
      </div>
      <el-table
        :data="tableData"
        v-loading="loading"
        :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="saleNum"
          label="售票数量">
        </el-table-column>
        <el-table-column
          prop="salePrice"
          label="售票金额">
        </el-table-column>
        <el-table-column
          prop="cancelNum"
          label="退票数量">
        </el-table-column>
        <el-table-column
          prop="cancelPrice"
          label="退票金额">
        </el-table-column>
        <el-table-column
          prop="actualNum"
          label="实售数量">
        </el-table-column>
        <el-table-column
          prop="actualPrice"
          label="实售金额">
        </el-table-column>
      </el-table>
      <el-pagination
        background
        :current-page.sync="form.pageNum"
        @current-change="getData"
        layout="total, prev, pager, next"
        :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { getTicketTypeList } from '@/api/ticketType'
import { getOtaProjectList } from '@/api/ota'
import { getSaleStatistics } from '@/api/queryReport'
import moment from 'moment'

export default {
  name: 'salesReport',
  data () {
    return {
      ticketTypeList: [],
      otaList: [],
      form: {
        export: false,
        timeBegin: new Date(moment().startOf('month')),
        timeEnd: new Date(moment().endOf('month')),
        otaSourceNameList: [], // 分销商名称
        ticketTypeIdList: [], // 票种名称
        pageNum: 1,
        pageSize: 10
      },
      projectName: localStorage.getItem('otaProject'),
      tableData: [],
      total: 0,
      loading: false,
      exportLoading: false
    }
  },
  computed: {
    queryClear () {
      return this.$store.state.user.permissionList.includes('stats:clearwx_query')
    }
  },
  created () {
    this.getTicketTypeList()
    this.getProductList()
    this.getData()
  },
  methods: {
    reset () {
      this.$refs.form.resetFields()
    },
    // 获取票种列表
    getTicketTypeList () {
      return getTicketTypeList({
        pageNum: 1,
        pageSize: -1
      }).then(res => {
        this.ticketTypeList = res.data.records || []
      })
    },
    getProductList () {
      return getOtaProjectList({
        pageNum: 1,
        pageSize: -1
      }).then(res => {
        this.otaList = res.data.records || []
      })
    },
    getData () {
      this.loading = true
      if (this.form.timeBegin) {
        this.form.timeBegin = moment(this.form.timeBegin).format('YYYY-MM-DD HH:mm:ss')
      }
      if (this.form.timeEnd) {
        this.form.timeEnd = moment(this.form.timeEnd).format('YYYY-MM-DD HH:mm:ss')
      }
      if (!this.form.otaSourceNameList || this.form.otaSourceNameList.length === 0) {
        this.form.otaSourceNameList = []
      }
      if (!this.form.ticketTypeIdList || this.form.ticketTypeIdList.length === 0) {
        this.form.ticketTypeIdList = []
      }
      getSaleStatistics(this.form).then(res => {
        res.data.forEach(item => {
          if (item.ticketTypeName === '总计') {
            item.otaSourceName = '总计'
            item.ticketTypeName = ''
            item.price = ''
          } else if (item.ticketTypeName === '小计') {
            item.otaSourceName = '小计'
            item.ticketTypeName = ''
            item.price = ''
          }
        })
        this.tableData = res.data
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    handelExport () {
      this.exportLoading = true
      this.form.export = true
      getSaleStatistics(this.form).then(res => {
        this.exportLoading = false
        this.$notify({
          title: '提示',
          message: '导出成功',
          type: 'success'
        })
      })
    }
  }
}
</script>

<style scoped>

</style>
