const { default: DPagination } = require("../../../utils/DPagination");
const { getProjectNameLang } = require("../common");
var location = require("../../../mybehaviors/location/location");
// packageH/project_verification/VerificationProjectList/VerificationProjectList.js
const App = getApp();
const NearbyProjectPagination = new DPagination("nearbyProjects");
let Location = null;
const ProjectLangName = getProjectNameLang();
Page({
  behaviors: [location],
  /**
   * 页面的初始数据
   */
  data: {
    carousels: [],
    recommendProjects: [],
    categories: [],
    category_10: [],
    nearbyProjects: [],
    projectNameLang: getProjectNameLang(),
    showLocation: false,
    Height: 0,
    LastHeight: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    this._getLocation((res,point) => {
      Location = {
        point: point
      };

      this.setData({
        showLocation: true,
      });
      wx.setNavigationBarTitle({
        title: ProjectLangName + "列表",
      });
      NearbyProjectPagination.bind(this);
      // NearbyProjectPagination.clean();
      this.getPageData();
      this.getNearbyProjects();
    });
  },

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

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

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

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

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

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

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  imgHeight: function (e) {
    var winWid = wx.getSystemInfoSync().windowWidth; //获取当前屏幕的宽度
    var imgh = e.detail.height; //图片高度
    var imgw = e.detail.width; //图片宽度
    var swiperH = winWid * imgh / imgw + "px"; //等比设置swiper的高度。 即 屏幕宽度 / swiper高度 = 图片宽度 / 图片高度  ==》swiper高度 = 屏幕宽度 * 图片高度 / 图片宽度
    if (parseInt(swiperH) > this.data.LastHeight) {
      this.setData({
        Height: swiperH, //设置高度
      });
      this.data.LastHeight = parseInt(swiperH);
    }
  },
  getPageData() {
    const url = App.getNetAddresss("plugin.store-projects.frontend.index.get-data");
    let point = App.qqMapTransBMap(parseFloat(Location.point.lng), parseFloat(Location.point.lat));
    App._getNetWork({
      url,
      data: {
        lng: point.lng,
        lat: point.lat,
      },
      success: ({ data: { data } }) => {
        this.setData({
          carousels: data.carousels,
          recommendProjects: data.projects,
          categories: data.project_category_list || [],
        });

        let category_10 = this.chunk(this.data.categories, 10);
        this.setData({
          category_10: category_10,
        });
      },
    });
  },
  chunk(arr, n) {
    var result = [];
    for (var i = 0, len = arr.length; i < len; i += n) {
      result.push(arr.slice(i, i + n));
    }
    return result;
  },
  goCategory(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/packageH/project_verification/CategoryProjectList/CategoryProjectList?category_id='+id,
    });
  },
  getNearbyProjects() {
    if (NearbyProjectPagination.finished || NearbyProjectPagination.loading) {
      return;
    }
    wx.showLoading();
    NearbyProjectPagination.loading = true;
    const url = App.getNetAddresss("plugin.store-projects.frontend.project.get-list");
    let point = App.qqMapTransBMap(parseFloat(Location.point.lng), parseFloat(Location.point.lat));
    App._getNetWork({
      url,
      data: {
        lng: point.lng,
        lat: point.lat,
        page: NearbyProjectPagination.loadPage,
      },
      success: ({ data: { data: response } }) => {
        wx.hideLoading();
        if (response.last_page == response.current_page || response.data.length < response.per_page) {
          NearbyProjectPagination.finished = true;
        }
        NearbyProjectPagination.push(response.data);
        NearbyProjectPagination.loading = false;
      },
      fail() {
        wx.hideLoading();
        NearbyProjectPagination.loading = false;
      },
    });
  },
});
