// pages/device/device.js - 设备管理页面
Page({
  data: {
    deviceList: [],
    activeTab: 'all',
    tabs: [
      { key: 'all', name: '全部' },
      { key: 'eartag', name: '耳标' },
      { key: 'collar', name: '项圈' },
      { key: 'host', name: '主机' },
      { key: 'fence', name: '围栏' }
    ],
    loading: false
  },

  onLoad() {
    console.log('设备管理页加载')
    this.loadDeviceList()
  },

  onShow() {
    console.log('设备管理页显示')
    this.loadDeviceList()
  },

  onPullDownRefresh() {
    this.loadDeviceList()
    setTimeout(() => {
      wx.stopPullDownRefresh()
    }, 1000)
  },

  // 切换标签
  switchTab(e) {
    const tab = e.currentTarget.dataset.tab
    this.setData({ activeTab: tab })
    this.loadDeviceList()
  },

  // 加载设备列表
  loadDeviceList() {
    this.setData({ loading: true })
    
    // TODO: 从API加载设备列表
    // 模拟数据
    setTimeout(() => {
      this.setData({
        deviceList: [
          { id: 1, name: '耳标-001', type: 'eartag', status: 'online', battery: 85 },
          { id: 2, name: '项圈-001', type: 'collar', status: 'online', battery: 92 },
          { id: 3, name: '主机-001', type: 'host', status: 'online', battery: 100 },
          { id: 4, name: '围栏-001', type: 'fence', status: 'online', battery: 78 }
        ],
        loading: false
      })
    }, 500)
  },

  // 查看设备详情
  viewDeviceDetail(e) {
    const id = e.currentTarget.dataset.id
    const type = e.currentTarget.dataset.type
    
    // 根据设备类型跳转到不同的详情页
    const urlMap = {
      eartag: `/pages/device/eartag/eartag?id=${id}`,
      collar: `/pages/device/collar/collar?id=${id}`,
      host: `/pages/device/host/host?id=${id}`,
      fence: `/pages/device/fence/fence?id=${id}`
    }
    
    const url = urlMap[type] || `/pages/device/eartag/eartag?id=${id}`
    
    wx.navigateTo({ url })
  },

  // 添加设备
  addDevice() {
    wx.navigateTo({
      url: '/pages/device/eartag-add/eartag-add'
    })
  }
})

