// pages/mine/shop/choose.js
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeNames: ['0'],
    tradeList: [], //一级行业类型
    tradeTwoList: [], //二级行业类型
    tradeThirdList: [], //三级行业类型,
    selectItems: [], //选中的行业,
    selectItemNames: ''
  },
  getTradeList() {
    wx.showLoading({
      title: '获取中...'
    })
    let _self = this
    app.http.get(app.urls.getIndustryList, {
        header: {
          token: app.globalData.token
        }
      })
      .then(res => {
        wx.hideLoading({})
        data.forEach(el => {
          el.isSel = false
        })
        _self.setData({
          tradeList: res.data
        })
      })
      .catch(err => {
        wx.hideLoading({})
      })
  },
  //type 2 二级   3三级
  getTradeListById(id, type) {
    console.log(type)
    wx.showLoading({
      title: '获取中...'
    })
    let _self = this
    app.http.get(app.urls.getIndustryList, {
        header: {
          token: app.globalData.token
        },
        data: {
          id: id
        }
      })
      .then(res => {
        wx.hideLoading({})
        res.data.forEach(el => {
          el.isSel = false
        })
        _self.data.selectItems.forEach(element => {
          res.data.forEach(el => {
            if (element.id == el.id) {
              el.isSel = true
            }
          })
        })
        if (type == '1') {
          _self.setData({
            tradeTwoList: res.data
          })
        } else if (type == '2') {
          _self.setData({
            tradeThirdList: res.data
          })
        }
      })
      .catch(err => {
        wx.hideLoading({})
      })
  },
  //选中
  selectItem(e) {
    let item = e.currentTarget.dataset.item
    let type = e.currentTarget.dataset.type
    this.doSelectItem(item)
    if (type == '1') {
      this.data.tradeList.forEach(el => {
        if (item.id == el.id) {
          el.isSel = !el.isSel
        }
      })
      this.setData({
        tradeList: this.data.tradeList
      })
    } else if (type == '2') {
      this.data.tradeTwoList.forEach(el => {
        if (item.id == el.id) {
          el.isSel = !el.isSel
        }
      })
      this.setData({
        tradeTwoList: this.data.tradeTwoList
      })
    } else {
      this.data.tradeThirdList.forEach(el => {
        if (item.id == el.id) {
          el.isSel = !el.isSel
        }
      })
      this.setData({
        tradeThirdList: this.data.tradeThirdList
      })
    }
  },
  //点击文字
  chooseItem(e) {
    let item = e.currentTarget.dataset.i
    let type = e.currentTarget.dataset.type
    if (type == '1') {
      this.setData({
        tradeTwoList: [],
        tradeThirdList: []
      })
      this.getTradeListById(item.id, type)
    } else if (type == '2') {
      this.setData({
        tradeThirdList: []
      })
      this.getTradeListById(item.id, type)
    } else {
      this.doSelectItem(item)
      this.data.tradeThirdList.forEach(el => {
        if (el.id == item.id) {
          el.isSel = !el.isSel
        }
      })
      this.setData({
        tradeThirdList: this.data.tradeThirdList
      })
    }
  },
  doSelectItem(item) {
    let _self = this
    let isSel = !item.isSel
    console.log(isSel)
    if (isSel) {
      _self.data.selectItems.push(item)

      _self.setData({
        selectItems: _self.data.selectItems
      })
      console.log(_self.data.selectItems)
    } else {
      //剔除
      let index = -1
      for (var i = 0; i < _self.data.selectItems.length; ++i) {
        if (item.id == _self.data.selectItems[i].id) {
          index = i
          break
        }
      }
      if (index != -1) {
        _self.data.selectItems.splice(index, 1)
      }
      _self.setData({
        selectItems: _self.data.selectItems
      })
    }
    var names = ''
    _self.data.selectItems.forEach(el => {
      names += el.name + ','
    })
    console.log(names)
    _self.setData({
      selectItemNames: names
    })
  },
  onChange(e) {
    this.setData({
      activeNames: e.detail
    })
  },
  selected() {
    let _self = this
    if (_self.data.selectItems.length > 0) {
      let pages = getCurrentPages()
      let prevPage = pages[pages.length - 2]
      prevPage.setData({
        selTradeList: _self.data.selectItems
      })
      wx.navigateBack({
        delta: 1,
      })
    } else {
      wx.showToast({
        title: '未选择任何行业',
        icon: 'error'
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getTradeList()
  },
})