<template>
	<view class="content">
		<view class="" v-if="type=='email'">
			<u-input v-model="email" border="bottom" placeholder="请输入邮箱账号"></u-input>
		</view>
		<view class="" v-if="type=='username'">
			<u-input v-model="username" border="bottom" placeholder="请输入昵称"></u-input>
			
		</view>
		<view class="" v-if="type=='sex'">
			<picker @change="bindPickerChange" :value="index" :range="array">
				<u-input v-model="sex" disabled disabledColor="#fff" border="bottom" placeholder="请选择性别"></u-input>
			</picker>
		</view>
		<view class="btn" @click="bind">确定</view>
	</view>
</template>

<script>
	import $api from '@/static/js/api.js'
	var that = ''
	export default {
		data() {
			return {
				email: '',
				type: '',
				username: '',
				sex: '',
				array: ["男","女"]
			}
		},
		onLoad(options) {
			that = this
			console.log(options);
			that.type = options.type
			
		},
		onReady() {
			var title = ""
			switch(that.type) {
				case 'username': 
					title = '修改昵称';
					break;
				case 'sex':
					title = '修改性别';
					break;
				case 'email':
					title = '邮箱绑定';
					break;
			}
			uni.setNavigationBarTitle({
				title: title
			})
		},
		methods: {
			// 修改信息
			bind() {
				var data = {}
				var url = ''
				switch(that.type) {
					case 'username': 
						data.nickname = that.username;
						url = '/data/api.business.User/user_edit';
						break;
					case 'sex':
						data.sex = that.sex == '男'?'1':'2';
						url = '/data/api.business.User/user_edit';
						break;
					case 'email':
						data.mailbox = that.email;
						url = '/data/api.business.User/user_mailbox';
						break;
				}
				$api.req({
					url: url,
					method: 'POST',
					data: data
				}, function(res) {
					if(res.code == 1) {
						$api.info(res.info)
						if(that.type == 'username') {
							let options = {
								nickname: that.username,
							}
							uni.WebIM.conn.updateOwnUserInfo(options).then((res) => {
							    console.log(res)
							})
						}
						$api.jump(1,-1)
						
					}
				})
			},
			// 选择性别
			bindPickerChange(e) {
				that.sex = that.array[e.detail.value]
			}
		},
	}
</script>

<style lang="scss" scoped>
	.content::v-deep {
		background-color: #fff;
		padding: 32rpx 30rpx 0;
		.u-input {
			padding: 28rpx 0 !important;
		}
		.btn {
			margin: 112rpx auto 0;
			width: 630rpx;
			height: 92rpx;
			border-radius: 50rpx;
			background-color: #506dff;
			color: #fff;
			font-size: 36rpx;
			text-align: center;
			line-height: 92rpx;
		}
	}
</style>