<template>
  <div class="box">
    <!-- 查询 -->
    <div class="inquire">
      <span>主播ID</span>
      <el-input
        v-model="inquire.anchorId"
        placeholder=""
        class="input"
      ></el-input>
    </div>
    <div class="inquire">
      <span>公会ID</span>
      <el-input
        v-model="inquire.guildId"
        placeholder=""
        class="input"
      ></el-input>
    </div>
    <div class="inquire">
      <span>公会长ID</span>
      <el-input
        v-model="inquire.guildUserId"
        placeholder=""
        class="input"
      ></el-input>
    </div>
    <div class="inquire">
      <span>周期</span>
      <el-select v-model="inquire.weekTime" placeholder="请选择">
        <el-option
          v-for="item in inquire.weekTimeOptions"
          :key="item.cycleDateBegin"
          :label="item.cycleDateShow"
          :value="item.cycleDateBegin"
        >
        </el-option>
      </el-select>
    </div>
    <div class="inquire">
      <span>发放状态</span>
      <el-select v-model="inquire.status" placeholder="请选择">
        <el-option
          v-for="item in inquire.statusOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
        </el-option>
      </el-select>
    </div>
    <!-- 查询按钮 -->
    <el-button class="primary" type="primary" @click="getData()"
      >查询</el-button
    >
    <el-button class="primary" type="primary" @click="confirmExport2Excel()"
      >导出</el-button
    >
    <el-button class="warning" type="warning " @click="sendGoldFun()"
      >批量发放</el-button
    >

    <!-- 表格 -->
    <el-table
      v-loading="loading"
      :data="tableData"
      ref="multipleTable"
      @selection-change="handleSelectionChange"
      border
      style="width: 100%; margin-top: 25px"
    >
      <el-table-column type="selection" :selectable="callback" label="选择" />
      <el-table-column prop="cycleDate" align="center" label="周期" />
      <el-table-column prop="erbanNo" align="center" label="主播ID" />
      <el-table-column prop="guildId" align="center" label="公会ID" />
      <el-table-column
        prop="guildDiamondNum"
        align="center"
        label="公会总流水"
      />
      <el-table-column prop="ssLevelName" align="center" label="公会等级" />
      <el-table-column prop="diamondNum" align="center" label="主播流水" />
      <el-table-column prop="memberLevelName" align="center" label="主播档位" />
      <el-table-column prop="goldNum" align="center" label="主播获得奖励" />
      <el-table-column align="center" prop="giftInfo" label="发放状态">
        <template v-slot="scope">
          {{ scope.row.sendStatusStr }}
        </template>
      </el-table-column>
      <el-table-column prop="sendUser" align="center" label="发放人" />
      <el-table-column align="center" label="操作" width="300">
        <template v-slot="scope">
          <el-button
            @click="sendGoldOneFun(scope.row)"
            :class="scope.row.sendStatusStr == '未发放' ? 'primary' : 'info'"
            :type="scope.row.sendStatusStr == '未发放' ? 'primary' : 'info'"
            size="default"
            :disabled="scope.row.sendStatusStr == '未发放' ? false : true"
            >{{
              scope.row.sendStatusStr == "未发放" ? "发放" : "已发放"
            }}</el-button
          >
        </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"
    />
  </div>
</template>
<script>
import {
  cycleDateList,
  list,
  ssGuildExport,
  sendGold,
} from "@/api/SsGuild/SsGuild";
// @ts-ignore
import { dateFormat } from "@/utils/system-helper";
// @ts-ignore
import { ElMessage, ElTable } from "element-plus";
import { ref } from "vue";
import { ElMessageBox } from "element-plus"; // 正确引入 ElM
export default {
  name: "SsGuild",
  data() {
    return {
      loading: false,
      delDialog: false,
      delDialogData: null,
      selectedRows: [], //存储选中行数据
      //查询所需条件对象
      inquire: {
        anchorId: "",
        guildId: "",
        guildUserId: "",
        weekTime: "",
        weekTimeOptions: [],
        status: "",
        statusOptions: [
          { value: 0, label: "未发放" },
          { value: 1, label: "已发放" },
        ],
      },
      // 表格
      tableData: [],
      // 分页
      total: 10, //总页数
      currentPage: 1, //页码
      pageSize: 10, //条数
    };
  },
  created() {
    cycleDateList().then((res) => {
      this.inquire.weekTimeOptions = res.data;
    });
    this.getData();
  },
  methods: {
    // 查询接口
    getData() {
      this.loading = true;
      let time = this.inquire.time;
      let startTime = "";
      let endTime = "";
      if (time && 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");
      }
      list({
        erbanNo: this.inquire.anchorId,
        guildId: this.inquire.guildId,
        ownerErbanNo: this.inquire.guildUserId,
        cycleDate: this.inquire.weekTime,
        status: this.inquire.status,
        pageNo: this.currentPage,
        pageSize: this.pageSize,
      }).then((res) => {
        if (res.code == 200) {
          this.total = res.data.total;
          this.tableData = res.data.rows;
        } else {
          ElMessage({
            showClose: true,
            message: res.message,
            type: "error",
          });
        }
        this.loading = false;
      });
    },
    handleSelectionChange(val) {
      this.selectedRows = val;
      console.log("Selected rows:", this.selectedRows);
    },
    handleGetSelection() {
      // 获取选中的行数据
      console.log("Current selected rows:", this.selectedRows);
    },
    // 确认导出
    confirmExport2Excel() {
      ElMessageBox.confirm("确定导出Excel吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          ssGuildExport({
            erbanNo: this.inquire.anchorId,
            guildId: this.inquire.guildId,
            ownerErbanNo: this.inquire.guildUserId,
            cycleDate: this.inquire.weekTime,
            status: this.inquire.status,
          })
            .then((res) => {
              // if (res) {
              //   this.exportVisible = false;
              //   this.btnLoading = false;
              //   let alink = document.createElement("a");
              //   alink.download = `SS公会奖励发放.xls`;
              //   alink.style.display = "none";
              //   const blob = new Blob([res.data]);
              //   alink.href = URL.createObjectURL(blob);
              //   document.body.appendChild(alink);
              //   alink.click();
              //   URL.revokeObjectURL(alink.href);
              // }
            })
            .catch((err) => {
              console.error(err); // 打印错误信息
              ElMessage.error("导出失败");
            });
        })
        .catch(() => {
          ElMessage({
            type: "info",
            message: "取消",
          });
        });
    },
    callback(row) {
      return row.sendStatusStr == "未发放" ? true : false;
    },
    // 确认批量发放
    sendGoldFun() {
      ElMessageBox.confirm("确定发放奖励吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          var str = "";
          this.selectedRows.forEach((res) => {
            str += `${res.id},`;
          });
          sendGold({ ids: str })
            .then((res) => {
              if (res.code == 200) {
                ElMessage({
                  showClose: true,
                  message: "发放成功",
                  type: "success",
                });
                this.getData();
              } else {
                ElMessage({
                  showClose: true,
                  message: res.message,
                  type: "error",
                });
              }
            })
            .catch((err) => {
              console.error(err); // 打印错误信息
              ElMessage.error("发放失败");
            });
        })
        .catch(() => {
          ElMessage({
            type: "info",
            message: "取消",
          });
        });
    },
    // 确认单个发放
    sendGoldOneFun(val) {
      ElMessageBox.confirm("确定发放奖励吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          sendGold({ ids: val.id })
            .then((res) => {
              if (res.code == 200) {
                ElMessage({
                  showClose: true,
                  message: "发放成功",
                  type: "success",
                });
                this.getData();
              } else {
                ElMessage({
                  showClose: true,
                  message: res.message,
                  type: "error",
                });
              }
            })
            .catch((err) => {
              console.error(err); // 打印错误信息
              ElMessage.error("发放失败");
            });
        })
        .catch(() => {
          ElMessage({
            type: "info",
            message: "取消",
          });
        });
    },
    // 分页导航
    handleSizeChange() {
      this.getData();
    },
    handleCurrentChange() {
      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;
    }
  }
  .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;
}
</style>
