// pages/o2oCategory/o2oCategory.js
var location = require("../../../mybehaviors/location/location");

var app = getApp();
Page({
  behaviors: [location],
  /**
   * 页面的初始数据
   */
  data: {
    id: "",
    city: "",
    point: {
      lat: "",
      lng: "",
    },
    page: 1,
    total_page: 0,
    stores: [],
    loading: false,
    allLoaded: false,
    isLoadMore: true,
    openTemplate: 0 //门店列表样式
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    if(options.name){
      this.setData({
        title:options.name
      });
      wx.setNavigationBarTitle({
        title: options.name,
      });
    }
    if (options.id) {
      this.setData({
        id: options.id,
      });
    }
    if (options.city) {
      this.setData({
        city: options.city,
        point: JSON.parse(options.point),
      });
      this.getStoresByCategoryIdLngAndLat();
    } else {
      this._getLocation(() => {this.getStoresByCategoryIdLngAndLat();});
    }
  },

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

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

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

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

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

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

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  getStoresByCategoryIdLngAndLat() {
    let that = this;
    let urlStr = app.getNetAddresss(
      "plugin.store-cashier.frontend.store.store.get-store-list-by-category-id-to-page"
    );
    let point = app.qqMapTransBMap(
      parseFloat(this.data.point.lng),
      parseFloat(this.data.point.lat)
    );
    if (!point.lng) {
      (point.lng = ""), (point.lat = "");
    }
    urlStr += "&category_id=" + this.data.id;
    urlStr += "&lng=" + point.lng;
    urlStr += "&lat=" + point.lat;
    urlStr += "&city_name=" + this.data.city;
    urlStr += "&page=" + this.data.page;
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            stores: res.data.data,
            openTemplate: res.data.store_list_style || 0
          });
          if (res.data.last_page) {
            that.setData({
              total_page: res.data.last_page,
            });
          } else if (res.data.lastPage) {
            that.setData({
              total_page: res.data.lastPage,
            });
          }
        } else {
          that.setData({
            stores: [],
          });
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1000,
          });
          setTimeout(() => {
            wx.navigateBack({
              delta: 1
            });
          }, 1500);
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  getMoreData() {
    var that = this;
    if (this.data.page == this.data.total_page) {
      return;
    }
    if (this.data.page >= this.data.total_page) {
      that.setData({
        loading: true,
        allLoaded: true,
      });
      return;
    } else {
      this.setData({
        page: this.data.page + 1,
      });
      let urlStr = app.getNetAddresss(
        "plugin.store-cashier.frontend.store.store.get-store-list-by-category-id-to-page"
      );
      let point = app.qqMapTransBMap(
        parseFloat(this.data.point.lng),
        parseFloat(this.data.point.lat)
      );
      urlStr += "&category_id=" + this.data.id;
      urlStr += "&lng=" + point.lng;
      urlStr += "&lat=" + point.lat;
      urlStr += "&city_name=" + this.data.city;
      urlStr += "&page=" + this.data.page;
      app._getNetWork({
        url: urlStr,
        success: function (resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            that.setData({
              loading: false,
              allLoaded: false,
            });
            let stores = that.data.stores;
            that.setData({
              stores: stores.concat(res.data.data),
            });
          } else {
            that.setData({
              page: that.data.page - 1,
              loading: true,
              allLoaded: true,
              isLoadMore: false,
            });
          }
        },
        fail: function (res) {
          console.log(res);
        },
      });
    }
  },
});
