// packageH/communityStore/communityStore.js
var app = getApp();
var location = require("../../../mybehaviors/location/location");
Page({
  behaviors: [location],
  /**
   * 页面的初始数据
   */
  data: {
    currentPage: 1,
    totalPage: 0,
    commmunityStoreList: [],
    search: "",
    preSearch: "",
    noDatas: false,
    point: {},
    address: "",
    city: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this._getLocation((mapdata, e) => {
      this._getData();
    });
  },
  chooseCommunityStore(e) {
    wx.navigateTo({
      url: "/packageH/o2o/communityStoreDetails/communityStoreDetails?id=" + e.currentTarget.dataset.id,
    });
  },
  showcarrfoot(e) {
    let bol = true;
    this.setData({
      showcarrfootBol: bol,
    });
  },
  showcarrfoot_ipx(e) {
    let bol = true;
    this.setData({
      showcarrfoot_ipxBol: bol,
    });
  },


  initData() {
    this.setData({
      commmunityStoreList: [],
      totalPage: 0,
      currentPage: 1,
    });
  },

  _getData() {
    this.initData();
    let _that = this;
    let urlStr = app.getNetAddresss("plugin.store-community.frontend.community.getList");
    console.log(urlStr, "urlStr");
    if (!_that.data.city) {
      _that.setData({
        noDatas: true,
      });
      return;
    }
    // console.error(_that.data.lat)
    app._getNetWork({
      url: urlStr,
      data: {
        lng: _that.data.point.lng,
        lat: _that.data.point.lat,
        city_name: _that.data.city,
        page: _that.data.currentPage,
        kwd: _that.data.search,
      },
      success: function (resdata) {
        let res = resdata.data;
        // console.info(resdata)
        if (res.result === 1 && res.data.list.data.length > 0) {
          let datas = res.data.list.data;
          _that.setData({
            commmunityStoreList: datas,
            totalPage: res.data.list.last_page,
            currentPage: res.data.list.current_page,
            noDatas: false,
          });
          wx.setNavigationBarTitle({
            title: res.data.plugin_name,
          });
          // console.info(res)
        } else {
          _that.setData({
            noDatas: true,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },

  tolocation() {
    wx.navigateTo({
      url: "/packageC/o2o/o2oLocation/o2oLocation?tag=communityStore",
    });
  },
  getCommunityStoreListBySearch() {
    // console.info(this.data.search, '搜索')
    let text = this.data.search.trim();
    if (text.trim() != this.data.preSearch) {
      this.setData({
        preSearch: text.trim(),
      });
      this._getData();
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

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

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

  _getMoreData() {
    let _that = this;
    let urlStr = app.getNetAddresss("plugin.store-community.frontend.community.getList");
    console.log(urlStr, "urlStr");

    // console.error(_that.data.lat)
    app._getNetWork({
      url: urlStr,
      data: {
        lng: _that.data.point.lng,
        lat: _that.data.point.lat,
        city_name: _that.data.city,
        page: _that.data.currentPage,
        kwd: _that.data.search,
      },
      success: function (resdata) {
        let res = resdata.data;
        // console.info(resdata)
        if (res.result === 1 && res.data.list.data.length > 0) {
          let datas = [..._that.data.commmunityStoreList, ...res.data.list.data];
          _that.setData({
            commmunityStoreList: datas,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.totalPage != 0 && this.data.totalPage > this.data.currentPage) {
      console.info("还有数据可以请求");
      this.setData({
        currentPage: this.data.currentPage + 1,
      });
      this._getMoreData();
    } else {
      console.info("没有数据可以请求");
    }
    // console.info(this.data.totalPage, this.data.currentPage, '底部')
  },

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