<template>
	<view style="padding: 30rpx;" >
		<nav-bar title="所有课程"></nav-bar>
		<view style="position: relative;">
			<view class="tip">
				请填写真实信息，以便我们后续为您提供完善的 <br>
				个人职业成长定制服务！
			</view>
			<!-- <view class="btn" style="position: absolute;right: 20rpx;top: 0;">
				编辑
			</view> -->
		</view>
		<view class="form">
			<u-form :model="form" ref="uForm">
				<u-form-item label="学校名称" :label-width="auto"><u-input v-model="schoolInfo.schoolName" /></u-form-item>
				<u-form-item label="全日制" :label-width="auto">
					<picker @change="bindPickerChangeFull" :value="index_full" :range="dictObj['full_time_school']">
						  <view class="picker">
							{{dictObj['full_time_school'][index_full]}}
						  </view>
					</picker>
				</u-form-item>
				<u-form-item  label="入学时间" prop="birth" :label-width="auto">
					<picker mode="date" :value="admissionTime"   @change="bindDateChange">
						<view class="picker">
						  {{admissionTime}}
						</view>
					  </picker>
				</u-form-item>
				<u-form-item  label="毕业时间" prop="birth" :label-width="auto">
					<picker mode="date" :value="graduationTime"   @change="bindDateChange2">
						<view class="picker">
						  {{graduationTime}}
						</view>
					  </picker>
				</u-form-item>
				<u-form-item label="学历">
					<picker @change="bindPickerChangeLevel" :value="index_level" :range="dictObj['edu_level']">
					              <view class="picker">
					                {{index_level?dictObj['edu_level'][index_level]:'未知'}}
					              </view>
					    </picker>
				</u-form-item>
				<u-form-item label="学位">
					<picker @change="bindPickerChangeDegree" :value="index_degree" :range="dictObj['academic_degree']">
					              <view class="picker">
					                {{dictObj['academic_degree'][index_degree]}}
					              </view>
					    </picker>
				</u-form-item>
				<u-form-item label="学制">
					<picker @change="bindPickerChangeSystem" :value="index_system" :range="dictObj['educational_system']">
					              <view class="picker">
					                {{dictObj['educational_system'][index_system]}}
					              </view>
					    </picker>
				</u-form-item>
				<u-form-item label="院系">
					<picker @change="bindPickerChangeDepartment" :value="index_department" :range="dictObj['department']">
					              <view class="picker">
					                {{dictObj['department'][index_department]}}
					              </view>
					    </picker>
				</u-form-item>
				<u-form-item label="专业" :label-width="auto"><u-input v-model="schoolInfo.major" /></u-form-item>
				<u-form-item label="班级">
					<picker @change="bindPickerChangeGrade" :value="index_grade" :range="dictObj['grade']">
					              <view class="picker">
					                {{dictObj['grade'][index_grade]}}
					              </view>
					    </picker>
				</u-form-item>
				<u-form-item label="学信网学籍" :label-width="auto" label-position="top">
					<u-upload @on-list-change="changePhotoListHeader" :auto-upload="false" custom-btn="true" :action="action" :file-list="fileList" width="120" height ="120" size-type="['compressed']" max-count="1">
						<template v-slot:addBtn >
							<image :src="schoolInfo.studentStatusImg ? $method.splitImgHost(schoolInfo.studentStatusImg) : '/static/info_4.png'" style="width: 120rpx; height: 120rpx;"></image>
						</template>  
					</u-upload> 
				</u-form-item>
				
			</u-form>
		</view>
		<button  @click="submitForm" class="submit_btn">提交</button>
	</view>
</template>

<script>
	import {mapGetters} from 'vuex';
	export default {
		data() {
			return {
				index_degree:0,
				index_system:0,
				index_department:0,
				index_grade:0,
				index_level:0,
				index_full:0,
				action: '33',
				fileList: [],
				defaultRegionCode:'440112',
				form:{
					date:'1990-01-01',
					region1: ['广东省', '广州市', '天河区'],
					region2: ['广东省', '广州市', '天河区'],
				},
				index_full_day: 0,
				full_day: ['男', '女'],	
				index_sex: 0,
				array_sex: ['男', '女'],	
				index_marry: 0,
				array_marry: ['未婚', '已婚'],	
				index_politic: 0,
				array_politic: ['群众', '团员', '党员'],	
				schoolInfo:{},
				admissionTime:'2011-09-01',
				graduationTime:'2015-09-01',
				studentStatusImg:''
			}
		},
		onLoad(option) {
			this.getSchoolInfo()
		},
		onShow(){
		},
		computed: {...mapGetters(['dictObj'])},
		methods: {	
			changePhotoListHeader(lists, name) {
				this.fileList = lists;
			},
			submitForm(){
				if(!this.schoolInfo.schoolName){
					uni.showModal({
						title: "提示",
						content: '学校名不能为空',
						showCancel: false
					})
					return
				}
				this.submitImage()
			},
			async submitImage(){
				console.log(this.fileList,33)
				if(this.fileList.length > 0&&this.fileList[0].url!=this.$method.splitImgHost(this.schoolInfo.studentStatusImg)){
					const waitYS = await this.imageInfos();
				}
				this.resultForm()
			},
			imageInfos(){
				var self = this
				return new Promise((resolve, reject) => {
					uni.getImageInfo({
						src: self.fileList[0].url,
						success: async res => {
							let canvasWidth = res.width; //图片原始长宽
							let canvasHeight = res.height;
							if (canvasWidth > 2000 || canvasHeight > 2000) {
								uni.compressImage({
									src: self.fileList[0].url,
									quality: 75,
									width: '35%',
									height: '35%',
									success: async rest => {
										const waitUpload = await self.uploadFile(rest.tempFilePath, 0);
										resolve(waitUpload)
									} 
								});
							} else if (canvasWidth > 1000 || canvasHeight > 1000) {
								uni.compressImage({
									src: self.fileList[0].url,
									quality: 75,
									width: '50%',
									height: '50%',
									success: async rest => {
										const waitUpload = await self.uploadFile(rest.tempFilePath, 0);
										resolve(waitUpload)
									} 
								});
							} else {
								console.log('无需压缩');
								const waitUpload = await self.uploadFile(self.fileList[0].url, 0);
								resolve(waitUpload)
							}
						}
					});
				});
			},
			uploadFile(options, int) {
				return new Promise((resolve, reject) => {
					var self = this;
					var data = {
						imageStatus: int
					};
					this.$api.aliyunpolicy(data).then(res => {
						var ossToken = res.data.data.resultContent;
						uni.uploadFile({
							url: ossToken.host,
							name: 'file',
							filePath: options,
							fileType: 'image',
							header: {
								AuthorizationToken: 'WX ' + uni.getStorageSync('token')
							},
							formData: {
								key: ossToken.dir,
								OSSAccessKeyId: ossToken.accessid,
								policy: ossToken.policy,
								Signature: ossToken.signature,
								callback: ossToken.callback,
								success_action_status: 200
							},
							success: result => {
								if (result.statusCode === 200) {
									self.schoolInfo.studentStatusImg = ossToken.dir;
									resolve();
								} else {
									uni.showToast({
										title: '上传失败',
										icon: 'none'
									});
									return;
								}
							},
							fail: error => {
								uni.showToast({
									title: '上传接口报错',
									icon: 'none'
								});
								return;
							}
						});
					});
				});
			},
			resultForm(){
				let that = this
				this.schoolInfo.fullTimeSchool = this.dictObj['full_time_school'][this.index_full]
				this.schoolInfo.admissionTime = this.admissionTime
				this.schoolInfo.graduationTime = this.graduationTime
				this.schoolInfo.education = this.dictObj['edu_level'][this.index_level]
				this.schoolInfo.academicDegree = this.dictObj['academic_degree'][this.index_degree]
				this.schoolInfo.educationalSystem = this.dictObj['educational_system'][this.index_system]
				this.schoolInfo.department = this.dictObj['department'][this.index_department]
				this.schoolInfo.grade = this.dictObj['grade'][this.index_grade]
				this.$api.updateSchoolInfo(this.schoolInfo).then(res => {
					if (res.data.code === 200) {
						setTimeout(()=>{
							uni.showToast({
								title: '提交成功',
								icon: 'none'
							});
						},500);
					}
				});
			},
			bindPickerChangeDegree(e){
				this.index_degree= e.detail.value
			},
			bindPickerChangeSystem(e){
				this.index_system= e.detail.value
			},
			bindPickerChangeDepartment(e){
				this.index_department= e.detail.value
			},
			bindPickerChangeGrade(e){
				this.index_grade= e.detail.value
			},
			bindPickerChangeFull(e){
				this.index_full= e.detail.value
			},
			bindPickerChangeLevel(e){
				this.index_level= e.detail.value
			},
			getSchoolInfo(){
				let that = this
				this.$api.getSchoolInfo().then(res => {
					if (res.data.code === 200) {
						that.schoolInfo = res.data.data
						console.log(that.schoolInfo)
						if(this.schoolInfo.fullTimeSchool)
							this.index_full = this.dictObj['full_time_school'].indexOf((this.schoolInfo.fullTimeSchool))
						if(this.schoolInfo.admissionTime)
							this.admissionTime = this.schoolInfo.admissionTime
						if(this.schoolInfo.graduationTime)
							this.graduationTime = this.schoolInfo.graduationTime
						if(this.schoolInfo.education)
							this.index_level = this.dictObj['edu_level'].indexOf((this.schoolInfo.education))
						if(this.schoolInfo.graduationTime)
							this.index_degree = this.dictObj['academic_degree'].indexOf((this.schoolInfo.academicDegree))
						if(this.schoolInfo.graduationTime)
							this.index_system = this.dictObj['educational_system'].indexOf((this.schoolInfo.educationalSystem))
						if(this.schoolInfo.graduationTime)
							this.index_department = this.dictObj['department'].indexOf((this.schoolInfo.department))
						if(this.schoolInfo.graduationTime)
							this.index_grade = this.dictObj['grade'].indexOf((this.schoolInfo.grade))
					}
				});
			},
			bindPickerChangeSex(e){
				this.index_sex= e.detail.value
			},
			bindPickerChangePolitic(e){
				this.index_politic= e.detail.value
			},
			bindPickerChangeMarry(e){
				this.index_marry= e.detail.value
			},
			handleGetRegion2(region){
				let array = []
			    array.push(region[0].name) 
				array.push(region[1].name) 
				array.push(region[2].name) 
				this.form.region2 = array
				
			},
			// 获取选择的地区
			handleGetRegion(region){
				let array = []
			    array.push(region[0].name) 
				array.push(region[1].name) 
				array.push(region[2].name) 
				this.form.region1 = array
				
			},
			bindDateChange(e){
				this.admissionTime=e.detail.value
			},
			bindDateChange2(e){
				this.graduationTime=e.detail.value
			},
			actionSheetCallback(index) {
				uni.hideKeyboard();
				this.form.sex_text = this.actionSheetList[index].text;
			},
		},
		
	}
</script>

<style scope>
	.submit_btn{
		background-color: #32467B !important;
		border-color: #32467B !important;
		color: #FFFFFF;
		font-size: 30rpx;
		padding: 5rpx;
	}
	.picker{
		text-align: right;
	}
	input{
		text-align: right;
	}
	.form{
		background: #FFFFFF;
		box-shadow: 0rpx 0rpx 16rpx 4rpx rgba(145, 156, 178, 0.1);
		border-radius: 32rpx;
		margin: 20rpx 0;
		padding: 15rpx;
	}
	.btn{
		width: 96rpx;
		height: 48rpx;
		background: #32467B;
		border-radius: 16rpx;
		font-size: 24rpx;
		color: #FFFFFF;
		line-height: 48rpx;
		text-align: center;
	}
	
page {
		background: #FDFDFD;
	}
	.tip{
		width: 70%;
		font-size: 20rpx;
		font-family: PingFang SC;
		font-weight: 400;
		color: #32467B;
	}
</style>
