const { default: DPagination } = require("../../../utils/DPagination");
const { getProjectNameLang, getLocation } = require("../common");

// packageH/project_verification/SearchProject/SearchProject.js
const App = getApp();
const GetProjectsUrl = App.getNetAddresss("plugin.store-projects.frontend.project.get-list");
const ProjectPagiation = new DPagination("projects");
let Location = null;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    projects: {},
    projectName: "",
    projectNameLang: getProjectNameLang(),
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    ProjectPagiation.bind(this);
    Location = await getLocation();
    this.getProjects();
    wx.setNavigationBarTitle({
      title: "搜索" + getProjectNameLang(),
    });
  },

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

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

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

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

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

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

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  projectNameInputChanged({ detail }) {
    this.setData({
      projectName: detail,
    });
  },
  clearInputProjectName() {
    this.setData({
      projectName: "",
    });
    this.searchProject();
  },
  searchProject() {
    ProjectPagiation.clean();
    this.getProjects();
  },
  getProjects() {
    if (ProjectPagiation.finished || ProjectPagiation.loading) {
      return;
    }
    wx.showLoading();
    ProjectPagiation.loading = true;
    let point = App.qqMapTransBMap(parseFloat(Location.point.lng), parseFloat(Location.point.lat));
    App._getNetWork({
      url: GetProjectsUrl,
      data: {
        lng: point.lng,
        lat: point.lat,
        name: this.data.projectName,
        page: ProjectPagiation.loadPage,
      },
      success: ({ data: { data: response, result, msg } }) => {
        ProjectPagiation.loading = false;
        wx.hideLoading();
        if (result === 0) {
          wx.showToast({
            title: msg,
            icon: "none",
          });
          return;
        }
        if (response.current_page == response.last_page || response.data.length < response.per_page) {
          ProjectPagiation.finished = true;
        }
        ProjectPagiation.push(response.data);
      },
    });
  },
});
