// pages/mine/product/productmanage/choosecatalog.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shopId: '',
    selCatalogId: '',
    selCatalogName: '',
    catalogList: [],
    currentPage: 1,
    isRefreshing: false,
    activeNames: ['0'],
    catalogOneList: [], //一级类目
    catalogTwoList: [], //二级类目
    catalogThirdList: [], //三级类目
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      shopId: options.shopId
    })
    this.getCatalogList(0, 1, 0)
  },
  doChoose() {
    if (this.data.selCatalogId == '') {
      wx.showToast({
        title: '请选择三级类目',
        icon: 'error'
      })
    } else {
      wx.redirectTo({
        url: './addproduct?shopId=' + this.data.shopId + '&cataLogId=' + this.data.selCatalogId,
      })
    }
  },
  //获取类目列表
  getCatalogList(id, level, code) {
    let _self = this
    wx.showLoading({
      title: '加载中...',
    })
    app.http.get(app.urls.getCatalogList.format({
        level: level
      }), {
        header: {
          token: app.globalData.token
        },
        data: {
          id: id,
          code: code
        }
      })
      .then(res => {
        wx.hideLoading({})
        _self.setData({
          isRefreshing: false
        })
        data.forEach(el => {
          el.isSel = false
        })
        if (level == '1') {
          _self.setData({
            catalogOneList: res.data
          })
        } else if (level == '2') {
          _self.setData({
            catalogTwoList: res.data
          })
        } else {
          _self.setData({
            catalogThirdList: res.data
          })
        }
      })
      .catch(err => {
        wx.hideLoading({})
        _self.setData({
          isRefreshing: false
        })
      })
  },
  // 选中了条目
  selectItem(e) {
    let _self = this
    let {
      type,
      item,
      index
    } = e.currentTarget.dataset
    if (type == '1') {
      _self.data.catalogOneList.forEach(el => {
        if (el.id == item.id) {
          el.isSel = true
        } else {
          el.isSel = false
        }
      })
      _self.setData({
        catalogOneList: _self.data.catalogOneList,
        catalogTwoList: [],
        catalogThirdList: []
      })
      _self.getCatalogList(item.id, '2', item.code)
    } else if (type == '2') {
      _self.data.catalogTwoList.forEach(el => {
        if (el.id == item.id) {
          el.isSel = true
        } else {
          el.isSel = false
        }
      })
      _self.setData({
        catalogTwoList: _self.data.catalogTwoList,
        catalogThirdList: []
      })
      _self.getCatalogList(item.id, '3', item.code)
    } else {
      _self.data.catalogThirdList.forEach(el => {
        if (el.id == item.id) {
          el.isSel = true
          this.setData({
            selCatalogId: el.id,
            selCatalogName: el.name
          })
        } else {
          el.isSel = false
        }
      })
      _self.setData({
        catalogThirdList: _self.data.catalogThirdList
      })
    }
  },
  toAddPage() {
    wx.navigateTo({
      url: './addcatalog',
    })
  },
  //刷新列表
  dorefreshList() {
    this.setData({
      catalogOneList: [], //一级类目
      catalogTwoList: [], //二级类目
      catalogThirdList: [], //三级类目
      isRefreshing: true,
      currentPage: 1
    })
    this.getCatalogList(0, 1, 0)
  }
})