// packageD/easyRefuel/easyRefuel.js\
var app = getApp();
var location = require("../../mybehaviors/location/location");
Page({
  behaviors: [location],

  /**
   * 页面的初始数据
   */
  data: {
    language: "",
    name: "",
    page: 1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let point = {};
    this._getLocation((mapdata, e) => {
      point = app.qqMapTransBMap(parseFloat(e.lng),parseFloat(e.lat));
      this.setData({
        lat: point.lat,
        lng: point.lng,
      })
      this.getData(point.lat, point.lng, this.data.city);
    });
  },
  search(e) {
    console.log(e.detail.value);
    this.setData({
      name: e.detail.value,
    });
  },
  gosearch() {
    let lat = this.data.lat;
    let lng = this.data.lng;
    wx.navigateTo({
      url: "/packageD/easyRefuel/easyRefuel_list/easyRefuel_list?lat=" +
        lat +
        "&lng=" +
        lng +
        "&name=" +
        this.data.name,
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let language = wx.getStorageSync("langIndex");
    this.setData({
      language: language.en
    });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},
  tolocation() {
    wx.navigateTo({
      url: "/packageC/o2o/o2oLocation/o2oLocation?tag=easyRefuel",
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  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 () {},
  getData(lat, lng, cityname) {
    let that = this;
    var urlStr = app.getNetAddresss("plugin.easy-refuel.frontend.index.index");
    var json = {
      lng: lng,
      lat: lat,
      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 {
          if (res.data.bindPhone == 1) {
            wx.showToast({
              icon: "none",
              title: res.msg,
              duration: 1000,
            });
            wx.navigateTo({
              url: "/packageA/member/editmobile/editmobile",
            });
          } else {
            wx.showToast({
              icon: "none",
              title: res.msg,
              duration: 1000,
              success: function () {
                setTimeout(() => {
                  wx.navigateBack({
                    delta: 1,
                  });
                }, 1000);
              },
            });
          }
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  getNextData() {
    let {lat, lng} = this.data.point;
    let cityname = this.data.city;
    let that = this;
    var urlStr = app.getNetAddresss("plugin.easy-refuel.frontend.index.index");
    var json = {
      lng: lng,
      lat: lat,
      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 {
          if (res.data.bindPhone == 1) {
            wx.showToast({
              icon: "none",
              title: res.msg,
              duration: 1000,
            });
            wx.navigateTo({
              url: "/packageA/member/editmobile/editmobile",
            });
          } else {
            wx.showToast({
              icon: "none",
              title: res.msg,
              duration: 1000,
              success: function () {
                setTimeout(() => {
                  wx.navigateBack({
                    delta: 1,
                  });
                }, 1000);
              },
            });
          }
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
});