import {
  getBanner,
  getIndexList
} from '../../services/index.js'
import {
  imgServerUrl
} from '../../config/config.js'
const app = getApp()
Page({
  data: {
    //banner图
    imgList: [],
    activity: [{
        src: imgServerUrl + "/images/happyjob/quanzhi.png",
        text: "全职工作"
      },
      {
        src: imgServerUrl + "/images/happyjob/jianzhi.png",
        text: "兼职工作"
      },
      {
        src: imgServerUrl + "/images/happyjob/fanxian.png",
        text: "入职奖励"
      },
      {
        src: imgServerUrl + "/images/happyjob/jipin.png",
        text: "高薪急聘"
      }
    ],
    activity_two: [{
        src: imgServerUrl + "/images/happyjob/fuli.png"
      },
      {
        src: imgServerUrl + "/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: true,
    disclaimer: false, //是否展示防欺诈弹窗 默认不弹出
  },
  onLoad: function () {
    // 如果传进来的值是 '1' 则展示防欺诈弹窗
    if (!wx.getStorageSync('disclaimer')) {
      wx.hideTabBar({
        success: () => {
          this.setData({
            disclaimer: true
          })
        }
      })
    }
    this.setData({
      cityName: wx.getStorageSync('city') || '无锡',
      cityCode: wx.getStorageSync('cityCode') || '320200',
      pageNo: 1, //当前分页
      isScroll: true, //是否可以滚动
      filter: -1, //岗位类型
      list: [],
    }, _ => {
      this.start();
    })
  },
  reload() {
    this.onPullDownRefresh();
  },
  start() {
    this.fetchBanner();
    this.fetchList();
  },
  onShow: function () {
    if (wx.getStorageSync('city') !== this.data.cityName) {
      this.setData({
        cityName: wx.getStorageSync('city') || '无锡',
        cityCode: wx.getStorageSync('cityCode') || '320200',
        pageNo: 1, //当前分页
        isScroll: true, //是否可以滚动
        filter: -1, //岗位类型
        list: [],
      }, _ => {
        this.start();
      })
    }
  },
  //下拉刷新
  onPullDownRefresh: function () {
    this.setData({
      pageNo: 1, //当前分页
      isScroll: true, //是否可以滚动
      filter: -1, //岗位类型
      list: [],
    }, _ => {
      this.start();
    })
  },
  //上拉加载
  onReachBottom: function () {
    var pageNo = this.data.pageNo + 1;
    this.setData({
      pageNo
    })
    this.fetchList();
  },

  onShareAppMessage: function () {

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

  //获取热门列表数据
  fetchList() {
    if (!this.data.isScroll) {
      return false
    }

    let paramsObj = {
      cityCode: this.data.cityCode,
      pageNo: this.data.pageNo
    }

    getIndexList(Object.assign(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)
    if (index === 1) {
      wx.aldPushSubscribeMessage({
        eventId: '5ea271177739104342928e99'
      });
    }
  },

  //判断当前工作类型
  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.detail;
    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',
    })
  },
  // 知晓规则
  knowDisclaimer() {
    this.setData({
      disclaimer: false
    })
    wx.showTabBar({
      success: _ => {
        wx.setStorageSync('disclaimer', true)
      }
    })
  },
  privacySubmit(e) {
    console.log('privacySubmit', e)
    console.log(this.data.disclaimer)
  },
  privacyCancel(){
    console.log(this.data.disclaimer)
  }
})