<template>
  <clinic-salesperson-page-layout title="我邀请的推广员" :show-back="true">
    <view class="page">
      <view v-if="!list.length && !loading" class="empty">暂无下级推广员</view>
      <view
        v-for="item in list"
        :key="item.id"
        class="row"
        hover-class="row-hover"
        @click="goDetail(item)"
      >
        <image v-if="item.avatar" :src="item.avatar" class="avatar" mode="aspectFill" />
        <view v-else class="avatar placeholder">{{ (item.nick_name || '?').charAt(0) }}</view>
        <view class="main">
          <text class="name">{{ item.nick_name || '推广员' }}</text>
          <text class="phone">{{ item.phone }}</text>
          <text class="sub">邀请分成 {{ item.inviter_split }}%</text>
        </view>
        <view class="right">
          <text class="amount">¥{{ item.invite_total_amount || '0.00' }}</text>
          <text class="hint">待结算 ¥{{ item.invite_pending_amount || '0.00' }}</text>
        </view>
      </view>
    </view>
  </clinic-salesperson-page-layout>
</template>

<script>
import ClinicSalespersonPageLayout from '@/subPackages/sub_clinic_salesperson/components/clinic-salesperson-page-layout.vue'
import { getClinicSalespersonInvitedList } from '@/api/clinicSalesperson.js'

const ROOT = '/subPackages/sub_clinic_salesperson'

export default {
  components: { ClinicSalespersonPageLayout },
  data() {
    return { list: [], loading: false, page: 1 }
  },
  onLoad() {
    this.loadList()
  },
  onPullDownRefresh() {
    this.page = 1
    this.loadList(true)
  },
  methods: {
    async loadList(stopRefresh = false) {
      this.loading = true
      try {
        const wrap = await getClinicSalespersonInvitedList({ page: this.page, pageSize: 50 })
        if (wrap && wrap.ok) this.list = wrap.data?.items || []
      } finally {
        this.loading = false
        if (stopRefresh) uni.stopPullDownRefresh()
      }
    },
    goDetail(item) {
      uni.navigateTo({
        url: `${ROOT}/invitees/detail?source_salesperson_id=${item.id}&nick_name=${encodeURIComponent(item.nick_name || '')}&inviter_split=${item.inviter_split || 0}&invite_total_amount=${item.invite_total_amount || 0}&invite_pending_amount=${item.invite_pending_amount || 0}&invite_settled_amount=${item.invite_settled_amount || 0}`,
      })
    },
  },
}
</script>

<style lang="scss" scoped>
.page { padding: 24rpx; }
.empty { text-align: center; color: #999; padding: 80rpx 0; }
.row {
  background: #fff; border-radius: 12rpx; padding: 24rpx; margin-bottom: 16rpx;
  display: flex; align-items: center;
}
.row-hover { opacity: 0.92; }
.avatar { width: 88rpx; height: 88rpx; border-radius: 50%; margin-right: 20rpx; flex-shrink: 0; }
.avatar.placeholder {
  background: #e8f8f5; color: #6acdbb; display: flex; align-items: center; justify-content: center;
  font-size: 36rpx; font-weight: 600;
}
.main { flex: 1; min-width: 0; }
.name { display: block; font-size: 28rpx; font-weight: 600; color: #333; }
.phone, .sub { display: block; font-size: 24rpx; color: #666; margin-top: 4rpx; }
.right { text-align: right; flex-shrink: 0; }
.amount { display: block; font-size: 30rpx; color: #4db6a8; font-weight: 600; }
.hint { font-size: 22rpx; color: #999; }
</style>
