<template>
	<view class="main">
		<view class="one">
			<upload class='upload' :list="user.logo" name="logo" :count="1" @uplSuc="uplSuc" @uplDel="uplDel">
			</upload>
		</view>
		<view class="two">
			<view class="two_1">
				<view class="left">真实姓名</view>
				<view class="right">
					<input v-model="user.name" placeholder="请输入真实姓名" />
					<text class="iconfont icon-dayuhao"></text>
				</view>
			</view>
			<view class="two_1">
				<view class="left">昵称</view>
				<view class="right">
					<input v-model="user.nick_name" placeholder="请输入昵称" />
					<text class="iconfont icon-dayuhao"></text>
				</view>
			</view>
			<view class="two_1">
				<view class="left">性别</view>
				<view class="right">
					<picker class="picker" mode="selector" :range="genderList" @change="genderChange" range-key="label">
						<view>{{gender_name||'请选择性别'}}</view>
					</picker>
					<text class="iconfont icon-dayuhao"></text>
				</view>
			</view>
			<view class="two_1">
				<view class="left">手机号</view>
				<view class="right">
					<input v-model="user.phone" placeholder="请输入手机号" />
					<text class="iconfont icon-dayuhao"></text>
				</view>
			</view>
			<view class="two_1">
				<view class="left">生日</view>
				<view class="right">
					<picker mode="date" :value="user.birthday" :start="startDate" :end="endDate"
						@change="bindDateChange">
						<view>{{user.birthday||'请选择生日'}}</view>
					</picker>
					<text class="iconfont icon-dayuhao"></text>
				</view>
			</view>
			<view class="two_1">
				<view class="left">所在城市</view>
				<view class="right">
					<picker class="picker" :value="user.city" mode="selector" :range="cityList" @change="cityChange"
						range-key="name">
						<view>{{user.city||'请选择所在城市'}}</view>
					</picker>
					<text class="iconfont icon-dayuhao"></text>
				</view>
			</view>
		</view>
		<view class="thr">
			<button class="button" type="primary" size="mini" @click="onSubmit()">保存</button>
		</view>
	</view>
</template>

<script>
	import upload from '../../components/upload/index.vue';
	export default {
		components: {
			upload
		},
		data() {
			return {
				user: {},
				gender_name: '',
				// 字典表
				genderList: [],
				cityList: [],
			}
		},
		onLoad: async function() {
			const that = this;
			await that.searchOther();
			await that.search();
		},
		onShow: async function(e) {
			const that = this;
			that.searchToken();
		},
		methods: {
			searchToken() {
				const that = this;
				try {
					const res = uni.getStorageSync('token');
					if (res) that.$set(that, `user`, res);
				} catch (e) {
					uni.showToast({
						title: err.errmsg,
						icon: 'error',
						duration: 2000
					});
				}
			},
			// 查询
			async search() {
				const that = this;
				let data = that.genderList.find(i => i.value == that.user.gender);
				if (data) that.$set(that, `gender_name`, data.label)
			},
			// 图片上传
			uplSuc(e) {
				const that = this;
				that.$set(that.user, `logo`, [e.data]);
			},
			// 图片删除
			uplDel(e) {
				const that = this;
				that.$set(that.user, `logo`, [])
			},
			// 性别选择
			genderChange(e) {
				const that = this;
				let data = that.genderList.find(i => i.value == e.detail.value);
				if (data) {
					that.$set(that.user, `gender`, data.value)
					that.$set(that, `gender_name`, data.label)
				}
			},
			// 生日选择
			bindDateChange: function(e) {
				const that = this;
				that.$set(that.user, `birthday`, e.detail.value)
			},
			// 城市选择
			cityChange(e) {
				const that = this;
				let data = that.cityList[e.detail.value];
				that.$set(that.user, `city`, data.name)
			},
			// 保存
			async onSubmit() {
				const that = this;
				const form = that.user;
				delete form.gender_name
				const res = await that.$api(`/user/${form._id}`, 'POST', form);
				if (res.errcode == '0') {
					uni.showToast({
						title: '维护信息成功',
						icon: 'none'
					})
					let arr = await that.$api(`/user/${form._id}`, 'GET', {})
					uni.setStorage({
						key: 'token',
						data: arr.data,
						success: function(res) {
							uni.navigateBack({
								delta: 1
							})
						},
						fail: function(err) {
							console.log(err);
						}
					})
				} else {
					uni.showToast({
						title: res.errmsg,
						icon: 'none'
					})
				}
			},
			async searchOther() {
				const that = this;
				let res;
				// 查询类型
				res = await that.$api(`/dictData`, 'GET', {
					type: 'gender',
					is_use: '0',
				})
				if (res.errcode == '0') that.$set(that, `genderList`, res.data);
				let config = that.$config;
				that.$set(that, `cityList`, config.china);
			}
		}
	}
</script>

<style lang="scss" scoped>
	.main {
		.one {
			display: flex;
			justify-content: center;
			padding: 2vw;
		}

		.two {
			padding: 2vw;

			.two_1 {
				display: flex;
				justify-content: space-between;
				padding: 4vw;
				border-bottom: 1px solid var(--f9Color);
				font-size: var(--font14Size);
				color: var(--f69Color);

				.right {
					display: flex;
					align-items: center;

					input {
						text-align: right;
						padding: 0 1vw;
					}
				}
			}
		}

		.thr {
			text-align: center;

			.button {
				margin: 2vw 0 0 0;
				background-color: var(--f3CColor);
				color: var(--mainColor);
				font-size: var(--font14Size);
			}
		}
	}
</style>