<template>
  <div class="table-box">
    <el-tabs v-model="activeName">
      <el-tab-pane name="enterAreaTime">
        <span
          slot="label">
          来园次数统计<Tip msg="一个会员卡一天进出多次，在来园统计中仅算一次，多人卡一天也仅计算一次"></Tip>
        </span>
        <EnterAreaTimeCount v-show="activeName==='enterAreaTime'"></EnterAreaTimeCount>
      </el-tab-pane>

      <el-tab-pane name="checkTicketsTime">
        <span slot="label">
          检票次数统计<Tip msg="会员一次进出算一次，一天进出多次计算为多次"></Tip>
        </span>
        <div
          class="form-wrap"
          v-show="activeName==='checkTicketsTime'">
          <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="keyWords2">
              <el-select
                v-model="form.keyWords2"
                clearable
                placeholder="请选择">
                <el-option
                  label="全部"
                  value="">
                </el-option>
                <el-option
                  v-for="item in checkType"
                  :key="item.value"
                  :label="item.name"
                  :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
                  v-for="item in statistType"
                  :key="item.value"
                  :label="item.name"
                  :value="item.value">
                </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="exportMemberCheckStatistics">
                导出
              </el-button>
            </div>
          </el-form>

          <div
            style="width:100%">
            <div class="block-title">
              统计信息
              <el-radio-group
                style="float:right"
                size="mini"
                v-model="viewType">
                <el-tooltip
                  content="图表视图"
                  placement="top"
                  effect="dark">
                  <el-radio-button label="chart">
                    <i class="el-icon-s-data"></i>
                  </el-radio-button>
                </el-tooltip>
                <el-tooltip
                  content="表格视图"
                  placement="top"
                  effect="dark">
                  <el-radio-button label="table">
                    <i class="el-icon-tickets"></i>
                  </el-radio-button>
                </el-tooltip>
              </el-radio-group>
            </div>

            <v-chart
              autoresize
              style="width:100%"
              :options="dayOption"
              v-if="tableData && viewType==='chart'" />

            <el-table
              v-else
              :data="tableData.data"
              border
              stripe>
              <el-table-column
                prop="date"
                label="日期"
              >
              </el-table-column>
              <el-table-column
                v-for="col in columns"
                :prop="col"
                :key="col"
                :label="col">
              </el-table-column>
            </el-table>
          </div>
        </div>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import dayOption from './memberCheckStatistics/dayOption'
import EnterAreaTimeCount from './memberCheckStatistics/EnterAreaTimeCount'

import { getMemberCheckStatistics, exportMemberCheckStatistics } from '@/api/member'
import moment from 'moment'
const statistType = [
  {
    name: '天',
    value: 'day'
  }, {
    name: '周',
    value: 'week'
  }, {
    name: '月',
    value: 'month'
  }, {
    name: '年',
    value: 'year'
  }
]

const checkType = [
  {
    name: '指纹',
    value: 'finger'
  }, {
    name: '人脸',
    value: 'face'
  }, {
    name: 'IC卡',
    value: 'iccard'
  }, {
    name: '身份证',
    value: 'idcard'
  }, {
    name: '人工检票',
    value: 'person'
  }
]
export default {
  name: 'memberCheckStatistics',
  components: {
    EnterAreaTimeCount
  },
  data () {
    return {
      dayOption,
      statistType,
      checkType,
      ticketTypeList: [],
      activeName: 'enterAreaTime',
      viewType: 'chart',
      columns: [],
      form: {
        startDate: new Date(moment().format('YYYY-MM-DD') + ' 00:00:00'),
        endDate: new Date(moment().format('YYYY-MM-DD') + ' 23:59:59'),
        keyWords1: '', // 会员种类id
        keyWords2: '', // 检票方式
        keyWords3: 'day' // 统计方式
      },
      tableData: [],
      loading: false
    }
  },
  created () {
    this.getList()
  },
  methods: {
    reset () {
      this.$refs.form.resetFields()
    },
    getList () {
      this.loading = true
      getMemberCheckStatistics(this.form).then(res => {
        this.tableData = res

        let cols = res.memberTypeList
        let totalIndex = cols.findIndex(i => i === '总计')
        cols[totalIndex] = cols.splice(cols.length - 1, 1, cols[totalIndex])[0]
        this.columns = cols
      }).finally(() => {
        this.loading = false
      })
    },
    exportMemberCheckStatistics () {
      exportMemberCheckStatistics(this.form).then(res => {
        this.$message.success('导出成功。')
        this.goToDownload()
      })
    }
  },
  watch: {
    tableData (val) {
      let dayOption = JSON.parse(JSON.stringify(this.dayOption))
      dayOption.xAxis[0].data = val.data.map(i => i.date)
      dayOption.legend.data = val.memberTypeList
      dayOption.series = val.memberTypeList.map(item => {
        return {
          name: item,
          type: 'bar',
          barGap: 0,
          data: val.data.map(i => {
            return i[item] || 0
          })
        }
      })
      this.dayOption = dayOption
    }
  }
}
</script>

<style lang="scss" scoped>
.table-box{
  border: 0;
  ::v-deep .el-tabs__nav-wrap{
   padding: 0 24px;
  }
}
</style>
