var location = require("../../../../mybehaviors/location/location");
var app = getApp();
Component({
  behaviors: [location],

  properties: {
    params: {
      type: null,
    },
  },
  data: {
    stores: [],
    address: "",
    point: {
      lat: "",
      lng: "",
    },
    city: "",
    showMore: false,
    page: 1,
    total_page: 0,
    loading: true,
    language: "",
  }, // 私有数据，可用于模板渲染

  lifetimes: {
    // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
    attached() {
      let language = wx.getStorageSync("langIndex");
      this.setData({ language: language.en });
    },
    moved() {},
    detached() {},
  },

  // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
  attached() {}, // 此处attached的声明会被lifetimes字段中的声明覆盖
  ready() {
    this._getLocation(()=> {
      this.getStores();
    });
  },

  pageLifetimes: {
    // 组件所在页面的生命周期函数
    show() {},
    hide() {},
    resize() {},
  },

  methods: {
    toGood(e) {
      let id = e.currentTarget.dataset.goods_id;
      wx.navigateTo({
        url: "/packageA/detail_v2/detail_v2?id=" + id,
      });
    },
    getStores() {
      let that = this;
      let urlStr = app.getNetAddresss(
        "plugin.store-cashier.frontend.store.store.get-store-list-to-page"
      );
      let point = app.qqMapTransBMap(
        parseFloat(this.data.point.lng),
        parseFloat(this.data.point.lat)
      );
      urlStr += "&page=" + that.data.page;
      urlStr += "&lng=" + (point.lng ? point.lng : "");
      urlStr += "&lat=" + (point.lat ? point.lat : "");
      urlStr += "&kwd=";
      urlStr += "&city_name=" + this.data.city;
      urlStr += "&pagesize=" + this.data.params.num;
      urlStr += "&showshop=" + this.data.params.showshop;
      app._getNetWork({
        url: urlStr,
        success: function (resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            that.setData({
              showMore: true,
              stores: res.data.data,
            });
            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,
              });
            }
            if (that.data.page >= that.data.total_page) {
              that.setData({
                loading: false,
              });
            } else {
              that.setData({
                loading: true,
              });
            }
          }
        },
        fail: function (res) {
          console.log(res.msg);
        },
      });
    },
    toRouter(e) {
      let specify_show = e.currentTarget.dataset.items.specify_show;
      let item = e.currentTarget.dataset.items;
      if (specify_show == 1) {
        wx.navigateTo({
          url: "/packageC/o2o/o2oStore/o2oStore?store_id=" + item.id,
        });
      } else {
        if(item.store_decorate == 1) {
          wx.navigateTo({
            url: "/packageG/o2o/HomeSeller/HomeSeller?store_id=" + item.id,
          });
        }else {
          wx.navigateTo({
            url: "/packageC/o2o/HomeSeller/HomeSeller?store_id=" + item.id,
          });
        }
      }
    },
    //跳转至分类
    toClassify(e) {
      let id = e.currentTarget.dataset.id;
      wx.navigateTo({
        url:
          "/packageC/o2o/o2oCategory/o2oCategory?id=" +
          id +
          "&city=" +
          this.data.city +
          "&point=" +
          JSON.stringify(this.data.point),
      });
    },
    gophone(e) {
      let mobile = e.currentTarget.dataset.mobile;
      wx.makePhoneCall({
        phoneNumber: mobile,
      });
    },
    goToAdress(e) {
      let latitude = e.currentTarget.dataset.items.latitude;
      let longitude = e.currentTarget.dataset.items.longitude;
      let store_name = e.currentTarget.dataset.items.store_name;
      let point = app.BMapTransqqMap(
        parseFloat(longitude),
        parseFloat(latitude)
      );
      wx.openLocation({
        latitude: point.lat,
        longitude: point.lng,
        scale: 18,
        name: store_name,
      });
    },
    getMoreStores() {
      let that = this;
      if (this.data.page == this.data.total_page) {
        that.setData({
          loading: false,
        });
        return;
      }
      if (this.data.page > this.data.total_page) {
        that.setData({
          loading: false,
        });
        return;
      } else {
        that.setData({
          page: this.data.page + 1,
        });
        let urlStr = app.getNetAddresss(
          "plugin.store-cashier.frontend.store.store.get-store-list-to-page"
        );
        let point = app.qqMapTransBMap(
          parseFloat(this.data.point.lng),
          parseFloat(this.data.point.lat)
        );
        urlStr += "&page=" + this.data.page;
        urlStr += "&lng=" + (point.lng ? point.lng : "");
        urlStr += "&lat=" + (point.lat ? point.lat : "");
        urlStr += "&kwd=";
        urlStr += "&city_name=" + this.data.city;
        urlStr += "&pagesize=" + this.data.params.num;
        urlStr += "&showshop=" + this.data.params.showshop;
        app._getNetWork({
          url: urlStr,
          success: function (resdata) {
            var res = resdata.data;
            if (res.result == 1) {
              let stores = that.data.stores;
              that.setData({
                stores: stores.concat(res.data.data),
              });
              if (that.data.page == that.data.total_page) {
                that.setData({
                  loading: false,
                });
              }
            } else {
              that.setData({
                page: that.data.page - 1,
                loading: false,
              });
            }
          },
          fail: function (res) {
            console.log(res);
          },
        });
      }
    },
  },
});
