<template>
	<view class="container">
		<view class="user-box">
			<view class="icon-name-box">
				<view class="icon-box">
					<image class="icon"
					:src="user.userIcon ?? '/static/images/avatar-img/1.png'"
					@click="onIconOpen"
					></image>
				</view>
				<view class="name-box">
					<view class="name" v-if="isLogin">
						<view class="text">
							{{user.name}}
						</view>
						<view class="iconfont icon-xiugai" @click="onIconOpen"></view>
					</view>
					<view class="function" v-else>
						<view class="function-item" @click="toLogin">
							登录
						</view>/
						<view class="function-item" @click="toRegister">
							注册
						</view>
					</view>
				</view>
			</view>
			<view class="info-box">
				<view class="info-item-box">
					<view class="label">
						任职机构:
					</view>
					<view class="text">
						{{isLogin ? user.corpName ?? '无' : ''}}
					</view>
				</view>
				<view class="info-item-box">
					<view class="label">
						机构备案证号:
					</view>
					<view class="text">
						{{isLogin ? user.corpRegNo ?? '无' : ''}}
					</view>
				</view>
			</view>
		</view>
		<view class="certificate-box">
			<view class="title-box">
				我的证件
			</view>
			<view class="list-box">
				<view class="list-item-box" @click="onShowCreditCard">
					<CreditCard :isLogin="isLogin" :data="creditCard"></CreditCard>
				</view>
				<view class="list-item-box" :class="[isLogin ? (zhongjie.zsbh ? 'zhongjie-back' : 'zhongjie-back-zhiyin') : 'zhongjie-back-login']" @click="onZhongJieRenZhengClick(zhongjie.zsbh)">
				</view>
				<view class="list-item-box" :class="[isLogin ? (anjie.zsbh ? 'anjie-back' : 'anjie-back-zhiyin') : 'anjie-back-login']" @click="onAnJieRenZhengClick(anjie.zsbh)">
				</view>
			</view>
		</view>
		<u-popup v-model="showCreditCard" mode="center">
			<view class="certificate-preview-box">
				<CreditCard :isLogin="isLogin" :size="`normal ${creditCard.isAgtNTAsst ? 'green' : creditCard.isAgtNT ? 'red' : 'blue'}`" :data="creditCard" @guide="onCreditZhiYin" @previewImage="onPreviewImage"></CreditCard>
			</view>
		</u-popup>
		<view class="menu-box">
			<view class="menu-item-box" @click="onHuiFeiJiaoJiao()">
				<view class="icon-box">
					<view class="iconfont icon-qianbi"></view>
				</view>
				<view class="content-box">
					会费缴交
				</view>
				<view class="suffix-box">
					<view class="iconfont icon-chevron-right"></view>
				</view>
			</view>
			<view class="menu-item-box" @click="onJiXuJiaoYuNianJian()">
				<view class="icon-box">
					<view class="iconfont icon-nianjianmingxi"></view>
				</view>
				<view class="content-box">
					继续教育年检
				</view>
				<view class="suffix-box">
					<view class="iconfont icon-chevron-right"></view>
				</view>
			</view>
			<view class="menu-item-box" @click="onWoDeDingDan()">
				<view class="icon-box">
					<view class="iconfont icon-dingdan"></view>
				</view>
				<view class="content-box">
					我的订单
				</view>
				<view class="suffix-box">
					<view class="iconfont icon-chevron-right"></view>
				</view>
			</view>
			<!-- <view class="menu-item-box">
				<view class="icon-box">
					<view class="iconfont icon-xiazai"></view>
				</view>
				<view class="content-box">
					视频缓存
				</view>
				<view class="suffix-box">
					<view class="iconfont icon-chevron-right"></view>
				</view>
			</view> -->
			<view class="menu-item-box">
				<view class="icon-box">
					<view class="iconfont icon-shoucang"></view>
				</view>
				<view class="content-box" @click="onWoDeShouChang()">
					我的收藏
				</view>
				<view class="suffix-box">
					<view class="iconfont icon-chevron-right"></view>
				</view>
			</view>
			<view class="menu-item-box" @click="onChangJianWenTi()" v-if="false">
				<view class="icon-box">
					<view class="iconfont icon-anquan"></view>
				</view>
				<view class="content-box">
					常见问题
				</view>
				<view class="suffix-box">
					<view class="iconfont icon-chevron-right"></view>
				</view>
			</view>
			<view class="menu-item-box" @click="onLianXiWoMen()">
				<view class="icon-box">
					<view class="iconfont icon-yonghuzu"></view>
				</view>
				<view class="content-box">
					联系我们
				</view>
				<view class="suffix-box">
					<view class="iconfont icon-chevron-right"></view>
				</view>
			</view>
			<view class="menu-item-box" @click="onLogout()">
				<view class="icon-box">
					<view class="iconfont icon-shezhi"></view>
				</view>
				<view class="content-box">
					退出/切换账号
				</view>
				<view class="suffix-box">
					<view class="iconfont icon-chevron-right"></view>
				</view>
			</view>
		</view>
		<u-popup v-model="iconVisible" mode="center" border-radius="10">
			<view class="icon-list-box">
				<view class="title">
					修改头像
				</view>
				<u-row gutter="20">
					<u-col span="3" v-for="icon in iconList" :key="icon.name" @click="currentIcon = icon">
						<view class="icon-item" :class="{'icon-active': icon.name === currentIcon.name}">
							<image class="icon" :src="icon.url" mode="aspectFill"></image>
						</view>
					</u-col>
				</u-row>
				<view class="button-box">
					<view class="button-item">
						<u-button type="primary" @click="onIconSubmit">修改</u-button>
					</view>
					<view class="button-item">
						<u-button type="info" @click="onIconCancel">取消</u-button>
					</view>
				</view>
			</view>
		</u-popup>
		<!-- 与包裹页面所有内容的元素u-page同级，且在它的下方 -->
		<u-tabbar v-model="tabbarCurrentIndex" :list="tabbarList" icon-size="50" :active-color="tabbarActiveColor" :inactive-color="tabbarInactiveColor"></u-tabbar>
	</view>
</template>

<script setup>
	import CreditCard from '@/components/creditCard.vue'

	import { ref, computed } from 'vue'
	import { onLoad, onShow } from '@dcloudio/uni-app'
	import { getToken, removeToken } from '@/utils/auth.js'
	import { useAuthStore } from '@/store/authStore'

	import { getCreditCard, getZhongjie, getAnjie, getIconList, updateIcon } from '@/api/user.js'
	import { msgError, msgSuccess, showConfirm } from '@/utils/common'

	import { useTabbarStore } from '@/store/tabbarStore.js'
	const tabbarStore = useTabbarStore()
	// 底部导航栏数据
	const tabbarList = computed(() => {
		return tabbarStore.list
	})
	// 底部导航栏选中颜色
	const tabbarActiveColor = computed(() => {
		return tabbarStore.activeColor
	})
	// 底部导航栏未选中颜色
	const tabbarInactiveColor = computed(() => {
		return tabbarStore.inactiveColor
	})
	const tabbarCurrentIndex = 0

	const authStore = useAuthStore()

	// 用户信息
	const user = ref({
		userIcon: null,
		name: '某某某',
		corpName: '广州市xx地产有限公司',
		corpRegNo: '123xxxx'
	})

	const iconVisible = ref(false)
	const iconList = ref([
		{
			name: '',
			src: ''
		}
	])
	const currentIcon = ref({
		name: '',
		src: ''
	})
	function onIconOpen() {
		if (!isLogin.value) {
			return
		}
		currentIcon.value = iconList.value[0]
		iconVisible.value = true
	}
	function onIconSubmit() {
		updateIcon({userIcon: currentIcon.value.name}).then(res => {
			if (res && res.message === 'success') {
				msgSuccess('修改成功')
				authStore.setUserIcon(currentIcon.value.url)
				iconVisible.value = false
			} else {
				msgError('修改失败')
			}
		})
	}
	function onIconCancel() {
		iconVisible.value = false
	}

	// 是否预览信用信息卡
	const showCreditCard = ref(false)
	// 信用信息卡
	const creditCard = ref({
		creditNo: '',
		name: '',
		date: '',
		publisher: '',
		headUrl: '',
		url: '',
		isAgtNT: false,
		isAgtNTAsst: false
	})
	function onShowCreditCard() {
		showCreditCard.value = false
		if (isLogin.value) {
			showCreditCard.value = true
			// if (creditCard.value.creditNo) {
			// 	showCreditCard.value = true
			// } else {
			// 	uni.navigateTo({
			// 		url: '/pages/zhiyin/zhiyin?title=信息信用卡指引'
			// 	})
			// }
		}
	}
	function onCreditZhiYin() {
		uni.navigateTo({
			url: '/pages/zhiyin/zhiyin?title=信息信用卡指引'
		})
	}
	function onPreviewImage(url) {
		uni.previewImage({
			current: 1,
			urls: [url],
			showmenu: false
		})
	}
	// 中介认证点击
	function onZhongJieRenZhengClick(valid) {
		if (!isLogin.value) {
			return
		}
		if (valid) {
			uni.navigateTo({
				url: '/pages/certificateDetail/certificateDetail?type=zhongjie&title=广州市房地产中介从业人员水平认证证书'
			})
		} else {
			uni.navigateTo({
				url: '/pages/zhiyin/zhiyin?title=中介认证报名指引'
			})
		}
	}
	// 按揭认证点击
	function onAnJieRenZhengClick(valid) {
		if (!isLogin.value) {
			return
		}
		if (valid) {
			uni.navigateTo({
				url: '/pages/certificateDetail/certificateDetail?type=anjie&title=广州市房地产按揭从业人员水平认证证书'
			})
		} else {
			uni.navigateTo({
				url: '/pages/zhiyin/zhiyin?title=按揭认证报名指引'
			})
		}
	}
	const zhongjie = ref({
		zsbh: null,
		fzbm: null,
		fzrq: null,
		xm: null,
		xb: null,
		sfzh: null,
		whcd: null
	})
	const anjie = ref({
		zsbh: null,
		fzbm: null,
		fzrq: null,
		xm: null,
		xb: null,
		sfzh: null,
		whcd: null
	})
	function showMsg() {
		showConfirm('登录状态已过期，您可以继续留在该页面，或者重新登录?').then(res => {
			if (res.confirm) {
				uni.reLaunch({
					url: '/pages/initial/initial'
				});
			}
		})
	}

	// 点击-会费缴交
	function onHuiFeiJiaoJiao() {
		if (!isLogin.value) {
			showMsg()
			return
		}
		uni.navigateTo({
			url: '/pages/huifeijiaojiaoRukou/huifeijiaojiaoRukou'
		})
	}
	// 点击-继续教育年检
	function onJiXuJiaoYuNianJian() {
		if (!isLogin.value) {
			showMsg()
			return
		}
		uni.navigateTo({
			url: '/pages/jixujiaoyunianjian/jixujiaoyunianjian'
		})
	}
	// 点击-我的订单
	function onWoDeDingDan() {
		if (!isLogin.value) {
			showMsg()
			return
		}
		uni.navigateTo({
			url: '/pages/order/order'
		})
	}
	// 点击-我的收藏
	function onWoDeShouChang() {
		if (!isLogin.value) {
			showMsg()
			return
		}
		uni.navigateTo({
			url: '/pages/collect/collect'
		})
	}
	// 点击-常见问题
	function onChangJianWenTi() {
		console.log('点击-常见问题');
	}
	// 点击-联系我们
	function onLianXiWoMen() {
		uni.navigateTo({
			url: '/pages/contactUs/contactUs'
		})
	}
	// 点击-退出登录
	function onLogout() {
		if (!isLogin.value) {
			return
		}
		showConfirm('是否确认退出登录').then(res => {
			authStore.clearAuthToken()
			authStore.cleanUserInfo()
			uni.redirectTo({
				url: '/pages/initial/initial'
			})
		})

	}

	function toLogin() {
		uni.navigateTo({
			url: '/pages/initial/initial'
		})
	}
	function toRegister() {
		uni.navigateTo({
			url: '/pages/register/register'
		})
	}

	const isLogin = computed(() => {
		return authStore.token ? true : false
	})

	onShow(() => {
		user.value = authStore.userInfo
		getCreditCard().then(res => {
			if (res && res.message === 'success') {
				creditCard.value = res.data
				authStore.setCreditCard(res.data)

				getZhongjie().then(res => {
					if (res && res.message === 'success') {
						zhongjie.value = res.data ?? {zsbh: null}
					}
				})
				getAnjie().then(res => {
					if (res && res.message === 'success') {
						anjie.value = res.data ?? {zsbh: null}
					}
				})
				getIconList().then(res => {
					if (res && res.message === 'success') {
						iconList.value = res.data
					}
				})
			}
		})
	})
</script>

<style lang="scss" scoped>
	$certificate-width: 220rpx;
	$certificate-height: 300rpx;
	.container {
		height: 100vh;
		width: 100vw;
		padding: 0 20rpx;
		background: rgb(141, 204, 255);
		background: -moz-linear-gradient(   90deg, rgb(141, 204, 255) 10%, rgb(247, 247, 247) 30%);
		background: -webkit-linear-gradient(90deg, rgb(141, 204, 255) 10%, rgb(247, 247, 247) 30%);
		background: -o-linear-gradient(     90deg, rgb(141, 204, 255) 10%, rgb(247, 247, 247) 30%);
		background: -ms-linear-gradient(    90deg, rgb(141, 204, 255) 10%, rgb(247, 247, 247) 30%);
		background: linear-gradient(       180deg, rgb(141, 204, 255) 10%, rgb(247, 247, 247) 30%);

		.user-box {
			padding: 0 40rpx;
			.icon-name-box {
				display: flex;
				align-items: center;
				gap: 30rpx;
				margin-bottom: 15rpx;
				.icon-box {
					width: 90rpx;
					height: 90rpx;
					background-color: #fff;
					border-radius: 50%;
					overflow: hidden;
					-webkit-box-shadow: 0rpx 0rpx 10rpx 0rpx rgba(168, 221, 239, 0.75);
					-moz-box-shadow:    0rpx 0rpx 10rpx 0rpx rgba(168, 221, 239, 0.75);
					box-shadow:         0rpx 0rpx 10rpx 0rpx rgba(168, 221, 239, 0.75);
					.icon {
						height: 100%;
						width: 100%;
						object-fit: cover;
					}
				}
				.name-box {
					font-size: $uni-title-font-size-1;
					letter-spacing: 5rpx;

					.name {
						display: flex;
						gap: 5rpx;
						.iconfont {
							font-size: $uni-font-size-3;
						}
					}
					.function {
						display: flex;
						gap: 5rpx;
					}
				}
			}
			.info-box {
				.info-item-box {
					display: flex;
					align-items: center;
					gap: 10rpx;
					margin-bottom: 10rpx;
					font-size: $uni-font-size-1;
					font-weight: bold;
					letter-spacing: 3rpx;
				}
			}
		}

		.certificate-box {
			padding: 0 20rpx;
			margin-top: 20rpx;
			.title-box {
				font-size: $uni-title-font-size-3;
				font-weight: bold;
			}
			.list-box {
				padding: 20rpx 50rpx;
				width: 100%;
				display: flex;
				gap: 130rpx;

				overflow-x: scroll;
				overflow-y: hidden;
				white-space:nowrap;

				.list-item-box {
					flex-shrink: 0;
					height: $certificate-height;
					width: $certificate-width;
					background-color: #fff;

					&:nth-child(1) {
						background-color: #fff;
					}
				}

				.zhongjie-back {
					z-index: 999;
					@include backgroundImg('http://www.gzrea.org.cn:8543/icon/wxmp/zhengshu/zhongjie-back.png')
				}
				.zhongjie-back-login {
					@include backgroundImg('http://www.gzrea.org.cn:8543/icon/wxmp/zhengshu/zhongjie-back-login.png')
				}
				.zhongjie-back-zhiyin {
					@include backgroundImg('http://www.gzrea.org.cn:8543/icon/wxmp/zhengshu/zhongjie-back-zhiyin.png')
				}
				.anjie-back {
					z-index: 999;
					@include backgroundImg('http://www.gzrea.org.cn:8543/icon/wxmp/zhengshu/anjie-back.png')
				}
				.anjie-back-login {
					@include backgroundImg('http://www.gzrea.org.cn:8543/icon/wxmp/zhengshu/anjie-back-login.png')
				}
				.anjie-back-zhiyin {
					@include backgroundImg('http://www.gzrea.org.cn:8543/icon/wxmp/zhengshu/anjie-back-zhiyin.png')
				}
			}
		}

		.certificate-preview-box {
			height: 65vh;
			width: 75vw;
		}

		.menu-box {
			padding: 20rpx 20rpx;
			margin: 10rpx 0;
			background-color: #fff;
			width: 100%;
			border-radius: $uni-card-border-radius;

			-webkit-box-shadow: 0px 0px 12rpx 0px rgba(50, 50, 50, 0.25);
			-moz-box-shadow:    0px 0px 12rpx 0px rgba(50, 50, 50, 0.25);
			box-shadow:         0px 0px 12rpx 0px rgba(50, 50, 50, 0.25);

			.menu-item-box {
				height: 100rpx;
				font-size: $uni-font-size-1;
				padding:  10rpx;
				display: flex;
				gap: 10rpx;
				align-items: center;
				border-radius: $uni-card-border-radius;
				&:active {
					background-color: $uni-bg-color-hover;
				}
				.icon-box {
					width: 5%;
					font-weight: bold;
				}
				.content-box {
					width: 75%;
					letter-spacing: 3rpx;
				}
				.suffix-box {
					width: 20%;
					text-align: right;
				}
			}
		}

		.icon-list-box {
			padding: 30rpx;
			width: 90vw;
			.title {
				font-size: $uni-title-font-size-2;
				font-weight: bold;
				margin-bottom: 30rpx;
			}
			.icon-item {
				width: 120rpx;
				height: 120rpx;
				border-radius: 50%;
				border: 1px solid #ccc;
				margin-bottom: 20rpx;
				.icon {
					height: 100%;
					width: 100%;
					object-fit: cover;
				}
			}
			.icon-active {
				border: 1px solid $uni-color-primary;
			}
			.button-box {
				margin-top: 30rpx;
				display: flex;
				align-items: center;
				gap: 30rpx;
				.button-item {
					&:first-child{
						margin-left: auto;
					}
					&:last-child {
						margin-right: auto;
					}
				}
			}
		}
	}
</style>
