// pages/fulltime/index.js
import {
  getFullList
} from '../../services/index.js'
import {
  imgServerUrl} from '../../config/config.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    pageNo: 1, //当前分页
    isScroll: true, //是否可以滚动
    showNoMore: "没有更多信息...", //显示已无更多
    cityCode: '320200',
    cityName: '无锡',
    selectId: -1, //选中筛选的条件
    totalSalary: '', //薪资待遇排序(1：正序；2：倒序)
    sort: '', //距离排序（1：正序；2：倒序）
    longitude: '', //经度
    latitude: '', //纬度
    imgServerUrl: imgServerUrl,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    countryCode: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    this.setData({
      pageNo: 1, //当前分页
      isScroll: true, //是否可以滚动
      list: [],
      selectId: -1, //选中筛选的条件
      totalSalary: '', //薪资待遇排序(1：正序；2：倒序)
      sort: '', //距离排序（1：正序；2：倒序）
      longitude: "",
      latitude: "",
      showAreaList: false
    }, _ => {
      this.start();
    })
  },

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

  },
  start() {
    let cityName = wx.getStorageSync('city') || '无锡';
    let cityCode = wx.getStorageSync('cityCode') || '320200';
    let areaList = wx.getStorageSync('areaList') || [];
    // let countryCode = wx.getStorageSync('countryCode');

    this.setData({
      cityName: cityName,
      cityCode: cityCode,
      areaList,
    }, _ => {
      this.fullList();
    })
  },

  //获取全职列表数据
  fullList() {
    if (!this.data.isScroll) {
      return false
    }
    let paramsObj = {
      longitude: this.data.longitude,
      latitude: this.data.latitude,
      sort: this.data.sort,
      totalSalary: this.data.totalSalary,
      cityCode: this.data.cityCode,
      countryCode: this.data.countryCode,
      pageNo: this.data.pageNo
    }

    getFullList(paramsObj).then(data => {
      var pageNo = data.data.pageNo;
      let setData = {}
      // 是否可以滚动加载数据
      if (data.data.isLast) {
        setData.isScroll = false
      }
      if (data.data.fullPosition.length != 0) {
        setData.list = this.data.list.concat(data.data.fullPosition)
        setData.pageNo = pageNo;
      } else {
        setData.list = this.data.list;
      }
      this.setData({
        ...setData
      })

    })
  },

  //获取当前定位
  cxgps: function () {
    var that = this;
    wx.showLoading({
      title: 'loading',
      mask: true
    });
    var distance = wx.getStorageSync('distance');
    if (distance) {
      wx.hideLoading();
      that.setData({
        longitude: distance.longitude,
        latitude: distance.latitude
      }, _ => {
        that.fullList();
      })
    } else {
      wx.getLocation({
        success:data=>{
          console.log(data);
          wx.hideLoading();
          that.setData({
            longitude: data.longitude,
            latitude: data.latitude
          }, _ => {
            wx.setStorageSync('distance', data);
            that.fullList();
          })
        },
        fail:()=>{
          wx.hideLoading();
          wx.showModal({
            title: '授权提示',
            content: '当前服务需要获取您的位置信息，请确认授权',
            confirmText: '去授权',
            cancelText: '暂不授权',
            success: function (res) {
              if (res.confirm) {
                wx.openSetting();
              }
            }
          })
        }
      })
    }
  },

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

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      pageNo: 1, //当前分页
      isScroll: true, //是否可以滚动
      list: [],
      selectId: -1, //选中筛选的条件
      totalSalary: '', //薪资待遇排序(1：正序；2：倒序)
      sort: '', //距离排序（1：正序；2：倒序）
      longitude: "",
      latitude: "",
      showAreaList: false
    }, _ => {
      this.fullList();
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var pageNo = this.data.pageNo + 1;
    this.setData({
      pageNo
    }, _ => {
      this.fullList();
    })
  },

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

  },
  chooseArea(e) {
    const countryCode = e.detail.countryCode;
    this.setData({
      countryCode,
      list: [],
      pageNo: 1, //当前分页
      isScroll: true,
    }, _ => {
      this.fullList()
    })
  },

  doSalay: function (e) {
    const { totalSalary, sort } = e.detail;
    this.setData({
      totalSalary,
      sort, //重置
      pageNo: 1, //当前分页
      isScroll: true, //是否可以滚动
      list: [],
      longitude: "",
      latitude: "",
    }, _ => {
      this.fullList();
    })

  },
  doDistince: function (e) {
    const { sort, totalSalary } = e.detail;
    this.setData({
      sort,
      totalSalary, //重置
      pageNo: 1, //当前分页
      isScroll: true, //是否可以滚动
      list: [],
    }, _ => {
      this.cxgps();
    })
  },
  //去详情页
  toDetail(e) {
    const {
      id,
      cate
    } = e.detail;
    wx.navigateTo({
      url: '../detail/index?hpPositionId=' + id + "&cate=" + cate,
    })
  },
})