Page({
  data: {
    students: [],
    modalVisible: false,
    modalVisible1:false,
    foundStudent: null, // 存储查询到的学生
    searchValue: ''
  },
  
  onLoad() {
    const app = getApp();
    let students = app.globalData.students || []; // 获取全局数据

    // 按照id排序 从小到大
    students = students.sort((a, b) => a.id - b.id)

    this.setData({
        students: students
    });

    // console.log("按照id排序", this.data.students);
 },

  // 发现图标被点击
  onIconClick() {
    // console.log("图标被点击了！");
    wx.navigateTo({
      url: '/pages/somePage/somePage', // 跳转到某个页面
    });
  },

  // 打开弹窗
  openModal() {
    this.setData({
      modalVisible: true
    });
  },

  // 监听输入框内容
  onInput(event) {
    this.setData({
      searchValue: event.detail.value
    });
  },

  onSearch() {
    // console.log("点击查询按钮");
    const { searchValue, students } = this.data;
  
    // 判断输入是否为数字
    const isNumeric = !isNaN(searchValue) && searchValue.trim() !== '';
  
    // 查找学生
    const foundStudent = students.find(student => {
      return isNumeric ? student.id === parseInt(searchValue) : student.name === searchValue;
    });
  
    // 更新查询结果
    this.setData({
      foundStudent: foundStudent, // 保存找到的学生
      searchValue: "",
      modalVisible: false // 关闭弹窗
    });
  
    // 可以在此处添加一个提示
    if (foundStudent) {
      // console.log("找到的学生：", foundStudent);
    } else {
      this.setData({
        modalVisible1:true
      });
    }
  },
  
  // 返回总人数页面
  closeFound(){
    this.setData({
      foundStudent: null
    })
  },

  closeModal(){
    this.setData({
      foundStudent: null,
      modalVisible: false,
      modalVisible1: false
    });
  }

});

