const app = getApp();

const commonUtil = require("../../utils/commonUtil.js");
Page({
	data: {
    recordShow: false,
		toped: "1",
		recordList: [],
		storeJobListSearchForm: {
			pageNum: 1,
			pageSize: 20,
			classify: 1,
			sex: -1,
			workTypeStr: "",
			lat: "",
			lng: "",
			jobClassify: "",
			sortTag: 0,
			jobSpecialLabelIds: "",
			cityName: "",
			brandIds: "",
			jobCategoryLabelIds: "",
			ujc: 1,
		},
		loading: true,
		hasMoreData: false, //下拉是否还有更多数据
		inputShowed: false,
		inputVal: "搜索工作",
		wxCode: "",
		isCollect: false,
		collectTxt: "收藏",
		searchCityParamStorage: {
			name: "",
			shortName: "",
		},
	
		pageShow: false,
		whichOneShow: "",
	
  },
  goBack(){
    wx.navigateBack({
      delta: -1,
    })
  },
  onShow(){
  
    var that = this;
    that.setData({
      recordList: [],
    });
    that.getJobList();
  },
  goDetail(e){
    console.log(e);
		var that = this;
			wx.navigateTo({
				url: "../detail/index?storeJobId=" + e.currentTarget.dataset.id + "&wxCode=" + that.data.wxCode,
			});
	
  },

	getJobList() {
    var that = this;
    // debugger
    that.setData({
      pageShow: false,
    });
    return new Promise(function (resolve, reject) {
      wx.showLoading({
        title: "加载中...",
      });
      console.log(that.data.storeJobListSearchForm);
      wx.request({
        url: app.globalData.ip + "/yishoudan/custom/job/listApp",
        method: "POST",
        header: app.globalData.headers,
        data: that.data.storeJobListSearchForm,
        success: function (res) {
          console.log("职位列表↓↓↓↓");
          console.log(res);

          that.setData({
            totalPage: res.data.data.pageBean.pageCount,
            currPage: res.data.data.pageBean.currentPage,
          });

          setTimeout(function () {
            that.setData({
              triggered: false,
            });
          }, 1000);
          if (res.data.data.pageBean.recordList == null || res.data.data.pageBean.recordList.length == 0 || res.data.data.pageBean.recordList.length < that.data.storeJobListSearchForm.pageSize) {
            var jobListTemp = commonUtil.disposeJobListData(res.data.data.pageBean.recordList);
            that.data.recordList = that.data.recordList.concat(jobListTemp);

            that.setData({
              recordList: that.data.recordList,
              hasMoreData: false,
              isTrigger: false,
            });
          } else {
            var jobListTemp = commonUtil.disposeJobListData(res.data.data.pageBean.recordList);
            that.data.recordList = that.data.recordList.concat(jobListTemp);

            that.setData({
              recordList: that.data.recordList,
              hasMoreData: true,
              isTrigger: false,
            });
          }

          wx.hideLoading({
            success: (res) => { },
          });
          that.setData({
            loading: false,
            pageShow: true,
            storeJobListSearchForm: that.data.storeJobListSearchForm,
          });
          resolve();
          // that.getTag();

          that.data.recordList.forEach((item, index) => {
            console.log(item.storeDistrict);
            item['citys'] = commonUtil.setJobInfoPosition(item.storeDistrict)
            // console.log(item);
            if (app.isNotEmptyCheck(item.returnFeeType) || item.returnFeeType == "0" || item.returnFee == "0") {
              item["fuWuFei"] = commonUtil.getReturnFeeTypeName1ById(item.returnFeeType, item.returnFee);
            } else {
              item["fuWuFei"] = "";
            }
           
          });
          that.setData({
            recordList: that.data.recordList,
            currentJobDrawer: that.data.currentJobDrawer,
          });
          console.log(that.data.recordList)
          // if (that.data.recordList.length < res.data.data.pageBean.recordCount) {
          // 	that.setData({
          // 		hasMoreData: true,
          // 	});
          // } else {
          // 	that.setData({
          // 		hasMoreData: false,
          // 	});
          // }
        },
      });
    });
  },
  collectPaste(e) {
		var txt;
		var that = this;
		var collected = e.currentTarget.dataset.collected;
		var storeJobId = e.currentTarget.dataset.jobid;
		that.doCollected(collected, storeJobId);
	},
	doCollected(collected, storeJobId) {
		var that = this;
    var url = "/yishoudan/custom/job/collect/" + storeJobId;
		console.log(app.globalData.headers);
		//发起网络请求
		wx.request({
			url: app.globalData.ip + url,
			data: {
				storeJobId: storeJobId,
			},
			header: app.globalData.headers,
			method: "GET",
			success: function (res) {
				var txt;
				if (collected - 1 == 0) {
					//取消收藏
					for (var i = 0; i != that.data.recordList.length; ++i) {
						if (that.data.recordList[i].id - storeJobId == 0) {
							that.data.recordList[i].collected = 2;
							break;
						}
					}
					txt = "取消收藏";
					if (that.data.choiceCollect == 1) {
						that.data.storeJobListSearchForm.pageNum = 1;
						that.setData({
							recordList: [],
							storeJobListSearchForm: that.data.storeJobListSearchForm,
						});
						that.getJobList();
					}
				} else {
					//收藏
					for (var i = 0; i != that.data.recordList.length; ++i) {
						if (that.data.recordList[i].id - storeJobId == 0) {
							that.data.recordList[i].collected = 1;
							break;
						}
					}
					txt = "收藏成功";
				}
				that.setData({
					recordList: that.data.recordList,
				});
				wx.showToast({
					icon: "none",
					title: txt,
				});
			},
			fail: function (res) {
				console.log("操作失败");
			},
		});
  },
  hidedrawershow (e) {
    console.log(e);
    this.setData({
      [e.detail.type]: false
    })
    wx.showTabBar({
      animation: false,
      success: (result) => {

      },
      fail: () => { },
      complete: () => { }
    });
  },
  recordBill (e) {
    console.log(e.currentTarget.dataset.job);
    this.setData({
      recordShow: true,
      currentInfo: e.currentTarget.dataset.job,
    })
    console.log(this.data.recordShow);
    wx.hideTabBar({
      animation: false,
      success: (result) => {

      },
      fail: () => { },
      complete: () => { }
    });
    return
    if (!this.data.isLogin) {
      wx.navigateTo({
        url: "/pages/login/index",
      });
      return;
    }
    console.log(e.currentTarget.dataset.job);
    console.log(123);
    let middleInfo = e.currentTarget.dataset.job;
    let info = JSON.stringify({
      id: middleInfo.id,
      jobName: middleInfo.jobName,
      storeName: middleInfo.storeName,
      aliasName: middleInfo.aliasName,
      storeId: middleInfo.storeId,
    });
    console.log(info);
    wx.navigateTo({
      url: `../newEnroll/enroll/index?applyType=1&info=${info}`,
    });
    // wx.navigateTo({
    // 	url: `../newEnroll/enroll/index?applyType=1&info=${info}`,
    // });
  },
});
