// packageF/storeManagement/editGoodsCatgory/editGoodsCatgory.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    categoryId: 0,
    parentId: 0,
    categoryData: {},
    parentData: {},

    display_order:0,
    name:'',
    is_home:0,
    enabled:0,

    thumb_src:''


  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.categoryId && options.categoryId!=0){
      this.setData({
        categoryId:options.categoryId
      });
      this.getData(options.categoryId);
    }
    if(options.parentId && options.parentId!=0){
      this.setData({
        parentId:options.parentId
      });
      this.getData(options.parentId);
    }
  },

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

  },

  updateGoodsImg(evt) {
    this.data.thumb_src = evt.detail[0] ? evt.detail[0] : '';
  },

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

  },
  switchChange(evt) {
    let name = evt.currentTarget.dataset.name;
    this.setData({
      [name]: evt.detail
    });
  },
  getData(id) {
    let urlStr = app.getNetAddresss("plugin.shop-assistant.frontend.category.detail");
    app._postNetWork({
      url: urlStr,
      data: {id},
      success: (resdata) => {
        var res = resdata.data;
        if (res.result != 1) return app.tips(res.msg);
        if (this.data.categoryId && this.data.categoryId>0) {
          this.setData({
            categoryData:res.data,
            display_order:res.data.display_order,
            name:res.data.name,
            thumb_src:res.data.thumb_src,
            is_home:res.data.is_home,
            enabled:res.data.enabled
          });
        } else {
          this.setData({
            parentData:res.data
          });
        }
      }
    });

  },

  sureEdit () {
    if (app._isTextEmpty(this.data.name)) {
      return app.tips("分类名称不能为空");
    }
    if (!app._isTextEmpty(this.data.categoryId)) {
      this.editCategory();
    } else {
      this.addCategory();
    }
  },

  editCategory(){
    let sendJson = {};
    let {level, parent_id} = this.data.categoryData;
    let { name, thumb_src, display_order, is_home, enabled} = this.data;
    sendJson.category = {
      level, parent_id, name, display_order, is_home, enabled,
      thumb: thumb_src,
    };
    sendJson.id = this.data.categoryId;
    let urlStr = app.getNetAddresss("plugin.shop-assistant.frontend.category.update");
    app._postNetWork({
      url: urlStr,
      data: sendJson,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result != 1) return app.tips(res.msg);
        let pages = getCurrentPages();
        let page = pages[pages.length-2];
        page.initData();
        page.getData();
        wx.navigateBack();
      }
    });
  },
  addCategory(){
    let sendJson = {};
    let { name, thumb_src, display_order, is_home, enabled} = this.data;
    sendJson.category = {
      name, display_order, is_home, enabled,
      level: this.data.parentData.level ? this.data.parentData.level + 1 : 1, // 顶级为一级
      thumb: thumb_src,
      parent_id: this.data.parentId
    };
    let urlStr = app.getNetAddresss("plugin.shop-assistant.frontend.category.create");
    app._postNetWork({
      url: urlStr,
      data: sendJson,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result != 1) return app.tips(res.msg);
        let pages = getCurrentPages();
        let page = pages[pages.length-2];
        page.initData();
        page.getData();
        wx.navigateBack();
      }
    });
  },

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

  },

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

  },

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

  },

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

  },

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

  }
});