// pages/device-inner/index.js
import http from '../../utils/http'
import util from '../../utils/util'
import api from '../../utils/api'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    page: 1,
    touchBottom: false,
    project_ids: [''],
    work_point_ids: [''],
    device: '',
    device_name_id: '',
    spec_id: '',
    list: [],
    number: '',
    stat: null,
    device_status: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  getList: function () {
    var data = this.data
    var that = this
    http({
      url: 'inner-devices/search',
      data: {
        number: data.number,
        project_ids: data.project_ids,
        work_point_ids: data.work_point_ids,
        device_id: data.device_id,
        device_name_id: data.device_name_id,
        spec_id: data.spec_id,
        page: data.page,
        stat: true,
        status: this.data.device_status
      },
      success: function (res) {
        if (res.code == 0) {
          var list = that.data.list
          var touchBottom = that.data.touchBottom
          list = list.concat(res.data);
          if (res.data.length <= 0) {
            touchBottom = true;
          }
          that.setData({
            touchBottom,
            list,
            isSearch: false,
            stat: res.stat
          })
        }
      }
    })
  },

  onChange: function (e) {
    var name = e.currentTarget.dataset.name
    this.setData({
      [name]: e.detail
    })
  },

  filterSelect: function (e) {
    let name = e.currentTarget.dataset.status
    this.setData({
      device_status: name,
      touchBottom: false,
      list: [],
      page: 1
    })
    this.getList()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  navigate: function (e) {
    // this.saveFilter()
    wx.navigateTo({
      url: e.currentTarget.dataset.url,
    })
  },

  saveFilter() {
    var data = {
      project_ids: this.data.project_ids,
      work_point_ids: this.data.work_point_ids,
      type: this.data.type,
      name: this.data.name,
      spec: this.data.spec
    }
    wx.setStorageSync('sg-device-filters', data)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var data = wx.getStorageSync('sg-device-filters')
    this.setData(data)
    this.search()
  },

  search: function () {
    this.setData({
      touchBottom: false,
      list: [],
      page: 1
    })
    this.getList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    // this.saveSearchOption()
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (!this.data.touchBottom) {
      this.setData({
        page: this.data.page + 1
      })
      this.getList()
    } else {
      util.error('没有更多数据了')
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})