<template>
  <div class="recharge-page">
    <div class="bg">
      <GeneralHeader
        :showLanguageList="true"
        :title="t('my_linked_recharge_agent')"
        color="black"
        style="box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); z-index: 999"
      />

      <main class="recharge-content">
        <section class="summary-card">
          <div class="metric-block">
            <div class="metric-label">{{ t('my_total_income') }}</div>
            <div class="metric-value">${{ totalRechargeDetail?.totalIncome || 0 }}</div>
          </div>
          <div class="metric-block">
            <div class="metric-label">{{ t('this_period_recharge') }}</div>
            <div class="metric-value">${{ totalRechargeDetail?.currentMonthRecharge || 0 }}</div>
          </div>
        </section>

        <div class="list-title">{{ t('recharge_agency_list') }}</div>

        <section
          v-for="(agency, aIndex) in agencyList"
          :key="aIndex"
          class="agency-card"
          :class="{ expanded: aIndex == selectedAgencyIndex }"
          v-touch-tap="() => showAgencyInfo(aIndex)"
        >
          <div class="agency-main">
            <div class="agency-profile">
              <img :src="agency.userAvatar" alt="" class="agency-avatar" />
              <div class="agency-meta">
                <div class="agency-name">{{ agency.userNickname }}</div>
                <div class="agency-id">{{ t('user_id_prefix') }}{{ agency.account }}</div>
            </div>
            </div>
            <div class="expand-icon">
              <img
                src="../../assets/icon/Aslan/arrow.png"
                alt=""
                :class="{ rotated: aIndex == selectedAgencyIndex }"
              />
            </div>
          </div>

          <transition name="info-fade">
            <div class="agency-detail" v-show="aIndex == selectedAgencyIndex">
              <div class="detail-metric">
                <div class="metric-label">{{ t('this_month') }}:</div>
                <div class="detail-value">${{ formatPrice(agency.thisMonth) || 0 }}</div>
              </div>
              <div class="detail-metric">
                <div class="metric-label">{{ t('last_month') }}:</div>
                <div class="detail-value">${{ formatPrice(agency.lastMonth) || 0 }}</div>
              </div>
            </div>
          </transition>
        </section>
        <div ref="Agloadmore" v-if="showAgLoading"></div>
      </main>
    </div>
  </div>
</template>

<script setup>
import { onMounted, ref } from 'vue'
import { useRouter } from 'vue-router'
import { useI18n } from 'vue-i18n'
import { useThrottle } from '@/utils/useDebounce'

import { getAgencyList, getAgencyTotalRecharge } from '@/api/userInfo'

import GeneralHeader from '@/components/GeneralHeader.vue'

const router = useRouter()
const { t, locale } = useI18n()

const selectedAgencyIndex = ref(-1) //选中的agency用户下标
const showAgencyInfo = (index) => {
  selectedAgencyIndex.value = selectedAgencyIndex.value != index ? index : -1
} //选中的agency用户

const send = async () => {
  router.push({ path: '/item-distribution' })
}

const totalRechargeDetail = ref({}) //代理消费金额
const agencyList = ref([]) // 代理列表
const agencyCurrent = ref(1) //代理列表第几页
const showAgLoading = ref(true) //触底加载功能

onMounted(async () => {
  getAgencise() // 获取代理列表
  getAgencyMonthsRecharge() //获取代理总支付

  // 开始监听底部元素
  if (Agloadmore.value) {
    observer.observe(Agloadmore.value)
  }
})

// 获取代理列表
const getAgencyMonthsRecharge = async () => {
  const resTotalRecharge = await getAgencyTotalRecharge()
  console.log('resTotalRecharge:', resTotalRecharge)
  if (resTotalRecharge.status && resTotalRecharge.body) {
    totalRechargeDetail.value = resTotalRecharge.body
  }
}

// 获取代理列表
const getAgencise = async () => {
  let data = {
    cursor: agencyCurrent.value,
    limit: 20,
  }
  const resagencyList = await getAgencyList(data)
  console.log('resagencyList:', resagencyList)
  if (resagencyList.status && resagencyList.body) {
    agencyList.value.push(...resagencyList.body.records)
    if (resagencyList.body.total <= resagencyList.body.current * resagencyList.body.size) {
      showAgLoading.value = false
    }
  }
}

const Agloadmore = ref(null)

const debouceGetAgList = useThrottle(getAgencise, 1000)

// IntersectionObserver配置
const observer = new IntersectionObserver((entries) => {
  entries.forEach((entry) => {
    if (entry.intersectionRatio > 0) {
      console.log('监控')
      if (entry.target == Agloadmore.value && agencyList.value.length != 0) {
        agencyCurrent.value++
        debouceGetAgList()
      }
    }
  })
})

const formatPrice = (value) => {
  if (value && value != 0) {
    return Number(value).toFixed(2)
  }
  return 0
}
</script>

<style scoped>
* {
  color: black;
  box-sizing: border-box;
}

.recharge-page {
  background-color: #f5f7fb;
  height: 100vh;
  position: relative;
  overflow-y: auto;
}

.recharge-page::-webkit-scrollbar {
  display: none;
}

.bg {
  width: 100vw;
  min-height: 100vh;
  background-image: url(../../assets/images/Aslan/secondBg.png);
  background-size: 100% auto;
  background-repeat: no-repeat;
}

.recharge-content {
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 16px 14px 28px;
}

.summary-card,
.agency-card {
  background: rgba(255, 255, 255, 0.96);
  border: 1px solid rgba(212, 218, 232, 0.9);
  border-radius: 18px;
  box-shadow: 0 10px 26px rgba(37, 45, 65, 0.08);
}

.summary-card {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
  padding: 16px;
}

.metric-block {
  min-width: 0;
}

.metric-label {
  color: rgba(31, 41, 55, 0.55);
  font-size: 1em;
  font-weight: 700;
  line-height: 1.35;
}

.metric-value {
  margin-top: 5px;
  color: #111827;
  font-size: 1.35em;
  font-weight: 800;
  line-height: 1.2;
  overflow-wrap: anywhere;
}

.list-title {
  color: #111827;
  font-size: 1.2em;
  font-weight: 800;
}

.agency-card {
  padding: 14px;
  transition:
    transform 0.2s ease,
    box-shadow 0.2s ease;
  touch-action: manipulation;
}

.agency-card.expanded {
  box-shadow: 0 14px 32px rgba(37, 45, 65, 0.12);
}

.agency-main,
.agency-profile,
.expand-icon {
  display: flex;
  align-items: center;
}

.agency-main {
  justify-content: space-between;
  gap: 12px;
}

.agency-profile {
  min-width: 0;
  flex: 1;
  gap: 12px;
}

.agency-avatar {
  display: block;
  width: 54px;
  height: 54px;
  flex: 0 0 54px;
  border-radius: 50%;
  object-fit: cover;
  background: #eef2f7;
}

.agency-meta {
  min-width: 0;
  flex: 1;
}

.agency-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: #111827;
  font-size: 1.12em;
  font-weight: 800;
}

.agency-id {
  margin-top: 4px;
  color: rgba(31, 41, 55, 0.5);
  font-size: 0.98em;
  font-weight: 700;
}

.expand-icon {
  width: 38px;
  height: 38px;
  justify-content: center;
  border-radius: 12px;
  background: #f4f2ff;
}

.expand-icon img {
  display: block;
  width: 22px;
  transition: transform 0.5s cubic-bezier(0.68, -0.55, 0.27, 1.55);
}

.agency-detail {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
  margin-top: 14px;
  padding-top: 14px;
  border-top: 1px solid rgba(229, 231, 235, 0.9);
}

.detail-metric {
  min-width: 0;
}

.detail-value {
  margin-top: 4px;
  color: rgba(31, 41, 55, 0.9);
  font-size: 1.08em;
  font-weight: 800;
  overflow-wrap: anywhere;
}

.info-fade-enter-from,
.info-fade-leave-to {
  transform: translateY(-10px);
  opacity: 0;
}

.info-fade-enter-active {
  transition: all 0.5s ease-out;
}

.info-fade-leave-active {
  transition: all 0.5s cubic-bezier(1, 0.5, 0.8, 1);
}

.rotated {
  transform: rotate(90deg);
}

@media screen and (max-width: 360px) {
  * {
    font-size: 12px;
  }
}

@media screen and (min-width: 360px) {
  * {
    font-size: 14px;
  }
}

@media screen and (min-width: 768px) {
  * {
    font-size: 24px;
  }
}

@media screen and (min-width: 1024px) {
  * {
    font-size: 32px;
  }
}
</style>
