<template>
  <div class="box">
    <!-- 查询 -->
    <div class="inquire">
      <span>奖励礼物id</span>
      <el-input
        v-model="inquire.giftId"
        placeholder=""
        class="input"
      ></el-input>
    </div>
    <div class="inquire">
      <span>用户id</span>
      <el-input
        v-model="inquire.userId"
        placeholder=""
        class="input"
      ></el-input>
    </div>
    <!-- 时间选择器 -->
    <div class="inquire">
      <div class="block">
        <span class="demonstration">日期</span>
        <el-date-picker
          v-model="inquire.time"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </div>
    </div>

    <!-- 查询按钮 -->
    <el-button class="primary" type="primary" @click="getData()"
      >查询</el-button
    >
    <!-- 导出按钮 -->
    <el-button class="primary" type="primary" @click="derive()">导出</el-button>
  </div>

  <!-- 表格 -->
  <el-table
    v-loading="loading"
    :data="tableData"
    border
    style="width: 100%; margin-top: 25px"
  >
    <el-table-column prop="createTimeStr" align="center" label="时间" />
    <el-table-column prop="sendErBanNo" align="center" label="送礼人id" />
    <el-table-column prop="sendNick" align="center" label="送礼人昵称" />
    <el-table-column prop="receiveErBanNo" align="center" label="收礼人id" />
    <el-table-column prop="receiveNick" align="center" label="收礼人昵称" />
    <el-table-column prop="luckyBagId" align="center" label="福袋id" />
    <el-table-column prop="luckyBagName" align="center" label="福袋昵称" />
    <el-table-column prop="destGiftName" align="center" label="礼物名称" />
    <el-table-column prop="luckyBagPrice" align="center" label="福袋流水" />
    <el-table-column prop="rewardGiftName" align="center" label="奖励名称">
      <template #default="{ row }">
        {{ row.rewardGiftName || "/" }}
      </template>
    </el-table-column>
    <el-table-column prop="rewardGiftPrice" align="center" label="奖励价值">
      <template #default="{ row }">
        {{ row.rewardGiftPrice || "/" }}
      </template>
    </el-table-column>
    <!-- <el-table-column prop="x" align="center" label="该用户是否中奖">
      <template #default="{ row }">
        {{ row.x ? "是" : "否" }}
      </template>
    </el-table-column> -->
  </el-table>

  <!-- 分页 -->
  <el-pagination
    style="margin-top: 10px"
    class="paginationClass"
    v-model:current-page="currentPage"
    v-model:page-size="pageSize"
    :page-sizes="[10, 20, 30, 40, 50, 100, 200, 300, 400, 500, 999999999]"
    layout="sizes, prev, pager, next"
    :total="total"
    @size-change="handleSizeChange"
    @current-change="handleCurrentChange"
  />
</template>

<script>
import { getRecordPage, exportForReward } from "@/api/luckyTycoon/luckyTycoon";
import { dateFormat } from "@/utils/system-helper";
import { ElMessage } from "element-plus";

export default {
  name: "luckyTycoonUserWinningRecord",
  data() {
    return {
      inquire: {
        giftId: "",
        userId: "",
        time: "",
      },
      loading: false,
      // 表格数据
      tableData: [],
      // 分页
      total: 10, //总页数
      currentPage: 1, //页码
      pageSize: 10, //条数
    };
  },
  created() {
    this.getData();
  },
  methods: {
    getData() {
      this.loading = true;
      let startTime = "";
      let endTime = "";
      if (this.inquire.time && this.inquire.time.length > 0) {
        startTime = dateFormat(this.inquire.time[0], "yyyy-MM-dd hh:mm:ss");
        endTime = dateFormat(this.inquire.time[1], "yyyy-MM-dd hh:mm:ss");
      }
      getRecordPage({
        page: this.currentPage,
        pageSize: this.pageSize,
        rewardGiftId: this.inquire.giftId,
        erBanNo: this.inquire.userId,
        startTime,
        endTime,
      })
        .then((res) => {
          if (res.code === 200) {
            this.tableData = res.data.records;
            this.total = res.data.total;
          } else {
            ElMessage({
              showClose: true,
              message: res.message || "获取数据失败",
              type: "error",
            });
          }
          this.loading = false;
        })
        .catch((error) => {
          console.error("请求出错:", error);
          ElMessage({
            showClose: true,
            message: "请求出错，请稍后重试",
            type: "error",
          });
          this.loading = false;
        });
    },
    derive() {
      let startTime = "";
      let endTime = "";
      if (this.inquire.time && this.inquire.time.length > 0) {
        startTime = dateFormat(this.inquire.time[0], "yyyy-MM-dd hh:mm:ss");
        endTime = dateFormat(this.inquire.time[1], "yyyy-MM-dd hh:mm:ss");
      }
      exportForReward({
        rewardGiftId: this.inquire.giftId,
        erBanNo: this.inquire.userId,
        startTime,
        endTime,
      })
        .then((res) => {
          if (res.code === 200) {
            // 可以添加导出成功的提示信息
          } else {
            ElMessage({
              showClose: true,
              message: res.message || "导出失败",
              type: "error",
            });
          }
        })
        .catch((error) => {
          console.error("导出请求出错:", error);
          ElMessage({
            showClose: true,
            message: "导出请求出错，请稍后重试",
            type: "error",
          });
        });
    },
    // 分页导航
    handleSizeChange(size) {
      this.pageSize = size;
      this.getData();
    },
    handleCurrentChange(page) {
      this.currentPage = page;
      this.getData();
    },
  },
};
</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;
    }
  }
  .paginationClass {
    margin: 15px 0 5px 0px;
  }
}
</style>