<template>
	<view class="main">
		<view class="one">
			<view class="one_1">
				<image class="image" :src="form.icon&&form.icon.length>0?form.icon[0].url:config.user_url[0].url"
					mode=""></image>
				<view class="right">
					<text class="name">{{form.name||'微信用户'}}</text>
					<text class="other" v-if="form.account">账号：{{form.account||'暂无手机号'}}</text>
					<text class="other">手机：{{form.mobile||'暂无手机号'}}</text>
				</view>
				<view class="right"></view>
			</view>
		</view>
		<view class="two">
			<view class="list" v-for="(item, index) in menuList" :key="index" @click="toCommon(item.route)">
				<view class="left">
					<view class="icon">
						<text class="iconfont" :class="[item.icon]"></text>
					</view>
					<text class="title">{{item.title||'暂无'}}</text>
				</view>
				<view class="right">
					<text class="iconfont icon-dayuhao"></text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				user: {},
				config: {},
				form: {},
				// 菜单
				menuList: []
			}
		},
		onShow: async function(e) {
			const that = this;
			await that.searchConfig();
			await that.searchToken();
			await that.searchOther();
			await that.search();
		},
		methods: {
			// 用户信息
			searchToken() {
				const that = this;
				try {
					const res = uni.getStorageSync('token');
					if (res) {
						const user = that.$jwt(res);
						that.$set(that, `user`, user);
					}
				} catch (e) {}
			},
			searchConfig() {
				const that = this;
				try {
					const res = uni.getStorageSync('config');
					if (res) that.$set(that, `config`, res);
				} catch (e) {}
			},
			// 查询
			async search() {
				const that = this;
				const user = that.user
				if (user) {
					let res;
					if (user.role == 'Doctor') res = await that.$api(`/doctor/${user._id}`, 'GET', {})
					else if (user.role == 'Nurse') res = await that.$api(`/nurse/${user._id}`, 'GET', {})
					else res = await that.$api(`/patient/${user._id}`, 'GET', {})
					if (res.errcode == '0') {
						that.$set(that, `form`, res.data)
					} else {
						uni.showToast({
							title: res.errmsg,
							icon: 'none'
						});
					}
				}
			},
			// 公共跳转
			toCommon(e) {
				uni.navigateTo({
					url: `/${e}`
				})
			},
			async searchOther() {
				const that = this;
				let config = that.$config;
				let menu = []
				for (let val of config.menuList) {
					const role = val.role.find((i) => i == that.user.role);
					if (role) menu.push(val)
				}
				that.$set(that, `menuList`, menu);
			}
		}
	}
</script>

<style lang="scss" scoped>
	.main {
		display: flex;
		flex-direction: column;
		width: 100vw;
		height: 100vh;
		background-image: linear-gradient(to top, rgba(241, 241, 241, 1), rgba(241, 241, 241, 1), rgba(0, 122, 255, 1));

		.one {
			.one_1 {
				padding: 15vw 5vw;
				display: flex;
				align-items: center;

				.image {
					width: 15vw;
					height: 15vw;
					border-radius: 2vw;
					margin: 0 2vw;
				}

				.right {
					display: flex;
					flex-direction: column;

					.name {
						font-size: var(--font16Size);
					}

					.other {
						margin: 2px 0 0 0;
						font-size: var(--font12Size);
						color: var(--f99Color);
					}
				}
			}
		}

		.two {
			display: flex;
			flex-direction: column;
			padding: 2vw;
			margin: 2vw;
			border-radius: 10px;
			background-color: var(--mainColor);

			.list {
				display: flex;
				justify-content: space-between;
				padding: 2vw;
				border-bottom: 1px solid var(--f9Color);

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

					.icon {
						padding: 0 1vw 0 0;

						.iconfont {
							color: var(--f3CColor);
							font-size: var(--font18Size);
						}
					}

					.title {
						display: inline-block;
						font-size: var(--font14Size);
					}
				}

				.right {
					font-size: var(--font12Size);
					color: var(--f99Color);
				}
			}
		}
	}
</style>