<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>
		
		<!-- 提交 -->
		<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>
	export default {
		data() {
			return {
				// 基础表单数据
				baseFormData: {
					name: '',
					age: '',
					introduction: '',
					sex: 2,
					// 政治面貌
					policy: '',
					phone: '',
					education: '',
					school: '',
					nation: '',
					datetimesingle: 1627529992399,
				},
				sexValue: '',
				filePath: [],
				uploadPath: [],
				// 单选数据源
				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:''
			}
		},
		onLoad(options) {
			console.log("options",options)
			// console.log("options",JSON.stringify(options).data)
			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'
						})
					})
				}
			},
			// 获取上传状态
			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: "http://deyanggaoxin.fengshiyun.com/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){
									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: "http://deyanggaoxin.fengshiyun.com/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'
</style>