// pages/filter/index.js
import http from '../../utils/http'
import util from '../../utils/util'
import api from '../../utils/api'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    projects: [],
    project_ids: [''],
    work_points: [],
    work_point_ids: [''],
    device_types: [],
    device_id: '',
    names: [],
    device_name_id: '',
    specs: [],
    spec_id: ''
  },

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

  onChange: function(e) {
    var name = e.currentTarget.dataset.name
    this.setData({
      [name]: e.detail
    })
    if(['device_id', 'device_name_id'].indexOf(name) != -1) {
      if(name == 'device_id') {
        this.setData({
          device_name_id: '',
          spec_id: ''
        })
      }
      if(name == 'device_name_id') {
        this.setData({
          spec_id: ''
        })
      }
      this.updateNameSpec()
    }
  },

  confirm: function() {
    this.saveFilter()
    wx.navigateBack()
  },

  updateNameSpec: function() {
    // var device_types = this.data.device_types
    // var typeItem = this.getItemById(device_types, this.data.device_id)
    var names = this.data.names;
    var nameItem = this.getItemById(names, this.data.device_name_id)
    // var specs = this.getItemById(nameItem.specs, this.data.spec)
    this.setData({
      names,
      specs: nameItem.specs
    })
  },

  getItemById(items, id) {
    for(var i = 0; i < items.length; ++i) {
      if(items[i].value == id) return items[i]
    }
    return ''
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  delete: function(e) {
    var name = e.currentTarget.dataset.name
    var index = e.currentTarget.dataset.index
    var items = this.data[name]
    if(index == 0) return false
    items.splice(index, 1)
    this.setData({
      [name]: items
    })
  },

  add: function(e) {
    var name = e.currentTarget.dataset.name
    var items = this.data[name]
    if(!items[items.length - 1]) {
      util.error('请先选择');
      return false
    }
    items.push('')
    this.setData({
      [name]: items
    })
  },

  onDropChange: function(e) {
    var index = e.currentTarget.dataset.index
    var name = e.currentTarget.dataset.name
    var val = e.detail
    var items = this.data[name]
    items[index] = val
    this.setData({
      [name]: items
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    api.getByName(this, 'projects/getAll', 'projects', {type: 'drop_menu'});
    api.getByName(this, 'work-points/get', 'work_points', {type: 'drop_menu'});
    var that = this
    api.getByName(this, 'device-names/getNamesAndSpecs', 'names', {type: 'drop_menu'}, function() {
      that.updateNameSpec()
    });
    var data = wx.getStorageSync('sg-device-filters')
    this.setData(data)
  },

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

  saveFilter() {
    var data = {
      project_ids: this.data.project_ids,
      work_point_ids: this.data.work_point_ids,
      device_id: this.data.device_id,
      device_name_id: this.data.device_name_id,
      spec_id: this.data.spec_id
    }
    wx.setStorageSync('sg-device-filters', data)
  },

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

  },

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

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

  }
})