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

  /**
   * 页面的初始数据
   */
  data: {
    appointmentLang: {},
    status: "1",
    category: {},
    show1: false,
    show2: false,
    className: [],
    url: "",
    json: {},
    sort: "asc",
    point: {},

    kwd: "",
    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.project + '/' + this.data.appointmentLang.worker
    });
    this.init();
    this.getClass();

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

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

  },
  goProject(e) {
    let id = e.currentTarget.dataset.id;
    let point = JSON.stringify(this.data.point);
    wx.navigateTo({
      url: '/packageE/appointment/ProjectDetails/ProjectDetails?project_id=' + id + '&point=' + point,
    });
  },
  toTechnicianDetails(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,
    });
  },
  bind(e) {
    this.setData({
      kwd: e.detail.value
    });
  },
  goBack() {
    let pages = getCurrentPages();
    if (pages.length <= 1) {
      wx.reLaunch({
        url: '/packageG/index/index',
      });
    } else {
      wx.navigateBack({ //返回
        delta: 1
      });
    }
  },
  getClass() {
    let that = this;
    let urlStr = app.getNetAddresss("plugin.appointment.frontend.project-category.get-list");
    app._getNetWork({
      url: urlStr,
      data: {},
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            className: res.data
          });
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  close() {
    this.setData({
      show1: false
    });
  },
  evaluateBtn() {
    if (this.data.sort == "asc") {
      this.setData({
        sort: "desc"
      });
    } else {
      this.setData({
        sort: "asc"
      });
    }
    this.setData({
      page: 1,
      isLoadMore: true,
      total_page: 0,
      show1: false
    });
    this.getData();
  },
  selectType(e) {
    let type = e.currentTarget.dataset.type;
    if (type == this.data.status) {
      this.setData({
        show2: false
      });
      return;
    }
    this.setData({
      status: type,
      projectList: []
    });
    this.getData();
    if (this.data.status === "2") {
      this.setData({
        category: {},
      });
    }
    this.setData({
      show2: false,
    });
  },
  showProject() {
    this.setData({
      show1: false,
      show2: !this.data.show2
    });
  },
  showClass() {
    if (this.data.status === "2") {
      return;
    }
    this.setData({
      show2: false,
      show1: !this.data.show1
    });
  },
  init() {
    this.setData({
      projectList: [],
      kwd: '',
      category: {},
      status: '1',
      show1: false,
      show2: false,
      sort: 'asc',
      isLoadMore: true,
      total_page: 0,
      'json.page': 1
    });
  },
  selectionBtn(e) {
    let item = e.currentTarget.dataset.item;
    if (!item.name) {
      this.setData({
        category: {}
      });
    } else {
      this.setData({
        category: item
      });
    }
    this.setData({
      page: 1,
      isLoadMore: true,
      total_page: 0,
      show1: false
    });
    this.getData();
  },

  getData() {
    this.setData({
      json: {},
    });
    if (this.data.status === "1") {
      this.setData({
        'json.sort': this.data.sort,
        'json.category_id': this.data.category.id,
        'json.name': this.data.kwd,
        'json.page': 1,
        url: app.getNetAddresss("plugin.appointment.frontend.project.get-list")
      });
    } else {
      this.setData({
        'json.lng': this.data.point.lng,
        'json.lat': this.data.point.lat,
        'json.city_name': this.data.city,
        'json.kwd': this.data.kwd,
        'json.page': 1,
        url: app.getNetAddresss("plugin.appointment.frontend.worker.get-workers-by-name")
      });
    }
    app._getNetWork({
      url: this.data.url,
      data: this.data.json,
      success: (resdata) => {
        var response = resdata.data;
        if (response.result == 1) {
          this.setData({
            isLoadMore: true,
            total_page: response.data.last_page,
            projectList: response.data.data
          });
          if (!this.data.total_page) {
            this.setData({
              total_page: 0
            });
          }
        } 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
      });
      app._getNetWork({
        url: this.data.url,
        data: this.data.json,
        success: (resdata) => {
          var response = resdata.data;
          if (response.result == 1) {
            var myData = response.data.data.concat(this.data.projectList);
            this.setData({
              projectList: 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);
        }
      });
    }
  },
});