// pages/member/GoodsBrand/GoodsBrand.js
var app = getApp();
// var WxParse = require('../../../../wxParse/wxParse.js');
Page({

  /**
	 * 页面的初始数据
	 */
  data: {
    language: '',
    id: '',
    goodsList: [],
    total_page: 0,
    isLoadMore: true,
    page: 1,
    brandTit: "",
    brandLogo: "",
    desc: "",
  },

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

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

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

  },

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

  },

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

  },

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

  },

  /**
	 * 页面上拉触底事件的处理函数
	 */
  onReachBottom: function() {
    if (this.data.isLoadMore) {
      this._getMoreData();
    } else {
      console.log("没有更多数据");
    }
  },

  /**
	 * 用户点击右上角分享
	 */
  onShareAppMessage: function() {
    return {
      path: "/packageB/member/goods/GoodsBrand/GoodsBrand?id=" + this.data.id,
    };
  },
  _getData() {
    let that = this;
    let urlStr = app.getNetAddresss("goods.brand.get-brand-goods");
    urlStr += '&id=' + this.data.id;
    app._getNetWork({
      url: urlStr,
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            goodsList: res.data.goods.data,
            total_page: res.data.goods.total
          });
          for(let i = 0;i<res.data.goods.data.length;i++){
            try{
              if (Number(res.data.goods.data[i].market_price) == 0) {
                that.data.goodsList[i].notMarket_price = 0;
              } else {
                that.data.goodsList[i].notMarket_price = 1;
              }
              that.setData({
                goodsList:that.data.goodsList
              });
            }catch(err){
              console.log(err);
            }
          }
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  _getInfo() {
    let that = this;
    let urlStr = app.getNetAddresss("goods.brand.get-brand-detail");
    urlStr += '&id=' + this.data.id;
    app._getNetWork({
      url: urlStr,
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          let desc = res.data.desc.replace(/<img/gi, '<img style="max-width:100%;height:auto;display:block"');
          that.setData({
            brandTit: res.data.name,
            brandLogo: res.data.logo,
            desc
          });
          // let article = that.data.desc;
          /**
					 * WxParse.wxParse(bindName , type, data, target,imagePadding)
					 * 1.bindName绑定的数据名(必填)
					 * 2.type可以为html或者md(必填)
					 * 3.data为传入的具体数据(必填)
					 * 4.target为Page对象,一般为this(必填)
					 * 5.imagePadding为当图片自适应是左右的单一padding(默认为0,可选)
					 */
          // WxParse.wxParse('article', 'html', article, that);
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  _getMoreData() {
    let that = this;
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      that.setData({
        page: this.data.page + 1
      });
      let urlStr = app.getNetAddresss("goods.brand.get-brand-goods");
      urlStr += '&id=' + this.data.id;
      urlStr += '&page=' + this.data.page;
      app._getNetWork({
        url: urlStr,
        success: function(resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            let myData = res.data.goods.data;
            if (myData.length > 0) {
              let goodsList = that.data.goodsList;
              that.setData({
                goodsList: 	goodsList.concat(myData)
              });
            } else {
              that.setData({
                isLoadMore: false
              });
            }
          } else {
            that.setData({
              page: that.data.page - 1,
            });
          }
        },
        fail: function(res) {
          console.log(res);
        }
      });
    }
  },
  gotoDetail(e) {
    var goodsid = e.currentTarget.dataset.goodsid;
    wx.navigateTo({
      url: '/packageA/detail_v2/detail_v2?id=' + goodsid
    });
  }
});
