import {
  getBanner,
  getIndexList
} from '../../services/index.js'
import {
  imgServerUrl,
  imgServerUrl_new
} from '../../config/config.js'
var app = getApp();

Page({
  data: {
    //banner图
    imgList: [],
    activity: [{
        src: "/images/happyjob/quanzhi.png",
        text: "全职工作"
      },
      {
        src: "/images/happyjob/jianzhi.png",
        text: "兼职工作"
      },
      {
        src: "/images/happyjob/fanxian.png",
        text: "入职奖励"
      },
      {
        src: "/images/happyjob/jipin.png",
        text: "高薪急聘"
      }
    ],
    activity_two: [{
        src: "/images/happyjob/fuli.png"
      },
      {
        src: "/images/happyjob/quanmin.png"
      }
    ],
    cityCode: '',
    keyWord: '',
    pageNo: 1, //当前分页
    isScroll: true, //是否可以滚动
    filter: -1, //岗位类型
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    list: [],
    showNoMore: "没有更多信息...", //显示已无更多
    broadcastList: [],
    imgServerUrl: imgServerUrl,
    hasLocation1: false,
  },
  onLoad: function(options) {
    if (!wx.getStorageSync('areaList')) {
      wx.setStorageSync('areaList', [{
          "countryCode": "",
          "countryName": "不限",
          "firstNameLetter": "X"
        }, {
          "countryCode": "320205",
          "countryName": "锡山区",
          "firstNameLetter": "X"
        },
        {
          "countryCode": "320206",
          "countryName": "惠山区",
          "firstNameLetter": "H"
        },
        {
          "countryCode": "320211",
          "countryName": "滨湖区",
          "firstNameLetter": "B"
        },
        {
          "countryCode": "320213",
          "countryName": "梁溪区",
          "firstNameLetter": "L"
        },
        {
          "countryCode": "320214",
          "countryName": "新吴区",
          "firstNameLetter": "X"
        },
        {
          "countryCode": "320281",
          "countryName": "江阴市",
          "firstNameLetter": "J"
        },
        {
          "countryCode": "320282",
          "countryName": "宜兴市",
          "firstNameLetter": "Y"
        }
      ])
    }
    this.start();
  },
  doArrow(e) {
    const index = e.currentTarget.dataset.index;
    const list = this.data.list;
    this.setData({
      [`list[${index}].up`]: !list[index].up
    })
  },
  reload() {
    this.onPullDownRefresh();
  },
  start() {
    this.fetchBanner();
    this.fetchList();
  },
  onShow: function() {
    var self = this;
    let cityName = wx.getStorageSync('city') || '无锡';
    let cityCode = wx.getStorageSync('cityCode') || '320200';
    this.setData({
      cityName: cityName,
      cityCode: cityCode
    })
    if (self.data.hasLocation1) {
      self.setData({
        pageNo: 1, //当前分页
        isScroll: true, //是否可以滚动
        filter: -1, //岗位类型
        list: [],
        hasLocation1: false
      })
      self.start();
    }
  },
  //下拉刷新
  onPullDownRefresh: function() {
    this.setData({
      pageNo: 1, //当前分页
      isScroll: true, //是否可以滚动
      filter: -1, //岗位类型
      list: [],
    })
    this.start();
    wx.stopPullDownRefresh();
  },
  //上拉加载
  onReachBottom: function() {
    var pageNo = this.data.pageNo + 1;
    this.setData({
      pageNo
    })
    this.fetchList();
  },

  onShareAppMessage: function(e) {

  },
  // 获取banner图
  fetchBanner() {
    let paramsObj = {
      application: 0
    }
    Object.assign(paramsObj);
    getBanner(paramsObj).then(data => {
      this.setData({
        imgList: data.data.bannerList,
        broadcastList: data.data.broadcastList
      })
    })
  },

  //获取热门列表数据
  fetchList(params) {
    if (!this.data.isScroll) {
      return false
    }
    let paramsObj = {
      cityCode: this.data.cityCode,
      pageNo: this.data.pageNo
    }
    Object.assign(paramsObj);

    getIndexList(paramsObj).then(data => {
      var pageNo = data.data.pageNo;
      let setData = {};
      // 是否可以滚动加载数据
      if (data.data.isLast) {
        setData.isScroll = false

      }
      if (data.data.hotPosition.length != 0) {
        setData.list = this.data.list.concat(data.data.hotPosition)
        setData.pageNo = pageNo;
      } else {
        setData.list = this.data.list;
      }
      this.setData({
        offline: false,
        ...setData
      })

    }).catch(() => {
      this.setData({
        offline: true
      })
    })
  },

  //改变列表
  changeList(e) {
    const {
      index
    } = e.currentTarget.dataset
    this.workType(index)
  },

  //判断当前工作类型
  workType(index) {
    switch (index) {
      case 0:
        wx.navigateTo({
          url: '../fulltime/index',
        })
        break;
      case 1:
        wx.navigateTo({
          url: '../parttime/index',
        })
        break;
      case 2:
        wx.navigateTo({
          url: '../reward/index',
        })
        break;
      case 3:
        wx.navigateTo({
          url: '../jipin/index',
        })
        break;
    }
  },

  changeList2(e) {
    const {
      index
    } = e.currentTarget.dataset
    this.workType2(index)
  },

  workType2(index) {
    switch (index) {
      case 0:
        wx.navigateTo({
          url: '../fuli/index',
        })
        break;
      case 1:
        wx.navigateTo({
          url: '../agent/index',
        })
        break;
    }
  },

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

  //轮播图跳转详情页
  toDetail_slide(e) {
    var id = e.currentTarget.dataset.id;
    var url = e.currentTarget.dataset.url;
    var arr;
    if (id) {
      arr = id.split(",");
      var positionId = arr[1];
      var cate = arr[0];
      wx.navigateTo({
        url: '../detail/index?hpPositionId=' + positionId + "&cate=" + cate,
      })
    } else if (url) {
      wx.navigateTo({
        url: '../web-view/web-view?url=' + url,
      })
    }
  },

  // 去搜索页
  toSearch() {
    wx.navigateTo({
      url: '../search/index',
    })
  },

  // 去城市选择页
  toCity() {
    wx.navigateTo({
      url: '../city/index',
    })
  },

  imageLoad() {
    var that = this
    var query = wx.createSelectorQuery()
    query.select('.slide-image').boundingClientRect()
    query.exec(function(res) {
      console.log(res)
      that.setData({
        swiperH: res[0].height
      })
    })
  }
})