// pages/filterPage/index.js
Page({
	/**
	 * 页面的初始数据
	 */
	data: {
		filterData: {
			yanba: [
				{
					name: "无",
					id: 1,
					active: 0,
				},
				{
					name: "小面积",
					id: 2,
					active: 0,
				},
				{
					name: "大面积",
					id: 3,
					active: 0,
				},
			],
			area: [
				{
					name: "江浙沪",
					id: 1,
					active: 0,
				},
				{
					name: "鄂豫皖",
					id: 2,
					active: 0,
				},
				{
					name: "京津冀",
					id: 3,
					active: 0,
				},
				{
					name: "晋西北",
					id: 4,
					active: 0,
				},
				{
					name: "粤桂闽",
					id: 5,
					active: 0,
				},
				{
					name: "鲁东南",
					id: 6,
					active: 0,
				},
				{
					name: "黑吉辽",
					id: 7,
					active: 0,
				},
				{
					name: "疆藏青",
					id: 8,
					active: 0,
				},
				{
					name: "渝湘赣",
					id: 9,
					active: 0,
				},
				{
					name: "陕甘宁",
					id: 10,
					active: 0,
				},
				{
					name: "云贵川",
					id: 11,
					active: 0,
				},
				{
					name: "港澳台",
					id: 12,
					active: 0,
				},
			],
			price: [
				{
					name: "3000-4000",
					id: 1,
					active: 0,
				},
				{
					name: "4000-5000",
					id: 2,
					active: 0,
				},
				{
					name: "5000-6000",
					id: 3,
					active: 0,
				},
				{
					name: "6000-7000",
					id: 4,
					active: 0,
				},
				{
					name: "7000-8000",
					id: 5,
					active: 0,
				},
				{
					name: "8000以上",
					id: 6,
					active: 0,
				},
			],
			sex: [
				{
					name: "男",
					id: 1,
					active: 0,
				},
				{
					name: "女",
					id: 2,
					active: 0,
				},
				{
					name: "男女不限",
					id: -1,
					active: 0,
				},
			],
			jobFilter: {
				tagArray0: [],
				tagArray1: [],
				tagArray2: [],
				tagArray3: [],
				tagArray4: [],
			},
			classifyList: [],
		},
		copyList: {},
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(options) {
		console.log(wx.getStorageSync("FILTER_LIST"));
		if (wx.getStorageSync("FILTER_LIST")) {
			let list = wx.getStorageSync("FILTER_LIST");
			list.forEach((item) => {
				item.active = 0;
				if (item.typeClassify == "0") {
					this.data.filterData.jobFilter["tagArray0"].push(item);
				} else if (item.typeClassify == "1") {
					this.data.filterData.jobFilter["tagArray1"].push(item);
				} else if (item.typeClassify == "2") {
					this.data.filterData.jobFilter["tagArray2"].push(item);
				} else if (item.typeClassify == "3") {
					this.data.filterData.jobFilter["tagArray3"].push(item);
				} else if (item.typeClassify == "4") {
					this.data.filterData.jobFilter["tagArray4"].push(item);
				}
			});
			let jobFilter = "filterData.jobFilter";
			this.setData({
				[jobFilter]: this.data.filterData.jobFilter,
			});
		}
		if (wx.getStorageSync("CLASSIFY_LIST")) {
			this.data.filterData.classifyList = wx.getStorageSync("CLASSIFY_LIST");
			this.data.filterData.classifyList.forEach((item) => {
				item.active = 0;
			});
			let classifyList = "filterData.classifyList";
			this.setData({
				[classifyList]: this.data.filterData.classifyList,
			});
		}
		this.setData({
			copyList: JSON.parse(JSON.stringify(this.data.filterData)),
		});
	},

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

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {},
	/**
	 * 筛选标签的点击
	 *
	 *
	 */
	setActive(e) {
		console.log(e);
		let data = e.currentTarget.dataset;
		if (data.type == "yanba" || data.type == "sex" || data.type == "price") {
			console.log(data.id);
			let index = this.data.filterData[data.type].findIndex((item) => {
				return item.id == data.id;
			});
			if (this.data.filterData[data.type][index].active == data.id) {
				this.data.filterData[data.type][index].active = 0;
			} else {
				this.data.filterData[data.type].forEach((item) => {
					item.active = 0;
				});
				this.data.filterData[data.type][index].active = data.id;
			}

			this.setData({
				filterData: this.data.filterData,
			});
		} else if (data.type != "jobFilter") {
			this.data.filterData[data.type].forEach((item) => {
				if (item.id == data.id) {
					if (item.active == data.id) {
						item.active = 0;
					} else {
						item.active = data.id;
					}
				}
			});
			console.log(this.data.filterData[data.type]);
		} else {
			this.data.filterData[data.type][data.arr].forEach((item) => {
				if (item.id == data.id) {
					if (item.active == data.id) {
						item.active = 0;
					} else {
						item.active = data.id;
					}
				}
			});
		}

		this.setData({
			filterData: this.data.filterData,
		});
	},
	/**
	 * 清除删选信息
	 *
	 *
	 */
	clearFilter() {
		this.setData({
			filterData: JSON.parse(JSON.stringify(this.data.copyList)),
		});
	},
	/**
	 * 前往职位页
	 *
	 *
	 */
	toList() {
		console.log(this.data.filterData);
		let formSearch = { pageNum: 1, pageSize: 100 };
		let filter = this.data.filterData;
		for (var k in filter) {
			if (k != "jobFilter") {
				let jobCategoryArr = [];
				filter[k].forEach((item) => {
					if (item.active != 0) {
						if (k == "price") {
							if (item.name == "8000以上") {

								formSearch.monthlyPayStr = '8001-20000';
							} else {
								formSearch.monthlyPayStr = item.name;
							}
						} else if (k == "sex") {
							formSearch.sex = item.id;
						} else if (k == "classifyList") {
							jobCategoryArr.push(item.id);
						}
					}
				});
				formSearch.jobCategoryLabelIds = jobCategoryArr.join();
			} else {
				let totalArr = [...filter["jobFilter"].tagArray0, ...filter["jobFilter"].tagArray1, ...filter["jobFilter"].tagArray2, ...filter["jobFilter"].tagArray3, ...filter["jobFilter"].tagArray4];
				let jobSpecialArr = [];
				totalArr.forEach((item) => {
					if (item.active != 0) {
						jobSpecialArr.push(item.id);
					}
				});
				formSearch.jobSpecialLabelIds = jobSpecialArr.join();
			}
		}
		console.log(formSearch);
		let pathData = JSON.stringify(formSearch);
		wx.navigateTo({
			url: `/pages/jobListSearch/index?from=filter&data=${pathData}`,
		});
	},
	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide() {},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload() {},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage1() {},
});
