// pages/o2oSearch/o2oSearch.js
var location = require("../../../mybehaviors/location/location");

var app = getApp();
Page({
  behaviors: [location],

  /**
   * 页面的初始数据
   */
  data: {
    city: '',
    point: {
      lat: '',
      lng: ''
    },
    stores: [],
    inputs: "",
    //more
    isLoadMore: true,
    page: 1,
    total_page: 0,
    showPage: false,

    ok_subs_select: null, //筛选器确定请求的二级选项参数
    successLocation: false, //是否已定位成功，筛选器需要定位信息为参数
    isOptionMenuMore: false, //是否为筛选器的加载更多
    activeType: '',
    subs_select: '',
    openTemplate: 0, //门店列表样式
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.city && options.city !== '1') {
      if (options.city) {
        this.setData({
          city: options.city
        });
      }
      if (options.point) {
        this.setData({
          point: JSON.parse(options.point)
        });
      }
      this.gotoSearch();
    } else {
      this._getLocation(() => {this.gotoSearch();});
    }
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let point = JSON.stringify(this.data.point);
    return {
      title: '',
      path: '/packageC/o2o/o2oSearch/o2oSearch?city=' + this.data.city + '&point=' + point,
    };
  },
  gotoSearch() {
    var that = this;
    that.setData({
      page: 1,
      successLocation: true
    });
    let urlStr = app.getNetAddresss('plugin.store-cashier.frontend.store.goods.get-store-goods-by-title');
    let point = app.qqMapTransBMap(parseFloat(that.data.point.lng), parseFloat(that.data.point.lat));
    urlStr += '&kwd=' + (this.data.inputs ? this.data.inputs : '');
    urlStr += '&lng=' + point.lng;
    urlStr += '&lat=' + point.lat;
    urlStr += '&city_name=' + this.data.city;
    urlStr += '&page=' + this.data.page;
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            isLoadMore: true,
            total_page: res.data.last_page,
            openTemplate: res.data.store_list_style || 0
          });
          if (!that.data.total_page) {
            that.setData({
              total_page: 0
            });
          }
          that.setData({
            stores: res.data.data
          });
        } else {
          that.setData({
            stores: []
          });
        }
        that.setData({
          showPage: true
        });
      },
      fail: function (res) {
        console.log(res);
        that.setData({
          showPage: true
        });
      }
    });
  },
  //获取更多数据
  getMoreData() {
    var that = this;

    let _url = !this.data.isOptionMenuMore ? 'plugin.store-cashier.frontend.store.goods.get-store-goods-by-title' : 'plugin.store-cashier.frontend.store.goods.get-store-search';
    let urlStr = app.getNetAddresss(_url);

    let point = app.qqMapTransBMap(parseFloat(that.data.point.lng), parseFloat(that.data.point.lat));
    urlStr += '&lng=' + point.lng;
    urlStr += '&lat=' + point.lat;
    urlStr += '&city_name=' + this.data.city;

    if (this.data.isOptionMenuMore) {
      //筛选器的加载更多
      urlStr += '&is_type=' + this.data.activeType;
      if (this.data.activeType == "classify") {
        urlStr += '&category_id=' + this.data.subs_select;
      }
      if (this.data.activeType == "distance") {
        urlStr += '&is_distance=' + this.data.subs_select;
      }
    } else {
      urlStr += '&kwd=' + (this.data.inputs ? this.data.inputs : '');
    }

    this.setData({
      isLoadMore: false
    }); // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      console.log("111111111111111",this.data.page, this.data.total_page);
      return;
    } else {
      this.setData({
        page: this.data.page + 1
      });
      urlStr += '&page=' + this.data.page;
      app._getNetWork({
        url: urlStr,
        success: function (resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            that.setData({
              isLoadMore: true
            });
            var myData = res.data.data;
            that.setData({
              stores: that.data.stores.concat(myData)
            });
          } else {
            that.setData({
              page: that.data.page - 1,
              isLoadMore: false
            }); // 防止多次请求分页数据
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },
  inpbtn(e) {
    let val = e.detail.value;
    this.setData({
      inputs: val
    });
  },
  changeOptiones(e) {
    this.data.isOptionMenuMore = true;
    this.data.activeType = e.detail.activeType;
    this.data.subs_select = e.detail.subs_select;
    console.log(e.detail);
    let that = this;
    let point = app.qqMapTransBMap(parseFloat(that.data.point.lng), parseFloat(that.data.point.lat));
    let json = {
      lng: point.lng,
      lat: point.lat,
      city_name: this.data.city,
      is_type: e.detail.activeType
    };
    if (e.detail.activeType == "classify") {
      json.category_id = e.detail.subs_select;
    }
    if (e.detail.activeType == "distance") {
      json.is_distance = e.detail.subs_select;
    }
    that.setData({
      stores: []
    });
    let _url = "plugin.store-cashier.frontend.store.goods.get-store-search";
    let urlStr = app.getNetAddresss(_url);
    app._getNetWork({
      url: urlStr,
      data: json,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            isLoadMore: true,
            page:1,
            total_page: res.data.last_page
          });
          if (!that.data.total_page) {
            that.setData({
              total_page: 0
            });
          }
          that.setData({
            ok_subs_select: e.detail.subs_select,
            stores: res.data.data
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  }
});