<template>
	<view class="bg-white">
		<!-- 顶部操作条 -->
		<cu-custom :bgColor="'bg-main-color'" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">头像及昵称修改</block>
		</cu-custom>
		<view class="margin-top-lg margin-lr">
			<view class="text-center padding-tb-lg">
				<button class="cu-avatar round"
					:style="'width: 120rpx; height: 120rpx; background-image:url(' + avatarUrl  + ');'"
					open-type="chooseAvatar" @chooseavatar="onChooseAvatar"></button>
			</view>
			<!-- <button class="avatar-wrapper" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
			  <image class="avatar" :src="{{avatarUrl}}"></image>
			</button> -->
			<form @submit="submit">
				<view class="cu-form-group">
					<view class="title">昵称</view>
					<input type="nickname" name="nickname" v-model="nickname" placeholder="请输入昵称"></input>
				</view>
				<view class="padding-tb">
					<button class="bg-main-color long-btn cu-btn shadow-blur radius" form-type="submit">提交</button>
				</view>
			</form>
		</view>
	</view>
</template>

<script>
	export default {
		onLoad() {
			this.loadCurBasicInfo();
		},
		data() {
			return {
				curUserInfo: {},
				avatarUrl: "",
				nickname: ""
			}
		},
		methods: {
			loadCurBasicInfo() {
				this.curUserInfo = this.$request.getCurUserInfo();
				this.avatarUrl = this.curUserInfo.customerLogoUrl;
				this.nickname = this.curUserInfo.name;
			},
			onChooseAvatar(e) {
				this.$request.uploadFile(e.detail.avatarUrl).then((url) => {
					this.avatarUrl = url;
				});
			},
			async submit(e) {
				let res = await this.$request.updateUser({
					customerId: this.curUserInfo.customerId,
					name: e.detail.value.nickname,
					customerLogoUrl: this.avatarUrl
				})
				if (res && res.code === 0) {
					uni.showToast({
						icon: 'success',
						title: '更新成功'
					})
					let timeout = setTimeout(() => {
						uni.navigateTo({
							url: '/pages/index/index?menuCode=myPage'
						})
						clearTimeout(timeout);
					}, 1500);
				}
			}
		}
	}
</script>
