<template>
  <div class="page">
    <h-navbar />
    <div class="page-body">
      <div class="top">
        <div class="left">
          <h-img
            v-if="visible"
            width="4rem"
            height="4rem"
            src="base/open-box.png"
          />
          <h-img v-else width="4rem" height="4rem" src="base/close-box.png" />
        </div>
        <div class="right">
          <div class="title">RECOMPENSA</div>
          <div class="jinbi flex-ct">
            <h-img width=".8rem" height=".8rem" src="base/JB_R$.png" />
            <span>
              {{ formatDigit((data.betRebateCoins / 10000).toFixed(2)) }}
            </span>
          </div>
          <h-img
            v-show="data.betRebateCoins"
            width="3rem"
            src="perfil/receber.png"
            @click="receive"
          />
        </div>
      </div>
      <div class="bottom">
        <h-img src="perfil/xzi_01.png" />
        <div class="box" v-if="data.records.length">
          <div class="title">Registro de fluxo</div>
          <div class="table">
            <div class="label">
              <span>Date</span>
              <span>Fluxo de aposta</span>
              <span>Bonus</span>
            </div>
            <div class="td" v-for="(item, index) in data.records" :key="index">
              <span>{{ fomartDate(item.createdAt, "YYYY-MM-DD") }}</span>
              <span>{{ formatDigit((item.betFlow / 10000).toFixed(2)) }}</span>
              <span class="bonus flex-ct">
                <h-img
                  width="0.5rem"
                  style="margin-right: 0.2rem"
                  src="base/JB_R$.png"
                />
                {{ formatDigit((item.bonus / 10000).toFixed(2)) }}
              </span>
            </div>
          </div>
        </div>
        <div class="box" v-else>
          <div class="title">Registro de fluxo</div>
          <div class="table">
            <div class="label">
              <span>Date</span>
              <span>Fluxo de aposta</span>
              <span>Bonus</span>
            </div>
            <div class="no-data">NO-DATA</div>
          </div>
        </div>
        <h-img src="perfil/xzi_03.png" />
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { formatDigit, fomartDate } from "@/utils";
import { toast } from "@/utils/toast";
import { ref, reactive } from "vue";
import socketApi from "@/socket/socketApi";
import { useUserStore } from "@/stores/modules/user";
import http from "@/https";
const userStore = useUserStore();
interface dataProps {
  betRebateCoins: number;
  betRebateFlow: number;
  records: Array<any>;
}

let data = reactive<dataProps>({
  betRebateCoins: 0,
  betRebateFlow: 0,
  records: [],
});

const visible = ref<boolean>(false);
const getData = async () => {
  http.getTotalRebate().then((res: any) => {
    console.log(res.data, "resresres");
    if (res.code !== 0) return;
    data.betRebateCoins = res.data.betRebateCoins;
    data.betRebateFlow = res.data.betRebateFlow;
    data.records = res.data.records;
    visible.value = !Boolean(data.betRebateCoins && data.betRebateFlow);
  });
};

getData();

const receive = async () => {
  const res = await http.receiveTotalRebate();
  console.log(res);
  if (res.code !== 0) {
    toast(res.txt);
    return;
  }
  userStore.getUserInfo();
  toast("success");
  getData();
};
</script>

<style lang="scss" scoped>
.page-body {
  background: url("https://cdn.18n.com/perfil/bj.png");
  background-size: 100% auto;
  .top {
    display: flex;
    .right {
      display: flex;
      flex-direction: column;
      align-items: center;
      .title {
        color: $font-text1;
        font-size: 0.7rem;
        font-weight: bold;
      }
      .jinbi {
        color: $font-text1;
        font-size: 0.8rem;
        font-weight: bold;
        margin: 0.2rem 0;
        span {
          margin-left: 0.2rem;
        }
      }
    }
  }
  .bottom {
    .box {
      margin: 0.2rem 0 0.6rem 0;
      .title {
        text-align: center;
        font-size: 0.5rem;
        background: #255746;
        padding: 0.15rem 0;
        border-top-left-radius: 0.3rem;
        border-top-right-radius: 0.3rem;
      }

      .table {
        width: 100%;
        font-size: 0.4rem;

        .no-data {
          height: 3rem;
          text-align: center;
          line-height: 3rem;
          font-size: 0.4rem;
          // border-bottom: 0.05rem solid rgba($color: #d3d3d3, $alpha: 0.5);
        }
        .label {
          display: grid;
          grid-template-columns: repeat(3, 1fr);
          text-align: center;
          border-bottom: 0.05rem solid rgba($color: #d3d3d3, $alpha: 0.5);
        }
        .td {
          display: grid;
          grid-template-columns: repeat(3, 1fr);
          padding: 0.2rem 0;
          align-content: center;
          align-items: center;
          text-align: center;
          color: $font-text1;
          border-bottom: 0.05rem solid rgba($color: #d3d3d3, $alpha: 0.5);
        }
      }
    }
  }
}
</style>
