<template>
	<view class="p-root-person-userinfo g_w_all g_h_all g_bg_f_5 g_kuaishou">
		<view class="g_h_16"></view>
		<view class="g_mb_10">
			<g-panel-form-item :list="[{ 
				icon: '', 
				label: '反馈与建议', 
				result: '', 
				path: '/root/person/feedback', 
				tip: 'message', 
			}]" @clickItem="handleClickItem" />
		</view>
		<view class="g_pl_10 g_pr_10 g_pb_16" v-if="isCreator">
			<view class="g_bg_f g_pl_20 g_pr_20 g_radius_8">
				<view class="g_flex_row_between g_h_56">
					<view class="g_flex_column_center g_fs_16 g_c_3">关注邀请</view>
					<view class="g_flex_column_center">
						<u-switch v-model="showInvite" activeColor="#00B666" @change="handleSee"></u-switch>
					</view>
				</view>
			</view>
			<view class="g_fs_14 g_mt_8 g_pl_20 g_c_9"> 关闭后，首页收到的发单号邀请不再主动弹出 </view>
		</view>
		<g-panel-form-item
			:list="[
				{
					icon: '',
					label: '修改密码',
					result: '',
					path: '/root/person/pwdLogin',
					tip: 'changePassword',
				},
			]"
			@clickItem="handleClickItem"
		/>
		<view class="g_mt_24">
			<g-panel-form-item
				:list="[
					{
						icon: '',
						label: '用户协议',
						result: '',
						path: '/root/person/agreeUser',
						tip: 'userAgreement',
					},
					{
						icon: '',
						label: '隐私政策',
						result: '',
						path: '/root/person/agreePrive',
						tip: 'proveAgreement',
					},
					{
						icon: '',
						label: '关于报名助手',
						result: '版本' + version,
						path: '/root/person/about',
						tip: 'about',
					},
				]"
				@clickItem="handleClickItem"
			/>
		</view>
		<!-- <view class="g_mt_10">
			<g-panel-form-item
				:list="[
					{
						label: '切换团队',
						result: '',
						path: 'qiehuan',
						tip: 'set',
						pRow: 12,
					},
				]"
				@clickItem="handleClickItem"
			/>
		</view> -->
		<view class="g_mt_24 g_ml_10 g_mr_10">
			<g-button btnText="退出登录" type="delete" size="medium" @clickBtn="handleLayout" mode="square" />
		</view>
		<u-popup v-model="showTeamToggle" class="" mode="bottom" uZindex="9999" :closeable="true" border-radius="12" :mask-close-able="true">
			<view class="title g_text_c g_fs_18 g_fw_600 g_pt_32 g_pb_20">切换团队</view>
			<scroll-view class="g_pb_32" scroll-y="true" hover-class="none" style="height: calc(70vh)">
				<view class="weui-check__label g_flex_row_between g_p_16 g_border_e_b" hover-class="thover" v-for="item in teamList" @click="toogleTeam(item)">
					<view class="weui-cell__bd">
						<view class="g_fs_16">{{ item.agencyName || item.userName }}</view>
					</view>
					<view class style="color: #00b666" hover-class="none">{{ item.checked ? "当前" : "" }}</view>
				</view>
			</scroll-view>
			<!-- <view class="g_flex_row_between g_pl_20 g_pr_20 g_pb_32 g_pt_12" style="box-shadow: 0px -3px 6px 0px rgba(153, 153, 153, 0.1)">
				<g-button btnText="创建团队" @clickBtn="goHome" size="160"></g-button>
				<g-button btnText="加入团队" type="primary" @clickBtn="goHome" size="160"></g-button>
			</view> -->
		</u-popup>
	</view>
</template>

<script>
export default {
	onReady() {
		this.G.setNavStyle();
	},
	onShareAppMessage() {
		return this.G.shareFun();
	},
	data() {
		return {
			loading: true,
			version: this.G.store().version,
			showInvite: true, // 邀请关注弹窗显示
			isCreator: false,
			showTeamToggle: false,
			teamList: [], // 团队列表
			userinfo: {},
		};
	},
	onLoad() {
		let that = this;
		that.userinfo = uni.getStorageSync("apply-userinfo") || "";
		this.showInvite = uni.getStorageSync("SHOW_INVITE_ALL") == 1 ? true : false;
		this.getTeamList();
	},
	onShow() {
		let that = this;
		that.isCreator = uni.getStorageSync("IS_CREATOR") == 1 ? true : false;
	},
	methods: {
		/**
		 * 获取团队列表
		 */
		getTeamList() {
			let that = this;
			that.G.Get(that.api.user_getTeamList, {}, (res) => {
				console.log("res", res);
				res.forEach((item) => {
					if (item.id == uni.getStorageSync("apply-agencyId")) {
						item.checked = true;
					}
				});
				that.teamList = res;
			});
		},
		handleLayout() {
			let that = this;
			that.G.handleConfirm({
				content: "确认退出登录吗？",
				success: (res) => {
					if (res.confirm) {
						that.G.Get(that.api.login_out, {}, (res) => {
							that.G.clearLocalStorage();
							let params = {
								path: "",
								level: "",
							};
							uni.reLaunch({
								url: "/root/person/loginIndex?" + that.G.objToStr(params),
							});
						});
					}
				},
			});
		},
		checkToken(_item) {
			let that = this;
			uni.setStorageSync("apply-agencyId", _item.id);
			that.G.checkTokenNew().then((res) => {
				console.log(" checkToken res", res);
				that.G.Get(that.api.login_agencyInfo, {}, (aRes) => {
					console.log(" checkToken aRes", aRes);
					uni.setStorageSync("IS_CREATOR", aRes.agency.userId == res.user.id || res.admin == true ? 1 : 2); // 1是创建者 2是普通成员
					// that.setUserInfo();

					// that.$forceupdate()
					// that.onShow();
				});
			});
		},
		handleClickItem(e) {
			console.log("item点击事件：", e);
			if (e.item.path == "qiehuan") {
				console.log("切换团队");
				this.showTeamToggle = true;
			} else {
				uni.navigateTo({
					url: e.item.path,
				});
			}
		},
		/**
		 *  设置是否显示邀请关注弹窗
		 */
		handleSee(e) {
			console.log(e);
			uni.setStorageSync("SHOW_INVITE_ALL", e == true ? 1 : 2);
		},
		/**
		 * 切换团队
		 */
		toogleTeam(item) {
			let that = this;
			console.log(item);
			if (wx.getStorageSync("apply-agencyId") == item.id) {
				return false;
			}
			wx.showLoading({
				title: "切换中...",
			});
			that.G.Get(that.api.user_toggleTeam + `/${item.id}`, {}, (res) => {
				uni.setStorageSync("apply-agencyId", item.id);
				console.log("res", res);
				that.G.checkTokenNew().then(() => {
					uni.hideLoading();
					uni.showToast({
						title: "切换成功",
						icon: "success",
						duration: 2000,
					});
					that.showTeamToggle = false;
					that.userInfo = uni.getStorageSync("apply-userinfo");
					that.getTeamList();
				});
			});
		},
	},
};
</script>

<style lang="scss">
.p-root-person-userinfo {
}
</style>
