// pages/member/CommodityRelease/CommodityRelease.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    DataInfo: [],
    store: '',
    store_id: '',
    isQuesetOk: false, // 数据获取成功

    postJson: {}, // 请求参数

    titleTest: '商品发布',
    isType: false, // 是否为商品发布
    showCoverLayer: false, // 是否显示遮盖层

    Signthree: 0,

    // 选择分类
    options: [],
    selectedOptions3: [],
    sectionOptions: [],
    jumpState: true,
    number: 1,

    show1: false,
    show2: false,
    checkbox3: ['2'],
    radio1: 1,
    radio: '1',
    list: ['a', 'b', 'c'], // 引用组件,
    result: ['a', 'b'],
    goods_id: '',
    is_tap:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.store) {
      this.setData({
        store: options.store
      });
      console.log(this.data.store, '详情参数');
    }
    if (options.store_id) {
      this.setData({
        store_id: options.store_id
      });
    }
    if (options.goods_id) {
      this.setData({
        goods_id: options.goods_id
      });
    }
    let isType = app._isTextEmpty(options.goods_id);
    this.setData({
      isType});
    this._setTitle();

    if(options.store=='store'){
      this.tab_discount();
    }
    
  },

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

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

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

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

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

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

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  // 设置标题
  _setTitle() {
    if (!this.data.isType) {
      wx.setNavigationBarTitle({
        title: '商品编辑' // 修改title
      });
    } else {
      wx.setNavigationBarTitle({
        title: '商品发布' // 修改title
      });
    }
  },
  // 获取数据
  getGoodsJson() {
    var urlStr = '';
    var json = {};
    if (!this.data.isType) {
      if (this.data.store == 'store') {
        urlStr = app.getNetAddresss('plugin.store-cashier.frontend.store.goods.get-goods-detail');
        json = { goods_id: this.options.goods_id };
      } else if (this.data.store == 'supplier') {
        urlStr = app.getNetAddresss('plugin.supplier.frontend.goods.goods-detail');
        json = { goods_id: this.options.goods_id };
      }
      app._postNetWork({
        url: urlStr,
        data: json,
        success: (resdata) => {
          var res = resdata.data;

          if (res.result == 1) {
            this.setData({
              DataInfo: res.data,
              isQuesetOk: true
            });
            console.log(this.data.DataInfo, '传递的数据啊');

            if (this.data.store == 'store') {
              if (this.data.tab_discount) {
                this.selectComponent('#componentId').getDataInfo();
              }
              if (this.data.tab_privilege) {
                this.selectComponent('#mychildTwo').getDataInfo();
              }
            }
          } else {
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res.msg);
        }
      });
    } else {
      this.setData({
        isQuesetOk: true
      });
    }
  },
  // 判断是否显示完成按钮
  isCoverLayer(data) {
    this.setData({
      showCoverLayer: data.detail
    });
  },
  // 判断是否开启折扣
  tab_discount(e) {
    let urlStr = app.getNetAddresss('plugin.store-cashier.frontend.store.goods.get-store-goods-setting');
    app._postNetWork({
      url: urlStr,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          console.log(res.data.goods_widgets.tab_discount, res.data.goods_widgets.tab_privilege, '权限', '折扣是否开启');
          if (res.data.goods_widgets.tab_discount == 1) {
            this.setData({
              tab_discount: true
            });
            this.getGoodsJson();
          }
          if (res.data.goods_widgets.tab_privilege == 1) {
            this.setData({
              tab_privilege: true
            });
            this.getGoodsJson();
          }
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      }
    });
  },
  getChildoneData(e) {
    let postJson = e.detail;

    this.setData({
      postJson});
  },
  getChildoneDataFun(val) {
    let postJson1 = val.detail;
    this.setData({
      postJson1});
  },
  getSignthree(a) {
    this.setData({
      Signthree: a.detail
    });
  },
  getChildoneDatas(e) {
    let postJson2 = e.detail;
    this.setData({
      postJson2});
  },
  // 确认发布/编辑按钮
  publishEvent() {
    let urls = '';
    let json = {};
    if (this.options.store == 'store' || this.data.store == 'store') {
      if (this.data.tab_privilege) {
        this.selectComponent('#mychildTwo').PermissionsJson();
      }
      if (this.data.tab_discount) {
        this.selectComponent('#componentId').DiscountJson();
      }
      this.selectComponent('#dataChildDone').setJson();
    } else if (this.options.store == 'supplier' || this.data.store == 'store') {
      this.selectComponent('#dataChildDone').setJson();
    }
    json = Object.assign(this.data.postJson, this.data.postJson1, this.data.postJson2);
    console.log(json, '总数据上面没有');

    if (app._isTextEmpty(json.title)) {
      wx.showToast({
        title: '请填写商品名称',
        icon: 'none'
      });
      return;
    }
    if (app._isTextEmpty(json.sku)) {
      wx.showToast({
        title: '请填写商品单位',
        icon: 'none'
      });
      return;
    }
    if (app._isTextEmpty(json.category_cid)) {
      wx.showToast({
        title: '请填写商品分类',
        icon: 'none'
      });
      return;
    }

    if (app._isTextEmpty(json.thumb)) {
      wx.showToast({
        title: '请上传商品图片',
        icon: 'none'
      });
      return;
    }
    if (this.data.isType) {
      if (app._isTextEmpty(json.good_content) && app._isTextEmpty(json.content_text)) {
        wx.showToast({
          title: '请上传商品详情图片或商品详情文字',
          icon: 'none'
        });
        return;
      }
    }
    // if (app._isTextEmpty(json.thumb_url)) {
    //   wx.showToast({
    //     title: '请上传商品其他图片',
    //     icon: 'none'
    //   })
    //   return
    // }
    if (app._isTextEmpty(json.price)) {
      wx.showToast({
        title: '请填写商品现价',
        icon: 'none'
      });
      return;
    }
    if (app._isTextEmpty(json.market_price)) {
      wx.showToast({
        title: '请填写商品原价',
        icon: 'none'
      });
      return;
    }
    if (app._isTextEmpty(json.cost_price)) {
      wx.showToast({
        title: '请填写商品成本价',
        icon: 'none'
      });
      return;
    }
    if (app._isTextEmpty(json.stock)) {
      wx.showToast({
        title: '请填写库存',
        icon: 'none'
      });
      return;
    }

    if (this.data.store == 'store') {
      if (this.data.tab_privilege) {
        if (app._isTextEmpty(json.once_buy_limit)) {
          wx.showToast({
            title: '请输入每次限购数量',
            icon: 'none'
          });
          return;
        }
        if (app._isTextEmpty(json.total_buy_limit)) {
          wx.showToast({
            title: '请输入会员限购总数',
            icon: 'none'
          });
          return;
        }
        if (app._isTextEmpty(json.day_buy_limit)) {
          wx.showToast({
            title: '请输入会员每天限购总数',
            icon: 'none'
          });
          return;
        }

        if (app._isTextEmpty(json.week_buy_limit)) {
          wx.showToast({
            title: '请输入会员每周期限购数量',
            icon: 'none'
          });
          return;
        }
        if (app._isTextEmpty(json.month_buy_limit)) {
          wx.showToast({
            title: '请输入会员每月限购数量',
            icon: 'none'
          });
          return;
        }
      }
      
    }
    if(!this.data.is_tap){
      this.setData({
        is_tap:true
      });
    }else{
      console.log('#############################出去不要重复点击');
      return;
    }
    if (this.data.isType) {
      // 商品发布的提交 GET
      if (this.options.store == 'store') {
        urls = 'plugin.store-cashier.frontend.store.goods.add-goods';
      }else if (this.options.store == 'supplier') {
        urls = 'plugin.supplier.frontend.goods.add-goods';
      }
    }else {
      // 商品编辑的提交 POST
      json.goods_id = this.data.goods_id;
      if (this.data.store == 'store') {
        urls = 'plugin.store-cashier.frontend.store.goods.edit-goods';
      }else if (this.data.store == 'supplier') {
        urls = 'plugin.supplier.frontend.goods.edit-goods';
      }
    }
    
    console.log(json, '编辑的总数据');
    var that = this;
    let urlStr = app.getNetAddresss(urls);
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: json,
      success: function (resdata) {
        var res = resdata.data;
        that.setData({
          is_tap:false
        });
        if (res.result == 1) {
          wx.showToast({
            icon: 'none',
            title: that.data.isType?'上传商品成功':'编辑成功',
            duration: 1500
          });
          setTimeout(() => {
            wx.navigateBack({
              delta: 1
            });
          }, 2000);
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        that.setData({
          is_tap:false
        });
      }
    });
  }
});
