<script setup>
// 设置
import {
	ref,
	computed,
} from 'vue'
import {
	useStore
} from 'vuex'
import {
	onLoad,
} from '@dcloudio/uni-app'
// 工具库
import util from '@/common/js/util.js'
// api
import api from '@/api/index.js'
// 腾讯云聊天
import TencentCloudChat from '@tencentcloud/chat';
// 仓库
const store = useStore()
// 用户信息
const userinfo = computed(() => store.state.userinfo)
// 添加好友方式
const allowType = [{
	id: 'AllowType_Type_AllowAny',
	name: '允许任何人添加',
}, {
	id: 'AllowType_Type_NeedConfirm',
	name: '需要确认才能添加',
}, {
	id: 'AllowType_Type_DenyAny',
	name: '不允许任何人添加 ',
},]
// 添加好友下标
const allowTypeIndex = ref(0)
// im用户信息
const imUserinfo = ref({})
// app版本号
const appVersion = ref(0)

onLoad(() => {
	util.isLogin(() => {
		// 获取添加好友方式
		getAddType()
	})
	// #ifdef APP
	let system = uni.getSystemInfoSync()
	appVersion.value = system.appVersion
	// #endif
})

// 修改加好友方式
function handleAllowType(ev) {
	const index = ev.detail.value
	if (index === allowTypeIndex.value) return
	allowTypeIndex.value = index
	// 更新个人资料
	updateMyProfile()
}

// 获取添加好友方式
function getAddType() {
	api.mine.getImUserInfo({
		query: {
			userIds: userinfo.value.id
		}
	}).then(rs => {
		if (rs.code === 200) {
			console.log('rs', rs)
			const result = JSON.parse(rs.msg)
			allowTypeIndex.value = allowType.findIndex(item => item.id === result.UserProfileItem[0]
				.ProfileItem[0].Value)
			return
		}
		util.alert({
			content: rs.msg,
			showCancel: false
		})
	})
}

// 更新im个人资料
function updateMyProfile() {
	// 修改个人标配资料
	api.mine.setImUserInfo({
		query: {
			'allowType': allowType[allowTypeIndex.value].id,
			userId: userinfo.value.id,
		},
	}).then(rs => {
		if (rs.code != 200)
			util.alert({
				content: rs.msg,
				showCancel: false
			})
	})
}

// 登录跳转
function loginLink(url) {
	util.isLogin().then(() => {
		link(url)
	}).catch(() => {
		link('/pages/login/login')
	})
}

// 跳转
function link(url) {
	uni.navigateTo({
		url,
	})
}

// 退出登录
function logOff() {
	util.alert({
		title: '提示',
		content: '确定退出登录？',
	}).then(res => {
		if (res.confirm) {
			api.login.logout({
				query: {
					phoneNumber: userinfo.phoneNumber,
				}
			}).then(rs => { })

			// 退出登录
			util.logout(() => {
				// #ifdef APP
				plus.runtime.restart()
				// #endif
			})
		}
	})
}
</script>

<template>
	<!-- 设置页 -->
	<view class="app">
		<view class="area">
			<template v-if="userinfo.id">
				<view class="line rows" @click="loginLink('/pages/mine/userinfo')">
					<view class="avatar">
						<image class="head-portrait wh80" :src="userinfo.avatar" mode="aspectFill" />
					</view>
					<view class="info f1 ml20">
						<view class="">{{ userinfo.userNickname }}</view>
						<!-- <view class="account mt10 c999 f26">账号：{{userinfo.account}}</view> -->
					</view>
				</view>

				<view class="line rows" @click="link('/pages/mine/setting/updateAccount')">
					<view class="key">修改账号</view>
					<view class="col f1 ml20 tar c999 f28">
						<view class="">{{ userinfo.account }}</view>
					</view>
					<uni-icons class="ml20" type="right" />
				</view>

				<view class="line rows" @click="link('/pages/mine/setting/updatePhoneNum')">
					<view class="key">修改手机号</view>
					<view class="col f1 ml20 tar c999 f28">
						<view class="">{{ userinfo.phoneNumber }}</view>
					</view>
					<uni-icons class="ml20" type="right" />
				</view>

				<view class="line rows" @click="link('/pages/mine/realname')">
					<view class="key">实名认证</view>
					<view class="col f1 ml20 tar c999 f28">
						<view class="" v-if="userinfo.isRealName">已认证</view>
						<view class="" v-else>未认证</view>
					</view>
					<uni-icons class="ml20" type="right" />
				</view>
			</template>
		</view>

		<template v-if="userinfo.id">
			<view class="area">
				<view class="line rows" @click="link('/pages/mine/setting/usePay')" v-if="0">
					<view class="">先用后付</view>
					<uni-icons type="right" />
				</view>

				<view class="line rows" @click="link('/pages/mine/setting/secondPwd')">
					<view class="">二级密码</view>
					<uni-icons type="right" />
				</view>

				<view class="line rows" @click="link('/pages/mine/setting/sequence')">
					<view class="">支付顺序</view>
					<uni-icons type="right" />
				</view>

				<view class="line rows" @click="link('/pages/mine/setting/loginPassword')">
					<view class="">登录密码</view>
					<uni-icons type="right" />
				</view>

				<!-- <view class="line rows">
					<view class="">邮箱</view>
					<uni-icons type="right" />
				</view> -->

				<view class="line rows ptb20 plr10" @click="link('/pages/mine/setting/teen')">
					<view class="">青少年模式</view>
					<view class="f1 tar">
						<uni-icons type="right" />
					</view>
				</view>

				<!-- 支付宝 微信 -->
				<view class="line rows" @click="link('/pages/mine/setting/binding')">
					<view class="">我的绑定</view>
					<uni-icons type="right" />
				</view>

				<view class="line rows" @click="link('/pages/mine/setting/bankCard')">
					<view class="">我的银行卡</view>
					<uni-icons type="right" />
				</view>

				<view class="line" v-if="userinfo.id">
					<picker :range="allowType" range-key="name" @change="handleAllowType" :value="allowTypeIndex">
						<view class=" rows">
							<view class="">好友验证方式</view>
							<view class="value mlr20 f1 tar c666 f28">
								{{ allowType[allowTypeIndex].name }}
							</view>
							<uni-icons type="right" />
						</view>
					</picker>
				</view>

				<view class="line rows" @click="link('/pages/mine/setting/privacySetting')">
					<view class="">隐私设置</view>
					<uni-icons type="right" />
				</view>
			</view>
		</template>

		<view class="area">
			<view class="line rows" @click="link('/pages/mine/setting/safeCenter')">
				<view class="">安全中心</view>
				<uni-icons type="right" />
			</view>

			<view class="line rows" @click="link('/pages/mine/setting/feedback')">
				<view class="">意见反馈</view>
				<uni-icons type="right" />
			</view>

			<view class="line rows" @click="link(util.setUrl('/pages/index/article', { id: 2, }))">
				<view class="">关于我们</view>
				<uni-icons type="right" />
			</view>

			<view class="line rows" @click="link(util.setUrl('/pages/index/article', { id: 3, }))">
				<view class="">用户协议</view>
				<uni-icons type="right" />
			</view>

			<view class="line rows" @click="link(util.setUrl('/pages/index/article', { id: 1, }))">
				<view class="">隐私政策</view>
				<uni-icons type="right" />
			</view>

			<!-- #ifdef APP -->
			<view class="line rows ptb20 plr10">
				<view class="">版本号</view>
				<view class="c999 f28">{{ appVersion }}</view>
			</view>
			<!-- #endif -->
		</view>

		<view class="fmid fdc mtb50">
			<image class="wh150 br20" src="/static/logo.png" mode="aspectFill" />
			<view class="mt20 c333 f26">塔罗科技网络(山东)有限公司旗下品牌---九亿</view>
		</view>

		<template v-if="userinfo.id">
			<!-- 退出登录 -->
			<view class="mtb30 c999">
				<view @click="logOff" class="btn">退出登录</view>
			</view>
		</template>

		<view class="fill" style="height: 30rpx;"></view>
	</view>
</template>

<style lang="scss" scoped>
// 
.area {
	margin: 30rpx;
	padding: 0 30rpx;
	color: #333;
	font-size: 30rpx;
	background-color: #fff;
	border-radius: 20rpx;

	.line {
		padding: 20rpx 10rpx;
	}

	.line+.line {
		border-top: 1px solid #eeeeee;
	}

	.head-portrait {
		background: #999999;
		border-radius: 50%;
		margin-right: 20rpx;
	}
}
</style>