<template>
  <div class="tourist-wrap">
    <el-form
      class="form search-box"
      inline
      :model="form"
      ref="form"
    >
      <el-form-item
        label="开始时间"
        prop="statisticsTimeBegin"
      >
        <el-date-picker
          v-model="form.statisticsTimeBegin"
          type="datetime"
          placeholder="选择日期时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item
        label="结束时间"
        prop="statisticsTimeEnd"
      >
        <el-date-picker
          default-time="23:59:59"
          v-model="form.statisticsTimeEnd"
          type="datetime"
          placeholder="选择日期时间"
        >
        </el-date-picker>
      </el-form-item>

      <el-form-item label="数据显示数量">
        <el-input-number v-model="form.pageSize"></el-input-number>
      </el-form-item>

      <!-- <el-form-item
        label="景点选择"
        prop="keyWords4"
      >
        <el-select
          v-model="form.keyWords4"
          placeholder="请选择">
          <el-option
            value=""
            label="全部">
          </el-option>
          <el-option
            v-for="item in scenicList"
            :key="item.id"
            :value="item.id"
            :label="item.name">
          </el-option>
        </el-select>
      </el-form-item> -->
      <div class="btn-wrap">
        <el-button
          type="primary"
          @click="getData">
          查询
        </el-button>
        <el-button
          type="primary"
          @click="reportExport">
          导出
        </el-button>
      </div>
    </el-form>
    <el-card>
      <el-tabs v-model="activeName">
        <el-tab-pane
          label="游客来源统计"
          name="source">
          <AreaStatics :table-data="statisticsInfo.provinceStatistics"></AreaStatics>
        </el-tab-pane>
        <el-tab-pane
          label="游客年龄统计"
          name="age">
          <Age :table-data="statisticsInfo.ageStatistics"></Age>
        </el-tab-pane>

        <el-tab-pane
          label="游客性别统计"
          name="gender">
          <Sex :table-data="statisticsInfo.sexStatistics"></Sex>
        </el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>

<script>
import Age from './Statistic/Age'
import Sex from './Statistic/Sex'
import AreaStatics from './Statistic/Area'
import { getVisitorStatistics } from '@/api/visitor'
import moment from 'moment'

export default {
  name: 'visitorStatistic',
  components: {
    Age,
    Sex,
    AreaStatics
  },
  data () {
    return {
      activeName: 'source',
      form: {
        statisticsTimeBegin: '',
        statisticsTimeEnd: '',
        pageSize: 10,
        pageNum: 1
      },
      statisticsInfo: {
        provinceStatistics: [],
        sexStatistics: [],
        ageStatistics: []
      }
    }
  },
  created () {
    this.getData()
  },
  methods: {
    setTime () {
      if (this.form.statisticsTimeBegin) {
        this.form.statisticsTimeBegin = moment(this.form.statisticsTimeBegin).format('YYYY-MM-DD HH:mm:ss')
      } else {
        this.form.statisticsTimeBegin = ''
      }

      if (this.form.statisticsTimeEnd) {
        this.form.statisticsTimeEnd = moment(this.form.statisticsTimeEnd).format('YYYY-MM-DD HH:mm:ss')
      } else {
        this.form.statisticsTimeEnd = ''
      }
    },

    //导出

    reportExport () {
      this.setTime()

      if (!this.form.ticketTypeIdList) {
        this.form.ticketTypeIdList = []
      }

      if (!this.form.otaSourceNameList) {
        this.form.otaSourceNameList = []
      }
      this.$confirm(`是否要导出明细数据`, '确认提示', {
        confirmButtonText: '是',
        cancelButtonText: '否',
        type: 'warning'
      }).then(() => {
        this.handleExport({
          ...this.form,
          export: true,
          exportDetail: true
        })
      }).catch(() => {
        this.handleExport({
          ...this.form,
          export: true,
          exportDetail: false
        })
      })
    },
    handleExport (params) {
      getVisitorStatistics(params).then(() => {
        this.$message.success('导出成功。')
        this.goToDownload()
      }, () => {
        this.$message.success('导出失败。')
      })
    },
    getData () {
      getVisitorStatistics(this.form).then(res => {
        this.statisticsInfo = res.data
        console.log(res, '1111')
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.tourist-wrap{
  .echarts{
    margin:0 auto;
  }
}
</style>
