<template>
	<view class="page">
		<navbar :title="isEdit ? '编辑招工' : '发布招工'" leftClick @leftClick="$utils.navigateBack" />
		
		
		<view class="form">
			<view class="help-issue">
				<text>招工标题</text>
				<text style="color: #BD3624;">*</text>
			</view>
			<view class="form-sheet-cell">
				<input type="text" 
				v-model="form.title"
				class="title-input"/>
			</view>
			
			<!-- <uv-cell
			title="学历要求"
			rightIconStyle="fontSize: 30rpx;"
			:value="form.school || '请选择学历要求'"
			@click="openPicker('school')"
			isLink
			></uv-cell> -->
			
			<!-- <uv-cell
			title="工作地点"
			rightIconStyle="fontSize: 30rpx;"
			:value="form.address || '请选择招工地点'"
			@click="selectAddr"
			isLink
			></uv-cell> -->
			
			<uv-cell
			title="工龄要求"
			rightIconStyle="fontSize: 30rpx;"
			:value="form.workYear || '请选择工龄要求'"
			@click="openPicker('workYear')"
			isLink
			></uv-cell>
			
			
			<!-- <view class="form-sheet-cell">
				<view class="label">
					薪资范围
				</view>
				<view class="price">
					<input placeholder="最小薪资" v-model="form.minPrice" />
					~
					<input placeholder="最大薪资" v-model="form.maxPrice" />
				</view>
			</view> -->
			
			<view class="form-sheet-cell">
				<view class="label">
					招聘公司
				</view>
				<input placeholder="请输入招聘公司" v-model="form.company" />
			</view>
			
			<view class="form-sheet-cell">
				<view class="label">
					薪资范围
				</view>
				<input placeholder="请输入薪资范围" v-model="form.minPrice" />
			</view>
			
			<!-- <view class="form-sheet-cell">
				<view class="label">
					结算方式
				</view>
				<uv-radio-group v-model="radiovalue">
					<view class="price">
						<uv-radio
							:customStyle="{margin: '8px'}" 
							v-for="(item, index) in priceList" 
							:key="index" 
							iconSize="30rpx"
							size="40rpx"
							labelSize="26rpx"
							:label="item.name" 
							:name="item.name">
						</uv-radio>
					</view>
				</uv-radio-group>
			</view>
			
			<view class="form-sheet-cell">
				<view class="label">
					工作性质
				</view>
				<uv-radio-group v-model="radiovalue">
					<view class="price">
						<uv-radio
							:customStyle="{margin: '8px'}" 
							v-for="(item, index) in jopList" 
							:key="index" 
							iconSize="30rpx"
							size="40rpx"
							labelSize="26rpx"
							:label="item.name" 
							:name="item.name">
						</uv-radio>
					</view>
				</uv-radio-group>
			</view> -->
			
			<view class="form-sheet-cell">
				<view class="label">
					标签
				</view>
				<input placeholder="请输入标签(多个用,号分割)" v-model="form.iconText" />
			</view>
			
			<!-- <view class="form-sheet-cell">
				<view class="label">
					联系人
				</view>
				<input placeholder="请输入联系人" v-model="form.userName" />
			</view> -->
			
			
			
			<view class="form-sheet-cell">
				<view class="label">
					联系人
				</view>
				<input placeholder="请输入联系人" v-model="form.userName" />
			</view>
			
			<view class="form-sheet-cell">
				<view class="label">
					联系电话
				</view>
				<input placeholder="请输入联系电话" v-model="form.userPhone" />
			</view>
			
			
			<view class="category">
				<view class="title">
					选择地区
				</view>
				<view class="tagList">
					<view 
					:class="{act : t.id == form.addId}"
					@click="form.addId = t.id"
					v-for="(t, i) in cityList"
					:key="i">
						{{ t.name }}
					</view>
				</view>
			</view>
			
			<view class=""
			style="margin-top: 20rpx;">
				<uv-textarea 
				v-model="form.jobContext" 
				count 
				:maxlength="300" 
				autoHeight
				placeholder="请输入职位描述"></uv-textarea>
			</view>
			
			<view class=""
			style="margin-top: 20rpx;">
				<uv-textarea 
				v-model="form.school" 
				count 
				:maxlength="300" 
				autoHeight
				placeholder="请输入岗位要求"></uv-textarea>
			</view>
			
			<view class=""
			style="margin-top: 20rpx;">
				<uv-textarea 
				v-model="form.jobDetails" 
				count 
				:maxlength="300" 
				autoHeight
				placeholder="请输入岗位详细介绍"></uv-textarea>
			</view>
			
			<view class=""
			style="margin-top: 20rpx;">
				<uv-textarea 
				v-model="form.gsContext" 
				count 
				:maxlength="300" 
				autoHeight
				placeholder="请输入公司介绍"></uv-textarea>
			</view>
			
			
			<view class="uni-color-btn"
			@click="submit">
				{{ isEdit ? '保存修改' : '发布' }}
			</view>
			
			<!-- 编辑模式下显示删除按钮 -->
			<view v-if="isEdit" class="delete-btn"
			@click="deleteJob">
				删除招工
			</view>
		</view>
		
		
		<uv-picker ref="picker" 
		:columns="columns" 
		@confirm="pickerConfirm"></uv-picker>
	</view>
</template>

<script>
	import { mapState } from 'vuex'
	import { subscribeBeforePublish } from '@/utils/subscribeMessage.js'
	export default {
		data() {
			return {
				jobId: '', // 工作ID，用于编辑模式
				isEdit: false, // 是否为编辑模式
				form : {
					title: '', // 招工标题
					school : '', // 岗位要求
					workYear : '', // 工龄要求
					minPrice : '', // 薪资范围
					maxPrice : '', // 最大薪资（保留字段）
					address : '', // 工作地点（保留字段）
					addId : '', // 地区ID
					company : '', // 招聘公司
					gsContext : '', // 公司介绍
					jobContext : '', // 职位描述
					jobDetails: '', // 岗位详细介绍
					iconText: '', // 标签
					userName: '', // 联系人
					userPhone: '', // 联系电话
				},
				radiovalue : '',
				priceList : [
					{
						name : '日结',
					},
					{
						name : '月结',
					},
				],
				jopList : [
					{
						name : '正式工',
					},
					{
						name : '临时工',
					},
				],
				picker : {
					workYear : [
						'不限',
					],
					school : [
						'不限',
						'初中',
						'高中',
						'专科',
						'本科',
						'研究生',
						'博士',
					],
				},
				pickerKey : 'workYear',
			}
		},
		onLoad(options) {
			// 初始化工龄选项
			for (var i = 0; i < 30; i++) {
				this.picker.workYear.push((i + 1) + '年')
			}
			
			// 检查是否是编辑模式
			if (options.id) {
				this.jobId = options.id
				this.isEdit = true
				this.getJobDetail()
			} else {
				// 新增模式，设置默认地区
				if (this.cityList && this.cityList.length > 0) {
					this.form.addId = this.cityList[0].id
				}
			}
		},
		computed : {
			columns(){
				return [this.picker[this.pickerKey]]
			},
			...mapState(['cityList', 'category']),
		},
		watch: {
			// 监听cityList变化，用于处理异步加载的城市数据
			cityList: {
				handler(newVal) {
					if (newVal && newVal.length > 0 && !this.isEdit && !this.form.addId) {
						this.form.addId = newVal[0].id
					}
				},
				immediate: true
			}
		},
		methods: {
			// 获取工作详情数据
			getJobDetail() {
				this.$api('getJobDetail', { id: this.jobId }, res => {
					if (res.code == 200) {
						const data = res.result
						
						// 回显基本信息
						this.form = {
							...this.form,
							title: data.title || '',
							school: data.school || '',
							workYear: data.workYear || '',
							minPrice: data.minPrice || '',
							maxPrice: data.maxPrice || '',
							address: data.address || '',
							addId: data.addId || (this.cityList.length > 0 ? this.cityList[0].id : ''),
							company: data.company || '',
							gsContext: data.gsContext || '',
							jobContext: data.jobContext || '',
							jobDetails: data.jobDetails || '',
							iconText: data.iconText || '',
							userName: data.userName || '',
							userPhone: data.userPhone || '',
						}
					} else {
						uni.showToast({
							title: res.msg || '获取工作信息失败',
							icon: 'none'
						})
					}
				})
			},
			
			openPicker(key){
				this.pickerKey = key
				this.$refs.picker.open()
			},
			pickerConfirm(e){
				this.form[this.pickerKey] = e.value[0]
			},
			// 提交
			async submit(){
				
				if (this.$utils.verificationAll(this.form, {
						title: '请输入招工标题',
						workYear : '请选择工龄要求',
						company : '请输入招聘公司',
						minPrice : '请输入薪资范围',
						iconText: '请输入标签',
						userName: '请输入联系人',
						userPhone: '请输入联系电话',
						jobContext : '请输入职位描述',
						school : '请输入岗位要求',
						jobDetails: '请输入岗位详细介绍',
						gsContext : '请输入公司介绍',
					})) {
					return
				}
				
				// 只有在新发布时才订阅消息通知，编辑时不需要
				if (!this.isEdit) {
					await subscribeBeforePublish()
				}
				
				// 根据是否是编辑模式调用不同的API
				const apiName = this.isEdit ? 'editMyJob' : 'publishJob'
				const params = this.isEdit ? { ...this.form, id: this.jobId } : this.form
				
				this.$api(apiName, params, res => {
					if(res.code == 200){
						uni.showToast({
							title: this.isEdit ? '修改成功!' : '发布成功!',
							icon: 'none'
						})
						
						setTimeout(() => {
							uni.navigateBack()
						}, 1000)
					} else {
						uni.showToast({
							title: res.msg || (this.isEdit ? '修改失败' : '发布失败'),
							icon: 'none'
						})
					}
				})
			},
			
			// 删除招工
			deleteJob() {
				uni.showModal({
					title: '确认删除',
					content: '确定要删除这个招工信息吗？删除后无法恢复。',
					confirmColor: '#ff4757',
					success: (res) => {
						if (res.confirm) {
							this.$api('deleteMyJob', { id: this.jobId }, res => {
								if (res.code == 200) {
									uni.showToast({
										title: '删除成功!',
										icon: 'none'
									})
									
									setTimeout(() => {
										uni.navigateBack()
									}, 1000)
								} else {
									uni.showToast({
										title: res.msg || '删除失败',
										icon: 'none'
									})
								}
							})
						}
					}
				})
			},
			
		}
	}
</script>

<style scoped lang="scss">
.page{
	background-color: #fff;
	min-height: 100vh;
	box-sizing: border-box;
	color: #333333;
	font-size: 28rpx;
	/deep/ text{
		font-size: 28rpx !important;
	}
	input{
		width: 450rpx;
	}
	.form{
		padding: 30rpx;
		.help-issue {
			margin: 20rpx;
		}
		.title-input{
			border: 1px solid $uni-color;
			width: 100%;
			border-radius: 10rpx;
			padding: 10rpx 20rpx;
			box-sizing: border-box;
			height: 65rpx;
		}
		.form-sheet-cell{
			display: flex;
			background-color: #fff;
			padding: 20rpx 30rpx;
			align-items: center;
			.label{
				width: 160rpx;
			}
			.price{
				display: flex;
				text-align: center;
				input{
					width: 150rpx;
					border: 1px solid $uni-color;
					margin: 0 10rpx;
				}
			}
			.right-icon{
				margin-left: auto;
			}
		}
	}
	
	.category{
		padding: 20rpx;
		.title{
			// font-weight: 900;
			// font-size: 30rpx;
		}
		.tagList{
			display: flex;
			flex-wrap: wrap;
			padding: 10rpx 0;
			view{
				background: rgba($uni-color, 0.1);
				padding: 10rpx 20rpx;
				margin: 10rpx;
				border-radius: 10rpx;
				font-size: 26rpx;
			}
			.act{
				color: #fff;
				background: $uni-color;
			}
		}
	}
	
	.delete-btn {
		background: #ff4757;
		color: white;
		text-align: center;
		padding: 20rpx;
		margin: 30rpx;
        border-radius: 40rpx;
		font-size: 28rpx;
		font-weight: bold;
		
		&:active {
			background: #ff3742;
		}
	}
}
</style>
