// packageD/easyRefuel/easyRefuel_list/easyRefuel_list.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    language: "",
    name: "",
    page: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      name: options.name,
    });
    this.getData();
  },
  gosearch(e) {
    this.getData();
  },
  search(e) {
    console.log(e);
    this.setData({
      name: e.detail.value,
    });
  },
  getData() {
    let points = wx.getStorageSync("address-point");
    let lat = points.point.lat;
    let lng = points.point.lng;
    let cityname = points.city;
    let that = this;
    var urlStr = app.getNetAddresss(
      "plugin.easy-refuel.frontend.index.searchByName"
    );
    var json = {
      lng: lng,
      lat: lat,
      name: that.data.name,
      cityName: cityname,
      page: that.data.page,
    };
    app._postNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          for (let i = 0; i < res.data.data.data.length; i++) {
            res.data.data.data[i].distance =
              res.data.data.data[i].distance.toFixed(2) + `km`;
            for (let k = 0; k < res.data.data.data[i].prices.length; k++) {
              res.data.data.data[i].prices[k].muns = (
                Number(res.data.data.data[i].prices[k].discountPrice) -
                Number(res.data.data.data[i].prices[k].countryPrice)
              ).toFixed(2);
            }
          }
          that.setData({
            banner: res.data.banner,
            datas: res.data.data.data,
            shopName: res.data.shopName,
            last_page: res.data.data.last_page,
            current_page: res.data.data.current_page,
          });
          wx.setNavigationBarTitle({
            title: res.data.shopName,
          });
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1000,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  getNextData() {
    let points = wx.getStorageSync("address-point");
    let lat = points.point.lat;
    let lng = points.point.lng;
    let cityname = points.city;
    let that = this;
    var urlStr = app.getNetAddresss(
      "plugin.easy-refuel.frontend.index.searchByName"
    );
    var json = {
      lng: lng,
      lat: lat,
      name: that.data.name,
      cityName: cityname,
      page: that.data.page,
    };
    app._postNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          for (let i = 0; i < res.data.data.data.length; i++) {
            res.data.data.data[i].distance =
              res.data.data.data[i].distance.toFixed(2) + `km`;
            for (let k = 0; k < res.data.data.data[i].prices.length; k++) {
              res.data.data.data[i].prices[k].muns = (
                Number(res.data.data.data[i].prices[k].discountPrice) -
                Number(res.data.data.data[i].prices[k].countryPrice)
              ).toFixed(2);
            }
          }
          that.setData({
            banner: res.data.banner,
            datas: that.data.datas.concat(res.data.data.data),
            shopName: res.data.shopName,
            last_page: res.data.data.last_page,
            current_page: res.data.data.current_page,
          });
          wx.setNavigationBarTitle({
            title: res.data.shopName,
          });
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1000,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let language = wx.getStorageSync("langIndex");
    this.setData({ language: language.en });
  },

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

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

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

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

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.current_page >= this.data.last_page) {
      wx.showToast({
        title: "暂无更多",
        duration: 1000,
        icon: "none",
      });
    } else {
      this.setData({
        page: this.data.page + 1,
      });
      this.getNextData();
    }
  },

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