// packageE/appointment/hot_technician/hot_technician.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    gold: {},
    second: {},
    three: {},
    kwd: "",
    search: false,
    // point: {
    //   lng: 113.25861276415654,
    //   lat: 23.152827312294523
    // },
    // city: "广州市",

    projectList: [],

    //more
    isLoadMore: true,
    page: 1,
    total_page: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      appointmentLang: wx.getStorageSync('yz_basic_info').lang.appointment,
      city: options.city,
      point: JSON.parse(options.point)
    });
    wx.setNavigationBarTitle({
      title: "热门" + this.data.appointmentLang.worker
    });
    this.init();
    this.getData();
  },

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

  },

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

  },

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

  },

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

  },

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      console.log('没有更多数据');
    }
  },

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

  },
  bind(e) {
    this.setData({
      kwd: e.detail.value
    });
  },
  init() {
    this.setData({
      kwd: '',
      page: 1,
      isLoadMore: false,
      total_page: 0,
      projectList: [],
      search: false,
      gold: {},
      second: {},
      three: {}
    });
    this.kwd = "";
    this.page = 1;
    this.isLoadMore = false;
    this.total_page = 0;
    this.projectList = [];
    this.search = false;
    this.gold = {};
    this.second = {};
    this.three = {};
  },
  getData() {
    let that = this;
    let urlStr = app.getNetAddresss("plugin.appointment.frontend.worker.get-workers-by-name");
    app._getNetWork({
      url: urlStr,
      data: {
        lng: this.data.point.lng,
        lat: this.data.point.lat,
        city_name: this.data.city,
        kwd: this.data.kwd,
        page: 1
      },
      success: (resdata) => {
        var response = resdata.data;
        that.setData({
          search: this.data.kwd ? true : false
        });
        if (response.result == 1) {
          that.setData({
            isLoadMore: true,
            total_page: response.data.last_page,
            projectList: response.data.data,
            gold: response.data.data[0],
            second: response.data.data[1],
            three: response.data.data[2],
          });
          if (!this.data.second) {
            this.setData({
              second: {}
            });
          }
          if (!this.data.three) {
            this.setData({
              three: {
                name: "null",
                has_one_store: {},
                tags: []
              }
            });
          }
        } else {
          wx.showToast({
            title: response.msg,
            icon: 'none'
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  // 获取更多数据
  getMoreData() {
    this.setData({
      isLoadMore: false
    });
    // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      let page = this.data.page + 1;
      this.setData({
        page
      });
      let urlStr = app.getNetAddresss('plugin.appointment.frontend.worker.get-workers-by-name');
      app._getNetWork({
        url: urlStr,
        data: {
          lng: this.data.point.lng,
          lat: this.data.point.lat,
          city_name: this.data.city,
          kwd: this.data.kwd,
          page: this.data.page
        },
        success: (resdata) => {
          var response = resdata.data;
          if (response.result == 1) {
            var myData = response.data.data.concat(this.data.commentList);
            this.setData({
              commentList: myData
            });
          } else {
            wx.showToast({
              title: response.msg,
              icon: 'none'
            });
            let pages = this.data.page - 1;
            this.setData({
              page: pages,
              isLoadMore: false
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },
  goBack() {
    let pages = getCurrentPages();
    if (pages.length <= 1) {
      wx.reLaunch({
        url: '/packageG/index/index',
      });
    } else {
      wx.navigateBack({ //返回
        delta: 1
      });
    }
  },
  gotoWorker(e) {
    let id = e.currentTarget.dataset.id;
    let point = JSON.stringify(this.data.point);
    wx.navigateTo({
      url: '/packageE/appointment/technician_details/technician_details?worker_id=' + id + '&point=' + point,
    });
  },
});