// packageF/storeManagement/storeGoodsCatgory/storeGoodsCatgory.js
var behavior = require('../common.js');
var app = getApp();
Page({
  behaviors: [behavior],
  /**
   * 页面的初始数据
   */
  data: {

    collapseActive1:[],
    collapseActive2:[],
    collapseActive3:[],

    treeData: [],
    maxLevel: 1,
    page:1,

    loading:false,
    finished:false
  },

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

  initData(){
    this.data.page=1;
    this.data.loading = false;
    this.setData({
      finished:false
    });
  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  getData(){
    if (this.data.loading || this.data.finished) return false;
    this.data.loading=true;
    let urlStr = app.getNetAddresss("plugin.shop-assistant.frontend.category.get-list");
    app._postNetWork({
      url: urlStr,
      data: {page:this.data.page},
      success: (resdata) => {
        this.data.loading=false;
        var res = resdata.data;
        if (res.result != 1) return app.tips(res.msg);
        
        let changeData={};
        changeData.maxLevel = res.data.setLevel;
        let list = res.data.list;
        if (list.current_page >= list.last_page || list.data.length < list.per_page){
          changeData.finished = true;
        }
        this.data.page++;
        changeData.treeData = list.current_page > 1 ? this.data.treeData.concat(list.data) : list.data;
        this.setData(changeData);
      }
    });
  },

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

  },

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

  },

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

  },

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

  },

  collapseActiveChange(evt){
    let name = evt.currentTarget.dataset.name;
    this.setData({
      [name]:evt.detail
    });
  },
  handOperation(evt){
    let id = evt.detail.name;
    let level = evt.detail.level;
    let itemList = ['添加子类', '编辑', '删除'];
    if(level==3 || (level==2 &&this.data.maxLevel==2)){
      itemList.shift();
    }
    wx.showActionSheet({
      itemList,
      success:(res)=> {
        let tapIndex = res.tapIndex;
        if(level==3 || (level==2 &&this.data.maxLevel==2)){
          if(tapIndex==1){
            this.deleteCategory(id);
          }else if(tapIndex==0){
            this.toDditCategory(id);
          }
        }else {
          if(tapIndex==0){
            this.toAddCategory(id);
          }else if(tapIndex==1){
            this.toDditCategory(id);
          }else if(tapIndex==2){
            this.deleteCategory(id);
          }
        }
      },
      fail (res) {
      
      }
    });
  },
  toAddCategory(id){
    wx.navigateTo({
      url: '/packageF/storeManagement/editGoodsCatgory/editGoodsCatgory?parentId='+id,
    });
  },
  toDditCategory(id){
    wx.navigateTo({
      url: '/packageF/storeManagement/editGoodsCatgory/editGoodsCatgory?categoryId='+id,
    });
  },

  // 删除分类
  async deleteCategory (id) {
    let confirmFlag = await app.confirm('正在删除改分类，请确认操作');
    if(!confirmFlag) return;
    let urlStr = app.getNetAddresss("plugin.shop-assistant.frontend.category.delete");
    app._postNetWork({
      url: urlStr,
      data: {id},
      success: (resdata) => {
        var res = resdata.data;
        if (res.result != 1) return app.tips(res.msg);
        this.initData();
        this.getData();
      }
    });
  },


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

  }
});