import commonData from '../../commonData.js'
import commonFun from '../../commonFun.js'

export default {
	data() {
		return {
			id: null,
			url: '&WsAjaxBiz=Trader&WsAjaxAction=entityDataHandle',
			releaseInfo: commonData.releaseInfo,
			calc_hours: commonFun.calc_hours,
			calc_date: commonFun.calc_date,
			sub_data: { //提交的数据
				settle_catalog: 0, //是否批量招聘
				age_mode: 0, //年龄， 0-不限；1-限制
				gender_mode: 0, //性别， 0-不限；1-限男；2-限女；
				meals_providing: 0, //是否管饭 0-不管；1-管饭；
				enroll_audit: 1, //是否需要审核
				settle_mode: 0, //核销模式
				begin_hour: '08:00',
				finish_hour: '17:00',
				urgent_level: 0, //0是完工即结，1是今日零工

				work_addr1: '',
				work_addr: '', //工作地点
				work_addr_lng: '', //经度117.333
				work_addr_lat: '', //纬度39.33
			},
			begin_start: '', //开始日期的可选开始日期
			end_start: '', //截止日期的可选开始日期
			total_sal: '', //计算页面总金额
			trader_level: 0, //是否vip
		}
	},
	onLoad(e) {
		this.begin_start = commonFun.formatDate2(new Date())
		this.end_start = commonFun.formatDate2(new Date())
		this.trader_level = e.trader_level || 0 //是否vip

		// e.id = 399
		if (e.id) {
			this.get_data(e.id)
		} else {
			let urgent_level = e.urgent_level || 0
			let sub_data = {
				...this.sub_data,
				urgent_level
			}
			this.sub_data = sub_data
			uni.setNavigationBarTitle({
				title: sub_data.urgent_level == 0 ? '发布完工即结' : '发布今日零工'
			})
			let DRAFT = urgent_level == 0 ? uni.getStorageSync('DRAFT0') : uni.getStorageSync('DRAFT1')
			if (DRAFT && DRAFT.title) {
				uni.showModal({
					title: '是否使用草稿内容？',
					content: '草稿标题：' + DRAFT.title,
					success: (res) => {
						if (res.confirm) {
							DRAFT.id = ''
							this.sub_data = {
								...DRAFT,
							}
							setTimeout(() => {
								this.cal_every_salary()
							}, 1000)
						} else {
							// 不使用草稿，则清空草稿
							if (urgent_level == 0) {
								uni.setStorageSync('DRAFT0', '')
							} else {
								uni.setStorageSync('DRAFT1', '')
							}
						}
					}
				})
			}
		}
	},
	methods: {
		// 获取数据
		get_data(bizId) {
			commonFun.requestUrl(this.url, {
				bizCatalog: 'RecruitEntity',
				handleMode: 'fetch',
				bizId
			}, res => {
				if (res.status) {
					let sub_data = res.data.Result
					if (sub_data.settle_catalog == 0) {
						sub_data.eve_feetotal = sub_data.feetotal
					}
					this.sub_data = sub_data
					uni.setNavigationBarTitle({
						title: sub_data.urgent_level == 0 ? '发布完工即结' : '发布今日零工'
					})
					setTimeout(() => {
						this.cal_every_salary()
					}, 1500)
					console.log('编辑页面获取发布详情')
					console.log(this.sub_data)
				}

			})
		},
		// 提交数据
		sub_data_req() {
			this.close_modal()
			uni.showLoading({
				title: '加载中...',
				mask: true
			})
			let sub_data = this.sub_data
			if (sub_data.settle_catalog == 0) {
				if (sub_data.urgent_level == 0) {
					sub_data.feetotal = this.total_sal
				} else {
					sub_data.feetotal = sub_data.eve_feetotal
				}
			}
			sub_data.id = ''
			let params = {
				bizCatalog: 'RecruitEntity',
				handleMode: 'upsertLaunchAudit',
				bizData: sub_data
			}
			console.log('发布请求数据')
			console.log(params.bizData)
			// return
			commonFun.requestUrl(this.url, params, res => {
				console.log('发布返回数据')
				console.log(res)
				uni.hideLoading()
				if (res.status) {
					uni.showModal({
						content: '发布成功',
						showCancel: false,
						success: () => {
							uni.navigateBack({
								delta: 1
							});
						}
					})
				} else {
					if (res.data && res.data.ErrCode && res.data.ErrCode == 'NoEnoughCount') {
						uni.showModal({
							title: '剩余发布人数不足',
							content: '发布扣除' + this.sub_data.need_total + '人，当前剩余' + (res.data
								.ErrMsg.useFreeCount + res.data.ErrMsg
								.balance) + '人',
							confirmText: '去充值',
							success: (res2) => {
								if (res2.confirm) {
									uni.navigateTo({
										url: '/pages/personal/wallet?dvalue=' + res.data
											.ErrMsg.dvalue
									})
								}
							}
						})
					}
				}
			})
		},
		// 点击提交数据
		sub() {
			let sub_data = {
				...this.sub_data,
			}
			if (sub_data.urgent_level == 1) {
				sub_data.begin_datetime = commonFun.formatDate2(new Date())
				sub_data.finish_datetime = commonFun.formatDate2(new Date())
			}
			console.log(this.sub_data)

			// 检测输入是否为空
			let form_collect = sub_data.settle_catalog == 1 ? commonData.releaseInfo.sub_arr0
				.concat(...
					commonData
					.releaseInfo.sub_arr1_s) :
				commonData.releaseInfo.sub_arr0.concat(...commonData.releaseInfo.sub_arr1)
			for (let i in form_collect) {
				let item = form_collect[i]
				if (!sub_data[item['key']]) {
					uni.showModal({
						title: '输入错误',
						content: item['name'] + '不允许为空',
						showCancel: false,
						success: function(res) {}
					});
					return
				}
			}

			if (!sub_data.begin_datetime) {
				uni.showModal({
					content: '请选择起始日期',
					showCancel: false,
				});
				return
			}

			// 非批量招聘需要结束日期
			if (sub_data.settle_catalog != 1 && !sub_data.finish_datetime) {
				uni.showModal({
					content: '请选择截至日期',
					showCancel: false,
				});
				return
			}

			if (sub_data.settle_catalog == 1 && sub_data.urgent_level == 1) {
				uni.showModal({
					content: '不允许发布今日零工的批量招聘',
					showCancel: false,
				});
				return
			}

			if (sub_data.settle_catalog != 1 && (sub_data.begin_datetime > sub_data.finish_datetime)) {
				uni.showModal({
					content: '起始日期不能大于截止日期',
					showCancel: false,
				});
				return
			}

			if (sub_data.work_addr == '') {
				uni.showModal({
					content: '请选择工作地点',
					showCancel: false,
				});
				return
			}
			if (!sub_data.work_desc) {
				uni.showModal({
					content: '岗位详情不允许为空',
					showCancel: false,
				});
				return
			}

			this.sub_data = sub_data
			this.open_modal()
		},

		// 计算每天工资
		cal_every_salary() {
			//起始/截至跨越天数
			let total_date = this.calc_date(this.sub_data.begin_datetime, this.sub_data.finish_datetime)
			let eve_feetotal = Number(this.sub_data.eve_feetotal)

			if (total_date != '--' && !isNaN(eve_feetotal)) {
				this.total_sal = (total_date * eve_feetotal).toFixed(2)
			} else {
				this.total_sal = ''
			}
		},
		// 选择单次/批量招聘按钮事件
		chenge_settle_catalog(settle_catalog) {
			let sub_data = {
				...this.sub_data,
			}
			sub_data['settle_catalog'] = settle_catalog
			this.sub_data = sub_data
			uni.setStorageSync('DRAFT0', sub_data)
		},
		// 单选事件
		radio_change(e) {
			let key = e.currentTarget.dataset.key
			let sub_data = {
				...this.sub_data,
			}
			sub_data[key] = e.target.value
			this.sub_data = sub_data
			if (sub_data.urgent_level == 0) {
				uni.setStorageSync('DRAFT0', sub_data)
			} else {
				uni.setStorageSync('DRAFT1', sub_data)
			}
		},
		// 开始日期改变事件
		date_change1(e) {
			let sub_data = {
				...this.sub_data
			}
			sub_data.begin_datetime = e
			this.sub_data = sub_data
			this.end_start = e
			uni.setStorageSync('DRAFT0', sub_data)
			this.cal_every_salary()
		},
		// 截至日期改变事件                                         
		date_change2(e) {
			let sub_data = {
				...this.sub_data
			}
			sub_data.finish_datetime = e
			this.sub_data = sub_data
			uni.setStorageSync('DRAFT0', sub_data)
			this.cal_every_salary()
		},
		// input和textarea的输入框输入事件
		ipt(e) {
			let key = e.currentTarget.dataset.key
			let sub_data = {
				...this.sub_data
			}
			sub_data[key] = e.target.value
			this.sub_data = sub_data
			this.cal_every_salary()
			if (sub_data.urgent_level == 0) {
				uni.setStorageSync('DRAFT0', sub_data)
			} else {
				uni.setStorageSync('DRAFT1', sub_data)
			}
		},
		// 删除招聘图片
		delete_img() {
			uni.showModal({
				content: '确认删除头图？',
				success: (res) => {
					if (res.confirm) {
						let sub_data = {
							...this.sub_data
						}
						sub_data.profile_photo = ''
						this.sub_data = sub_data
						if (sub_data.urgent_level == 0) {
							uni.setStorageSync('DRAFT0', sub_data)
						} else {
							uni.setStorageSync('DRAFT1', sub_data)
						}
					}
				}
			})
		},
		// 上传招聘图片
		upload_img(e) {
			commonFun.uploadFileApiOSS(res => {
				if (res.status) {
					let sub_data = {
						...this.sub_data
					}
					sub_data.profile_photo = res.data
					this.sub_data = sub_data
					if (sub_data.urgent_level == 0) {
						uni.setStorageSync('DRAFT0', sub_data)
					} else {
						uni.setStorageSync('DRAFT1', sub_data)
					}
				}
			})
		},
		// 时间选择
		bindTimeChange(e) {
			const key = e.currentTarget.dataset.key
			const value = e.target.value
			let sub_data = {
				...this.sub_data
			}
			sub_data[key] = value
			this.sub_data = sub_data
			if (sub_data.urgent_level == 0) {
				uni.setStorageSync('DRAFT0', sub_data)
			} else {
				uni.setStorageSync('DRAFT1', sub_data)
			}
		},
		// 打开窗口
		open_modal() {
			this.$refs.popup.open('top')
		},
		// 关闭窗口
		close_modal() {
			this.$refs.popup.close('top')
		},
		// 打开地图
		openAddress() {
			uni.chooseLocation({
				success: (res) => {
					let sub_data = {
						...this.sub_data,
					}
					sub_data['work_addr1'] = res.name
					sub_data['work_addr'] = res.address
					sub_data['work_addr_lng'] = res.longitude
					sub_data['work_addr_lat'] = res.latitude
					this.sub_data = sub_data
					if (sub_data.urgent_level == 0) {
						uni.setStorageSync('DRAFT0', sub_data)
					} else {
						uni.setStorageSync('DRAFT1', sub_data)
					}
				},
				fail: (res) => {
					if (res.errMsg.indexOf('cancel') == -1) {
						uni.showModal({
							title: '打开地图失败',
							content: JSON.stringify(res)
						})
					}
				}
			});
		},
	}
}
