<template>
	<view class="page">
		<!-- 反馈内容 -->
		<view class="feedback-data">
			<view class="titleall-box">
				<view class="titleall-left">
					<view class="titleall-left-line"></view>人才信息
				</view>
			</view>
			<uni-forms ref="baseForm" :modelValue="baseFormData" :rules="rules">
				<view class="example-body">
					<uni-file-picker limit="1" @select="select" title="头像"></uni-file-picker>
				</view>
				<uni-forms-item label="姓名" required name="name" style="margin-top: 10rpx;">
					<uni-easyinput v-model="baseFormData.name" placeholder="请输入姓名" />
				</uni-forms-item>
				<uni-forms-item label="年龄" required name="age">
					<uni-easyinput v-model="baseFormData.age" placeholder="请输入年龄" />
				</uni-forms-item>
				<uni-forms-item label="性别" required>
					<uni-data-checkbox v-model="baseFormData.sex" :localdata="sexs" @change="change" />
				</uni-forms-item>
				<uni-forms-item label="政治面貌" required name="policy">
					<uni-easyinput v-model="baseFormData.policy" placeholder="请输入政治面貌" />
				</uni-forms-item>
				<uni-forms-item label="民族" required name="nation">
					<uni-easyinput v-model="baseFormData.nation" placeholder="请输入民族" />
				</uni-forms-item>
				<uni-forms-item label="电话号码" required name="phone">
					<uni-easyinput v-model="baseFormData.phone" placeholder="请输入电话号码" />
				</uni-forms-item>
				<uni-forms-item label="学历" required name="education">
					<uni-easyinput v-model="baseFormData.education" placeholder="请输入学历" />
				</uni-forms-item>
				<uni-forms-item label="毕业院校" required name="school">
					<uni-easyinput v-model="baseFormData.school" placeholder="请输入毕业院校" />
				</uni-forms-item>
				<uni-forms-item label="工作经历">
					<uni-easyinput type="textarea" v-model="baseFormData.introduction" placeholder="请输入工作经历" />
				</uni-forms-item>
			</uni-forms>
			<view class="isNotice" style="display: flex; align-items: center; margin-top: 20rpx;justify-content: space-between;">
				<view class="lef" style="margin-left: 30rpx;">
					已选择<span>{{companysId.length}}</span>家企业
				</view>
				<view class="rig" @click="toTellInfo()" style="display: flex;align-items: center;">
					<text>选择企业</text>
					<view class="more-content">
						<view class="more-content-img">
							<image src="../../../static/right2.png"></image>
						</view>
					</view>
				</view>
			</view>
		</view>		
		<!-- 提交 -->
		<view v-if="have" class="submit-btn" @click="postInfo('baseForm')">
			<text>发布</text>
		</view>
		<view v-else class="submit-btn" @click="updateInfo('baseForm')">
			<text>保存</text>
		</view>
	</view>
</template>

<script>
	import request from '@/utils/request.js'
	export default {
		data() {
			return {
				// 基础表单数据
				baseFormData: {
					name: '',
					age: '',
					introduction: '',
					sex: 2,
					// 政治面貌
					policy: '',
					phone: '',
					education: '',
					school: '',
					nation: '',
					datetimesingle: 1627529992399,
				},
				sexValue: '',
				filePath: [],
				uploadPath: [],
				// 需要通知的公司的id
				companysId: [],
				tellInfo:[],
				// 单选数据源
				sexs: [{
					text: '男',
					value: 0
				}, {
					text: '女',
					value: 1
				}, {
					text: '保密',
					value: 2
				}],
				// 校验规则
				rules: {
					name: {
						rules: [{
							required: true,
							errorMessage: '姓名不能为空'
						}]
					},
					age: {
						rules: [{
							required: true,
							errorMessage: '年龄不能为空'
						}, {
							format: 'number',
							errorMessage: '年龄只能输入数字'
						}]
					},
					phone: {
						rules: [{
							required: true,
							errorMessage: '电话号码不能为空'
						}]
					},
					policy: {
						rules: [{
							required: true,
							errorMessage: '不能为空'
						}]
					},
					education: {
						rules: [{
							required: true,
							errorMessage: '不能为空'
						}]
					},
					school: {
						rules: [{
							required: true,
							errorMessage: '不能为空'
						}]
					},
					nation: {
						rules: [{
							required: true,
							errorMessage: '不能为空'
						}]
					},
				},
				have:false,
				isId:''
			}
		},
		onUnload() {
			uni.$off('idInfo')
			uni.$off('tellInfo')
		},
		onLoad(options) {
			console.log("options",options)
			// console.log("options",JSON.stringify(options).data)
			uni.$on('idInfo', res => {
				console.log("选择公司页面返回的公司ID", res)
				if(res.length > 0)
				this.companysId = res
			})
			uni.$on('tellInfo', res => {
				console.log("tell信息", res)
				if(res.length > 0)
				this.tellInfo = res
			})
			if(JSON.stringify(options) == "{}"){
				this.have = true
				console.log(this.have)
			}else{
				this.isId = JSON.parse(options.data)
				console.log("Id",this.isId)
				this.getDetail(this.isId)
			}
		},
		methods: {
			// 获取用户信息
			getUser() {
				if (uni.getStorageSync('user')) {
					let userCode = uni.getStorageSync("user")
					console.log(userCode)
					if(userCode.userInfo.userId === 'admin'){
						this.isAdmin = true
					}
					console.log('用户已登录！')
				} 
				else {
					uni.showToast({
						title: '请登录',
						icon: 'none',
						duration:1500
					})
					setTimeout(() => {
						uni.reLaunch({
							url: '/pages/login/index'
						})
					})
				}
			},
			// 跳转到选择通知企业页面
			toTellInfo() {
				uni.navigateTo({
					url: '/pages/tellInfo/tellInfo'
				})
			},
			// 进行通知
			tellNotice(id){
				// 进行通知
				request({
					url:'/api/extend/demo/SendMessageByApp',
					method:'post',
					data:{
						title:this.tellInfo[0],
						companys:this.companysId,
						desc:this.tellInfo[1],
						reid:id,
						link:'/pages/recommend/recommendInfo/recommendInfo',
						fangshi:'通知',
						type:'人才推介通知'
					}
				}).then(res=>{
					console.log('通知结果',res)
				})
			},
			// 获取上传状态
			select(e) {
				console.log('选择文件：', e)
				this.filePath = e.tempFilePaths
			},
			change(e) {
				console.log("e:", e.detail.value);
				console.log("sex:", this.baseFormData.sex)
			},
			// 发布信息
			postInfo(ref) {
				let that = this
				this.uploadPath = []
				if (this.filePath.length == 0) {
					uni.showToast({
						icon: "error",
						title: "请上传头像"
					})
					return
				}
				this.$refs[ref].validate().then(res => {
					console.log(res)
					this.sexValue = this.sexs[this.baseFormData.sex].text
					console.log('xinbie', this.sexValue)
					uni.uploadFile({
						url: "https://www.dygxq-es.cn/api/file/Uploader/1",
						filePath: this.filePath[0],
						name: 'file',
						success: (res) => {
							console.log(JSON.parse(res.data))
							let data = JSON.parse(res.data).data
							console.log(data)
							this.uploadPath.push(data)
							console.log(this.uploadPath)
							// if (res.code == 200) {
								
							// }
							console.log('进入')
							let info = {
								name: that.baseFormData.name,
								headImg: that.uploadPath,
								gender: that.sexValue,
								age: that.baseFormData.age,
								nation: that.baseFormData.nation,
								phoneNumber: that.baseFormData.phone,
								education:that.baseFormData.education,
								graduationInstitution:that.baseFormData.school,
								workExperience:that.baseFormData.introduction,
								politicalOutlook:that.baseFormData.policy
							}
							that.API.postRecommend(info).then(res=>{
								console.log("成功上传",res)
								if(res.code === 200){
									if(this.companysId.length > 0){
										this.tellNotice(res.data.Id)
									}
									uni.showToast({
										icon:"success",
										title:"发布成功！"
									}).then(()=>{
										uni.navigateTo({
											url:'/pages/recommend/recommend'
										})
									})
								}
							})
						}
					})
				})
			},
			
			// 修改信息
			updateInfo(ref) {
				let that = this
				this.uploadPath = []
				if (this.filePath.length == 0) {
					uni.showToast({
						icon: "error",
						title: "请上传头像"
					})
					return
				}
				this.$refs[ref].validate().then(res => {
					console.log(res)
					this.sexValue = this.sexs[this.baseFormData.sex].text
					console.log('xinbie', this.sexValue)
					uni.uploadFile({
						url: "https://www.dygxq-es.cn/api/file/Uploader/1",
						filePath: this.filePath[0],
						name: 'file',
						success: (res) => {
							console.log(JSON.parse(res.data))
							let data = JSON.parse(res.data).data
							console.log(data)
							this.uploadPath.push(data)
							console.log(this.uploadPath)
							// if (res.code == 200) {
								
							// }
							console.log('进入')
							let info = {
								name: that.baseFormData.name,
								headImg: that.uploadPath,
								gender: that.sexValue,
								age: that.baseFormData.age,
								nation: that.baseFormData.nation,
								phoneNumber: that.baseFormData.phone,
								education:that.baseFormData.education,
								graduationInstitution:that.baseFormData.school,
								workExperience:that.baseFormData.introduction,
								politicalOutlook:that.baseFormData.policy,
								id:that.isId
							}
							that.API.updateRecommendInfo(info).then(res=>{
								console.log("修改成功",res)
								if(res.code === 200){
									uni.showToast({
										icon:"success",
										title:"修改成功！"
									})
								}
							})
						}
					})
				})
			},
			
			//获取详细信息
			 getDetail(id){
				 let that = this
				 this.API.getRecommendInfo(id).then(res=>{
					 console.log(res)
					 if(res.code === 200){
						 let detailData = res.data
						 that.baseFormData.name = detailData.name
						 that.baseFormData.age = detailData.age
						 that.baseFormData.nation = detailData.nation
						 that.baseFormData.phone = detailData.phoneNumber
						 that.baseFormData.education = detailData.education
						 that.baseFormData.school = detailData.graduationInstitution
						 that.baseFormData.introduction = detailData.workExperience
						 that.baseFormData.policy = detailData.politicalOutlook
					 }
				 })
			 },
		}
	}
</script>

<style lang="scss" scoped>
	@import 'Feedback.scss';
	.more-content{
		display: flex;
		align-items: center;
		image{
			width: 100rpx;
			height: 100rpx;
			border-radius: 100%;
		}
		.more-content-img {
			image {
				width: 24rpx;
				height: 24rpx;
			}
		}
	}
</style>