<template>
  <view class="promotion-page">
    <!-- 顶部渐变导航栏 -->
    <view class="nav-bar" :style="{height: navBarTotalHeight + 'px', paddingTop: statusBarHeight + 'px'}">
      <view class="nav-bar-inner">
        <view class="back-icon" @tap="navigateBack">
          <uni-icons type="left" size="22" color="#222" />
        </view>
        <view class="nav-bar-title">回收侠·推客联盟</view>
        <view class="nav-bar-right">
          <uni-icons type="more-filled" size="22" color="#222" />
        </view>
      </view>
    </view>
    <!-- 打钩标签区 -->
    <view class="feature-tags" :style="{marginTop: navBarTotalHeight+statusBarHeight + 'px'}">
      <view class="tag" v-for="(t, i) in featureTags" :key="i">
        <text class="check">✓</text>
        <text class="text">{{t}}</text>
      </view>
    </view>
    <!-- 主内容区（卡片悬浮覆盖打钩区） -->
    <view class="main-content">
      <!-- 用户卡片 -->
      <view class="user-card">
        <view class="user-info-row">
          <view class="avatar-box">
            <view class="avatar-frame">
              <image class="avatar-img" :src="userInfo.avatar" mode="aspectFill" />
              <view class="user-type">{{userInfo.type}}</view>
            </view>
          </view>
          <view class="user-meta">
            <view class="nickname">{{userInfo.name}}</view>
            <view class="userid">ID: {{userInfo.intentioCode}}</view>
          </view>
          <view class="user-count">推广人数</view>
        </view>
        <view class="user-stats-row">
          <view class="stat-block">
            <view class="stat-label">可提现佣金(元)</view>
            <view class="stat-value main">{{userInfo.money}}</view>
            <button class="withdraw-btn" @click="goWithdraw">提现</button>
          </view>
          <view class="stat-divider"></view>
          <view class="stat-block">
            <view class="stat-label">累计提现(元)</view>
            <view class="stat-value">{{userInfo.integerPrice}}</view>
            <button class="record-btn" @click="goWithdrawRecord">提现记录</button>
          </view>
        </view>
        <view class="bottom-menu-row">
          <view class="menu-item" @tap="goProfitDetail">
            <view class="menu-icon red">
              <uni-icons type="wallet" size="32" color="#fff" />
            </view>
            <text class="menu-text">收益明细</text>
          </view>
          <view class="menu-item" @tap="goTeam">
            <view class="menu-icon blue">
              <uni-icons type="person" size="32" color="#fff" />
            </view>
            <text class="menu-text">我的团队</text>
          </view>
        </view>
      </view>
      <!-- 推广排行榜 -->
      <view class="rank-section">
        <view class="rank-title">推广排行榜</view>
        <view class="rank-list">
          <view class="rank-item" v-for="(item, idx) in rankList.slice(0, 5)" :key="idx">
            <view class="rank-icon">
              <image v-if="idx<3" :src="getRankIcon(idx+1)" class="crown" mode="aspectFit" />
              <image v-else :src="getRankIcon(idx+1)" class="num-icon" mode="aspectFit" />
            </view>
            <image class="rank-avatar" :src="item.headImage" mode="aspectFill" />
            <text class="rank-phone">{{item.nickName}}</text>
            <text class="rank-amount">¥{{item.price}}</text>
          </view>
        </view>
      </view>
      <!-- 权益说明表格 -->
      <view class="rights-section">
        <view class="rights-title">权益说明</view>
        <view class="rights-table">
          <view class="table-header">
            <view class="th" v-for="(h, i) in rightsHeader" :key="i">{{h}}</view>
          </view>
          <view class="table-row" v-for="(row, i) in rightsRows" :key="i">
            <view class="td" v-for="(cell, j) in row" :key="j">
              <text v-if="cell==='✓'" class="cell-check">✓</text>
              <text v-else-if="cell==='X'" class="cell-x">X</text>
              <text v-else>{{cell}}</text>
            </view>
          </view>
        </view>
      </view>
      <!-- 联系客服二维码 -->
      <view class="qrcode-section">
        <view class="qrcode-title">联系客服</view>
        <image class="qrcode-img" :src="qrcodeUrl" mode="aspectFit" />
      </view>
    </view>
    <!-- 底部按钮区 -->
    <view class="bottom-btns">
      <button class="btn gray" @tap="goQrcode">我的二维码</button>
      <button class="btn green" @tap="goUpgrade" v-if="status===''">立即升级推广官</button>
      <button class="btn gray"  v-if="status==0" @tap="showProgressModal = true">查看申请进度</button>
    </view>
    <!-- 申请进度弹窗 -->
    <view v-if="showProgressModal" class="progress-modal-mask">
      <view class="progress-modal-box">
        <view class="progress-modal-title">联系运营商\n升级解锁更多权益</view>
        <view class="progress-modal-list">
          <view class="progress-modal-item" @tap="goToFaq">
            <text>在线客服</text>
            <uni-icons type="right" size="22" color="#bbb" />
          </view>
          <view class="progress-modal-item" @tap="goToFaq">
            <text>客服电话</text>
            <uni-icons type="right" size="22" color="#bbb" />
          </view>
        </view>
        <button class="progress-modal-btn" @tap="showProgressModal = false">我知道了</button>
      </view>
    </view>
  </view>
</template>

<script>
import pullRefreshMixin from '@/pages/mixins/pullRefreshMixin.js'

export default {
  mixins: [pullRefreshMixin],
  data() {
    return {
      statusBarHeight: 0,
      navBarHeight: 44, // px
      navBarTotalHeight: 44, // px
      featureTags: ['收益高', '品类全', '到账快', '城市多'],
      userInfo: {
        avatar: '',
        name: '',
        id: '',
        type: '',
        balance: '',
        totalWithdraw: '',
      },
      tabActive: 0,
      rankList: [
      ],
      rightsHeader: ['推广锁粉权限', '用户', '推广官', '合伙人'],
      rightsRows: [
        ['推广锁粉权限', 'X', '✓', '✓'],
        ['直接推广收益', 'X', '20%', '20%'],
        ['间接推广收益', 'X', 'X', '5%'],
        ['官方群服务', 'X', '20%', '25%'],
        ['推广课程支持', 'X', '5%', '25%'],
        ['推广工具包', 'X', '20%', '25%'],
        ['VIP专属客服', 'X', 'X', '25%'],
        ['收益模式', 'X', '5%', '主动收益'],
        ['线下沙龙', 'X', '✓', '主动收益'],
      ],
      status: 0,
      showProgressModal: false
    }
  },
  computed: {
    qrcodeUrl() {
			console.log(getApp().globalData.configData,'getApp().globalData.configData')
      const item =  getApp().globalData.configData.find(i => i.keyName === 'kefu_code')
      return item ? item.keyContent : ''
    },
  },
  onLoad() {
    const sysInfo = uni.getSystemInfoSync()
    this.statusBarHeight = sysInfo.statusBarHeight
    this.navBarHeight = 44
    this.navBarTotalHeight = this.statusBarHeight + this.navBarHeight
    this.getUserInfoByToken()
    this.getRankList()
    this.getMyPromotionInfo()
  },
  onShow(){
    this.getMyPromotionInfo()
  },
  onPullDownRefresh() {
    this.getUserInfoByToken()
    uni.stopPullDownRefresh()
  },
  methods: {
    async onRefresh() {
      // 模拟刷新数据
      await new Promise(resolve => setTimeout(resolve, 1000))
      uni.stopPullRefresh()
    },
    navigateBack() {
      uni.navigateBack()
    },
    getRankIcon(idx) {
      return `/static/tui/${idx}.png`
    },
    goQrcode() {
      uni.navigateTo({ url: '/pages/subcomponent/promo-qrcode' })
    },
    goUpgrade() {
      uni.navigateTo({ url: '/pages/subcomponent/upgrad' })
    },
    getUserInfoByToken() {
      this.$api('getUserByToken', {}, (res) => {
        if (res && res.success && res.result) {
          this.userInfo.avatar = res.result.headImage || ''
          this.userInfo.name = res.result.nickName || ''
          this.userInfo.id = res.result.id || ''
          this.userInfo.intentioCode = res.result.intentioCode || ''
          this.userInfo.money = res.result.money || ''
          this.userInfo.integerPrice = res.result.integerPrice || ''
          // 你可以根据实际接口补充type、balance、totalWithdraw等字段
        }
      })
    },
    goWithdrawRecord() {
      uni.navigateTo({ url: '/pages/subcomponent/promotion-withdraw-record' })
    },
    goWithdraw() {
      uni.navigateTo({ url: '/pages/subcomponent/withdraw?fromPromotion=true' })
    },
    goProfitDetail() {
      uni.navigateTo({ url: '/pages/subcomponent/promotion-profit-detail' })
    },
    goTeam() {
      uni.navigateTo({ url: '/pages/subcomponent/promotion-team' })
    },
    getRankList(){
      this.$api('getRankList', {}, (res) => {
        if (res && res.success && res.result) {
          this.rankList = res.result.records || []
        }
      })
    },
    getMyPromotionInfo(){
      this.$api('getMyPromotionInfo', {}, (res) => {
        if (res && res.success && res.result) {
          // console.log(res.result,'res.result')
          this.status = res.result.status
          console.log(this.status,'this.status')
        }
      })
    },
    goToFaq() {
      uni.navigateTo({ url: '/pages/subcomponent/admin_faq' })
    }
  }
}
</script>

<style lang="scss" scoped>
.promotion-page {
  min-height: 100vh;
  background: linear-gradient(to right, #b2f08d 0%, #39e9d2 100%);
  padding-bottom: calc(env(safe-area-inset-bottom) + 120rpx);
}

.nav-bar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 1000;
  width: 100vw;
  background: linear-gradient(to right, #b2f08d 0%, #39e9d2 100%);
  box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.03);
  .nav-bar-inner {
    display: flex;
    align-items: center;
    height: 44px;
    width: 100vw;
    position: relative;
  }
  .back-icon {
    width: 44px;
    height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
    position: absolute;
    left: 0;
    top: 0;
  }
  .nav-bar-title {
    flex: 1;
    text-align: center;
    font-size: 36rpx;
    font-weight: bold;
    color: #222;
    letter-spacing: 2rpx;
    line-height: 44px;
  }
  .nav-bar-right {
    width: 44px;
    height: 44px;
    position: absolute;
    right: 0;
    top: 0;
    display: flex;
    align-items: center;
    justify-content: center;
  }
}

.feature-tags {
  // background: linear-gradient(to right, #b2f08d 0%, #39e9d2 100%);
  border-radius: 32rpx;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24rpx;
  height: 80rpx;
  box-shadow: 0 4rpx 24rpx rgba(60, 167, 250, 0.08);
  .tag {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    .check {
      width: 36rpx;
      height: 36rpx;
      background: #fff;
      border-radius: 50%;
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 28rpx;
      color: #13ac47;
      font-weight: bold;
      margin-right: 10rpx;
      box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.08);
    }
    .text {
      color: #fff;
      font-size: 28rpx;
      font-weight: bold;
    }
  }
}

.main-content {
  margin-top: 20rpx;
  padding-top: 88rpx;
  background: linear-gradient(180deg, #eaffe6 0%, #fff 30%);
  min-height: 100vh;
  width: 100vw;
  box-sizing: border-box;
  position: relative;
  z-index: 11;
  border-radius: 40rpx 40rpx 0 0;
  overflow: hidden;
}

.user-card {
  margin: 0 32rpx;
  background: linear-gradient(to bottom, #f6fff2 0%, #fff 100%);
  border-radius: 32rpx;
  box-shadow: 0 8rpx 32rpx rgba(60, 167, 250, 0.12);
  padding: 48rpx 36rpx 36rpx 36rpx;
  border: none;
  position: relative;
  top: -40rpx;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  .user-info-row {
    display: flex;
    align-items: center;
    padding-bottom: 0;
    margin-bottom: 32rpx;
    // background-color: #fff;
    .avatar-box {
      margin-right: 24rpx;
      display: flex;
      flex-direction: column;
      align-items: center;
      .avatar-frame {
        width: 104rpx;
        height: 104rpx;
        border-radius: 10rpx;
        overflow: hidden;
        position: relative;
        background: #f2f2f2;
        box-shadow: 0 4rpx 16rpx rgba(0,0,0,0.10);
        .avatar-img {
          width: 104rpx;
          height: 80rpx;
          object-fit: cover;
          display: block;
        }
        .user-type {
          position: absolute;
          left: 0;
          bottom: 0;
          width: 100%;
          height: 32rpx;
          background: #e5e5e5;
          color: #888;
          font-size: 22rpx;
          font-weight: 500;
          text-align: center;
          line-height: 32rpx;
          border-bottom-left-radius: 10rpx;
          border-bottom-right-radius: 10rpx;
        }
      }
    }
    .user-meta {
      flex: 1;
      display: flex;
      flex-direction: column;
      justify-content: center;
      .nickname {
        font-size: 32rpx;
        font-weight: bold;
        color: #222;
        margin-bottom: 6rpx;
      }
      .userid {
        font-size: 24rpx;
        color: #bbb;
      }
    }
    .user-count {
      font-size: 30rpx;
      color: #222;
      font-weight: bold;
      text-align: right;
      min-width: 120rpx;
      margin-left: 16rpx;
      align-self: center;
    }
  }
  .user-stats-row {
    display: flex;
    align-items: stretch;
    justify-content: center;
    background: #f2fff6;
    border-radius: 24rpx;
    margin: 0 0 8rpx 0;
    padding: 32rpx 0;
    box-sizing: border-box;
    .stat-block {
      flex: 1;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      padding: 0 24rpx;
      .stat-label {
        font-size: 24rpx;
        color: #bbb;
        margin-bottom: 12rpx;
        text-align: center;
      }
      .stat-value {
        font-size: 40rpx;
        color: #222;
        font-weight: bold;
        margin-bottom: 18rpx;
        &.main {
          color: #13ac47;
          font-size: 48rpx;
        }
      }
      .withdraw-btn, .record-btn {
        width: 180rpx;
        height: 56rpx;
        border-radius: 28rpx;
        font-size: 26rpx;
        font-weight: bold;
        margin-top: 0;
        display: flex;
        align-items: center;
        justify-content: center;
        text-align: center;
        box-sizing: border-box;
      }
      .withdraw-btn {
        background: linear-gradient(90deg, #b2f08d, #39e9d2);
        color: #fff;
        border: none;
      }
      .record-btn {
        background: #fff;
        color: #13ac47;
        border: 2rpx solid #13ac47;
        &:active, &:focus {
          opacity: 0.8;
        }
      }
    }
    .stat-divider {
      width: 2rpx;
      height: 72rpx;
      background: #e0e0e0;
      margin: 0 16rpx;
      border-radius: 2rpx;
      align-self: center;
    }
  }
  .bottom-menu-row {
    display: flex;
    justify-content: center;
    align-items: center;
    margin: 40rpx 0 0 0;
    .menu-item {
      display: flex;
      align-items: center;
      margin: 0 40rpx;
      .menu-icon {
        width: 56rpx;
        height: 56rpx;
        border-radius: 50%;
        display: flex;
        align-items: center;
        justify-content: center;
        margin-right: 12rpx;
        &.red {
          background: #ff7e6a;
        }
        &.blue {
          background: #7eb6ff;
        }
      }
      .menu-text {
        font-size: 28rpx;
        color: #222;
        font-weight: 500;
      }
    }
  }
}

.rank-section {
  margin: 32rpx 32rpx 0 32rpx;
  .rank-title {
    font-size: 30rpx;
    color: #222;
    font-weight: bold;
    text-align: center;
    margin-bottom: 24rpx;
  }
  .rank-list {
    background: #fff;
    border-radius: 24rpx;
    box-shadow: 0 2rpx 8rpx rgba(60, 167, 250, 0.04);
    overflow: hidden;
    .rank-item {
      display: flex;
      align-items: center;
      padding: 0 44rpx;
      height: 104rpx;
      border-bottom: 1rpx solid #f3f3f3;
      &:last-child {
        border-bottom: none;
      }
      .rank-icon {
        width: 44rpx;
        display: flex;
        align-items: center;
        justify-content: center;
        .crown, .num-icon {
          width: 36rpx;
          height: 36rpx;
        }
      }
      .rank-avatar {
        width: 48rpx;
        height: 48rpx;
        border-radius: 50%;
        margin: 0 18rpx 0 10rpx;
        background: #f5f5f5;
      }
      .rank-phone {
        flex: 1;
        font-size: 28rpx;
        color: #222;
        font-weight: bold;
      }
      .rank-amount {
        font-size: 30rpx;
        color: #b98c4a;
        font-weight: bold;
        min-width: 120rpx;
        text-align: right;
      }
    }
  }
}

.rights-section {
  margin: 40rpx 32rpx 0 32rpx;
  .rights-title {
    font-size: 30rpx;
    color: #222;
    font-weight: bold;
    text-align: center;
    margin-bottom: 24rpx;
  }
  .rights-table {
    background: #fff;
    border-radius: 24rpx;
    border: 1rpx solid #ff9900;
    overflow: hidden;
    box-sizing: border-box;
    .table-header {
      display: flex;
      background: #fff7ed;
      border-bottom: 1rpx solid #ff9900;
      .th {
        flex: 1;
        text-align: center;
        font-size: 26rpx;
        color: #ff9900;
        font-weight: bold;
        padding: 24rpx 0;
        border-right: 1rpx solid #ff9900;
        &:last-child { border-right: none; }
      }
    }
    .table-row {
      display: flex;
      border-bottom: 1rpx solid #ffe5c2;
      &:last-child { border-bottom: none; }
      .td {
        flex: 1;
        text-align: center;
        font-size: 26rpx;
        color: #222;
        padding: 20rpx 0;
        border-right: 1rpx solid #ffe5c2;
        &:last-child { border-right: none; }
        .cell-check {
          color: #13ac47;
          font-size: 30rpx;
          font-weight: bold;
        }
        .cell-x {
          color: #bbb;
          font-size: 30rpx;
        }
      }
    }
  }
}

.qrcode-section {
  margin: 48rpx 32rpx 0 32rpx;
  background: #fff;
  border-radius: 32rpx;
  box-shadow: 0 4rpx 24rpx rgba(60, 167, 250, 0.08);
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 32rpx 0 40rpx 0;
  .qrcode-title {
    font-size: 28rpx;
    color: #222;
    font-weight: bold;
    margin-bottom: 24rpx;
  }
  .qrcode-img {
    width: 220rpx;
    height: 220rpx;
    background: #fff;
    border-radius: 24rpx;
    box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.08);
  }
}

.bottom-btns {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  justify-content: space-between;
  padding: 24rpx 32rpx calc(env(safe-area-inset-bottom) + 24rpx) 32rpx;
  background: #fff;
  z-index: 100;
  .btn {
    flex: 1;
    height: 88rpx;
    border-radius: 44rpx;
    font-size: 32rpx;
    font-weight: bold;
    margin: 0 12rpx;
    border: none;
    &.gray {
      background: #f5f5f5;
      color: #999;
    }
    &.green {
      background: linear-gradient(90deg, #42dfc2, #9be48f);
      color: #fff;
    }
  }
}

.bottom-menu-row {
  display: flex;
  justify-content: center;
  align-items: center;
  margin: 40rpx 0 0 0;
  .menu-item {
    display: flex;
    align-items: center;
    margin: 0 40rpx;
    .menu-icon {
      width: 56rpx;
      height: 56rpx;
      border-radius: 50%;
      display: flex;
      align-items: center;
      justify-content: center;
      margin-right: 12rpx;
      &.red {
        background: #ff7e6a;
      }
      &.blue {
        background: #7eb6ff;
      }
    }
    .menu-text {
      font-size: 28rpx;
      color: #222;
      font-weight: 500;
    }
  }
}

.progress-modal-mask {
  position: fixed;
  left: 0; right: 0; top: 0; bottom: 0;
  background: rgba(0,0,0,0.25);
  z-index: 2000;
  display: flex;
  align-items: center;
  justify-content: center;
}
.progress-modal-box {
  width: 80vw;
  max-width: 420px;
  background: linear-gradient(180deg, #f3fff2 0%, #eafff6 100%);
  border-radius: 32rpx;
  box-shadow: 0 8rpx 32rpx rgba(60, 167, 250, 0.10);
  padding: 56rpx 0 32rpx 0;
  display: flex;
  flex-direction: column;
  align-items: center;
}
.progress-modal-title {
  font-size: 34rpx;
  color: #222;
  font-weight: bold;
  text-align: center;
  margin-bottom: 48rpx;
  white-space: pre-line;
}
.progress-modal-list {
  width: 88%;
  display: flex;
  flex-direction: column;
  gap: 18rpx;
  margin-bottom: 48rpx;
}
.progress-modal-item {
  // background: #fff;
  border-radius: 18rpx;
  height: 80rpx;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 30rpx;
  color: #222;
  padding: 0 32rpx;
  box-shadow: 0 2rpx 8rpx rgba(60, 167, 250, 0.04);
}
.progress-modal-btn {
  width: 88%;
  height: 88rpx;
  background: linear-gradient(90deg, #b2f08d, #39e9d2);
  color: #222;
  font-size: 32rpx;
  font-weight: bold;
  border-radius: 44rpx;
  margin-top: 8rpx;
  box-shadow: 0 4rpx 16rpx rgba(60, 167, 250, 0.08);
  display: flex;
  align-items: center;
  justify-content: center;
  border: none;
}
</style>