// pages/farmers/farmers.js
Page({
  data: {
    farmers: [],
    allFarmers: [], // 存储所有养殖户数据
    loading: false,
    searchKeyword: '',
    activeTab: 'all', // 当前激活的筛选标签
    currentPage: 1,
    pageSize: 10,
    total: 0
  },

  onLoad: function (options) {
    this.checkLoginStatus();
    this.loadFarmers();
  },

  onShow: function () {
    this.loadFarmers();
  },

  checkLoginStatus: function() {
    const token = wx.getStorageSync('token');
    if (!token) {
      wx.reLaunch({
        url: '/pages/login/login'
      });
      return false;
    }
    return true;
  },

  loadFarmers: function() {
    if (!this.checkLoginStatus()) return;
    
    this.setData({
      loading: true
    });

    // 模拟数据
    const mockFarmers = [
      {
        id: 1,
        name: '张三',
        phone: '13800138001',
        address: '宁夏银川市兴庆区',
        farmName: '张三养殖场',
        animalCount: 120,
        status: 'active',
        registerTime: '2023-01-15'
      },
      {
        id: 2,
        name: '李四',
        phone: '13800138002',
        address: '宁夏石嘴山市大武口区',
        farmName: '李四牧业',
        animalCount: 85,
        status: 'pending',
        registerTime: '2023-02-20'
      },
      {
        id: 3,
        name: '王五',
        phone: '13800138003',
        address: '宁夏吴忠市利通区',
        farmName: '王五农场',
        animalCount: 200,
        status: 'inactive',
        registerTime: '2023-03-10'
      },
      {
        id: 4,
        name: '赵六',
        phone: '13800138004',
        address: '宁夏固原市原州区',
        farmName: '赵六养殖合作社',
        animalCount: 150,
        status: 'active',
        registerTime: '2023-04-05'
      }
    ];

    setTimeout(() => {
      this.setData({
        allFarmers: mockFarmers,
        loading: false
      });
      this.filterFarmers();
    }, 1000);
  },

  // 筛选标签切换
  onTabChange: function(e) {
    const tab = e.currentTarget.dataset.tab;
    this.setData({
      activeTab: tab
    });
    this.filterFarmers();
  },

  // 根据当前标签和搜索关键词筛选养殖户
  filterFarmers: function() {
    const { allFarmers, activeTab, searchKeyword } = this.data;
    let filteredFarmers = [...allFarmers];

    // 按状态筛选
    if (activeTab !== 'all') {
      filteredFarmers = filteredFarmers.filter(farmer => farmer.status === activeTab);
    }

    // 按搜索关键词筛选
    if (searchKeyword.trim()) {
      const keyword = searchKeyword.trim().toLowerCase();
      filteredFarmers = filteredFarmers.filter(farmer => 
        farmer.name.toLowerCase().includes(keyword) ||
        farmer.farmName.toLowerCase().includes(keyword) ||
        farmer.phone.includes(keyword)
      );
    }

    this.setData({
      farmers: filteredFarmers,
      total: filteredFarmers.length
    });
  },

  onSearchInput: function(e) {
    this.setData({
      searchKeyword: e.detail.value
    });
  },

  onSearch: function() {
    this.filterFarmers();
  },

  onFarmerTap: function(e) {
    const farmerId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/farmer/farmer?id=${farmerId}`
    });
  },

  onAddFarmer: function() {
    wx.navigateTo({
      url: '/pages/farmer/farmer?mode=add'
    });
  },

  onPullDownRefresh: function() {
    this.loadFarmers();
    wx.stopPullDownRefresh();
  },

  onReachBottom: function() {
    if (this.data.farmers.length < this.data.total) {
      this.loadMoreFarmers();
    }
  },

  loadMoreFarmers: function() {
    // 实际项目中这里会加载更多数据
    console.log('加载更多养殖户数据');
  },

  onShareAppMessage: function() {
    return {
      title: '养殖户管理',
      path: '/pages/farmers/farmers'
    };
  }
});