var location = require("../../../../mybehaviors/location/location");
var app = getApp();
Component({
  behaviors: [location],
  properties: {
    params: {
      type: null
    },
    index: {
      type: null
    },
    data: {
      type: null
    },
    designer: {
      type: null
    },
    sessionurl: {
      type: null
    },
    Identification: {
      type: null
    },
    grouid: {
      type: null
    },
    total: {
      type: null
    },
  },
  data: {
    language: '',
    showMore: true,
    page: 1,
    total_page: 0,
    isLoadMore: true,
    goodsShow:true
  }, // 私有数据，可用于模板渲染

  lifetimes: {
    // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
    attached() {
      let language = wx.getStorageSync('langIndex');
      this.setData({ 'language': language.en});

      this._getLocation(() => {this.getdata();});
      // this.setData({
      // 	total_page: Math.ceil(this.data.total / 12)
      // })
      // if (this.data.page >= this.data.total_page) {
      // 	this.setData({
      // 		isLoadMore: false
      // 	})
      // }
    },
    moved() {},
    detached() {},
  },

  // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
  attached() {

  }, // 此处attached的声明会被lifetimes字段中的声明覆盖
  ready() {

  },

  pageLifetimes: {
    // 组件所在页面的生命周期函数
    show() {},
    hide() {},
    resize() {},
  },

  methods: {

    gotoDetail(e) {
      let id = e.currentTarget.dataset.good.goodid;
      wx.navigateTo({
        url: '/packageA/detail_v2/detail_v2?id=' + id
      });
    },
    // 获取更多数据
    getMoreData() {
      let that = this;
      console.log(that.data.current_page, that.data.last_page);
      if (that.data.current_page >= that.data.last_page) {
        return;
      } else {
        that.setData({
          page: that.data.page + 1
        });
        let ressa = wx.getStorageSync("address-point");
        console.log(that.data.params.display_num);
        let urlStr = app.getNetAddresss('plugin.nearby-store-goods.frontend.controllers.designer.get-goods');
        let point = app.qqMapTransBMap(parseFloat(ressa.point.lng), parseFloat(ressa.point.lat));
        urlStr += '&page=' + that.data.page;
        urlStr += "&lng=" + point.lng;
        urlStr += "&lat=" + point.lat;
        urlStr += "&city_name=" + ressa.city;
        urlStr += "&display_num=" + that.data.params.displaynum;
        app._getNetWork({
          url: urlStr,
          success: function(resdata) {
            var res = resdata.data;
            if (res.result == 1) {
              let data = that.data.data;
              that.setData({
                data: data.concat(res.data.data),
                current_page: res.data.current_page,
                last_page: res.data.last_page
              });
              if (that.data.current_page <= that.data.last_page) {
                that.setData({
                  isLoadMore: false
                });
              } else if (that.data.current_page > that.data.last_page){
                that.setData({
                  isLoadMore: true
                });
              }
            } else {
              that.setData({
                page: that.data.page - 1,
                isLoadMore: false
              });
            }
          },
          fail: function(res) {
            console.log(res);
          }
        });
      }
    },
    getdata(){
      var that =this;
      let ressa = wx.getStorageSync("address-point");
      let point = {};
      console.log(that.data.params.display_num);
      let urlStr = app.getNetAddresss('plugin.nearby-store-goods.frontend.controllers.designer.get-goods');
      if(ressa) {
        point = app.qqMapTransBMap(parseFloat(ressa.point.lng), parseFloat(ressa.point.lat));
      }else {
        point = {lng:'',lat:''};
      }

      urlStr += '&page=' + that.data.page;
      urlStr += "&lng=" + point.lng;
      urlStr += "&lat=" + point.lat;
      urlStr += "&city_name=" + ressa.city;
      urlStr += "&display_num=" + that.data.params.displaynum;
      app._getNetWork({
        url: urlStr,
        success: function (resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            var isdata = [];
            that.setData({
              data: isdata.concat(res.data.data),
              current_page: res.data.current_page,
              last_page: res.data.last_page
            });
            wx.setStorageSync('fjstore', res.data.data);
            if(res.data.current_page < res.data.last_page){
              that.setData({
                isLoadMore: true
              });
            } else if (res.data.current_page >= res.data.last_page){
              that.setData({
                isLoadMore: false
              });
            }
            console.log(res.data.data);
            if(res.data.data <= 0){
              that.setData({
                goodsShow:false
              });
            }
            console.log(res);
            console.log(that.data.isLoadMore);
          } else {
            that.setData({
              page: that.data.page - 1,
              isLoadMore: false
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },
  observers: {
    'params': function() {
      console.log(this.data.params);
      this.setData({
        showMore: true,
        page: 1,
        total_page: 0,
        isLoadMore: true
      });
      this.setData({
        total_page: this.data.total
      });
      console.log(this.data.total_page);
      if (this.data.page >= this.data.total_page) {
        this.setData({
          isLoadMore: false
        });
      }
    }
  }
});
