const app = getApp();

const commonUtil = require("../../utils/commonUtil.js");
Page({
	data: {
		toped: "1",
		recordList: [],
		storeJobListSearchForm: {
			pageNum: 1,
			pageSize: 20,
			classify: 1,
			sex: -1,
			workTypeStr: "",
			lat: "",
			lng: "",
			jobClassify: "",
			sortTag: 0,
			jobSpecialLabelIds: "",
			cityName: "",
			brandIds: "",
			jobCategoryLabelIds: "",
			ucj: 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.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({
     
    // })
		that.setData({
      pageShow: false,
      recordList: []
		});
		return new Promise(function (resolve, reject) {
			if (that.data.storeJobListSearchForm.sortTag == 2) {
				//如果按照距离排序，拼接上坐标
				that.data.storeJobListSearchForm.lng = app.globalData.lng;
				that.data.storeJobListSearchForm.lat = app.globalData.lat;
			}

			console.log(that.data.inputVal);
			that.data.storeJobListSearchForm.cityName = that.data.searchCityParamStorage.name;
			if (app.isEmptyCheck(that.data.searchCityParamStorage.name)) {
				that.data.storeJobListSearchForm.cityName = "全国";
			}
			that.data.storeJobListSearchForm.keys = that.data.inputVal == "搜索工作" ? "" : that.data.inputVal;
			// debugger;
			wx.showLoading({
				title: "加载中...",
			});
			console.log(that.data.storeJobListSearchForm);
			wx.request({
				url: app.globalData.ip + "/overall/store/job/list",
				method: "POST",
				header: app.globalData.headers,
				data: that.data.storeJobListSearchForm,
				success: function (res) {
					console.log("职位列表↓↓↓↓");
					console.log(res);

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

					setTimeout(function () {
						that.setData({
							triggered: false,
						});
					}, 1000);
					if (res.data.data.recordList == null || res.data.data.recordList.length == 0 || res.data.data.recordList.length < that.data.storeJobListSearchForm.pageSize) {
						var jobListTemp = commonUtil.disposeJobListData(res.data.data.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.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.data.recordList.forEach((item, index) => {
						// 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,
					});
					// if (that.data.recordList.length < res.data.data.recordCount) {
					// 	that.setData({
					// 		hasMoreData: true,
					// 	});
					// } else {
					// 	that.setData({
					// 		hasMoreData: false,
					// 	});
					// }
				},
			});
		});
  },
  collectPaste(e) {
		var txt;
		var that = this;
		if (!this.data.isLogin) {
			wx.navigateTo({
				url: "/pages/login/index",
			});
			return;
		}
		var collected = e.currentTarget.dataset.collected;
		var storeJobId = e.currentTarget.dataset.jobid;
		that.doCollected(collected, storeJobId);
	},
	doCollected(collected, storeJobId) {
		var that = this;
		var url = "/user/collect/job/add";
		if (collected - 1 == 0) {
			url = "/user/collect/job/remove";
		}
		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 = "取消收藏";
					that.data.currentJobDrawer.collected = 2;

					if (that.data.choiceCollect == 1) {
						if (that.data.drawerShow) {
							that.data.drawerShow = false;
						}
						that.data.storeJobListSearchForm.pageNum = 1;
						that.setData({
							recordList: [],
							drawerShow: that.data.drawerShow,
							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;
						}
					}
					that.data.currentJobDrawer.collected = 1;

					txt = "收藏成功";
				}

				console.log(that.data.currentJobDrawer);

				that.setData({
					recordList: that.data.recordList,
					currentJobDrawer: that.data.currentJobDrawer,
					isLogin: app.globalData.isLogin,
				});
				wx.showToast({
					icon: "none",
					title: txt,
				});
			},
			fail: function (res) {
				console.log("操作失败");
			},
		});
	},
});
