<!-- 游戏汇总 -->
<template>
  <div>
    <el-card>
      <div class="topInquire">
        <div>
          <span>日期：</span>
          <el-date-picker
            v-model="date"
            type="daterange"
            range-separator="至"
            start-placeholder="选择开始日期"
            end-placeholder="选择结束日期"
            value-format="timestamp"
            size="small"
          />
        </div>
        <div>
          <span>服务器：</span>
          <el-input v-model="serverText" style="width:200px" size="small" disabled placeholder="请选择" />
          <el-button
            size="small"
            type="primary"
            plain
            icon="el-icon-circle-plus-outline"
            @click="radioDialog = true"
          >选择服务器</el-button>
        </div>

        <el-button
          size="small"
          type="primary"
          icon="el-icon-search"
          @click="handleSearch"
        >搜索</el-button>
      </div>

      <div>
        <el-table
          ref="table"
          border
          :data="gameSumData"
          :header-cell-style="{background:'#f5f7fa'}"
          :height="tableHeight"
        >
          <el-table-column prop="id" label="日志ID" min-width="120" />
          <el-table-column prop="dateStr" label="日期" min-width="180">
            <template slot-scope="scope">
              {{ scope.row.dateStr|formatTime }}
            </template>
          </el-table-column>
          <el-table-column prop="playerNum" label="总玩家数" min-width="120" />
          <el-table-column prop="newPlayerNum" label="新玩家数" min-width="120" />
          <el-table-column prop="loginPlayerNum" label="登录玩家数" min-width="120" />
          <el-table-column prop="loginNum" label="登录次数" min-width="120" />
          <el-table-column prop="oldLoginNum" label="去新登录玩家数" min-width="120" />
          <el-table-column prop="rechargeNum" label="充值金额 (单位：元)" min-width="150">
            <template #default="{row}">
              {{ (row.rechargeNum)/100 }}
            </template>
          </el-table-column>
          <el-table-column prop="rechargePlayerNum" label="充值人数" min-width="120" />
          <el-table-column prop="rechargeForDiamond" label="直购钻石金额 (单位：元)" min-width="180">
            <template #default="{row}">
              {{ (row.rechargeForDiamond)/100 }}
            </template>
          </el-table-column>
        </el-table>
      </div>

      <!-- 分页 -->
      <el-pagination
        background
        layout="total, prev, pager, next, jumper"
        :total="total"
        :current-page="currentPage"
        :page-size="pageSize"
        @current-change="changePage"
      />
    </el-card>

    <!-- 服务器-单选弹窗 -->
    <ServerDialog
      :visible.sync="radioDialog"
      :server-options="serverOptions"
      @checkedId="handleCheckedId"
    />
  </div>
</template>

<script>
import ServerDialog from '@/components/radioDialog/index.vue'
import { gameSum } from '@/api/gameStatistics'
import { multiServer } from '@/api/aboutServer'

export default {
  components: {
    ServerDialog
  },
  data() {
    return {
      gameSumData: [],
      total: 0,
      currentPage: 1, // 当前页
      pageSize: 200, // 每页显示条数
      tableHeight: 0, // 表格高度
      date: [new Date().setDate(1), new Date()], // 日期
      serverUid: '', // 选中的服务器id
      serverOptions: [], // 服务器选项
      radioDialog: false, // 服务器弹窗
      serverText: '' // 服务器名称
    }
  },

  created() {
    this.getServer()
  },

  mounted() {
    this.$nextTick(() => {
      this.getTableHeight()
    })
    window.addEventListener('resize', this.$debounce(this.getTableHeight, 100))
  },

  beforeDestroy() {
    window.removeEventListener('resize', (this.getTableHeight))
  },

  methods: {
    // 获取服务器
    async getServer() {
      const res = await multiServer()
      this.serverOptions = res.data.data.options.sort((a, b) => a.value - b.value)
    },

    // 选中的服务器ID
    handleCheckedId(val) {
      this.serverUid = val

      const matchedOption = this.serverOptions.find(obj => obj.value === this.serverUid)
      if (matchedOption) {
        this.serverText = matchedOption.text
      } else {
        this.serverText = ''
      }
      this.radioDialog = false
    },

    // 搜索按钮
    async handleSearch() {
      try {
        const query = { page: this.currentPage, limit: this.pageSize }
        if (this.date && this.date.length === 2) {
          // 时间戳毫秒转化为秒
          query.startTime = this.$moment(this.date[0]).unix()
          query.endTime = this.$moment(this.date[1]).unix()
        }

        if (!this.serverText) {
          this.$message.error('请选择服务器，服务器为必选项')
        } else {
          query.serverUid = this.serverUid

          const result = await gameSum(query)
          this.gameSumData = result.data.data.data
          this.total = result.data.data.count
        }
      } catch (error) {
        console.error(error)
      }
    },

    // 切换页面
    changePage(page) {
      this.currentPage = page
      this.handleSearch()
    },

    // 表格自适应高度
    getTableHeight() {
      if (this.$refs.table && this.$refs.table.$el) {
        this.tableHeight = window.innerHeight - this.$refs.table.$el.offsetTop - 170
      } else {
        this.tableHeight = 300
      }
    }

  }
}
</script>
<style lang="scss" scoped>

.topInquire{
  font-weight: bold;
  font-size: 14px;
  color: #6a7488;
  margin-bottom: 20px;
  display: flex;
  flex-wrap: wrap;
  gap: 30px;
}

</style>
