<template>
	<view class="content">
		<!-- 主体区域 -->
		<view class="content-body">
			<!-- 通告列表页 -->
			<notice-index ref="noticeIndex" :nowUserType="nowUserType" v-if="'home'==menuCode"
				@openVip="openVip"></notice-index>
			<mine-index ref="minePage" :key="nowUserType" :nowUserType="nowUserType" v-if="'my'==menuCode"
				@refreshUserType="refreshUserType()"></mine-index>
			<subscribe :nowUserType="nowUserType" v-if="'dingyue'==menuCode"></subscribe>
			<my-notice ref="myNotice" :nowUserType="nowUserType"
				v-if="'myNotice'==menuCode && '02'==nowUserType"></my-notice>
			<my-notice-tg ref="myNoticeTg" :nowUserType="nowUserType"
				v-if="'myNotice'==menuCode && '01'==nowUserType"></my-notice-tg>
		</view>
		<tabBarVue :menuCode="menuCode" :nowUserType="nowUserType" ref="tarBar" @changeMenu="changeMenu"></tabBarVue>
		<!-- 普通弹窗 -->
		<uni-popup ref="popup">
			<view class="popup-content" @click="goVip">
				<image src="@/static/VIP.png" mode="widthFix"></image>
			</view>
		</uni-popup>
		<!-- 普通弹窗 -->
		<uni-popup ref="popupNew">
			<view class="popup-content" @click="getGift()">
				<view class="dl-popup-content">
					<view class="dl-title-1">新用户领好礼</view>
					<view class="dl-title-2">5 <text>次报名</text></view>
					<view class="dl-title-3">限注册新用户领取</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import {
		changeUserType,
		getUserType,
		formatNumberWithUnits,
		calculateTimeDifference
	} from '@/utils/common.js'
	import tabBarVue from '@/components/tabbar/tabBar.vue'
	import noticeIndex from '@/pages/components/notice-index.vue'
	import subscribe from '@/pages/components/subscribe.vue'
	import mineIndex from '@/pages/mine/mine-index.vue'
	import myNotice from '@/pages/components/my-notice.vue'
	import myNoticeTg from '@/pages/components/my-notice-tg.vue'
	import {
		getSiteConfig
	} from '@/api/system/config.js'
	import constant from '@/utils/constant'
	import {
		setStrData,
		getToken
	} from '@/utils/auth.js'
	import {
		toast,
		hasRights
	} from '@/utils/common.js'
	import rightsCode from '@/utils/rightsCode'
	import {
		getMemberDetail,
		isGetGift,
		sendNewGift
	} from '@/api/business/member.js'
	export default {
		components: {
			tabBarVue,
			noticeIndex,
			subscribe,
			mineIndex,
			myNotice,
			myNoticeTg
		},
		data() {
			return {
				//当前用户类型
				nowUserType: 'bz',
				menus: ['全部', '最新', '高奖励', '急招', '品牌置换'],
				menuIndex: 0,
				dataList: ['1', '2', '2', '2'],
				queryParams: {
					pageNo: 1,
					pageSize: 10,
				},
				total: 0,
				//下来刷新状态
				isTriggered: false,
				menuCode: "home",
			}
		},
		watch: {
			nowUserType(newVal, oldVal) {
				console.log(newVal, "newVal")
				console.log(oldVal, "oldVal")
				this.nowUserType = newVal;
			},
		},
		onReady() {
			this.$refs.minePage.getDetail()
			this.$refs.myNotice.search()
			this.$refs.myNoticeTg.search()
		},
		mounted() {
			this.refreshUserType()
		},
		onShow() {
			this.$refs.minePage.getDetail()
			this.refreshUserType()
			setTimeout(() => {
				this.checkUserIfLogin()
			}, 1000)
		},
		onLoad(param) {
			if (param.menuCode) {
				this.menuCode = param.menuCode
			}
			this.selectSiteConfig("platform_tel")
			this.selectSiteConfig("join_community")
			let that = this

		},
		methods: {
			/**
			 * 领取礼物
			 */
			getGift() {
				sendNewGift().then(res => {
					uni.showToast({
						title: '领取成功',
						icon: 'none',
						duration: 1000
					})
					this.$refs.popupNew.close()
				})
			},
			/**
			 * 判断用户是否登录
			 */
			checkUserIfLogin() {
				let that = this
				if (getToken() && "" != getToken() && null != getToken()) {
					//已登录。查后台判断是否已领取过新人礼
					isGetGift().then(res => {
						if (res.code == 200 && "1" == res.data) {
							//已经领取过
						} else {
							//没领取，弹窗
							that.$refs.popupNew.open('center')
						}
					})
				}
			},
			/**
			 * 打开VIP弹窗
			 */
			openVip() {
				this.$refs.popup.open('center')
			},
			/**
			 * 去VIP开通页
			 */
			goVip() {
				this.$tab.navigateTo('/pages/mine/member/member-card?userType=' + this.nowUserType)
			},
			/**
			 * 重新获取当前角色
			 */
			refreshUserType() {
				this.nowUserType = getUserType()
				console.log(this.nowUserType, 90);
			},
			updateParams(newVal) {
				this.nowUserType = newVal;
			},

			itemClick(index, item) {
				this.menuIndex = index
			},
			/**
			 * 菜单切换
			 * @param {Object} code
			 */
			async changeMenu(code) {
				this.nowUserType = getUserType()
				this.$refs.popupNew.close()
				if ("fabu" == code) {
					getMemberDetail().then(res => {
						if (!res.data.identityType) {
							uni.showToast({
								title: '暂未通告主认证，请先认证',
								icon: 'none',
								duration: 1000
							})
						} else {
							this.$tab.navigateTo('/pages/notice/public-notice')
						}
					})

				} else {
					if (code == 'dingyue') {
						if (!await this.checkIfHasRights(rightsCode.subscribeSelect)) {
							return
						}
					}
					this.menuCode = code
				}
			},
			/**
			 * 鉴权通用方法
			 * @param {Object} rigthsCode
			 */
			async checkIfHasRights(rigthsCode) {
				let res = await hasRights(rigthsCode)
				if (!res) {
					this.openVip()
					return false
				}
				return true
			},
			/**
			 * 上滑加载数据
			 */
			onReachBottomCus() {
				//判断 如果页码*页容量大于等于总条数，提示该页数据加载完毕
				if (this.queryParams.pageNo * this.queryParams.pageSize >= this.total) {
					uni.$u.toast('没有更多数据了')
					return
				}
				//页码+1,调用获取数据的方法获取第二页数据
				this.queryParams.pageNo++
			},
			/**
			 * 下拉刷新数据
			 */
			onRefresherrefresh() {
				this.isTriggered = true
				this.queryParams.pageNo = 1
				this.total = 0
			},
			/**
			 * 查询平台配置
			 */
			selectSiteConfig(code) {
				getSiteConfig({
					code: code
				}).then(res => {
					let jsonArray = JSON.parse(res.data)
					jsonArray.map((item) => {
						if (constant.customer_service == item.name) {
							setStrData(constant.customer_service, item.value)
						} else if (constant.wechat_img == item.name) {
							setStrData(constant.wechat_img, item.value)
						} else if (constant.join_str == item.name) {
							setStrData(constant.join_str, item.value)
						}
					})
				}).catch((e) => {
					uni.showToast({
						icon: 'error',
						duration: 2000,
						title: e
					});
				})
			}
		}
	}
</script>

<style lang="scss">
	.content {
		width: 100%;
		color: #363636;
		background-color: #F6F6F6;
		font-size: 38rpx;
		height: calc(100vh - var(--window-bottom) - 117rpx);
	}

	.content-body {
		width: 100%;
		height: 100%;
		background-color: white;
		// padding-bottom: calc(var(--window-bottom) + 127rpx);

	}

	.dl-popup-content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: start;
		width: 570rpx !important;
		height: 670rpx !important;
		background: url(data:image/png;base64,[encoded data, 62448 characters]);
		background-size: 100% 100%;
		background-position: center;
		/* 图片居中显示 */
		background-repeat: no-repeat;
		padding-top: 200rpx;

		.dl-title-1 {
			color: #FC1F3E;
			font-size: 20pt;
		}

		.dl-title-2 {
			color: #FC1F3E;
			font-size: 28pt;

			text {
				color: #FC1F3E;
				font-size: 11pt;
			}
		}

		.dl-title-3 {
			color: #FC1F3E;
			font-size: 11pt;
		}
	}

	.popup-content {
		padding: 0 !important;
		background-color: transparent;

		image {
			width: 70vw;
		}
	}
</style>