<template>
	<cwg-page-wrapper>
		<view class="page">
			<view class="user-card">
				<view class="avatar"></view>
				<view class="user-info">
					<view class="phone">{{ userInfo?.customInfo?.email }}</view>
				</view>
			</view>
			<view class="group">
				<view class="group-item" @click="openProfile()">
					<cwg-icon color="#000" icon="shield-lock-open-outline" />
					<text class="item-text">{{ t("language.i3") }}</text>
					<text class="item-text version" v-if="!isPersonalInfoCompleted">{{ t("language.i0") }}</text>
					<text class="item-text version" v-if="isPersonalInfoCompleted">{{ t("State.ToCertified") }}</text>
					<cwg-icon color="#000" icon="chevron-right" />
				</view>
				<view class="group-item" @click="router.push('/pages/mine/language')">
					<cwg-icon color="#000" icon="web" />
					<text class="item-text">{{ t("language.i1") }}</text>
					<text class="version item-text">{{ t(`language.${lang}`) }}</text>
					<cwg-icon color="#000" icon="chevron-right" />
				</view>
				<view class="group-item" @click="router.push('/pages/apply-record/list')">
					<cwg-icon color="#000" icon="history" />
					<text class="item-text">{{ t("card.tab15") }}</text>
					<text class="version item-text"></text>
					<cwg-icon color="#000" icon="chevron-right" />
				</view>
				<view class="group-item" @click="router.push('/pages/wallet/balance')">
					<cwg-icon color="#000" icon="history" />
					<text class="item-text">{{ t("card.tab17") }}</text>
					<text class="version item-text"></text>
					<cwg-icon color="#000" icon="chevron-right" />
				</view>
			</view>
			<view class="group">
				<view class="group-item" @click="router.push('/pages/mine/pay-password')">
					<cwg-icon color="#000" icon="lock-reset" />
					<text class="item-text">{{ t("language.i2") }}</text>
					<text class="version item-text"></text>
					<cwg-icon color="#000" icon="chevron-right" />
				</view>
			</view>
			<!-- <view class="group">
	          <view class="group-item">
	              <cwg-icon color="#000" icon="information-outline"/>
	              <text class="item-text">{{ t('language.i5') }}</text>
	              <text class="version item-text"></text>
	              <cwg-icon color="#000" icon="chevron-right" />
	          </view>
	      </view> -->
			<view class="group">
				<view class="group-item" @click="showLogoutPopup = true">
					<cwg-icon color="#000" icon="logout" />
					<text class="item-text">{{ t("language.i6") }}</text>
					<text class="version item-text"></text>
					<cwg-icon color="#000" icon="chevron-right" />
				</view>
			</view>
			<u-modal class="code-dialog" :show="showLogoutPopup" title="" :show-confirm-button="false"
				:show-cancel-button="false" :close-on-click-overlay="false">
				<view class="modal-class">
					<view class="p1 title">{{ t('mine.logout') }}</view>
					<view class="p1">{{ t('mine.p') }}</view>

					<view class="cwg-button ok-button">
						<u-button type="primary" block @click="handleLogout('confirm')">{{
							t("mine.b1")
						}}</u-button>
					</view>
					<view class="cwg-button no-button">
						<u-button type="primary" block @click="handleLogout">{{
							t("mine.b2")
						}}</u-button>
					</view>
				</view>
			</u-modal>
		</view>
	</cwg-page-wrapper>

</template>

<script setup lang="ts">
import { ref, onMounted, watch, computed } from "vue";
import { useI18n } from "vue-i18n";
import useRouter from "@/hooks/useRouter";
import { userApi } from "@/api/user";
import { lang } from "@/composables/config";
import useUserStore from "@/stores/use-user-store";
import { showToast } from "@/utils/toast";
const { t } = useI18n();
const userStore = useUserStore();
const router = useRouter();
const userInfo = computed(() => userStore.userInfo);

// 判断是否已完成个人信息认证
const isPersonalInfoCompleted = computed(() => {
	if (userInfo?.approveStatus == 2) {
		return true;
	} else {
		return false;
	}
});

function openProfile() {
	if (!isPersonalInfoCompleted.value) {
		router.push('/pages/mine/info');
	} else {
		router.push('/pages/mine/improve');
	}
}
const showLogoutPopup = ref(false);
async function handleLogout(action: string) {
	if (action === "confirm") {
		try {
			const res = await userApi.logout();
			if (res.code === 200) {
				userStore.clearUserInfo();
				router.push("/pages/login/index");
			}
		} catch (error) {
			showToast(error.message || "退出登录失败");
		}
	}
	showLogoutPopup.value = false;
}
</script>

<style scoped lang="scss">
@import "@/uni.scss";

:deep(.u-modal__content) {
	padding: px2rpx(24) px2rpx(40);

	.p1 {
		color: #363130;
		font-family: "League Spartan";
		font-size: px2rpx(17);
		font-style: normal;
		font-weight: 400;
		line-height: normal;
		margin-bottom: px2rpx(32);
	}

	.modal-class {
		width: px2rpx(300);

	}

	.title {
		color: #1a1a1a;
		text-align: center;
		font-family: Roboto;
		font-size: px2rpx(22);
		font-style: normal;
		font-weight: 600;
		line-height: px2rpx(22);
	}

	.no-button {
		width: 100%;
		margin: px2rpx(12) 0;

		.u-button {
			background-color: #ffbdc8 !important;
		}
	}

	.ok-button {
		margin: px2rpx(4) 0;
		/* background-color: #EA002A; */
	}
}

.user-card {
	width: 100%;
	display: inline-flex;
	flex-direction: column;
	justify-content: center;
	align-items: center;
	gap: px2rpx(24);
	padding-bottom: px2rpx(48);

	.avatar {
		width: px2rpx(64);
		height: px2rpx(64);
		border-radius: 50%;
		background: #d8d8d8 url("/static/images/avatars.png") center/cover no-repeat;
		border: 4rpx solid #fff;
	}

	.phone {
		color: #1a1a1a;
		font-family: Roboto;
		font-size: px2rpx(22);
		font-style: normal;
		font-weight: 600;
		line-height: px2rpx(4);
	}
}

.user-info .group {
	background: var(--action-bg);
	border-radius: px2rpx(16);
	margin-top: px2rpx(24);
	overflow: hidden;
}

.group-item {
	display: flex;
	align-items: center;
	padding: px2rpx(16) 0;
	color: #000;
	font-size: px2rpx(16);
	/* border-bottom: 2rpx solid var(--action-bg); */
	position: relative;
}

.group-item:last-child {
	border-bottom: none;
}

svg {
	width: px2rpx(24);
	height: px2rpx(24);
	color: #000;
	font-size: px2rpx(24);
}

.group-item .version {
	margin-left: auto;
	color: #000;
	font-size: px2rpx(16);
}

.item-text {
	margin-left: px2rpx(12);
}

.code-dialog {
	display: flex;
	justify-content: center;
	gap: px2rpx(12);
	padding: px2rpx(16);

	:deep(.u-popup__content) {
		width: 90% !important;
	}

	:deep(.u-modal) {
		width: 100% !important;
	}
}
</style>