<template>
  <div class="box">
    <div class="inquire">
      <span>游戏名称</span>
      <el-select v-model="formData.gameId" placeholder="请选择">
        <el-option
          v-for="item in gameList"
          :key="item.mgId"
          :label="JSON.parse(item.name).zh"
          :value="item.mgId"
        >
        </el-option>
      </el-select>
    </div>
    <div class="inquire">
      <span class="demonstration">月份</span>
      <el-config-provider :locale="zhCn">
        <el-date-picker
          v-model="formData.month"
          type="month"
          placeholder="选择月"
          value-format="YYYY-MM"
          :clearable="false"
        >
        </el-date-picker
      ></el-config-provider>
    </div>
    <el-button style="" type="primary" @click="getData()">查询</el-button>
    <el-button class="primary" type="primary" @click="resetFormData()"
      >重置</el-button
    >
    <el-button class="primary" type="primary" @click="confirmExport2Excel()"
      >导出</el-button
    >

    <!-- 表格数据 -->
    <el-table
      v-loading="tableData.loading"
      :data="tableData.data"
      ref="multipleTable"
      @selection-change="handleSelectionChange"
      border
      style="width: 100%; margin-top: 25px"
    >
      <el-table-column prop="statMonth" align="center" label="月份" />
      <el-table-column prop="gameName" align="center" label="游戏名称" />
      <el-table-column prop="totalDuration" align="center" label="总时长(秒)">
        <template v-slot="scope">
           <el-button @click="detailFun(scope.row)" type="text" size="small">
          {{ scope.row.totalDuration }}
        </el-button>
        </template>
      </el-table-column>
      <el-table-column prop="arDuration" align="center" label="阿拉伯时长(秒)" />
      <el-table-column prop="enDuration" align="center" label="英语区时长(秒)" />
      <el-table-column prop="en2Duration" align="center" label="英语2区时长(秒)" />
      <el-table-column prop="trDuration" align="center" label="土耳其区时长(秒)" />
      <el-table-column prop="zhDuration" align="center" label="华语区时长(秒)" />
    </el-table>
    <!-- 明细弹窗 -->
    <el-dialog
      v-model="dailydetailDialog"
      title="休闲游戏时长明细"
      width="30%"
      center
    >
    <el-button style="" type="primary" @click="confirmExport2ExcelDetail()"
          >导出</el-button
        >
      <el-table
        :data="detailtableData.data"
        style="width: 100%;margin-top: 20px;"
        ref="multipleTable"
        border
      >
        <el-table-column prop="date" align="center" label="日期" />
        <el-table-column prop="gameName" align="center" label="游戏名称" />
        <el-table-column prop="erbanNo" align="center" label="房间号" />
        <el-table-column prop="partitionDesc" align="center" label="分区" />
        <el-table-column prop="duration" align="center" label="游戏时长(秒)" />
      </el-table>
      <el-pagination
      style="margin-top: 10px"
      class="paginationClass"
      :current-page="detailData.page"
      :page-size="detailData.size"
      :page-sizes="[10, 20, 50, 100, 200]"
      layout="sizes, prev, pager, next"
      :total="detailtableData.total"
      @size-change="handleSizeChangeDetail"
      @current-change="handleCurrentChangeDetail"
    />
      <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" @click="dailydetailDialog = false">
            关闭
          </el-button>
        </span>
      </template>

    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "statisticsIeisureGameDuration",
};
</script>
<script setup>
import { ref, onMounted, reactive, computed } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { formatDate } from "@/utils/relDate";
import { dateFormat } from "@/utils/system-helper";
import {
  mgList,
  getMinigameListStat,
  getMinigamePageDetail,
  exportMiniGameStat,
  exportMiniGameDetail,
} from "@/api/statisticsIeisureGameDuration/statisticsIeisureGameDuration";
import zhCn from "element-plus/dist/locale/zh-cn.mjs";
const formData = reactive({
  month: "",
  gameId: "",
});
const tableData = reactive({
  data: [],
  total: 0,
  loading: false,
});
const detailData = reactive({
  month: "",
  gameId: "",
  page: 1,
  size: 10,
});
const detailtableData = reactive({
  data: [],
  total: 0,
  loading: false,
});
const gameList = ref([]);
const dailydetailDialog = ref(false);
// 查询
const getData = () => {
  if (formData.month == "" && !formData.month) {
    ElMessage.warning("请选择月份");
    return;
  }
  tableData.loading = true;
  getMinigameListStat(formData).then((res) => {
    if (res.code == 200) {
      tableData.data = res.data;
      tableData.loading = false;
      tableData.total = res.total;
    } else {
      tableData.loading = false;

      ElMessage.error(res.message);
    }
  });
};
// 明细
const detailFun = (row) => {
  if(row){
      detailData.gameId = row.gameId;
  detailData.month = row.statMonth;
  }
  getMinigamePageDetail(detailData).then((res) => {
    if (res.code == 200) {
      detailtableData.data = res.data.records;
      detailtableData.loading = false;
      detailtableData.total = res.data.total;
       dailydetailDialog.value = true;
    } else {
      ElMessage.error(res.message);
    }
  });
};
//重置
const resetFormData = () => {
  Object.assign(formData.value, {
    gameId: "",
  });
};
// 导出
const confirmExport2Excel = async () => {
  try {
        const res = await exportMiniGameStat(formData)
        if (res) {
          ElMessage({
            message: '导出成功',
            type: 'success',
          });
          let time = formatDate(new Date());
          let alink = document.createElement("a");
          alink.download = `休闲游戏时长统计${time}.xls`;
          alink.style.display = "none";
          const blob = new Blob([res]);
          alink.href = URL.createObjectURL(blob);
          document.body.appendChild(alink);
          alink.click();
          URL.revokeObjectURL(alink.href);
        }

      } catch (error) {
        ElMessage({
          message: error.message,
          type: 'error',
        });
      }
};
// 导出
const confirmExport2ExcelDetail = async () => {
  try {
        const res = await exportMiniGameDetail(detailData)
        if (res) {
          ElMessage({
            message: '导出成功',
            type: 'success',
          });
          let time = formatDate(new Date());
          let alink = document.createElement("a");
          alink.download = `休闲游戏时长明细统计${time}.xls`;
          alink.style.display = "none";
          const blob = new Blob([res]);
          alink.href = URL.createObjectURL(blob);
          document.body.appendChild(alink);
          alink.click();
          URL.revokeObjectURL(alink.href);
        }

      } catch (error) {
        ElMessage({
          message: error.message,
          type: 'error',
        });
      }
};
const handleSizeChangeDetail = (val) => {
  detailData.size = val;
  detailFun();
};
const handleCurrentChangeDetail = (val) => {
  detailData.page = val;
  detailFun();
};
onMounted(() => {
  mgList().then((res) => {
    if (res.code == 200) {
      gameList.value = res.data;
    } else {
      ElMessage.error(res.message);
    }
  });
});
</script>

<style lang="less" scoped>
.box {
  padding-top: 20px;
  background: #ecf0f5;

  .inquire {
    display: inline-block;
    margin-right: 20px;

    span {
      margin-right: 10px;
    }

    .input {
      width: 180px;
      margin-right: 10px;
    }
  }

  .dialogTableVisibleBut {
    display: block;
    margin: 30px 0 0 830px;
  }

  .paginationClass {
    margin: 15px 0 5px 0px;
  }
}
.selectBox {
  display: flex;
  height: 35px;
  line-height: 35px;
  margin-bottom: 20px;
}

.selectBoxImg {
  height: 150px;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
