var app = getApp();
var $ = require('../../../js/util.js');
var QQMapWX = require('../../../js/qqmap-wx-jssdk.min.js');
var qqmapsdk;
Page({
  data: {
    user: [],
    list: [],
    cat_list: [],
    catid:0,
    page: 1,//当前页码
    param: {
      catid: 0,
      page: 1,
      is_pt:1
    },
    location_n: '',
    shop_name: '',
    time: '',
    small_title: '',
    promat_list: [],
    current_catid:0,
    hasMore: false,//无更多数据提示
    dataNull: true//未获取到数据提示
  },
  onLoad: function (options) {//接收上个页面传的参数
    if (options.invite_key) {
      app.globalData.invite_key = options.invite_key;
    }
  },
  onReady: function () {
    var self = this;
    var animation = wx.createAnimation({
      duration: 500,
      transformOrigin: "50% 50%",
      timingFunction: 'ease',
    })
    this.animation = animation;
    qqmapsdk = new QQMapWX({
      key: 'JFNBZ-ZGIKU-ESEVG-4RQGG-U5UUK-42B6B'
    });

    var location_n = app.globalData.location_n;
    if (location_n) {
      self.setData({
        location_n: location_n
      });
    } else {
      app.HCgetlocation(function (ret) {
        qqmapsdk.reverseGeocoder({
          location: {
            latitude: ret.latitude,
            longitude: ret.longitude
          },
          coord_type: 1,
          success: function (res) {
            if (res.message == 'query ok') {
              app.globalData.location_n = res.result.formatted_addresses.recommend;
              self.setData({
                location_n: res.result.formatted_addresses.recommend
              });
              app.HCsetHCshopid(function (res) {
                self.getshopname();
              })
            }
          },
        });
      });
    }
  },
  onShow: function () {
    var self = this;
    var location_n = app.globalData.location_n;
    if (location_n) {
      self.setData({
        location_n: location_n
      });
    } else {
      app.HCgetlocation(function (ret) {
        qqmapsdk.reverseGeocoder({
          location: {
            latitude: ret.latitude,
            longitude: ret.longitude
          },
          coord_type: 1,
          success: function (res) {
            if (res.message == 'query ok') {
              app.globalData.location_n = res.result.formatted_addresses.recommend;
              self.setData({
                location_n: res.result.formatted_addresses.recommend
              });
              app.getUserInfo(function (ret) {
                if (ret) {
                  app.HCsetHCshopid(function (res) {
                    self.getshopname();
                  })
                }
              });

            }
          },
        });
      });
    }
    if (app.globalData.HCshopid) {
      self.getshopname();
    }
    app.HCgetStorage('initlist' + self.data.time, function (ret) {
      if (ret) {
        self.setData({
          list: [],
        })
      }
      self.getIndex();

    });
  },
  getshopname: function () {
    var self = this;
    var param = new Object;
    param.shop_id = app.globalData.HCshopid;
    var action = 'c=Index&a=get_shop_name';
    param = $.ObjectToStr(param);
    app.ajaxRequest(action, 'GET', param, function (ret) {
      if (ret.status == 1) {
        self.setData({
          shop_name: ret.data
        })
      }
    })
  },
  getIndex: function () {
    var param = new Object;
    param.catid = this.data.param.catid
    param.page = this.data.param.page
    param.is_pt = this.data.param.is_pt
    param.uu = wx.getStorageSync('rank_id') ? wx.getStorageSync('rank_id') : 1;
    //$.ObjectToStr(self.data.param)
    var self = this;
    var action = 'c=Index&a=get_fight_group';
    app.showLoading('加载中...');
    app.ajaxRequest(action, 'GET', $.ObjectToStr(param), function (ret) {
      if (ret.status == 1) {
        $.init_page_list(self, ret.data.list);
      } else if (ret.status == 2) {
        $.init_page_empty_list(self, ret.msg);
      }
      self.setData({
        cate_list: ret.data.cat_list,
      })
    });
  },
  onReachBottom: function () {
    var self = this;
    if (self.data.user != '') {
      self.data.page++;
      self.setData({
        param: {
          page: self.data.page,
        }
      });
      self.getIndex();
    }
  },
  GotoCont: function (event) {
    app.HCnavigateTo('../fightgroup/cont/cont?id=' + event.currentTarget.dataset.id + '&name=' + event.currentTarget.dataset.goodsname);
  },
  get_cate: function (event){
    var self=this;
    var catid = event.currentTarget.dataset.id;
    var param=self.data.param;
    param.catid = catid;
    self.setData({
      param:param,
      current_catid: catid
    })
    self.getIndex();
  }
})