// pages/parttime/index.js
import {
  getParttimeList
} from '../../services/index.js'
import {
  imgServerUrl} from '../../config/config.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgServerUrl: imgServerUrl,
    list: [],
    pageNo: 1, //当前分页
    isScroll: true, //是否可以滚动
    showNoMore: "没有更多信息...", //显示已无更多
    cityName: '无锡',
    cityCode: '320200',
    selectId: -1, //选中筛选的条件
    settlementMethod: [{
      index: -1,
      name: '不限'
    },
    {
      index: 2,
      name: '日结'
    },
    {
      index: 1,
      name: '周结'
    },
    {
      index: 0,
      name: '月结'
    },
    {
      index: 3,
      name: '完工结'
    },
    {
      index: 4,
      name: '其他'
    },
    ], //结算方式(1：正序；2：倒序)
    settleme: -1, //结算方式索引
    settle: '', //结算方式
    sort: '', //距离排序（1：正序；2：倒序）
    longitude: '', //经度
    latitude: '', //纬度
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    countryCode: ''
  },

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

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

  },
  start() {
    let cityName = wx.getStorageSync('city') || '无锡';
    let cityCode = wx.getStorageSync('cityCode') || '320200';
    let areaList = wx.getStorageSync('areaList') || [];
    this.setData({
      cityName: cityName,
      cityCode: cityCode,
      areaList,
    }, _ => {
      this.parttimeList();
    })
  },

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

  },

  //获取兼职列表数据
  parttimeList() {
    if (!this.data.isScroll) {
      return false
    }
    let paramsObj = {
      longitude: this.data.longitude,
      latitude: this.data.latitude,
      sort: this.data.sort,
      settlementMethod: this.data.settle,
      cityCode: this.data.cityCode,
      countryCode: this.data.countryCode,
      pageNo: this.data.pageNo
    }
    Object.assign(paramsObj);

    getParttimeList(paramsObj).then(data => {
      var pageNo = data.data.pageNo;
      let setData = {}
      // 是否可以滚动加载数据
      if (data.data.isLast) {
        setData.isScroll = false
      }
      if (data.data.parttimePosition.length != 0) {
        setData.list = this.data.list.concat(data.data.parttimePosition)
        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.parttimeList();
    } 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();
              }
            }
          })
        }
      })
    }
  },

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

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

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

  },
  chooseArea(e) {
    const { countryCode } = e.detail;
    this.setData({
      countryCode,
      list: [],
      pageNo: 1, //当前分页
      isScroll: true,
    }, _ => {
      this.parttimeList()
    })
  },
  doDistince: function (e) {
    const { sort, settle } = e.detail;
    this.setData({
      sort,
      settle, //重置
      pageNo: 1, //当前分页
      isScroll: true, //是否可以滚动
      list: [],
      showAreaList: false
    }, _ => {
      this.cxgps();
    })

  },

  // 结算方式
  doSettlemethod: function (e) {
    const { settle, sort } = e.detail;
    this.setData({
      settle,
      sort,
      longitude: "",
      latitude: "",
      pageNo: 1, //当前分页
      isScroll: true, //是否可以滚动
      list: [],
    }, _ => {
      this.parttimeList();
    })


  },
  //去详情页
  toDetail(e) {
    const {
      id,
      cate
    } = e.detail;
    wx.navigateTo({
      url: '../detail/index?hpPositionId=' + id + "&cate=" + cate,
    })
  },
})