<template>
	<view class="container">
		<view class="page">
			<view class="information">
				<view class="form">
					<uni-forms>
						<view class="form-item" @click="onOpenAvatar">
							<view class="form-item-fl">
								头像
							</view>
							<view class="form-item-fr">
								<image :src="formData.avatar || 'https://life.cloud.hjxbc.cn/sys/common/static/miniapp/avatar.png'" class="icon"></image>
								<uni-icons type="forward" color="#E4E4EA"></uni-icons>
							</view>
						</view>
						<view class="form-item" @click="onOpenAvatar">
							<view class="form-item-fl">
								姓名
							</view>
							<view class="form-item-fr">
								<view class="form-item-tit">{{formData.residentName}}</view>
								<!-- <input class="form-item-tit" v-model="formData.residentName" focus placeholder="请输入姓名" /> -->
							</view>
						</view>
						<view class="form-item">
							<view class="form-item-fl">
								性别
							</view>
							<view class="form-item-fr">
								<uni-data-checkbox v-model="formData.sex" :localdata="sexs" class="form-item-tit" />
							</view>
						</view>
						<view class="form-item">
							<view class="form-item-fl">
								年龄
							</view>
							<view class="form-item-fr">
								<input class="form-item-tit" v-model="formData.age" focus placeholder="请输入年龄" />
							</view>
						</view>
						<view class="form-item">
							<view class="form-item-fl">
								<view class="asterisk">*</view>手机号码
							</view>
							<view class="form-item-fr">
								<view class="form-item-tit">{{formData.residentPhone}}</view>
							</view>
						</view>
						<view class="form-item">
							<view class="form-item-fl">
								地址
							</view>
							<view class="form-item-fr">
								<view class="form-item-tit">{{formData.address}}</view>
							</view>
						</view><strong></strong>
					</uni-forms>
				</view>

				<view class="form-submit" @click="onSaveInfo">保存</view>
			</view>
		</view>

		<user-vatar ref="userVatar" @saveUserOk="saveUserOk" />
	</view>
</template>

<script>
	import userVatar from '@/components/user-vatar.vue';
	import {
		uploadFile,
		getUserInfoApi,
		editOwnerApi
	} from '@/config/api.js'
	import serverConfig from "@/config/server_config.js";
	export default {
		components: {
			userVatar
		},
		data() {
			return {
				formData: {
					avatar: '',
					temAvatar: '',
					residentName: '',
					sex: '',
					age: '',
					address: '',
					residentPhone: '',
					platformCode: ''
				},
				sexs: [
					{ text: '男', value: 1 },
					{ text: '女', value: 2 }
				],
				hasTmp: false
			}
		},
		methods: {
			async getUserInfo() {
				let userType = uni.getStorageSync('user_type')
				let {result} = await getUserInfoApi({userType})
				if(userType === '1') {
					let avatar = result.avatar ? `${serverConfig.imageURL}${result.avatar}` : ''
					this.formData = {
						avatar,
						temAvatar: result.avatar,
						residentName: result.residentName,
						sex: result.sex,
						age: result.age,
						residentPhone: result.residentPhone,
						address: result.platformName,
						platformCode: result.platformCode
					}
				} else {
					let avatar = result.employeeAvatar ? `${serverConfig.imageURL}${result.employeeAvatar}` : ''
					this.formData = {
						avatar,
						temAvatar: result.employeeAvatar,
						residentName: result.employeeName,
						sex: result.sex,
						age: result.age,
						residentPhone: result.employeePhone,
						address: result.platformName,
						platformCode: result.platformCode
					}
				}
			},
			onOpenAvatar() {
				this.$refs.userVatar.userForm.userAvatarUrl = this.formData.avatar
				this.$refs.userVatar.userForm.nickname = this.formData.residentName
				this.$nextTick(()=> {
					this.$refs.userVatar.$refs.userProfileShow.open()
				})
			},
			saveUserOk(form) {
				this.formData.avatar = form.userAvatarUrl
				this.formData.residentName = form.nickname
				this.hasTmp = form.isTmp
			},
			async onSaveInfo() {
				let uploadImg = ''
				if(this.hasTmp) {
					let {message} = await uploadFile({
						filePath: this.formData.avatar,
						name: 'file',
						formData: {                                  //其他需要携带的参数
							'biz': 'scott/pic'
						}
					})
					uploadImg = message
				}
				let avatar = uploadImg || this.formData.temAvatar
				let userType = uni.getStorageSync('user_type')
				await editOwnerApi({
					avatar,
					residentName: this.formData.residentName,
					sex: this.formData.sex,
					age: this.formData.age,
					residentPhone: this.formData.residentPhone,
					platformCode: this.formData.platformCode,
					userType
				})
				let user_avatar = avatar ? `${serverConfig.imageURL}${avatar}` : ''
				uni.setStorageSync('user_avatar', user_avatar)
				uni.setStorageSync('user_name', this.formData.residentName)
				var pages = getCurrentPages();
				var page = pages[pages.length - 2];
				page.$vm.userAvatar = user_avatar
				page.$vm.userName = this.formData.residentName
				setTimeout(()=> {
					uni.showToast({
						title: '保存成功！',
						icon: 'none'
					});
					setTimeout(()=> {
						uni.navigateBack()
					}, 1500)
				}, 30)
			}
		},
		onLoad() {
			this.getUserInfo()
		}
	}
</script>
<style lang="scss" scoped>
	.page {
		padding: 30rpx;
	}

	.information {

		.form {
			background-color: #ffffff;
			border-radius: 16rpx;
			padding: 30rpx;
			margin-bottom: 30rpx;

			.form-item {
				display: flex;
				align-items: center;
				justify-content: space-between;
				height: 84rpx;
				line-height: 84rpx;
				border-bottom: 1px solid #F8F6F9;
				overflow: hidden;
			}

			.form-item-fl {
				font-size: 28rpx;
				color: #373737;
				display: flex;

				.asterisk {
					color: #F42E2E;
					width: 10rpx;
					margin-right: 6rpx;
				}

			}

			.form-item-fr {
				display: flex;
				flex: 2;
				align-items: center;
				justify-content: flex-end;
				margin-left: 20rpx;

				.icon {
					height: 64rpx;
					width: 64rpx;
					margin-right: 20rpx;
				}
			}

			.form-item-tit {
				font-size: 28rpx;
				color: #666;
				margin-right: 20rpx;
				text-align: right;
			}
		}
	}

	.form-submit {
		height: 80rpx;
		line-height: 80rpx;
		background-color: #6A59F2;
		border-radius: 80rpx;
		width: 100%;
		text-align: center;
		font-size: 28rpx;
		color: #FFFFFF;
	}
</style>