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

  /**
   * 页面的初始数据
   */
  data: {
    venueUrl : app.venueUrl,
    imgRoute  : '/route/file/downloadfile/true/',
    venueTypeParentId : 'df8b758c-f6d8-465a-9aa8-c451fde47076',
    page : '1',
    rows : '10',
    venueTypeList: [],
    venuesList: [],
    orderKey : '',
    venueType : '',
    latitude: '',
    longitude: ''
  },

  //获取定位
  getLocation: function () {
    var self = this;
    wx.getLocation({
      type: 'gcj02',
      success: function (res) {
        self.setData({
          latitude: res.latitude,
          longitude: res.longitude
        })
      },
      complete: function(){
        self.listPageVenuesInfo();
      }
    })
  },
  
  //获取场馆分类
  listVenueType : function(){
    var self = this;
    app.restAjax.get(app.restAjax.path('{venueUrl}/app/venuedictionary/listvenuedictionarybyparentidrelease/{venueTypeParentId}', 
      [app.venueUrl, self.data.venueTypeParentId]), {}, null, 
      function(code,data){
        self.setData({
          venueTypeList: data.data
        });
      },function(code, data){
        app.dialog.msg(data.msg);
      }
    );
  },

  //获取场馆列表
  listPageVenuesInfo : function(){
    var self = this;
    app.restAjax.get(app.restAjax.path('{venueUrl}/app/venuesinfo/listpagevenuesinforelease', [app.venueUrl]), 
      {
        page : self.data.page,
        rows : self.data.rows,
        orderKey : self.data.orderKey,
        venueType : self.data.venueType,
        latitude: self.data.latitude,
        longitude: self.data.longitude,
        selfLocation: 'TX',
      }, null,
      function(code, data){
        data.rows.forEach(element => {
          element.venuePanorama = element.venuePanorama.split(',')[0];
          if(element.apart >= 1000){
            element.apart = (element.apart / 1000).toFixed(2) + 'km'; 
          } else {
            element.apart += 'm';
          }
        });
        self.setData({
          venuesList : data.rows,
          page : data.page
        });
      }, function(code,data){
        app.dialog.msg(data.msg);
      },function(){
        wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh();
      }
    );
  },

  //切换场馆类型
  changeVenueType: function(options){
    var id = options.currentTarget.dataset.id;
    if(id == this.data.venueType){
      id = ''
    }
    this.setData({
      venueType : id
    })
    this.listPageVenuesInfo();
  },

  //切换场馆排序
  changeVenuesOrderBy: function(options){
    var beCheck = options.currentTarget.dataset.becheck;
    this.setData({
      orderKey : beCheck
    })
    this.listPageVenuesInfo();
  },

  //去详情页
  goDetail: function (options) {
    var venuesInfoId = options.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../venueDetail/venueDetail?venuesInfoId=' + venuesInfoId,
    })
  },

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

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

  },

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

  },

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

  },

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

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading();
    this.setData({
      venueType: '',
      orderKey: '',
    })
    this.onLoad();
  },

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

  },

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

  }
})