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

Page({

  /**
   * 页面的初始数据
   */
  data: {
    status: '',
    active: 0,
    devices: [],
    device_ids: [''],
    names: [],
    device_name_ids: [''],
    specs: [],
    spec_ids: [''],
    rent_types: [],
    rent_type_ids: ['']
  },

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

  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()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  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
    })
  },
  onrentChange: function (e) {
    this.setData({
      rent_type_ids: [e.detail]
    })
    console.log(this.data.rent_type_ids)
  },
  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
    })
    if (name == 'device_ids') {
      this.updateDeviceNames()
    }
    if (name == 'device_name_ids') {
      this.updateSpecs()
    }
  },

  updateDeviceNames() {
    var that = this;
    that.setData({
      device_name_ids: ['']
    })
    api.getByName(this, 'device-names/get', 'names', {
      type: 'drop_menu',
      device_ids: this.data.device_ids
    }, function () {
      that.updateSpecs();
    });
  },

  updateSpecs() {
    var that = this;
    that.setData({
      spec_ids: ['']
    })
    api.getByName(this, 'specs/get', 'specs', {
      type: 'drop_menu',
      device_name_ids: this.data.device_name_ids
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (this.data.status != 1) {
      var data = wx.getStorageSync('sg-data-filters')
      this.setData(data)
    }
    api.getByName(this, 'devices/get', 'devices', {
      type: 'drop_menu'
    });
    api.getByName(this, 'device-names/get', 'names', {
      type: 'drop_menu',
      device_ids: this.data.device_ids
    });
    api.getByName(this, 'specs/get', 'specs', {
      type: 'drop_menu',
      device_name_ids: this.data.device_name_ids
    });
    api.getByName(this, 'rent-types/get', 'rent_types', {
      type: 'drop_menu'
    });
  },

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

  saveFilter() {
    var data = {
      devices: this.data.devices,
      device_ids: this.data.device_ids,
      names: this.data.names,
      device_name_ids: this.data.device_name_ids,
      specs: this.data.specs,
      spec_ids: this.data.spec_ids,
      rent_types: this.data.rent_types,
      rent_type_ids: this.data.rent_type_ids
    }
    wx.setStorageSync('sg-data-filters', data)
  },

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

  },

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

  },

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

  },

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

  }
})