// packageH/o2o/storeConsumeIndex/storeConsumeIndex.js
const app = getApp();
var location = require("../../../mybehaviors/location/location");

// var qqmapsdk;
Page({
  behaviors: [location],

  /**
   * 页面的初始数据
   */
  data: {
    isvalue: 1,
    cityName: "",
    page: 1,
    current_page: 1,
    last_page: 1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // let city = wx.getStorageSync('storeConsume').city;
    // this.setData({
    //   cityName: city
    // })
    this.setData({
      id: options.id,
    });
    if (options.addressData) {
      let addressData = JSON.parse(options.addressData);
      this.setData({
        "point.lat": addressData.latitude,
        "point.lng": addressData.longitude,
        address: addressData.title,
        title: addressData.title,
        city: addressData.city,
      });
      // this.getStorePage();
      this.getdata();
    } else {
      this._getLocation(() => {this.getdata();});
    }
  },


  tapShow(e) {
    console.log(e);
    let id = this.data.isInfo.hold_id;
    let store_id = e.currentTarget.dataset.item.store_id;
    wx.navigateTo({
      url:
        "/packageH/o2o/storeConsumeShow/storeConsumeShow?id=" +
        id +
        "&store_id=" +
        store_id,
    });
  },
  tapLoaction() {
    wx.navigateTo({
      url:
        "/packageC/o2o/o2oLocation/o2oLocation?tag=storeConsume" +
        "&id=" +
        this.data.id,
    });
  },
  searchTxt(e) {
    console.log(e);
    this.setData({
      store_name: e.detail.value,
    });
  },
  goSearch(e) {
    this.setData({
      page: 1,
    });
    this.getdata();
  },
  blurgoSearch(e) {
    if (app._isTextEmpty(this.data.store_name)) {
      return;
    }
    this.setData({
      page: 1,
    });
    this.getdata();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

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

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

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

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.current_page >= this.data.last_page) {
      console.log("没有更多");
      return;
    } else {
      let pages = this.data.page + 1;
      this.setData({
        page: pages,
      });
      this.getMoreData();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  getdata() {
    wx.showLoading({
      title: "加载中",
    });
    let that = this;
    let urlStr = app.getNetAddresss(
      "plugin.store-card.Frontend.Modules.Card.Controllers.detail.index"
    );
    let point = app.qqMapTransBMap(
      parseFloat(that.data.point.lng),
      parseFloat(that.data.point.lat)
    );
    app._postNetWork({
      url: urlStr,
      data: {
        card_id: that.data.id,
        lat: point.lat,
        lng: point.lng,
        store_name: that.data.store_name,
        page: that.data.page,
      },
      success: (resdata) => {
        let res = resdata.data;
        wx.hideLoading({
          success: (res) => {},
        });
        if (res.result == 1) {
          console.log(res);
          that.setData({
            isInfo: res.data,
            page_list: res.data.page_list.data,
            current_page: res.data.page_list.current_page,
            last_page: res.data.page_list.last_page,
          });
        } else {
          wx.showToast({
            title: res.msg,
            duration: 1000,
            icon: "none",
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      },
    });
  },
  getMoreData() {
    wx.showLoading({
      title: "加载中",
    });
    let that = this;
    let urlStr = app.getNetAddresss(
      "plugin.store-card.Frontend.Modules.Card.Controllers.detail.index"
    );
    let point = app.qqMapTransBMap(
      parseFloat(that.data.point.lng),
      parseFloat(that.data.point.lat)
    );
    app._postNetWork({
      url: urlStr,
      data: {
        card_id: that.data.id,
        lat: point.lat,
        lng: point.lng,
        store_name: that.data.store_name,
        page: that.data.page,
      },
      success: (resdata) => {
        let res = resdata.data;
        wx.hideLoading({
          success: (res) => {},
        });
        if (res.result == 1) {
          let nexdata = res.data.page_list.data;
          that.setData({
            page_list: Object.assign(that.data.page_list, nexdata),
            current_page: res.data.page_list.current_page,
            last_page: res.data.page_list.last_page,
          });
        } else {
          wx.showToast({
            title: res.msg,
            duration: 1000,
            icon: "none",
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      },
    });
  },
});
