<template>
	<view class="p-person-inedx g_w_all g_bg_f_5 g_kuaishou">
		<view :class="isLogin ? 'g_h_36' : 'g_h_10'"></view>
		<!-- 基本信息 -->
		<g-panel-card-info
			:info="{
				avatar: userInfo.avatar,
				title: userInfo.name,
				num: userInfo.tel,
				isHaveOrder: isHaveOrder,
			}"
			@clickCard="goPage('/root/person/info')"
			v-if="false"
		/>
		<view class="m-info g_p_16 g_bg_f g_m_10 g_radius_8 g_position_rela" v-if="isLogin" hover-class="none" style="margin-top: 0">
			<view class="g_flex_row_start">
				<view class="g_w_71">
					<view class="g_position_abso g_p_4 g_radius_13 g_bg_f" style="top: -20px">
						<g-panel-image :radius="13" :size="134" :url="userInfo.avatar" />
					</view>
				</view>
				<view class="g_flex_1 g_ml_11">
					<view class="g_flex_row_between flex_center">
						<view class="g_flex_row_start flex_center" style="line-height: 22px">
							<view class="g_flex_none g_ell_1 g_fs_20 g_fw_bold g_c_3" style="max-width: 240rpx">
								{{ userInfo.name ? userInfo.name : "-" }}
							</view>
							<!-- <view class="g_ml_4 iconfont icon-gerenshiming g_fs_14" :class="userInfo.user.realName ? 'g_c_027' : 'g_c_8'"> </view> -->
						</view>
						<view class="g_flex_row_start flex_center g_fs_14 g_border_d g_radius_4 g_c_8 g_p_4" hover-class="thover" @click="goPage('/root/person/info?active=0')">
							<i class="iconfont icon-edit g_fs_12 g_mr_4"></i>
							编辑资料
						</view>
					</view>
				</view>
			</view>
			<view class="g_flex_row_start flex_center g_mt_26 g_fs_16 g_c_8" hover-class="thover" @click="goPage('/root/person/info?active=1')">
				<view class="">
					{{ userInfo.agencyName || "未创建/加入企业" }}
				</view>
				<i class="iconfont icon-gengduo11" style="line-height: 22px; font-size: 10px"></i>
			</view>
		</view>
		<view v-else @click="goPage" class="g_text_c g_pt_24 g_pb_24 g_c_f g_radius_8 g_bg_main g_ml_10 g_mr_10">
			<view class="g_mb_12">
				<image class="g_w_64 g_h_64 g_radius_50" src="https://matripe-cms.oss-cn-beijing.aliyuncs.com/1shoudan/defaultAva.svg" mode="aspectFit|aspectFill|widthFix" lazy-load="false" binderror="" bindload=""> </image>
			</view>
			<view class="g_fs_18 g_fw_600 g_mb_4"> 登录/注册 </view>
			<view class="g_fs_14"> 让天下没有难做的劳务 </view>
		</view>
		<view class="g_flex_row_between g_m_10" v-if="false">
			<g-panel-fond
				style="width: 100%"
				:showRemark="false"
				:item="{
					icon: 't-icon-fazhandaili',
					title: '发展代理',
					remark: '让更多代理关注我',
					remarkFS: 14,
				}"
				themeColor="green"
				btnText="去添加"
				@clickCard="goPage('/root/bind/search?active=1')"
			/>
		</view>
		<view class="m-set g_bg_f g_radius_8" v-if="false" @click="showToggle">
			<view class="item g_flex_row_start" hover-class="g_bg_e" @click="handleClickItem" :style="{ padding: '19px 12px' }">
				<view class="g_flex_1 g_flex_row_start flex_center g_pl_10">
					<view class="t-icon g_h_22 g_w_22 g_pl_7 g_mr_5 t-icon-wodetuandui" style="width: 20px; height: 20px; background-repeat: no-repeat"></view>
					<view class="g_flex_column_center g_c_3 g_fs_16">切换团队</view>
					<!-- <view class="g_flex_row_start flex_center g_fs_11 g_ml_4" style="padding: 2px 6px; background-color: #f2fcf7; color: #00b666; border-radius: 10px" hover-class="none" hover-stop-propagation="false" @click="showToggle">
						切换
						<view class="iconfont icon-qiehuantuandui g_ml_4 g_fsi_10"></view>
					</view> -->
				</view>
				<i class="iconfont icon-gengduo11 g_c_c"></i>
				<!-- <view class="g_flex_row_end g_fs_16"> {{ userInfo.agencyName }} </view> -->
			</view>
		</view>
		<!-- <g-panel-form-item :list="[{ icon: 'icon-my_setup', label: '创建发单号', result: '', path: '/root/person/order', tip: 'createOrder', pRow: 12 }]" @clickItem="handleClickItem" /> -->

		<view class="g_mt_10" v-if="userInfo.admin">
			<g-panel-form-item
				:list="[
					{
						icon: 'icon-wodetuandui',
						label: '团队管理',
						result: '',
						path: '/root/person/teamManage',
						pRow: 12,
					},
				]"
				@clickItem="handleClickItem"
			/>
		</view>


		<view class="g_mt_24">
			<g-panel-form-item
				:list="[
					{
						icon: 'icon-shezhi',
						label: '设置',
						result: '',
						path: '/root/person/set',
						tip: 'set',
						pRow: 12,
					},
				]"
				@clickItem="goPage('/root/person/set')"
			/>
		</view>
		<view class="g_h_100"></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 {
	// 		title: " ",
	// 		imageUrl: "../../static/image/fdzsshare.png",
	// 	};
	// 	// return this.G.shareFun();
	// },
	watch: {
		showTeamToggle(val) {
			if (val) {
				uni.hideTabBar();
			} else {
				uni.showTabBar();
			}
		},
	},
	data() {
		return {
			scrollTop: 0,
			defaultTop: 0,
			pageSpeed: -1,
			isLogin: null,
			localBaseImg: this.G.store().localBaseImg,
			cdnBaseImg: this.G.store().cdnBaseImg,
			showDot: true, // 小红点是否显示
			showTeamToggle: false,
			teamList: [], // 团队列表
			speed: -1,
			mamageList: [
				{
					icon: "icon-zhiweiguanli",
					name: "我的职位",
					path: "/pages/bill/index",
				},
				{
					icon: "icon-fazhandaili1",
					name: "发展代理",
					path: "/root/bind/search?active=1",
				},
			],
			userInfo: {
				all: {},
				avatar: "",
				name: "请登录",
				tel: "-",
				num: [
					{
						name: "",
						num: "",
					},
				],
				agencyName: "",
				teamType: 0,
			},

			serverList: [
				{
					icon: "t-icon-dakaixiaoxitongzhi",
					name: "消息通知",
					tip: "message",
				},
				{
					icon: "t-icon-shangjiahoutai",
					name: "商家后台",
					tip: "admin",
				},
				{
					icon: "t-icon-fankuiyujianyi",
					name: "反馈与建议",
					tip: "feedback",
				},
				{
					icon: "t-icon-shezhi4",
					name: "设置",
					tip: "set",
				},
			],

			isHaveOrder: false,
			isHaveInvite: false,
			waitNum_ups: 0,
			itemList: [],
			isCreator: false,
		};
	},
	onShow() {
		let that = this;
		that.isCreator = uni.getStorageSync("IS_CREATOR") == 1 ? true : false;

		if (uni.getStorageSync("apply-token")) {
			that.setUserInfo();
		} else {
			that.waitNum_ups = 0;
			that.pageSpeed = 1;
			that.isLogin = false;
			that.userInfo.avatar = that.G.store().localBaseImg + "fadanzhushou.png";
			that.userInfo.name = "请登录";
			that.userInfo.tel = "-";
			that.userInfo.agencyName = "";
			that.userInfo.teamType = 0;
			that.speed = 0;
		}
	},
	onTabItemTap(e) {
		let that = this;
		if (uni.getStorageSync("apply-token")) {
			that.getWaitNum();
		}
	},
	methods: {
		setUserInfo() {
			let that = this;
			let defaultList = [
				// {
				// 	icon: "icon-dakaixiaoxitongzhi",
				// 	label: "打开消息通知",
				// 	result: "",
				// 	path: "/root/person/message",
				// 	tip: "open-message",
				// 	pRow: 12,
				// },
				{
					icon: "icon-wodetuandui",
					label: "团队管理",
					result: "",
					path: "/root/person/teamManage",
					pRow: 12,
				},
				// {
				// 	icon: "icon-shangjiahoutai",
				// 	label: "进入商家后台",
				// 	result: "",
				// 	path: "/root/person/admin",
				// 	tip: "go-admin",
				// 	pRow: 12,
				// },
				{
					icon: "icon-fankuiyujianyi",
					label: "反馈与建议",
					result: "",
					path: "/root/person/feedback",
					tip: "message",
					pRow: 12,
				},
			];
			// if (uni.getStorageSync("apply-supplierAccount") == 1) {
			// 有
			that.isHaveOrder = true;
			if (that.isCreator) {
				that.itemList = defaultList;
			} else {
				that.itemList = [
					{
						icon: "icon-wodetuandui",
						label: "团队管理",
						result: "",
						path: "/root/person/teamManage",
						pRow: 12,
					},
					{
						icon: "icon-fankuiyujianyi",
						label: "反馈与建议",
						result: "",
						path: "/root/person/feedback",
						tip: "message",
						pRow: 12,
					},
				];
			}
			// }
			let userInfo = uni.getStorageSync("apply-userinfo");
			that.isLogin = true;
			that.userInfo = { ...userInfo };
			console.log(that.userInfo, "that.userInfo");
			that.userInfo.avatar = uni.getStorageSync("apply-avatar");
			that.userInfo.name = userInfo.userName || userInfo.aliasName;
			that.userInfo.tel = uni.getStorageSync("apply-tel");

			// if (that.userInfo.num[0].name == "-") {
			// 	that.userInfo.num = [
			// 		{
			// 			name: "-",
			// 			num: "-",
			// 		},
			// 	];
			// }

			that.userInfo.agencyName = userInfo.agencyName;
			that.userInfo.teamType = userInfo.teamType;
			// that.getWaitNum();
			// that.checkInvite();
			// that.getTeamList();

			if (uni.getStorageSync("watch_invite")) {
				if (uni.getStorageSync("watch_invite") == 1) {
					that.showDot = false;
				} else if (uni.getStorageSync("watch_invite") == 2) {
					that.showDot = true;
				}
			}
		},
		checkInvite() {
			let that = this;
			that.G.Get(
				that.api.user_getInviteList,
				{
					pageSize: 15,
					pageNum: 1,
				},

				(res) => {
					if (res.length > 0) {
						that.isHaveInvite = true;
					} else {
						that.isHaveInvite = false;
					}
				}
			);
		},
		getWaitNum() {
			let that = this;
			if (!that.isHaveOrder) {
				that.getDetail();
				return false;
			}
			that.G.Get(
				that.api.bind_getWaitNum + "/" + 2,
				{
					type: 2,
				},
				(res) => {
					// if (res.total > 0) {
					// 	uni.setTabBarBadge({
					// 		index: 2,
					// 		text: String(res.total),
					// 	});
					// } else {
					// 	uni.removeTabBarBadge({
					// 		index: 2,
					// 	});
					// }
					that.waitNum_ups = res.total;
					that.getDetail();
				}
			);
		},
		getDetail() {
			let that = this;
			if (uni.getStorageSync("apply-agencyId")) {
				that.pageSpeed = 0;
				that.G.Get(
					that.api.bind_enterpriseDetail + "/" + uni.getStorageSync("apply-agencyId"),
					{
						agencyId: uni.getStorageSync("apply-agencyId"),
						type: 1,
					},
					(res) => {
						console.log("item.", res);
						that.pageSpeed = 1;
						if (uni.getStorageSync("apply-supplierAccount") == 1) {
							// 有
							that.userInfo.num = [
								{
									name: "全部职位",
									num: res.jobNum,
								},
								{
									name: "在招职位",
									num: res.recruitmentJobNum,
								},
								{
									name: "关注",
									num: res.upNum,
									tip: that.isHaveInvite && that.showDot ? 1 : 0,
								},
								{
									name: "粉丝",
									num: res.downNum,
									tip: that.isHaveOrder ? that.waitNum_ups : 0,
								},
							];
						} else {
							// 无
							that.userInfo.num = [
								{
									name: "关注",
									num: res.upNum,
									tip: that.isHaveInvite && that.showDot ? 1 : 0,
								},
							];
						}
					}
				);
			}
		},
		goPage($path) {
			this.G.isLogin();
			if (this.G.isLogin()) {
				uni.navigateTo({
					url: $path,
				});
			}
		},
		handleClickNum(item, index) {
			let that = this;
			this.G.isLogin();
			if (this.G.isLogin()) {
				console.log("数据展示：", item.name);
				switch (item.name) {
					case "全部职位":
						uni.navigateTo({
							url: "/pages/bill/index",
						});
						break;
					case "在招职位":
						uni.navigateTo({
							url: "/pages/bill/index?active=1",
						});
						break;
					case "关注":
						uni.navigateTo({
							url: "/root/bind/order?active=0",
						});
						break;
					case "粉丝":
						if (that.waitNum_ups == 0) {
							uni.navigateTo({
								url: "/root/bind/fensi?active=1&tab=1",
							});
						} else {
							uni.navigateTo({
								url: "/root/bind/fensi?active=1&tab=0",
							});
						}
						break;
					default:
						break;
				}
			}
		},
		handleClickItem(e) {
			this.G.isLogin();
			if (this.G.isLogin()) {
				console.log("e", e);
				if (e.item.path == "qiehuan") {
					console.log("切换团队");
					this.showTeamToggle = true;
				} else {
					uni.navigateTo({
						url: e.item.path,
					});
				}
			}
		},
		/**
		 * 获取团队列表
		 */
		getTeamList() {
			let that = this;
			that.G.Get(that.api.user_getTeamList, {}, (res) => {
				console.log("res", res);
				res.forEach((item) => {
					if (item.id == that.userInfo.user.agencyId) {
						item.checked = true;
					}
				});
				that.teamList = res;
			});
		},
		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.showTeamToggle = false;
					that.setUserInfo();

					// that.$forceupdate()
					// that.onShow();
				});
			});
		},
		/**
		 * 切换团队
		 */
		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) => {
				console.log("res", res);
				that.checkToken(item);
				uni.hideLoading();
				uni.showToast({
					title: "切换成功",
					icon: "success",
					duration: 2000,
				});
			});
		},
		showToggle() {
			let that = this;
			that.showTeamToggle = true;
		},
	},
};
</script>

<style lang="scss">
.p-person-inedx {
	min-height: 100%;
	background-color: #f5f5f5;

	.m-info {
		width: calc(100% - 40rpx);
		margin: 0 auto;
		margin-bottom: 20rpx;

		.data-box {
			.item {
				width: 33.3333%;
			}
		}
	}

	.m-state {
		width: calc(100% - 40rpx);
		margin: 0 auto;
		margin-bottom: 48rpx;
	}

	.m-set {
		width: calc(100% - 40rpx);
		margin: 0 auto;
	}
}
</style>
