<view class="page-container">
  <!-- 分销概览 -->
  <view class="card dist-header">
    <view class="dist-stats">
      <view class="dist-stat">
        <text class="dist-stat-num">{{info.team_count || 0}}</text>
        <text class="dist-stat-label">团队人数</text>
      </view>
      <view class="dist-stat">
        <text class="dist-stat-num">¥{{(info.total_commission || 0).toFixed(2)}}</text>
        <text class="dist-stat-label">累计佣金</text>
      </view>
      <view class="dist-stat">
        <text class="dist-stat-num">¥{{(info.withdrawable || 0).toFixed(2)}}</text>
        <text class="dist-stat-label">可提现</text>
      </view>
    </view>
    <view class="dist-sub" wx:if="{{info.limit}}">
      <text class="tip">免费版佣金上限 ¥{{info.limit.commission_cap}} / 团队上限 {{info.limit.team_cap}} 人</text>
    </view>
  </view>

  <!-- 🩹开通分销提示(未成为分销商时) -->
  <view class="card dist-header" wx:if="{{distStatus.status !== 'active'}}" style="background:#FFF7EE;border:1rpx solid #FFD9A8;">
    <view class="dist-sub" style="margin:0">
      <text style="font-size:26rpx;color:#B26A00;font-weight:600">分销未开通</text>
      <text style="display:block;font-size:22rpx;color:#8A6D3B;margin-top:6rpx">购买付费会员或</text>
      <text style="font-size:22rpx;color:#8A6D3B">{{distStatus.paid_member ? '直接开通' : '申请开通分销后即可赚佣金'}}</text>
      <button class="btn btn-primary" style="margin-top:14rpx" bindtap="applyDist">申请开通分销</button>
    </view>
  </view>

  <!-- 推广链接 -->
  <view class="card">
    <view class="card-body">
      <button class="btn btn-primary btn-block" bindtap="copyLink">复制推广链接</button>
    </view>
  </view>

  <!-- 绑定上级 -->
  <view class="card" wx:if="{{!hasBind}}">
    <view class="card-header"><text>绑定上级</text></view>
    <view class="card-body">
      <input class="bind-input" placeholder="请输入上级用户ID" value="{{parentId}}" bindinput="onParentInput" />
      <button class="btn btn-primary btn-block" style="margin-top:16rpx" bindtap="bindParent">确认绑定</button>
    </view>
  </view>

  <!-- 统计 -->
  <view class="card">
    <view class="card-body">
      <view class="stat-row">
        <text>今日佣金</text>
        <text>¥{{(stat.today || 0).toFixed(2)}}</text>
      </view>
      <view class="stat-row">
        <text>本月佣金</text>
        <text>¥{{(stat.month || 0).toFixed(2)}}</text>
      </view>
      <view class="stat-row">
        <text>累计已结算</text>
        <text>¥{{(stat.total || 0).toFixed(2)}}</text>
      </view>
    </view>
  </view>

  <!-- Tabs -->
  <view class="tabs">
    <view class="tab {{activeTab==='team'?'tab-active':''}}" data-tab="team" bindtap="switchTab">团队</view>
    <view class="tab {{activeTab==='commission'?'tab-active':''}}" data-tab="commission" bindtap="switchTab">佣金</view>
    <view class="tab {{activeTab==='withdraw'?'tab-active':''}}" data-tab="withdraw" bindtap="switchTab">提现</view>
    <view class="tab tab-action {{activeTab!=='withdraw'?'':'tab-active'}}" data-tab="withdraw" bindtap="openWithdraw">提现</view>
  </view>

  <!-- 团队 -->
  <view class="card" wx:if="{{activeTab==='team'}}">
    <view class="team-item" wx:for="{{team}}" wx:key="id">
      <image class="team-avatar" src="{{item.avatar}}" mode="aspectFill" />
      <view class="team-info">
        <text>{{item.nickname || item.phone}}</text>
        <text class="team-time">{{item.created_at}}</text>
      </view>
      <text class="team-consumed">¥{{(item.total_consumed||0).toFixed(2)}}</text>
    </view>
    <empty-state wx:if="{{!loading && team.length===0}}" text="暂无团队成员" />
  </view>

  <!-- 佣金 -->
  <view class="card" wx:if="{{activeTab==='commission'}}">
    <view class="comm-item" wx:for="{{commission}}" wx:key="id">
      <view class="comm-left">
        <text>佣金 +¥{{(item.commission||0).toFixed(2)}}</text>
        <text class="comm-time">{{item.created_at}}</text>
      </view>
      <text class="comm-status">{{item.status}}</text>
    </view>
    <empty-state wx:if="{{!loading && commission.length===0}}" text="暂无佣金记录" />
  </view>

  <!-- 提现记录 -->
  <view class="card" wx:if="{{activeTab==='withdraw'}}">
    <view class="wd-item" wx:for="{{withdrawList}}" wx:key="id">
      <view class="wd-left">
        <text>¥{{(item.amount||0).toFixed(2)}}</text>
        <text class="wd-time">{{item.created_at}}</text>
      </view>
      <text class="wd-status">{{item.status}}</text>
    </view>
    <empty-state wx:if="{{!loading && withdrawList.length===0}}" text="暂无提现记录" />
  </view>

  <!-- 提现弹窗 -->
  <view class="modal-mask" wx:if="{{showWithdraw}}" bindtap="closeWithdraw">
    <view class="modal-card" catchtap="noop">
      <view class="modal-title">申请提现</view>
      <view class="modal-form">
        <view class="form-label">金额（可提现 ¥{{(info.withdrawable||0).toFixed(2)}}）</view>
        <input class="form-input" type="digit" value="{{withdrawAmount}}" bindinput="onAmountInput" placeholder="请输入提现金额" />
        <view class="form-label">收款方式</view>
        <view class="acct-types">
          <text class="acct-type {{accountType==='wechat'?'acct-active':''}}" data-type="wechat" bindtap="onAccountType">微信</text>
          <text class="acct-type {{accountType==='alipay'?'acct-active':''}}" data-type="alipay" bindtap="onAccountType">支付宝</text>
        </view>
        <view class="form-label">收款账号</view>
        <input class="form-input" value="{{accountInfo}}" bindinput="onAccountInfo" placeholder="请输入收款账号" />
      </view>
      <button class="btn btn-primary btn-block" bindtap="submitWithdraw">确认提现</button>
    </view>
  </view>
</view>
