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

  /**
   * 页面的初始数据
   */
  data: {
    show1: false,
    sort: "asc",
    category: {id:''},
    name: "",
    // more
    isLoadMore: true,
    page: 1,
    total_page: 0,
    titleInfo: "", // 标题
    commentList: [],
    categoryList: [],
    appointmentLang: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      appointmentLang: wx.getStorageSync('yz_basic_info').lang.appointment
    });
    wx.setNavigationBarTitle({
      title: this.data.appointmentLang.project + "列表"
    });
    this.init();
    this.search();
    this.getCategoryList();
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  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({
      name:e.detail.value
    });
  },
  search(e) {
    
    let urlStr = app.getNetAddresss('plugin.appointment.frontend.project.get-list');
    app._getNetWork({
      url: urlStr,
      data:{  
        sort: this.data.sort,
        category_id: this.data.category.id,
        name: this.data.name,
        page: this.data.page
      },
      success: (resdata) => {
        var response = resdata.data;
        if (response.result == 1) {
          this.setData({
            isLoadMore: true,
            total_page: response.data.last_page,
            commentList: 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
      });
      let urlStr = app.getNetAddresss('plugin.appointment.frontend.project.get-list');
      app._getNetWork({
        url: urlStr,
        data:{
          sort: this.data.sort,
          category_id: this.data.category.id,
          name: this.data.name,
          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);
        }
      });
    }
  },
  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.search();
  },
  goProjectDetails(e){
    let id = e.currentTarget.dataset.id;
    let point = JSON.stringify(wx.getStorageSync('address-point').point);
    wx.navigateTo({
      url: '/packageE/appointment/ProjectDetails/ProjectDetails?project_id=' + id + '&point=' + point,
    });
  },
  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.search();
  },
  toIndex() {
    wx.navigateTo({
      url: '/packageE/appointment/appointment_index'
    });
  },
  toUrl() {
    var str = JSON.stringify(this.data.appointmentLang);
    wx.navigateTo({
      url: '/packageE/appointment/mine/mine?appointmentLang=' + str,
    });
  },
  showClass() {
    this.setData({
      show1:!this.show1
    });
  },
  close(){
    this.setData({
      show1: false
    });
  },
  enterSearch(event) {
    this.setData({
      page:1,
      isLoadMore:true,
      total_page:0,
    });
    this.search();
  },
  getCategoryList() {
    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({
            categoryList: res.data
          });
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  init() {
    this.setData({
      category:{},
      name:'',
      sort:'asc',
      show1:false, 
      page:1,
      isLoadMore:true,
      total_page:0,
      commentList:[],
    });
  },
});