<template>
	<view>
		<cpn-navbar title="设置" :isBack="true"></cpn-navbar>

		<view>
			<!-- 个人信息 -->
			<view class="user-info">
				<view class="user-info-title">个人信息</view>

				<view class="user-info-content">
					<view class="head-img">
						<view class="text">头像</view>
						<view style="display: flex">
							<view class="avatar">
								<u-avatar
									:src="vuex_user.upload.url || (vuex_user.sex === '男' ? vuex_male_img : vuex_female_img)"
									:size="104" mode="circle">
								</u-avatar>
							</view>
							<view class="arrow" @click="chooseAvatar">
								<u-icon name="arrow-right" size="24"></u-icon>
							</view>
						</view>
					</view>

					<view class="line"></view>

					<view class="name">
						<view class="name-text">账户名</view>
						<view class="name-user">{{ vuex_user.name || "" }}</view>
					</view>
				</view>
			</view>

			<!-- 版本信息 -->
			<view class="sys-info">
				<view class="sys-info-title">版本信息</view>

				<view class="sys-info-content">
					<view>当前版本</view>
					<view>V{{ vuex_version }}</view>
				</view>
			</view>

			<view class="logout">
				<u-button :hair-line="false" :custom-style="btnStyle" @click="logout">退出登录</u-button>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		ROOTPATH
	} from '@/common/config.js'
	export default {
		data() {
			return {
				btnStyle: {
					width: "670rpx",
					height: "76rpx",
					color: "#1479FF",
					border: "2rpx solid #1479FF",
				},
			};
		},
		methods: {
			logout() {
        if (this.vuex_my_role === 1) {
          this.$u.api.logout().then((res) => {
            uni.showToast({
              title: res.msg,
              icon: 'none'
            })
            setTimeout(() => {
              uni.navigateTo({
                url: "/pages/login/login",
                fail: (err) => {
                  console.log(err);
                },
              });
              uni.clearStorageSync();
            }, 1500)


          });
        } else {
          this.$u.api.adminLogout().then((res) => {
            uni.showToast({
              title: res.msg,
              icon: 'none'
            })
            setTimeout(() => {
              uni.navigateTo({
                url: "/pages/login/login",
                fail: (err) => {
                  console.log(err);
                },
              });
              uni.clearStorageSync();
            }, 1500)
          });
        }

			},
			chooseAvatar() {
				// 此为uView的跳转方法，详见"文档-JS"部分，也可以用uni的uni.navigateTo
				this.$u.route({
					// 关于此路径，请见下方"注意事项"
					url: '/uview-ui/components/u-avatar-cropper/u-avatar-cropper',
					// 内部已设置以下默认参数值，可不传这些参数
					params: {
						// 输出图片宽度，高等于宽，单位px
						destWidth: 300,
						// 裁剪框宽度，高等于宽，单位px
						rectWidth: 200,
						// 输出的图片类型，如果'png'类型发现裁剪的图片太大，改成"jpg"即可
						fileType: 'jpg',
					}
				})
			},
		},
		computed: {

		},
		created() {
			// 监听从裁剪页发布的事件，获得裁剪结果
			uni.$on('uAvatarCropper', path => {
				this.avatar = path;
				// 可以在此上传到服务端
				uni.uploadFile({
					url: `${ROOTPATH}/api/nurse/upload-file`,
					header: {
						Authorization: `Bearer ${this.vuex_token}`
					},
					filePath: path,
					name: 'file',
					success: (res) => {

						this.$u.api.save({
							avatar: JSON.parse(res.data).id
						}).then(() => {
							uni.showToast({
								icon: "none",
								title: '更换头像成功'
							})

							this.$store.dispatch('me')
						})

					},
					fail: (err) => {
						uni.showToast({
							icon: "none",
							title: '更换头像失败'
						})
					}
				});
			})
		},
	};
</script>

<style scoped lang="scss">
	.user-info {
		margin-top: 28rpx;

		&-title {
			height: 40rpx;
			font-size: 28rpx;
			font-family: PingFang-SC-Medium, PingFang-SC;
			font-weight: 500;
			color: #abaebe;
			line-height: 40rpx;

			padding-left: 40rpx;
		}

		&-content {
			width: 750rpx;
			background: #ffffff;
			box-shadow: 0rpx 4rpx 10rpx 0rpx rgba(219, 218, 218, 0.5);

			margin: 26rpx auto 0 auto;

			.head-img {
				display: flex;
				justify-content: space-between;
				align-items: center;

				&>view>view {
					align-self: center;
				}

				.text {
					font-size: 32rpx;
					font-family: PingFang-SC-Medium, PingFang-SC;
					font-weight: 500;
					color: #333333;

					padding-left: 40rpx;
				}

				.avatar {
					padding: 18rpx 0rpx;
				}

				.arrow {
					padding: 0 50rpx 0 40rpx;
				}
			}

			.line {
				width: 670rpx;
				height: 2rpx;
				border: 2rpx solid #eeeff5;

				margin: 0 auto;
			}

			.name {
				display: flex;
				align-items: center;
				justify-content: space-between;

				padding: 20rpx 40rpx;

				&-text {
					font-size: 32rpx;
					font-family: PingFang-SC-Medium, PingFang-SC;
					font-weight: 500;
					color: #333333;
				}

				&-user {
					font-size: 32rpx;
					font-family: PingFang-SC-Medium, PingFang-SC;
					font-weight: 500;
					color: #333333;
				}
			}
		}
	}

	.sys-info {
		margin-top: 34rpx;

		&-title {
			font-size: 28rpx;
			font-family: PingFang-SC-Medium, PingFang-SC;
			font-weight: 500;
			color: #abaebe;

			padding: 0 0 26rpx 36rpx;
		}

		&-content {
			background: #ffffff;
			box-shadow: 0rpx 4rpx 10rpx 0rpx rgba(219, 218, 218, 0.5);
			display: flex;
			justify-content: space-between;
			align-items: center;

			padding: 22rpx 40rpx;

			&>view {
				font-size: 32rpx;
				font-family: PingFang-SC-Medium, PingFang-SC;
				font-weight: 500;
				color: #333333;
			}
		}
	}

	.logout {
		margin-top: 40rpx;
	}
</style>
