// packageC/hotel/HotelSearch/HotelSearch.js
var location = require("../../../mybehaviors/location/location");
var app = getApp();
Page({
  behaviors: [location],
  /**
	 * 页面的初始数据
	 */
  data: {
    SEARCH_URL: "plugin.hotel.frontend.hotel.goods.get-hotel-goods-by-title",
    address: "",
    point: {
      lat: '',
      lng: ''
    },
    city: '',
    title: '',
    start_time: '',
    end_time: '',
    price: '',
    star: '',
    category: '',
    loading: false,
    allLoaded: false,
    goload: true,
    isLoadMore: true,
    page: 1,
    total_page: 0,
    list_arr: [],
    search_value: "",
    toolbar: false,
    PageNameList: {}
  },

  /**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function (options) {
    if (options.start) {
      this.setData({
        start_time: options.start
      });
    }
    if (options.star) {
      this.setData({
        star: options.star
      });
    }
    if (options.end) {
      this.setData({
        end_time: options.end
      });
    }
    if (options.price) {
      this.setData({
        price: options.price
      });
    }
    if (options.category) {
      this.setData({
        category: options.category
      });
    }

    this.getCustomizeHotelHead();
    this._getLocation(()=> {this.search();});
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

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

  },

  //附近搜索
  search() {
    var that = this;
    let urlStr = app.getNetAddresss(that.data.SEARCH_URL);
    urlStr += '&lng=' + this.data.point.lng;
    urlStr += '&lat=' + this.data.point.lat;
    urlStr += '&city_name=' + that.data.city;
    urlStr += '&page=' + this.data.page;
    urlStr += '&entrance_time=' + (this.data.start_time ? this.data.start_time : '');
    urlStr += '&price=' + (this.data.price ? this.data.price : '');
    urlStr += '&star_rated=' + (this.data.star ? this.data.star : '');
    urlStr += '&category_id=' + (this.data.category ? this.data.category : '');
    urlStr += '&leave_at=' + (this.data.end_time ? this.data.end_time : '');
    urlStr += '&kwd=' + (this.data.search_value ? this.data.search_value : '');
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            list_arr: res.data.data,
            total_page: res.data.last_page
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  //获取酒店自定义字段
  getCustomizeHotelHead() {
    let that = this;
    let isQuestHotel = "";
    try {
      const value = wx.getStorageSync('customizeHotelHead');
      if (value) {
        isQuestHotel = value;
        // Do something with return value
      }
    } catch (e) {
      // Do something when catch error
    }
    if (!app._isTextEmpty(isQuestHotel)) {
      this.setData({
        PageNameList: JSON.parse(isQuestHotel)
      });
      wx.setNavigationBarTitle({
        title: this.data.PageNameList.hotels + '搜索页'
      });
    } else {
      let urlStr = app.getNetAddresss("plugin.hotel.frontend.hotel.get-hotel-info.get-custom-name");
      app._getNetWork({
        url: urlStr,
        success: function (resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            that.setData({
              PageNameList: res.data
            });
            wx.setNavigationBarTitle({
              title: (res.data.PageNameList.hotels + '搜索页') ? (res.data.PageNameList.hotels + '搜索页') : ''
            });
            try {
              wx.setStorageSync('customizeHotelHead', JSON.stringify(res.data));
            } catch (e) {
              console.log(e);
            }
          }

        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },
  gotoSearch() {
    this.initData();
    this.search();
  },
  initData() {
    this.setData({
      page: 1,
      total_page: 0,
      goload: true,
      loading: true,
      allLoaded: false,
      isLoadMore: true,
      list_arr: []
    });
  },
  searchMore() {
    var that = this;
    if (this.data.page == this.data.total_page) {
      return;
    }
    if (this.data.page >= this.data.total_page) {
      this.setData({
        loading: true,
        allLoaded: true
      });
      return;
    } else {
      this.setData({
        page: this.data.page + 1
      });
      let urlStr = app.getNetAddresss(that.data.SEARCH_URL);
      urlStr += '&lng=' + this.data.point.lng;
      urlStr += '&lat=' + this.data.point.lat;
      urlStr += '&city_name=' + this.data.city;
      urlStr += '&page=' + this.data.page;
      urlStr += '&entrance_time=' + (this.data.start_time ? this.data.start_time : '');
      urlStr += '&price=' + (this.data.price ? this.data.price : '');
      urlStr += '&star_rated=' + (this.data.star ? this.data.star : '');
      urlStr += '&category_id=' + (this.data.category ? this.data.category : '');
      urlStr += '&leave_at=' + (this.data.end_time ? this.data.end_time : '');
      urlStr += '&kwd=' + (this.data.search_value ? this.data.search_value : '');
      app._getNetWork({
        url: urlStr,
        success: function (resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            that.setData({
              loading: false,
              allLoaded: false,
              list_arr: that.data.list_arr.concat(res.data.data) //数组拼接
            });
          } else {
            that.setData({
              page: that.data.page - 1,
              loading: true,
              allLoaded: true,
              isLoadMore: false
            });
            return;
          }

        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },
  seainp(e) {
    let val = e.detail.value;
    this.setData({
      search_value: val
    });
  }
});