<template>
  <div class="form-wrap">
    <el-form
      class="search-box"
      ref="form"
      :model="form"
      :inline="true"
      label-width="70px">
      <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">
          <el-option
            value=""
            label="全部"></el-option>
          <el-option
            v-for="item in ticketTypeList"
            :key="item.id"
            :value="item.id"
            :label="item.name">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item
        label="销售渠道"
        prop="keyWords2">
        <el-select v-model="form.keyWords2">
          <el-option
            label="全部"
            value=""></el-option>
          <el-option
            v-for="(item,index) in saleChannel"
            :key="index"
            :label="item"
            :value="item"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item
        v-if="scenicName==='铭投山庄'"
        label="景点选择"
        prop="keyList2"
        :class="hideClear()"
      >
        <el-select
          multiple
          collapse-tags
          v-model="form.keyList2"
          placeholder="请选择">
          <el-option
            v-for="item in userScenic"
            :disabled="disabled(item)"
            :key="item.id"
            :value="item.id"
            :label="item.name">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        v-if="scenicName!=='铭投山庄'"
        label="景点选择"
        prop="keyWords3"
      >
        <el-select
          v-model="form.keyWords3"
          placeholder="请选择">
          <el-option
            value=""
            label="全部">
          </el-option>
          <el-option
            v-for="item in userScenic"
            :key="item.id"
            :value="item.id"
            :label="item.name">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="售票类型"
        prop="keyWords7"
      >
        <el-select
          v-model="form.keyWords7"
          placeholder="请选择">
          <el-option
            value=""
            label="全部">
          </el-option>
          <el-option
            :value="1"
            label="散客">
          </el-option>
          <el-option
            :value="2"
            label="团体">
          </el-option>
        </el-select>
      </el-form-item>

      <div class="btn-wrap">
        <el-button
          @click="reset">
          重置
        </el-button>
        <el-button
          :disabled="loading"
          type="primary"
          @click="getCheckStatistic">
          搜索
        </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
        v-loading="loading"
        :data="typeTableData"
        :row-class-name="totalRowClassName">
        <el-table-column
          prop="ticketType"
          label="票种"
        >
        </el-table-column>
        <el-table-column
          v-for="(item, index) in scenicList"
          :key="index"
          :prop="item"
          :label="item"
        >
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>
<script>
import { exportCheckStatistics } from '@/api/queryReport'
import { getTicketTypeList } from '@/api/ticketType'
import { totalRowClassName } from '@/utils'
import { apiOrder } from '@/api/order.js'

import moment from 'moment'
export default {
  computed: {
    scenicName () {
      return this.$localStore.get('scenicName') || this.$store.state.user.scenicName
    },
    saleChannel () {
      return this.$store.state.app.saleChannel
    },
    allScenicList () {
      return this.$store.state.app.scenicList
    },
    userScenic () {
      return this.$store.state.app.account.managerScenicMatrix ? this.$store.state.app.account.managerScenicMatrix : this.$store.state.app.scenicList
    }
  },
  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'),
        keyList2: [],
        keyWords1: '', // ticketTypeId
        keyWords2: '',
        keyWords3: '',
        keyWords7: '' // 散票or团体
      },
      scenicList: [],
      typeTableData: [],
      loading: false
    }
  },
  created () {
    this.getTicketTypeList()
    this.getCheckStatistic()
  },
  mounted () {
    if (this.scenicName !== '铭投山庄') return
    setTimeout(() => {
      this.form.keyList2 = this.userScenic.map(v => v.id)
      this.getCheckStatistic()
    }, 1000)
  },
  methods: {
    hideClear () {
      if (['铭投山庄'].includes(this.scenicName)) {
        if (this.form.keyList2.length === 1) {
          return 'hideClear'
        } else {
          return ''
        }
      } else {
        return ''
      }
    },
    disabled (item) {
      if (['铭投山庄'].includes(this.scenicName)) {
        if (this.form.keyList2.length > 1) {
          return false
        } else if (this.form.keyList2.length === 1) {
          if (this.form.keyList2.includes(item.id)) {
            return true
          } else {
            return false
          }
        }
      } else {
        return false
      }
    },
    totalRowClassName,
    reset () {
      this.$refs.form.resetFields()
    },
    // 获取票种列表
    getTicketTypeList () {
      return getTicketTypeList({
        pageNum: 1,
        pageSize: 999
      }).then(res => {
        this.ticketTypeList = res.list
      })
    },
    // 获取表格数据
    getCheckStatistic () {
      this.loading = true
      apiOrder('getCheckStatistics', this.form).then(res => {
        this.scenicList = res.scenicList
        this.typeTableData = res.checkCount
      }).finally(() => {
        this.loading = false
      })
    },
    reportExport () {
      exportCheckStatistics(this.form).then(() => {
        this.$message.success('导出成功。')
        this.goToDownload()
      }, () => {
        this.$message.success('导出失败。')
      })
    }
  }
}
</script>

<style scoped lang="scss">
.hideClear{
  ::v-deep .el-tag.el-tag--info .el-tag__close{
    display: none;
  }
}
</style>
