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

Page({

  /**
   * 页面的初始数据
   */
  data: {
    device_types: [],
    type: '',
    names: [],
    name: '',
    specs: [],
    spec: '',
    number: '',
    list: [],
    page: 1,
    touchBottom: false,
    showDate: false,
    default_dates: [],
    add_devices: [],
    showAdded: false
  },

  /**
   * 生命周期函数--监听页面加载d
   */
  onLoad: function (options) {
    var that = this
    api.getByName(this, 'devices/getThreeLevel', 'device_types', {
      type: 'drop_menu'
    }, function () {
      that.updateNameSpec()
    });
    // api.getByName(this, 'inner-devices/get', 'names', {type: 'drop_menu'});
    // api.getByName(this, 'specs/get', 'specs', {type: 'drop_menu'});
    this.getList()
  },

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

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

  switchShowDate: function (e) {
    this.setData({
      showDate: e.currentTarget.dataset.show,
    })
  },

  switchShowAdded: function (e) {
    this.complete()
    this.setData({
      showAdded: e.currentTarget.dataset.show
    })
  },

  complete: function (e) {
    wx.setStorageSync('sg-added-devices', this.data.add_devices)
    wx.navigateBack({
      delta: 0,
    })
  },

  confirmDate: function (e) {
    var ids = []
    var add_devices = this.data.add_devices
    for (var i = 0; i < add_devices.length; ++i) {
      ids.push(add_devices[i].id)
    }
    var list = this.data.list
    var [start_date, end_date] = e.detail;
    start_date = util.formatDate(start_date)
    end_date = util.formatDate(end_date)
    for (var i = 0; i < list.length; ++i) {
      if (ids.indexOf(list[i].id) == -1 && list[i].checked) {
        var device = list[i]
        device.start_date = start_date,
          device.end_date = end_date
        add_devices.push(device)
      }
    }
    this.switchShowDate(e)
    this.setData({
      add_devices: add_devices,
      showAdded: true
    })
  },

  switchSelect: function (e) {
    var list = this.data.list
    var index = e.detail.index
    list[index].checked = list[index].checked ? false : true
    this.setData({
      list
    })
  },

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

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

  search: function () {
    this.resetList()
    this.getList()
  },

  getList: function () {
    var that = this
    http({
      url: 'inner-devices/search',
      data: {
        number: this.data.number,
        device_id: this.data.type,
        device_name_id: this.data.name,
        spec_id: this.data.spec,
        page: this.data.page,
        free: true
      },
      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
          })
        }
      }
    })
  },

  deleteDevice: function (e) {
    var index = e.currentTarget.dataset.index
    var add_devices = this.data.add_devices
    add_devices.splice(index, 1)
    this.setData({
      add_devices
    })
  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var add_devices = wx.getStorageSync('sg-added-devices')
    add_devices = add_devices ? add_devices : []
    this.setData({
      add_devices
    })
  },

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

  },

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

  },

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

  },

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

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

  }
})