// 💬 @提醒选择组件逻辑
const groupChatManager = require('../../utils/group-chat-manager.js');

Component({
  properties: {
    // 是否显示
    visible: {
      type: Boolean,
      value: false
    },
    
    // 群ID
    groupId: {
      type: String,
      value: ''
    },
    
    // 当前用户ID
    currentUserId: {
      type: String,
      value: ''
    },
    
    // 是否显示@全体成员
    showMentionAll: {
      type: Boolean,
      value: true
    }
  },

  data: {
    // 成员数据
    allMembers: [],
    filteredMembers: [],
    
    // 搜索关键词
    searchKeyword: '',
    
    // 加载状态
    loading: false
  },

  observers: {
    'visible': function(visible) {
      if (visible && this.data.groupId) {
        this.loadGroupMembers();
      }
    },
    
    'groupId': function(groupId) {
      if (groupId && this.data.visible) {
        this.loadGroupMembers();
      }
    }
  },

  methods: {
    // 加载群成员
    async loadGroupMembers() {
      if (!this.data.groupId) return;
      
      try {
        this.setData({
          loading: true
        });
        
        const result = await groupChatManager.getGroupMembers(this.data.groupId);
        
        if (result.success) {
          // 过滤掉当前用户
          const members = result.data.filter(member => member.userId !== this.data.currentUserId);
          
          this.setData({
            allMembers: members,
            loading: false
          });
          
          // 应用搜索过滤
          this.applyFilter();
          
          console.log('✅ 群成员加载完成:', members.length);
        } else {
          throw new Error(result.error || '获取群成员失败');
        }
        
      } catch (error) {
        this.setData({
          loading: false
        });
        
        console.error('❌ 加载群成员失败:', error);
        wx.showToast({
          title: '加载成员失败',
          icon: 'none'
        });
      }
    },

    // 搜索输入
    onSearchInput(e) {
      const keyword = e.detail.value;
      this.setData({
        searchKeyword: keyword
      });
      this.applyFilter();
    },

    // 清除搜索
    clearSearch() {
      this.setData({
        searchKeyword: ''
      });
      this.applyFilter();
    },

    // 应用搜索过滤
    applyFilter() {
      const keyword = this.data.searchKeyword.toLowerCase();
      let filtered = this.data.allMembers;
      
      if (keyword) {
        filtered = this.data.allMembers.filter(member => {
          const name = (member.nickname || member.username || '').toLowerCase();
          return name.includes(keyword);
        });
      }
      
      this.setData({
        filteredMembers: filtered
      });
    },

    // @全体成员
    onMentionAll() {
      console.log('💬 @全体成员');
      
      this.triggerEvent('mention', {
        type: 'all',
        text: '所有人',
        userIds: this.data.allMembers.map(member => member.userId)
      });
      
      this.onClose();
    },

    // @特定成员
    onMentionMember(e) {
      const member = e.currentTarget.dataset.member;
      console.log('💬 @特定成员:', member);
      
      this.triggerEvent('mention', {
        type: 'user',
        text: member.nickname || member.username,
        userId: member.userId,
        userIds: [member.userId]
      });
      
      this.onClose();
    },

    // 关闭选择器
    onClose() {
      this.setData({
        searchKeyword: '',
        filteredMembers: this.data.allMembers
      });
      
      this.triggerEvent('close');
    },

    // 点击遮罩
    onMaskTap() {
      this.onClose();
    },

    // 阻止事件冒泡
    stopPropagation() {
      // 阻止点击事件冒泡
    }
  }
});
