<!-- 好友请求管理页面 -->
<view class="requests-container">
  
  <!-- 自定义导航栏 -->
  <view class="custom-nav-bar" style="padding-top: {{statusBarHeight}}px; height: {{navBarHeight}}px;">
    <view class="nav-content">
      <view class="nav-left" bindtap="goBack">
        <text class="back-icon">←</text>
      </view>
      <view class="nav-center">
        <text class="nav-title">好友请求</text>
      </view>
      <view class="nav-right"></view>
    </view>
  </view>

  <!-- 标签页 -->
  <view class="tabs-container">
    <view class="tab-item {{activeTab === item.key ? 'active' : ''}}"
          wx:for="{{tabs}}" 
          wx:key="key"
          bindtap="switchTab"
          data-tab="{{item.key}}">
      <text class="tab-text">{{item.label}}</text>
      <view class="tab-badge" wx:if="{{item.count > 0}}">
        <text class="badge-text">{{item.count > 99 ? '99+' : item.count}}</text>
      </view>
    </view>
  </view>

  <!-- 内容区域 -->
  <scroll-view class="content-container" 
               scroll-y="true"
               refresher-enabled="true"
               refresher-triggered="{{refreshing}}"
               bindrefresherrefresh="onRefresh">
    
    <!-- 加载中 -->
    <view class="loading-container" wx:if="{{loading}}">
      <view class="loading-spinner"></view>
      <text class="loading-text">加载中...</text>
    </view>

    <!-- 空状态 -->
    <view class="empty-container" wx:if="{{!loading && (activeTab === 'pending' ? pendingRequests.length === 0 : processedRequests.length === 0)}}">
      <view class="empty-icon">
        <text class="icon-text">{{activeTab === 'pending' ? '📭' : '📋'}}</text>
      </view>
      <text class="empty-title">{{activeTab === 'pending' ? '暂无待处理请求' : '暂无已处理记录'}}</text>
      <text class="empty-desc">{{activeTab === 'pending' ? '当有人向您发送好友请求时，会在这里显示' : '您处理过的好友请求会在这里显示'}}</text>
    </view>

    <!-- 请求列表 -->
    <view class="requests-list" wx:if="{{!loading && (activeTab === 'pending' ? pendingRequests.length > 0 : processedRequests.length > 0)}}">
      <view class="request-item" 
            wx:for="{{activeTab === 'pending' ? pendingRequests : processedRequests}}" 
            wx:key="requestId">
        
        <!-- 用户信息 -->
        <view class="user-section" bindtap="viewUserDetail" data-custom-id="{{item.senderCustomId}}">
          <!-- 头像 -->
          <view class="user-avatar">
            <image wx:if="{{item.senderAvatar}}" 
                   src="{{item.senderAvatar}}" 
                   class="avatar-image" 
                   mode="aspectFill" />
            <view wx:else class="avatar-placeholder">
              <text class="avatar-text">{{item.senderNickname ? item.senderNickname.charAt(0) : '?'}}</text>
            </view>
          </view>

          <!-- 用户详情 -->
          <view class="user-info">
            <view class="user-name">
              <text class="nickname">{{item.senderNickname}}</text>
              <view class="status-badge {{getStatusClass(item.status)}}" wx:if="{{activeTab === 'processed'}}">
                <text class="status-text">{{getStatusText(item.status)}}</text>
              </view>
            </view>
            
            <text class="user-id">ID: {{item.senderCustomId}}</text>
            
            <view class="request-message" wx:if="{{item.message}}">
              <text class="message-text">{{item.message}}</text>
            </view>
            
            <view class="time-info">
              <text class="time-text">{{formatTime(item.createdAt)}}</text>
              <text class="handle-time" wx:if="{{item.handleTime}}">处理时间: {{formatTime(item.handleTime)}}</text>
            </view>
          </view>
        </view>

        <!-- 操作按钮 -->
        <view class="action-section" wx:if="{{item.status === 0}}">
          <view class="action-buttons">
            <view class="action-btn reject-btn"
                  bindtap="handleRequest"
                  data-request-id="{{item.requestId}}"
                  data-accept="false">
              <text class="btn-text">拒绝</text>
            </view>
            
            <view class="action-btn accept-btn"
                  bindtap="handleRequest"
                  data-request-id="{{item.requestId}}"
                  data-accept="true">
              <text class="btn-text">接受</text>
            </view>
          </view>
        </view>

        <!-- 已处理状态的操作 -->
        <view class="processed-actions" wx:if="{{item.status === 1}}">
          <view class="message-btn"
                bindtap="sendMessage"
                data-custom-id="{{item.senderCustomId}}"
                data-nickname="{{item.senderNickname}}">
            <text class="message-btn-text">发消息</text>
          </view>
        </view>
      </view>
    </view>

    <!-- 底部安全区域 -->
    <view class="safe-area-bottom"></view>
  </scroll-view>
</view>

<wxs module="utils">
  var getCurrentRequests = function(friendRequests, activeTab) {
    if (!friendRequests || !friendRequests.length) return [];
    
    if (activeTab === 'pending') {
      return friendRequests.filter(function(req) {
        return req.status === 0;
      });
    } else {
      return friendRequests.filter(function(req) {
        return req.status !== 0;
      });
    }
  };
  
  var formatTime = function(timeStr) {
    if (!timeStr) return '';
    
    var time = getDate(timeStr);
    var now = getDate();
    var diff = now.getTime() - time.getTime();
    
    var minute = 60 * 1000;
    var hour = 60 * minute;
    var day = 24 * hour;
    var week = 7 * day;
    
    if (diff < minute) {
      return '刚刚';
    } else if (diff < hour) {
      return Math.floor(diff / minute) + '分钟前';
    } else if (diff < day) {
      return Math.floor(diff / hour) + '小时前';
    } else if (diff < week) {
      return Math.floor(diff / day) + '天前';
    } else {
      return time.toLocaleDateString();
    }
  };
  
  var getStatusText = function(status) {
    if (status === 0) return '待处理';
    if (status === 1) return '已接受';
    if (status === 2) return '已拒绝';
    return '未知';
  };
  
  var getStatusClass = function(status) {
    if (status === 0) return 'pending';
    if (status === 1) return 'accepted';
    if (status === 2) return 'rejected';
    return 'unknown';
  };
  
  module.exports = {
    getCurrentRequests: getCurrentRequests,
    formatTime: formatTime,
    getStatusText: getStatusText,
    getStatusClass: getStatusClass
  };
</wxs>
