// packageH/communityStoreDetails/communityStoreDetails.js
var app = getApp();
var location = require("../../../mybehaviors/location/location");
Page({
  behaviors: [location],
  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    communityStoreDetails: null,
    communityId: 0,
    Height: "340rpx",
    isMoreShow: false,
    stores: [],
    point: {},
    address: "",
    city: "",
    search: "",
    preSearch: "",
    currentPage: 1,
    totalPage: 0,

    iPnoneBottomBol: false,
    openTemplate: 0 //门店列表样式
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    this.getiPnoneBottom();
    try {
      //  this._initData();
      if (options.id) {
        this.setData({
          communityId: options.id,
        });
      } else if (!this.data.communityId) {
        wx.navigateTo({
          url: "/packageH/o2o/communityStore/communityStore",
        });
      }
      this._getLocation((mapdata, e) => {
        this._getData();
      });
    } catch (e) {
      console.error(e);
    }
  },
  initData() {
    this.setData({
      stores: [],
      totalPage: 0,
      currentPage: 1,
    });
  },
  _getData() {
    this.initData();
    let _that = this;
    let tempNoDatas = false;
    let urlStr = app.getNetAddresss("plugin.store-community.frontend.community.getInfo");
    // console.info(_that.data, '=---', !_that.data.city, _that.data.noDatas)
    if (!_that.data.communityId) {
      _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,
        community_id: _that.data.communityId,
        page: _that.data.currentPage,
        kwd: _that.data.search,
      },
      success: function (resdata) {
        let res = resdata.data;
        // console.info(resdata)
        if (res.result === 1) {
          console.info(res);
          let store_list = res.data.store_list;
          let datas = store_list ? store_list.data : [];
          if (datas.length === 0) {
            tempNoDatas = true;
          }
          if (res.data.info.explain) {
            res.data.info.explain = res.data.info.explain.replace(/<img/gi, '<img style="max-width:100%;height:auto;display:block"');
          }

          //res.data.info.explain = _that.delHtmlTag(res.data.info.explain);
          _that.setData({
            stores: datas,
            currentPage: store_list ? store_list.current_page : 0,
            totalPage: store_list ? store_list.last_page : 0,
            communityStoreDetails: res.data.info,
            noDatas: tempNoDatas,
          });
          wx.setNavigationBarTitle({
            title: res.data.plugin_name,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  tolocation() {
    const pages = getCurrentPages();
    const currentPage = pages[pages.length - 1];
    const url = `/${currentPage.route}?id=${this.data.communityId}`;
    wx.setStorageSync("communityStoreDetailsUrl", url);
    wx.navigateTo({
      url: "/packageC/o2o/o2oLocation/o2oLocation?tag=communityStoreDetails",
    });
  },
  getiPnoneBottom() {
    let that = this;
    try {
      var value = wx.getStorageSync('iPhoneBottom');
      if (value) {
        that.setData({
          iPnoneBottomBol: true
        });
      }
    } catch (e) {
      // Do something when catch error
    }
  },
  showCommunityIntroduction() {
    this.setData({
      show: true,
    });
  },
  popupClose() {
    this.setData({
      show: false,
    });
  },
  getStoresBySearch() {
    // console.info('搜索')
    let text = this.data.search.trim();
    if (text.trim() != this.data.preSearch) {
      this.setData({
        preSearch: text.trim(),
      });
      this._getData();
    }
  },
  delHtmlTag(str) {
    return str.replace(/<[^>]+>/g, "").replace(/&nbsp;/gi, "");
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

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

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

  _getMoreData() {
    let _that = this;
    let urlStr = app.getNetAddresss("plugin.store-community.frontend.community.getInfo");
    app._getNetWork({
      url: urlStr,
      data: {
        lng: _that.data.point.lng,
        lat: _that.data.point.lat,
        community_id: _that.data.communityId,
        page: _that.data.currentPage,
        kwd: _that.data.search,
      },
      success: function (resdata) {
        let res = resdata.data;

        if (res.result === 1) {
          console.info(res);
          let store_list = res.data.store_list;
          let nextData = store_list ? store_list.data : [];
          let datas = [..._that.data.stores, ...nextData];
          _that.setData({
            stores: datas,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.info(this.data.totalPage, this.data.currentPage);
    if (this.data.totalPage !== 0 && this.data.totalPage > this.data.currentPage) {
      this.setData({
        currentPage: this.data.currentPage + 1,
      });
      this._getMoreData();
    }
  },

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