<template>
	<view class="edit-info">
		<view class="user-info">
			<view class="info-avatar" @click="changeAvatar">
				<u-avatar :src="avatar" :size="120"></u-avatar>
				<view class="avatar-text">更换头像</view>
			</view>
			<view class="info-item">
				<view class="title">昵称</view>
				<view class="value">
					<input type="text" v-model="nickname" />
				</view>
			</view>
			<view class="info-item">
				<view class="title">性别</view>
				<view class="value" @click="selectGender=true">{{ gender == '1' ? '男' : '女' }}</view>
			</view>
			<u-select v-model="selectGender" mode="single-column" :list="list" @confirm="setGender"></u-select>
			<view class="info-item">
				<view class="title">生日</view>
				<view class="value" @click="selectBirthday=true">{{ birthday }}</view>
			</view>
			<u-picker mode="time" v-model="selectBirthday" :params="params" @confirm="setBirthday"></u-picker>
			<view class="info-item phone" @click="updatePhone">
				<view class="title">手机号</view>
				<view class="value">{{ phoneNumber }}</view>
				<image src="../static/mine/21.png"></image>
			</view>
		</view>
		<view class="edit-tips">注意：修改手机号需要原手机号获取验证码，无原手机验证码，请联系客服</view>
		<view class="edit-btn" @click="updateMemberInfo">完成</view>
	</view>
</template>
<script>
import common from '@/static/js/common.js'
export default {
	data() {
		return {
			selectGender: false,
			selectBirthday: false,
			list: [
				{
					value: 1,
					label: '男'
				},
				{
					value: 2,
					label: '女'
				}
			],
			params: {
				year: true,
				month: true,
				day: true,
				hour: false,
				minute: false,
				second: false
			},
			nickname: '',
			gender: '', // 1男  2女
			birthday: '',
			phoneNumber: '',
			avatar: '',
			uploadPath: '', // 上传时的路径
		}
	},
	onLoad() {
		this.getUserInfo();
	},
	onNavigationBarButtonTap(e) {
		if( e.index == 0 ) uni.navigateBack();
	},
	methods: {
		changeAvatar() {
			const url = this.$u.http.config.baseUrl + '/Upload/uploadfile';
			uni.chooseImage({
				count: 1,
				sizeType: ['original', 'compressed'],
				sourceType: ['album'],
				success: (res) => {
					this.avatar = res.tempFilePaths[0];
					common.uploadFile({ 
						url: url,
						name: 'avatar',
						filePath: res.tempFilePaths[0]
					}).then(result => {
						// console.log(result);
						this.$set(this, 'avatar', result.file_path);
						// this.avatar = result.file_path;
						this.uploadPath = result.file_name;
					}, error => {
						this.$u.toast(error);
					})
				}
			});
		},
		getUserInfo() {
			this.$u.api.getMemberInfo().then(res => {
				if (res.errCode == 0) {
					let userInfo = res.data.MemberArray;
					[this.nickname, this.phoneNumber, this.birthday, this.gender, this.avatar] = [
						userInfo.member_nickname,
						userInfo.member_mobile,
						userInfo.member_birthday,
						userInfo.member_sex,
						userInfo.member_avatar,
					];
				}
			})
		},
		updateMemberInfo() {
			this.$u.api.updateMemberInfo({
				nickname: this.nickname,
				gender: this.gender,
				avatar: this.uploadPath,
				birthday: this.birthday,
			}).then(res => {
				if (res.errCode == 0) {
					this.getUserInfo();
				}
			})
		},
		setGender(value) {
			// console.log(value);
			this.gender = value[0].value;
		},
		setBirthday(value) {
			// console.log(value);
			this.birthday = value.year + '-' + value.month + '-' + value.day;
		},
		updatePhone() {
			uni.navigateTo({
				url: '/pageE/mine/UpdatePhone'
			});
		}
	},
};
</script>
<style lang="scss" scoped>
.edit-info {
	min-height: calc(100vh - var(--window-top));
	background-color: #ECECEC;
	padding-top: 1rpx;
	.user-info {
		.info-avatar {
			height: 202rpx;
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			background: rgba(255,255,255,1);
			.avatar-text {
				margin-top: 12rpx;
				font-size: 24rpx;
				color: rgba(255,120,15,1);
			}
		}
		.info-item {
			display: flex;
			align-items: center;
			padding: 35rpx 30rpx;
			border-top: 2rpx solid #ECECEC;
			font-size: 30rpx;
			height: 98rpx;
			background: rgba(255,255,255,1);
			.title {
				width: 240rpx;
				color: rgba(51,51,51,1);
			}
			.value {
				flex: 1;
				height: 98rpx;
				line-height: 98rpx;
				color: rgba(102,102,102,1);
				> input {
					height: 98rpx;
				}
			}
		}
		.phone {
			> image {
				margin-left: auto;
				width: 11rpx;
				height: 22rpx;
			}
		}
	}
	.edit-tips {
		font-size: 24rpx;
		color: rgba(102,102,102,1);
		line-height: 40rpx;
		margin: 20rpx 0 120rpx;
		padding: 0 30rpx;
	}
	.edit-btn {
		margin: 0 auto;
		width: 690rpx;
		height: 98rpx;
		background: rgba(255,120,15,1);
		border-radius: 49rpx;
		font-size: 36rpx;
		color: rgba(255,255,255,1);
		line-height: 98rpx;
		text-align: center;
	}
}
</style>