<template>
	<view class="page">
		<navbar title="在线简历"
		leftClick 
		@leftClick="$utils.navigateBack"/>
		
		
		
		<view class="box">
			<view class="list">
				<view class="item" 
				v-for="(item, index) in list" 
				:key="index">
					<view class="title">
						{{ item.title }}
					</view>
					<!-- 工种选择器模式 -->
					<view class="job-type-selector" v-if="item.useJobTypePicker" @click="openJobTypePicker">
						<view class="selected-job-type">
							{{ selectedJobType || form.typeId_dictText || '请选择工种' }}
						</view>
						<uv-icon name="arrow-right" size="30rpx"></uv-icon>
					</view>
					<view class="tagList" v-else-if="!item.useAddressPicker">
						<view :class="{act : i == item.index}" 
						@click="clickTag(item, i)" v-for="(t, i) in item.tag"
							:key="t.id">
							{{ t.name || t.adress }}
						</view>
					</view>
					<!-- 地址选择器模式 -->
					<view class="address-selector" v-else @click="openAddressPicker">
						<view class="selected-address">
							{{ selectedAddress || form.expectAddress_dictText || '请选择工作地区' }}
						</view>
						<uv-icon name="arrow-right" size="30rpx"></uv-icon>
					</view>
				</view>
			</view>
			
			
			<view class="form-sheet-cell">
				<view class="label">
					求职岗位
				</view>
				
				
				<input placeholder="请输入求职岗位"
				v-model="form.title" />
			</view>
			
			<view class="form-sheet-cell">
				<view class="label">
					您的年龄
				</view>
				<input placeholder="请输入年龄" 
				type="number"
				v-model="form.age" />
			</view>
			
			<view class="form-sheet-cell">
				<view class="label">
					您的工龄
				</view>
				<input placeholder="请输入工龄" 
				type="number"
				v-model="form.workTime" />
			</view>
			
			<view class="form-sheet-cell">
				<view class="label">
					您的性别
				</view>
				<uv-radio-group v-model="form.sex">
					<view style="display: flex;justify-content: center;">
						<uv-radio
							:customStyle="{margin: '8px'}" 
							v-for="(item, index) in sexList" 
							: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.nation" />
			</view>
			
			<view class="form-sheet-cell">
				<view class="label">
					期望薪资
				</view>
				<view class="price">
					<input placeholder="下限" v-model="form.salaryLow" />
					~
					<input placeholder="上限" v-model="form.salaryUp" />
				</view>
			</view>
			
			<view class="form-sheet-cell"
			@click="openPicker('qualification')">
				<view class="label">
					您的学历
				</view>
				<input placeholder="请选择学历" 
				disabled
				v-model="form.qualification" />
			</view>
			
			<!-- <view class="form-sheet-cell">
							<view class="label">
								您的学历
							</view>
							<input placeholder="请输入您的学历" 
							v-model="form.qualification" />
						</view> -->
			
			<uv-textarea
			v-model="form.brief" 
			count 
			:maxlength="300" 
			autoHeight
			placeholder="请输入个人介绍"></uv-textarea>
			
			
			<view class="uni-color-btn"
			@click="submit">
				发布
			</view>
		</view>
		
		<uv-picker ref="picker"
		:columns="columns" 
		@confirm="pickerConfirm"></uv-picker>
		
		<!-- 地址选择组件 -->
		<AddressPicker ref="addressPicker" :multiple="true" :showSelectWholeCity="true" :onlyCity="false" @confirm="onAddressConfirm" />
		
		<!-- 工种选择组件 -->
		<JobTypePicker ref="jobTypePicker" :multiple="true" @confirm="onJobTypeConfirm" />
	</view>
</template>

<script>
	import { mapState } from 'vuex'
	import AddressPicker from '@/components/AddressPicker.vue'
	import JobTypePicker from '@/components/JobTypePicker.vue'
	export default {
		components: {
			AddressPicker,
			JobTypePicker
		},
		data() {
			return {
				list: [
					{
						title: '您希望从事的工种',
						tag: [],
						index: 0,
						type : 'typeId',
						useJobTypePicker: true, // 标记使用工种选择器
					},
					{
						title: '您希望从事工作的地区',
						tag: [],
						index: 0,
						type : 'expectAddress',
						useAddressPicker: true, // 标记使用地址选择器
					},
					{
						title: '您希望从事工作的性质',
						tag: [],
						index: 0,
						type : 'natureId',
					},
				],
				form : {
					sex : '男',
					qulification : '',
					title: '',
					typeId : '',
					expectAddress : '',
					natureId : '',
					age : '',
					nation : '',
					salaryLow : '',
					salaryUp : '',
					qualification : '',
					brief: '',
				},
				sexList : [
					{
						name : '男',
					},
					{
						name : '女',
					},
				],
				picker : {
					qualification : [
						'初中',
						'高中',
						'专科',
						'本科',
						'研究生',
						'博士',
					],
				},
				pickerKey : 'workAge',
				selectedAddress: '', // 选中的地址文本
				selectedJobType: '', // 选中的工种文本
			}
		},
		computed : {
			...mapState(['natureList', 'jobTypeList', 'addressList']),
			columns(){
				return [this.picker[this.pickerKey]]
			},
		},
		onLoad() {
			this.list[0].tag = this.jobTypeList
			this.list[1].tag = this.addressList
			this.list[2].tag = this.natureList
			
			this.queryResumeByUserId()
		},
		methods: {
			clickTag(item, index){
				item.index = index
			},
			openPicker(key, picker){
				this.pickerKey = key
				if(picker){
					picker.open()
				}else{
					this.$refs.picker.open()
				}
			},
			pickerConfirm(e){
				console.log(e);
				let data = e.value[0]
				if(data && data.id){
					this.form[this.pickerKey] = data.id
					this.form[this.pickerKey + '_dictText'] = data.name || data.adress
				}else{
					this.form[this.pickerKey] = data
				}
			},
			// 提交
			submit(){
				
				// if(this.fileList.length == 0){
				// 	return uni.showToast({
				// 		title: '请上传图片',
				// 		icon : 'none'
				// 	})
				// }
				
				this.list.forEach(n => {
					// 如果使用地址选择器或工种选择器，跳过从tag获取值
					if(!n.useAddressPicker && !n.useJobTypePicker) {
						this.form[n.type] = n.tag[n.index].id
					}
				})
				
				if (this.$utils.verificationAll(this.form, {
					    title: '请输入求职岗位',
						typeId : '请选择工种',
						expectAddress : '请选择工作的地区',
						natureId : '请选择工作的性质',
						age : '请输入您的年龄',
						workTime : '请输入您的工龄',
						sex : '请选择性别',
						nation : '请输入您的民族',
						salaryLow : '请输入期望薪资下限',
						salaryUp : '请输入期望薪资上限',
						qualification : '请选择您的学历',
						brief: '请输入个人介绍',
					})) {
					return
				}
				
				let data = {
					title: this.form.title,
					typeId : this.form.typeId,
					expectAddress : this.form.expectAddress,
					natureId : this.form.natureId,
					age : this.form.age,
					workTime : this.form.workTime,
					nation : this.form.nation,
					salaryLow : this.form.salaryLow,
					salaryUp : this.form.salaryUp,
					qualification : this.form.qualification,
					brief: this.form.brief,
					sex: this.form.sex,
				}
				
				if(this.form.id){
					data.id = this.form.id
				}
				
				this.$api('addResume', data, res => {
					if(res.code == 200){
						uni.showToast({
							title: '保存成功!',
							icon: 'none'
						})
						
						setTimeout(uni.navigateBack, 1000, -1)
					}
				})
			},
			queryResumeByUserId(){
				this.$api('queryResumeByUserId', res => {
					if(res.code == 200 && res.result && res.result.records[0]){
						this.form = res.result.records[0]
						
						this.list.forEach((n, i) => {
					// 如果是地址选择器，需要根据ID找到对应的地址文本
					if(n.useAddressPicker && this.form[n.type]) {
						// 回显地址：根据存储的ID找到对应的地址文本
						this.selectedAddress = this.getAddressTextById(this.form[n.type])
					} else if(n.useJobTypePicker && this.form[n.type]) {
						// 回显工种：根据存储的ID找到对应的工种文本
						this.selectedJobType = this.getJobTypeTextById(this.form[n.type])
					} else {
						n.tag.forEach((e, index) => {
							if(this.form[n.type] == e.id){
								n.index = index
							}
						})
					}
				})
						
					}
				})
			},
			// 打开地址选择器
			openAddressPicker() {
				this.$refs.addressPicker.open()
			},
			// 根据ID获取地址文本（用于回显）
			getAddressTextById(idOrIds) {
				if (!idOrIds) return ''
				
				// 如果是多个ID（逗号分隔）
				if (typeof idOrIds === 'string' && idOrIds.includes(',')) {
					const ids = idOrIds.split(',')
					const addressTexts = []
					
					ids.forEach(id => {
						const address = this.findAddressById(id.trim())
						if (address) {
							addressTexts.push(address.adress)
						}
					})
					
					return addressTexts.join(',')
				} else {
					// 单个ID
					const address = this.findAddressById(idOrIds)
					return address ? address.adress : ''
				}
			},
			// 递归查找地址
			findAddressById(id) {
				// 在省级地址中查找
				for (let province of this.addressList) {
					if (province.id == id) {
						return province
					}
				}
				
				// 如果没找到，需要异步获取下级地址进行查找
				// 这里简化处理，实际项目中可能需要更复杂的缓存机制
				return null
			},
			// 地址选择确认回调
			onAddressConfirm(addressResult) {
				// 显示地址文本给用户看
				this.selectedAddress = addressResult.fullAddress
				// 传给后端的是ID或ID数组
				if (addressResult.selectedIds && addressResult.selectedIds.length > 0) {
					// 多选模式，传ID数组的字符串形式
					this.form.expectAddress = addressResult.selectedIds.join(',')
				} else {
					// 单选模式，传单个ID
					this.form.expectAddress = addressResult.selectedId
				}
			},
			// 打开工种选择器
			openJobTypePicker() {
				this.$refs.jobTypePicker.open()
			},
			// 工种选择确认回调
			onJobTypeConfirm(jobTypeResult) {
				// 显示工种文本给用户看
				this.selectedJobType = jobTypeResult.fullJobType
				// 传给后端的是ID或ID数组
				if (jobTypeResult.selectedIds && jobTypeResult.selectedIds.length > 0) {
					// 多选模式，传ID数组的字符串形式
					this.form.typeId = jobTypeResult.selectedIds.join(',')
				} else {
					// 单选模式，传单个ID
					this.form.typeId = jobTypeResult.selectedId
				}
				this.form.typeId_dictText = jobTypeResult.fullJobType
			},
			// 根据ID获取工种文本（用于回显）
			getJobTypeTextById(idOrIds) {
				if (!idOrIds) return ''
				
				// 如果是多个ID（逗号分隔）
				if (typeof idOrIds === 'string' && idOrIds.includes(',')) {
					const ids = idOrIds.split(',')
					const jobTypeTexts = []
					
					ids.forEach(id => {
						const jobType = this.findJobTypeById(id.trim())
						if (jobType) {
							jobTypeTexts.push(jobType.name)
						}
					})
					
					return jobTypeTexts.join(',')
				} else {
					// 单个ID
					const jobType = this.findJobTypeById(idOrIds)
					return jobType ? jobType.name : ''
				}
			},
			// 递归查找工种
			findJobTypeById(id) {
				// 在一级工种中查找
				for (let jobType of this.jobTypeList) {
					if (jobType.id == id) {
						return jobType
					}
				}
				
				// 如果没找到，需要异步获取下级工种进行查找
				// 这里简化处理，实际项目中可能需要更复杂的缓存机制
				return null
			},
		},
	}
</script>

<style scoped lang="scss">
	.page{
		background-color: #fff;
		min-height: 100vh;
		.box{
			padding: 30rpx;
			.list {
				.item {
					margin-top: 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;
					}
				}
				
				.address-selector {
					display: flex;
					justify-content: space-between;
					align-items: center;
					background: rgba($uni-color, 0.1);
					padding: 20rpx;
					margin: 10rpx;
					border-radius: 10rpx;
					font-size: 26rpx;
					
					.selected-address {
						flex: 1;
						color: #333;
					}
				}
				
				.job-type-selector {
					display: flex;
					justify-content: space-between;
					align-items: center;
					background: rgba($uni-color, 0.1);
					padding: 20rpx;
					margin: 10rpx;
					border-radius: 10rpx;
					font-size: 26rpx;
					
					.selected-job-type {
						flex: 1;
						color: #333;
					}
				}
			}
			}
			
			.form-sheet-cell{
				display: flex;
				background-color: #fff;
				padding: 20rpx;
				align-items: center;
				.label{
					width: 160rpx;
					font-weight: 900;
				}
				.price{
					display: flex;
					text-align: center;
					input{
						width: 150rpx;
						border: 1px solid $uni-color;
						margin: 0 10rpx;
					}
				}
				input{
					flex: 1;
					background-color: rgba($uni-color, 0.1);
					padding: 10rpx 20rpx;
					border-radius: 10rpx;
				}
				.right-icon{
					margin-left: auto;
				}
			}
		
			/deep/ .uv-textarea{
				background-color: rgba($uni-color, 0.1) !important;
				min-height: 400rpx;
			}
		}
	}
</style>