<template>
	<view>
		<view class="se-pt-200 se-flex se-flex-h-c se-flex-ff-cw">
			<image class="se-w-150 se-h-150" src="@/static/image/logo.png" mode=""></image>
			<view class="se-flex se-flex-h-c se-fs-34 se-fw-6 se-c-black se-mt-40">
				智选猎头
			</view>
			<view class="se-mt-20 se-fs-26 se-c-black se-flex se-flex-h-c">
				申请获取您的头像、昵称
			</view>
		</view>
		<view class="se-mx-40 se-py-100">
			<view class="se-b-t se-b-b se-flex se-h-140 se-lh-140 se-flex-h-sb">
				<text>头像</text>
				<button class="se-w-100 se-h-100 se-p-0 se-m-0" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
					<image v-if="imagUrl" class="se-w-100 se-h-100"  :src="imagUrl"></image>
					<image v-else class="se-w-100 se-h-100" src="@/static/image/header.png" mode=""></image>
				</button>
				
			</view>
			<view class=" se-b-b se-flex se-h-140 se-lh-140  se-flex-h-sb">
				<text>昵称</text>
				<input type="nickname" placeholder="请输入昵称" v-model="nameUser"
						@blur="bindblur" class="se-h-80 se-lh-80 se-w-320 se-fs-24 se-ta-r" placeholder-class="se-fs-24 se-ta-r"   />
			</view>
			<view class=" se-b-b se-flex se-h-140 se-lh-140  se-flex-h-sb">
				<text>手机号</text>
				<button v-if="phone" class="default-btn se-h-100 se-p-0 se-m-0"  open-type="getPhoneNumber" @getphonenumber="onPhonenumber">{{phone}}</button>
				<button v-else class="se-w-140 se-h-60 se-lh-60 se-fs-20 se-bgc-orange se-c-white se-py-0 se-br-30 se-px-20 se-m-0" open-type="getPhoneNumber" @getphonenumber="onPhonenumber">
					获取手机号
				</button>
			</view>
		</view>
		<view class="se-mx-40">
			<view @click="onUpdateInfo" class="se-br-40 se-flex-h-c se-h-80 se-lh-80 se-ta-c se-fs-32 se-c-white se-bgc-orange">
				确认
			</view>
		</view>
	</view>
</template>

<script>
	import { updateUserInfo,bindPhone,userInfo,getSessionKey } from "@/common/api.js"
	export default {
		name : 'Login',
		data() {
			return {
				nameUser:"",
				imagUrl:"",
				phone:"",
				back:0,
				code:""
			}
		},
		onLoad(options) {
			console.info(options)
			// this.onSessionKey()
			if(options.back==1){
				this.back = options.back
				this.onUserInfo()
			}
		},
		methods: {
			onSessionKey(){
				// let that = this
				// uni.login({
				// 	success(res) {
				// 		that.code = res.code
				// 		getSessionKey({code:res.code}).then(response=>{
				// 			uni.setStorageSync('sessionKey',response.result)
				// 		}).catch(error=>{
							
				// 		})
				// 	}
				// })
			},
			onUserInfo(){
				let params={}
				userInfo(params).then(response=>{
					this.imagUrl=response.result.headImage
					this.nameUser=response.result.nickName?response.result.nickName:"微信昵称"
					this.phone=response.result.phone?response.result.phone:""
					
				}).catch(error=>{
					
				})
			},
			bindblur(event) {
				this.nameUser = event.target.value
			},
			onChooseAvatar(res) {
				let self = this
				self.$Oss.ossUpload(res.target.avatarUrl).then(url => {
					self.imagUrl = url
				})
			},
			onPhonenumber(event){
				let that = this
				var detail = event.detail;
				if(detail.errMsg=='getPhoneNumber:ok'){
					let params={
						code : detail.code,
						encryptedData: detail.encryptedData,
						iv:detail.iv,
					}
					bindPhone(params).then((response) => {
						console.info(response)
						let obj = JSON.parse(response.result)
						that.phone = obj.phone_info.phoneNumber
					}).catch(error=>{
						
					})
				}
			},
			onUpdateInfo(){
				let that = this
				uni.showLoading({
					title: '保存中...'
				})
				uni.login({
					success(res) {
						console.info(res.code)
						if(res.errMsg != "login:ok"){
							return
						}
						let params={
							code : res.code,
							headimgurl:that.imagUrl,
							nickName:that.nameUser,
							openid:uni.getStorageSync('userInfo').appletOpenid,
							phoneNumber:that.phone
						}
						// 
						if(that.imagUrl&&that.nameUser&&that.phone){
							updateUserInfo(params).then((response) => {
								if(that.back==1){
									uni.navigateBack({
										delta:1
									})
								}else{
									uni.navigateTo({
										url:"/pages/identity/index"
									})
								}
							}).catch((error) =>{
								
							})
						}else{
							return uni.showToast({
								icon:"none",
								title:"头像,昵称和手机号不能为空！"
							})
						}
					}
				})
				
				
			}
			
		}
	}
</script>

<style scoped lang="scss">
	.default-btn{
		background-color: #fff;
	}
	.default-btn::after{
		content: "";
		border: none;
	}
</style>
