// pages/manage_shop/manage_shop.js
var app = getApp();
Page({

  /**
	 * 页面的初始数据
	 */
  data: {
    language: '',
    datas: []
  },

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

  /**
	 * 生命周期函数--监听页面初次渲染完成
	 */
  onReady: function() {
    let language = wx.getStorageSync('langIndex');
    this.setData({ 'language': language.en});
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },
  //获取数据
  getData() {
    let that = this;
    let urlStr = app.getNetAddresss('plugin.micro.frontend.controllers.MicroShop.get-goods.index');
    app._getNetWork({
      url: urlStr,
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            datas: res.data.list
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  delteteItem(e) {
    let item = e.currentTarget.dataset.item;
    let index = e.currentTarget.dataset.index;
    let that = this;
    let urlStr = app.getNetAddresss('plugin.micro.frontend.controllers.MicroShop.del-goods.index');
    urlStr += '&id=' + item.id;
    wx.showModal({
      title: '提示',
      content: '是否删除该商品？',
      success(res) {
        if (res.confirm) {
          app._getNetWork({
            url: urlStr,
            success: function(resdata) {
              var res = resdata.data;
              if (res.result == 1) {
                let datas = that.data.datas;
                datas.splice(index, 1);
                that.setData({
                  datas: datas
                });
                wx.showToast({
                  icon: 'none',
                  title: '操作成功',
                  duration: 1500
                });
              } else {
                wx.showToast({
                  icon: 'none',
                  title: res.msg,
                  duration: 1500
                });
              }
            },
            fail: function(res) {
              console.log(res);
            }
          });
        } else if (res.cancel) {
          console.log('用户点击取消');
        }
      }
    });
  },
  toItem(e) {
    let goods = e.currentTarget.dataset.goods;
    wx.navigateTo({
      url: '/packageA/detail_v2/detail_v2?id=' + goods.id
    });
  }
});
