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

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    goodsId: 0,
    getUrl: '',
    getJson: {},
    levelList: [], // 权限列表
    groupsList: [],
    isFinished: false,
    goodsInfo: {},
    widgetsInfo: {},
    discountValues:[],

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({goodsId:options.id});
    if (!this.data.goodsId) {
      this.data.getJson = {};
      this.data.getDatUrl = "plugin.shop-assistant.frontend.goods.add";
      wx.setNavigationBarTitle({
        title: '商品发布',
      });
    } else {
      wx.setNavigationBarTitle({
        title: '商品修改',
      });
      this.data.getJson = {id: this.data.goodsId};
      this.data.getDatUrl = "plugin.shop-assistant.frontend.goods.get-detail";
    }
    this.getGoodsDetail();
  },

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

  },

  releaseGoods(){
    let baseSet = this.selectComponent('.base-set');
    let baseData = baseSet ? baseSet.validatorData() : null;
    if(!baseData)return;

    let permissions = this.selectComponent('.permissions');
    let permissionData = permissions ? permissions.validatorData() : null;
    if(!permissionData)return;

    let discount = this.selectComponent('.discount');
    let discountData = discount ? discount.validatorData() : null;
    if(!discountData)return;

    let mergeData = baseData;
    mergeData.widgets.privilege = permissionData;
    mergeData.widgets.discount = discountData;
    console.log(mergeData);
    if (!app._isTextEmpty(this.data.goodsId)) {
      // 修改商品
      mergeData.id = this.data.goodsId;
      this.saveRequest("plugin.shop-assistant.frontend.goods.update", mergeData);
    } else {
      // 添加商品
      this.saveRequest("plugin.shop-assistant.frontend.goods.create", mergeData);
    }

  },

  saveRequest(url, data) {
    let urlStr = app.getNetAddresss(url);
    app._postNetWork({
      url: urlStr,
      data:  data,
      success: (resdata) => {
        var res = resdata.data;
        app.tips(res.msg);
        if (res.result != 1) return;
        let pages = getCurrentPages();
        let page = pages[pages.length-2];
        page.getGoodsList();
        wx.navigateBack();
      }
    });
  },

  getGoodsDetail(){
    let urlStr = app.getNetAddresss(this.data.getDatUrl);
    app._postNetWork({
      url: urlStr,
      data:  this.data.getJson,
      success: (resdata) => {
        var res = resdata.data;
        
        if (res.result != 1) return app.tips(res.msg);
        let {levels, groups, goods, category_menus, dispatch_templates, dispatch_types} = res.data;
        this.setData({
          isFinished:true,
          levelList : levels,
          groupsList : groups,
          goodsInfo : goods,
          categoryMenus : category_menus,
          dispatchTemplates : dispatch_templates,
          dispatchTypes : dispatch_types,
          widgetsInfo : goods ? goods.widgets : {},
          //discountValues:this.data.goodsId ? goods.widgets.discount.discount_value : levels
          discountValues: levels
        });
    
      }
    });
  },

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

  },

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

  },

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

  },

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

  },

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

  },

  changeTabs(evt){
    this.setData({
      active:evt.detail.name
    });
  },

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

  }
});