// pages/fuli/index.js
import {
  getWelfareList
} from '../../services/index.js'
import {
  imgServerUrl} from '../../config/config.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    pageNo: 1, //当前分页
    isScroll: true, //是否可以滚动
    showNoMore: "没有更多信息...", //显示已无更多
    cityName: '无锡',
    activityWide: '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: 0, //结算方式索引
    settle: '', //结算方式
    imgServerUrl: imgServerUrl,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
  },

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

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

  },
  start() {
    let cityName = wx.getStorageSync('city') || '无锡';
    let cityCode = wx.getStorageSync('cityCode') || '320200';
    let areaList = [
      {
        countryCode: "000000",
        countryName: "全国"
      },
      {
        countryCode: "320200",
        countryName: "无锡"
      },
      {
        countryCode: "320500",
        countryName: "苏州"
      },
      {
        countryCode: "310000",
        countryName: "上海"
      },
      {
        countryCode: "440100",
        countryName: "广州"
      },
      {
        countryCode: "320100",
        countryName: "南京"
      },
      {
        countryCode: "110000",
        countryName: "北京"
      },
      {
        countryCode: "440300",
        countryName: "深圳"
      },
      {
        countryCode: "510100",
        countryName: "成都"
      },
      {
        countryCode: "330100",
        countryName: "杭州"
      },
      {
        countryCode: "320400",
        countryName: "常州"
      },
      {
        countryCode: "320900",
        countryName: "盐城"
      },
    ];
    this.setData({
      cityName: cityName,
      activityWide: cityCode,
      areaList
    }, _ => {
      this.welfareList();
    })

  },
  chooseArea(e) {
    const activityWide = e.detail.countryCode;
    this.setData({
      activityWide,
      list: [],
      pageNo: 1, //当前分页
      isScroll: true,
    }, _ => {
      this.welfareList()
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  //获取福利岗位列表数据
  welfareList() {
    if (!this.data.isScroll) {
      return false
    }
    let paramsObj = {
      settlementMethod: this.data.settle,
      activityWide: this.data.activityWide,
      pageNo: this.data.pageNo,
    }
    Object.assign(paramsObj);

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

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

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

  },

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

  },
  // 结算方式
  doSettlemethod: function (e) {
    const { settle } = e.detail;
    this.setData({
      settle,
      pageNo: 1, //当前分页
      isScroll: true, //是否可以滚动
      list: [],
    }, _ => {
      this.welfareList();
    })
  },
  //去详情页
  toDetail(e) {
    const {
      id,
      cate
    } = e.detail
    wx.navigateTo({
      url: '../detail/index?hpPositionId=' + id + "&cate=" + cate,
    })
  },
})