<template>
	<view class="container">
		<view class="page">
			<view class="user">
				<image :src="userAvatar || 'https://life.cloud.hjxbc.cn/sys/common/static/miniapp/avatar.png'" class="icon"></image>
				<view class="name" v-if="userName">{{userName}}</view>
				<view class="name" v-else @click="quickEntry">请认证</view>
				<view class="phone" v-if="userName">{{userPhone}}</view>
			</view>
			<view class="service">
				<template v-if="userType === '2'">
					<view class="item" @click="employeeEntry(1)">
						<view class="left">
							<image src="../../static/images/user01.png" class="icon"></image>
							<div class="title">个人资料</div>
						</view>
						<image src="../../static/images/icon_more.png" class="arrow"></image>
					</view>
					<view class="item" @click="employeeEntry(2)">
						<view class="left">
							<image src="../../static/images/user03.png" class="icon"></image>
							<div class="title">我的维修</div>
						</view>
						<image src="../../static/images/icon_more.png" class="arrow"></image>
					</view>
					<view class="item" @click="quickEntry(7)">
						<view class="left">
							<image src="../../static/images/user07.png" class="icon"></image>
							<div class="title">切换业主</div>
						</view>
						<image src="../../static/images/icon_more.png" class="arrow"></image>
					</view>
				</template>
				<template v-else>
					<view class="item" @click="quickEntry(1)">
						<view class="left">
							<image src="../../static/images/user01.png" class="icon"></image>
							<div class="title">个人资料</div>
						</view>
						<image src="../../static/images/icon_more.png" class="arrow"></image>
					</view>
					<view class="item" @click="quickEntry(2)">
						<view class="left">
							<image src="../../static/images/user02.png" class="icon"></image>
							<div class="title">我的房屋</div>
						</view>
						<image src="../../static/images/icon_more.png" class="arrow"></image>
					</view>
					<view class="item" @click="quickEntry(3)">
						<view class="left">
							<image src="../../static/images/user03.png" class="icon"></image>
							<div class="title">我的报修</div>
						</view>
						<image src="../../static/images/icon_more.png" class="arrow"></image>
					</view>
					<view class="item" @click="quickEntry(4)">
						<view class="left">
							<image src="../../static/images/user04.png" class="icon"></image>
							<div class="title">我的投诉</div>
						</view>
						<image src="../../static/images/icon_more.png" class="arrow"></image>
					</view>
					<view class="item" @click="quickEntry(5)">
						<view class="left">
							<image src="../../static/images/user05.png" class="icon"></image>
							<div class="title">缴费记录</div>
						</view>
						<image src="../../static/images/icon_more.png" class="arrow"></image>
					</view>
					<view class="item" @click="quickEntry(6)">
						<view class="left">
							<image src="../../static/images/user06.png" class="icon"></image>
							<div class="title">我的收据</div>
						</view>
						<image src="../../static/images/icon_more.png" class="arrow"></image>
					</view>
					<view class="item" @click="quickEntry(9)">
						<view class="left">
							<image src="../../static/images/user06.png" class="icon"></image>
							<div class="title">我的积分</div>
						</view>
						<image src="../../static/images/icon_more.png" class="arrow"></image>
					</view>
				</template>
				<view v-if="isEmployee && userType !== '2'" class="item" @click="quickEntry(8)">
					<view class="left">
						<image src="../../static/images/user08.png" class="icon"></image>
						<div class="title">维修员</div>
					</view>
					<image src="../../static/images/icon_more.png" class="arrow"></image>
				</view>
			</view>
		</view>

		<authorization-modal ref="authorization" @loginEnd="onLoginEnd" @refuse="onRefuse" />
	</view>
</template>

<script>
	import authorizationModal from '@/components/authorization.vue'
	let timer = null
	export default {
		components: {
			authorizationModal
		},
		data() {
			return {
				currentHouseVerify: false,
				userType: '0',
				userName: '',
				userAvatar: '',
				userPhone: '',
				isEmployee: false
			}
		},
		methods: {
			onDetail(url) {
				uni.navigateTo({url});
			},
			quickEntry(type) {
				let isLogin = uni.getStorageSync('openid')
				if (!!isLogin) {
					if (type == 7) {
						const that = this
						uni.showModal({
							title: '提示',
							content: '确认切换到业主吗？',
							success: function (res) {
								if (res.confirm) {
									that.onChangeEmployee('1')
								} else if (res.cancel) {
									console.log('用户点击取消');
								}
							}
						});
					} else if(type == 8) {
						const that = this
						uni.showModal({
							title: '提示',
							content: '确认切换到维修员吗？',
							success: function (res) {
								if (res.confirm) {
									that.onChangeEmployee('2')
								} else if (res.cancel) {
									console.log('用户点击取消');
								}
							}
						});
					} else {
						let currentHouseVerify = getApp().globalData.currentHouseVerify
						if (type == 2) {
							let code = uni.getStorageSync('platform_code')
							if(code) {
								uni.navigateTo({
									url: '/pages/myHouse/index',
								});
							} else {
								uni.showToast({
									icon: "none",
									title: '暂未查到业主信息！'
								})
							}
							return
						}
						if(!currentHouseVerify) {
							uni.showToast({
								icon: "none",
								title: '暂未查到业主信息！'
							})
							// if(timer) clearTimeout(timer)
							// timer = null
							// timer = setTimeout(()=> {
							// 	uni.navigateTo({
							// 		url: '/pages/housing/index',
							// 	});
							// }, 1500)
							// return
						} else {
							if (type == 1) {
								uni.navigateTo({
									url: '/pages/myInformation/index',
								});
							} else if (type == 3) {
								uni.navigateTo({
									url: '/pages/maintenance/index',
								});
							} else if (type == 4) {
								uni.navigateTo({
									url: '/pages/complaints/index',
								});
							} else if (type == 5) {
								uni.navigateTo({
									url: '/pages/myPaycost/index',
								});
							} else if (type == 6) {
								uni.navigateTo({
									url: '/pages/myReceipt/index',
								});
							} else if (type == 9) {
								uni.navigateTo({
									url: '/pages/integralList/index',
								});
							} else {
								return false
							}
						}
					}
				} else {
					uni.hideTabBar()
					this.$nextTick(() => {
						this.$refs.authorization.onOpenLogin()
					});
				}
			},
			employeeEntry(type) {
				if(type == 1) {
					uni.navigateTo({
						url: '/pages/myInformation/index',
					});
				} else {
					uni.navigateTo({
						url: '/pages/repairList/index',
					});
				}
			},
			onLoginEnd() {
				uni.showTabBar()
				uni.reLaunch({
					url: '/pages/home/index'
				})
			},
			onRefuse() {
				uni.showTabBar()
			},
			onChangeEmployee(type) {
				uni.setStorageSync('user_type', type)
				uni.reLaunch({
					url: '/pages/home/index'
				})
			}
		},
		onLoad() {
			this.userType = uni.getStorageSync('user_type')
			this.userName = uni.getStorageSync('user_name')
			this.userAvatar = uni.getStorageSync('user_avatar')
			this.userPhone = uni.getStorageSync('user_phone')
			this.isEmployee = !!uni.getStorageSync('employee_code')
		},
		onHide() {
			if(timer) clearTimeout(timer)
			timer = null
		}
	}
</script>
<style>
	page {
		background-color: #F8F6F9;
	}
</style>
<style lang="scss" scoped>
	.page {
		padding: 20rpx 30rpx 20rpx;
	}

	.user {
		background: url('https://life.cloud.hjxbc.cn/sys/common/static/miniapp/bg_mine.png') no-repeat 100%/100%;
		background-size: 1;
		border-radius: 16rpx;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		padding: 30rpx ;

		.icon {
			width: 160rpx;
			height: 160rpx;
			border-radius: 160rpx;
		}

		.name {
			font-size: 36rpx;
			color: #ffffff;
			margin: 20rpx 0 10rpx;
		}

		.phone {
			font-size: 28rpx;
			color: #ffffff;
			opacity: 0.55;
		}
	}

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

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

			.left {
				display: flex;
				align-items: center;
				.icon {
					width: 36rpx;
					height: 36rpx;
					margin-right: 36rpx;
				}

				.title {
					font-size: 32rpx;
					color: #191A23;
				}
			}

			.arrow {
				width: 16rpx;
				height: 24rpx;
			}
		}
	}
</style>