<template>
	<mobile-frame>
		<view class="info">
			<view class="one">
				<uni-forms ref="form" :modelValue="form" :rules="rules" label-width="auto">
					<uni-forms-item label="头像" name="icon">
						<view class="icon">
							<view class="icon_1" v-if="form.icon&&form.icon.length>0">
								<image class="image" v-for="i in form.icon" :key="i" :src="i.url" mode=""></image>
								<uni-icons class="del" type="close" size="30" color="#ff0000" @click="iconDel()">
								</uni-icons>
							</view>
							<view class="icon_2" v-else>
								<button open-type="chooseAvatar" @chooseavatar="chooseavatar">上传头像</button>
							</view>
						</view>
					</uni-forms-item>
					<uni-forms-item label="用户名" name="name">
						<input class="input" type="nickname" :value="form.name" placeholder="请输入用户名" @input="nameInput">
						<!-- <uni-easyinput type="nick" v-model="form.name" placeholder="请输入用户名" /> -->
					</uni-forms-item>
					<uni-forms-item label="性别" name="gender">
						<picker @change="genderChange" name="gender" :value="form.gender" :range="genderList"
							range-key="label">
							<view class="uni-input">{{form.zhGender||'请选择性别'}}</view>
						</picker>
					</uni-forms-item>
					<uni-forms-item label="生日" name="birth">
						<picker mode="date" :value="form.birth" name="birth" @change="bindDateChange">
							<view class="uni-input">{{form.birth||'请选择日期'}}</view>
						</picker>
					</uni-forms-item>
				</uni-forms>
				<view class="btn">
					<button type="primary" @click="onSubmit('form')" size="small">提交</button>
				</view>
			</view>
		</view>
	</mobile-frame>
</template>

<script>
	export default {
		components: {},
		data() {
			return {
				frameStyle: {
					useBar: false
				},
				form: {
					icon: []
				},
				rules: {
					name: {
						rules: [{
							required: true,
							errorMessage: '请输入用户名',
						}]
					},
					phone: {
						rules: [{
							required: true,
							errorMessage: '请输入手机号',
						}]
					},
				},
				genderList: [],
			};
		},
		onLoad: async function(e) {
			const that = this;
			await that.searchOther();
			that.watchLogin();
		},
		methods: {
			async searchOther() {
				const that = this;
				let res;
				res = await that.$api(`/dictData`, 'GET', {
					code: "gender"
				});
				if (res.errcode == '0') {
					that.$set(that, `genderList`, res.data)
				}
			},
			watchLogin() {
				const that = this;
				uni.getStorage({
					key: 'token',
					success: async (res) => {
						let user = that.$jwt(res.data);
						if (user) {
							const arr = await that.$api(`/user/${user.id}`, 'GET');
							if (arr.errcode == '0') {
								let gender = that.genderList.find(i => i.value == arr.data
									.gender)
								if (gender) arr.data.zhGender = gender.label;
								that.$set(that, `form`, arr.data)
							} else {
								uni.showToast({
									title: arr.errmsg,
									icon: 'error',
									duration: 2000
								});
							}
						}
					},
					fail: (err) => {}
				})
			},
			toPath(e) {
				if (e && e.route) uni.redirectTo({
					url: `/${e.route}`
				})
			},
			// 删除头像
			iconDel() {
				const that = this;
				that.$set(that.form, `icon`, [])
			},
			// 头像上传
			async chooseavatar(e) {
				const that = this;
				let avatarUrl = e.detail.avatarUrl;
				let serverUrl = that.$config.serverUrl;
				const res = await that.$apifile(`/point/upload`, 'file', avatarUrl, 'file');
				if (res.errcode == '0') {
					let obj = {
						name: res.name,
						uri: res.uri,
						url: serverUrl + res.uri
					}
					that.$set(that.form, `icon`, [obj])
				} else {
					uni.showToast({
						title: res.errmsg,
						icon: 'none'
					})
				}
			},
			// 选择,输入昵称
			nameInput(e) {
				const that = this;
				that.$set(that.form, `name`, e.detail.value);
			},
			// 选择性别
			genderChange(e) {
				const that = this;
				let data = that.genderList[e.detail.value];
				if (data) {
					that.$set(that.form, `gender`, data.value);
					that.$set(that.form, `zhGender`, data.label);
				}
			},
			// 选择日期
			bindDateChange(e) {
				const that = this;
				that.$set(that.form, `birth`, e.detail.value);
			},
			// 返回
			back() {
				uni.navigateBack({
					delta: 1
				})
			},
			// 提交保存
			onSubmit(ref) {
				const that = this;
				that.$refs[ref].validate().then(async params => {
					const arr = await that.$api(`/user/${that.form.id}`, 'POST', params);
					if (arr.errcode == '0') {
						uni.showToast({
							title: `维护信息成功`,
							icon: 'success',
						});
						that.back();
					} else {
						uni.showToast({
							title: arr.errmsg,
						})
					}
				})
			},
		}
	}
</script>

<style lang="scss">
	.info {
		display: flex;
		flex-direction: column;
		width: 100vw;
		height: 100vh;

		.one {
			padding: 2vw;

			.uni-input {
				border: #f1f1ff 1px solid;
				padding: 2vw 2vw;
				border-radius: 1vw;
			}

			.input {
				border: #f1f1ff 1px solid;
				padding: 1.5vw 1vw;
				border-radius: 5px;
			}

			.btn {
				text-align: center;

				button {
					margin: 0 2vw 2vw 2vw;
					background-color: var(--fFB1Color);
					color: var(--fffColor);
				}

				.name {
					color: var(--f85Color);
					font-size: var(--font14Size);
				}
			}

			.icon {
				display: flex;

				.icon_1 {
					position: relative;

					.image {
						width: 30vw;
						height: 30vw;
					}

					.del {
						position: absolute;
						top: 0;
						left: 22vw;
					}
				}

				.icon_2 {
					text-align: left;

					button {
						width: 30vw;
						padding: 0vw 1vw;
						font-size: 14px;
					}
				}
			}
		}
	}

	.uni-forms-item {
		margin-bottom: 6vw !important;
		display: flex;
		flex-direction: row;
	}
</style>
