<template>
	<view class="">
		<view>
			<!-- 			<view style="margin: 40px 0 0 20px; font-size: 20px;">
				{{form.orgName}}
			</view> -->
			<u-sticky>
				<view class="topToolsBody uni-flex uni-row">
					<view class="topTools flex-item" @click="takeMeal()" :style="'width:'+topToolsWidth">
						<view>
							<image src="../../static/img/home/sys.png"></image>
						</view>
						扫一扫
					</view>
					<view class="topTools flex-item" @click="toPayCodePage" :style="'width:'+topToolsWidth">
						<view>
							<image src="../../static/img/home/qcCode.png"></image>
						</view>
						HS码
					</view>
					<view v-if="type === 3" class="topTools flex-item" @click="toIntegralPage"
						:style="'width:'+topToolsWidth">
						<view>
							<image src="../../static/img/home/jifenshangcheng.png"></image>
						</view>
						兑换
					</view>
					<view class="topTools flex-item" :style="'width:'+topToolsWidth" @click="$showModal('功能暂未开放')">
						<view>
							<image src="../../static/img/home/task.png"></image>
						</view>
						任务
					</view>
					<view class="topTools flex-item" style="position: relative;" @click="toXiaoxiPage"
						:style="'width:'+topToolsWidth">
						<view>
							<image src="../../static/img/home/xiaoxi.png"></image>
						</view>
						消息
						<view v-if="msgNum > 0" class="hs-hzl-count count" :style="'left:' +xxjbleft+'rpx'">
							{{msgNum}}
						</view>
					</view>
				</view>
			</u-sticky>
			<view class="modules">
				<homelist title="单位通告" :limit="2" :showType="1" @click="onClick" :list="noticeList"
					:url="this.$BASE_URL">
				</homelist>
			</view>
			<view class="modules" v-if="type === 3">
				<homelist title="相关资讯" :limit="4" :showType="2" :list="xgzxList" :url="baseURL">
				</homelist>
			</view>
			<view class="modules">
				<homelist title="平台资讯" :limit="4" :showType="2" :list="ptzxList" :url="baseURL">
				</homelist>
			</view>
		</view>
		<!-- <wp-tabbar ref="tab" pagePath="/pages/home/index" :type="type"></wp-tabbar> -->
	</view>
</template>

<script>
	// 页面模块组件
	import Classify1 from '@/components/classify-1/classify-1.vue'
	import homelist from '@/components/homelist/homelist.vue'
	export default {
		name: 'Index',
		components: {
			Classify1,
			homelist
		},
		data() {
			return {
				msgNum: 0,
				topToolsWidth: "15%",
				baseURL: this.$BASE_URL,
				ptzxList: [],
				xgzxList: [],
				xxjbleft: 85,
				indexConfig: [{
					name: 'banner-1',
					data: []
				}, {
					name: 'classify-1',
					data: []
				}, {
					name: 'homelist',
					data: []
				}],
				noticeList: [],
				form: {

				},
				type: undefined,
				openid: '',
				tabMark: []
			}
		},
		mounted() {
			const userInfo = uni.getStorageSync('userInfo');
			const orgInfo = uni.getStorageSync('orgInfo');
			this.form.userName = userInfo.nickName
			this.form.userNo = userInfo.userName
			this.form.orgNo = orgInfo.organizationCode
			this.form.orgName = orgInfo.organizationName
			uni.setStorageSync('refreshStart', 1)
			let baseUrlSocket = this.$BASE_URL_SOCKET
			if (!uni.getStorageSync('deptData')) {
				this.$httpRequest({
					url: '/app/deptInfo?id=' + uni.getStorageSync('org_dept_parent_id'),
					urlType: this.$getUrlType()
				}).then(res => {
					if (res.data.code === 200) {
						uni.setStorageSync('deptData', res.data.data)
					}
				})
			}
			if (!uni.getStorageSync('postsData')) {
				this.$httpRequest({
					url: '/app/posts',
					urlType: this.$getUrlType()
				}).then(res => {
					if (res.data.code === 200) {
						uni.setStorageSync('postsData', res.data.data)
					}
				})
			}
			this.msgs()
			setInterval(() => {
				this.msgs()
			}, 600);
			this.type = uni.getStorageSync("tabType")
			if (this.type === 2) {
				this.topToolsWidth = "20%"
				this.xxjbleft = 100
			} else if (this.type === 3) {
				this.topToolsWidth = "15%"
				this.xxjbleft = 85
			} else if (this.type === 4) {
				this.topToolsWidth = "20%"
				this.xxjbleft = 100
			} else {
				this.topToolsWidth = "20%"
				this.xxjbleft = 100
			}
			this.getCachaData()
			//获取主页数据
			this.getHomeConfigData(this.$getUrlType())
			this.getApiHomeConfigData()
			//获取任务提示
			//this.getNotRead()
			//获取公告
			this.getNoice()
			this.getOpenIdtoDb()
		},
		onShow() {
			uni.reLaunch({
				url: '/pages/tabbar/wpTabbar'
			})
			this.getCachaData()
			//获取主页数据
			this.getHomeConfigData(this.$getUrlType())
			this.getApiHomeConfigData()
			//获取任务提示
			//this.getNotRead()
			//获取公告
			this.getNoice()
			this.getOpenIdtoDb()
		},
		onNavigationBarButtonTap() {

		},
		onHide() {},
		methods: {
			getCachaData() {
				const userInfo = uni.getStorageSync('userInfo');
				const orgInfo = uni.getStorageSync('orgInfo');
				if (!(userInfo && orgInfo)) {
					uni.reLaunch({
						url: '/pageA/login/login'
					})
				}
				this.form.userName = userInfo.nickName
				this.form.userNo = userInfo.userName
				this.form.orgNo = orgInfo.organizationCode
				this.form.orgName = orgInfo.organizationName
				uni.setNavigationBarTitle({
					title: this.form.orgName
				})
			},
			async getHomeConfigData(type) {
				const {
					data: res
				} = await this.$httpRequest({
					url: '/app/getHomeConfigData?orgCode=' + this.form.orgNo,
					method: 'get',
					urlType: type
				})
				if (res.code == 200) {
					if (type === 2) {
						this.ptzxList = res.data
					} else if (type === 3) {
						this.xgzxList = res.data
					}
				} else {
					this.indexConfig = []
				}
			},
			async getApiHomeConfigData() {
				const {
					data: res
				} = await this.$httpRequest({
					url: '/api/getHomeConfigData?orgCode=' + "G00000003",
					method: 'get',
					urlType: 2,
					isNotToken: true
				})
				if (res && res.code == 200) {
					this.ptzxList = res.data
				} else {
					this.indexConfig = []
				}
			},
			msgs() {
				let data = uni.getStorageSync("msgData")
				if (!data) {
					this.msgNum = 0
					return
				}
				data = JSON.parse(data)
				this.msgNum = data.m1 + data.m2 + data.m3 + data.m4 + data.m5 + data.m6 + data.m7
			},
			toPayCodePage() {
				//跳转非pages.json>tabbar>配置过的页面，使用navigateTo
				//跳转时保留老页面，一般用于需要返回
				uni.navigateTo({
					url: "../user/payCode"
				})
			},
			toIntegralPage() {
				//跳转非pages.json>tabbar>配置过的页面，使用navigateTo
				//跳转时保留老页面，一般用于需要返回
				let userInfo = uni.getStorageSync("userInfo")
				if (userInfo.userType != '1' && userInfo.userType != '2') {
					this.$showModal('仅限内部人员使用')
					return
				}
				uni.navigateTo({
					url: "../../pageA/integral/index"
				})
			},
			toXiaoxiPage() {
				uni.navigateTo({
					url: "../../pageA/msg/userMsgList"
				})
			},
			onClick(e) {

			},
			async getOpenIdtoDb(type) {

			},
			async getNotRead() {
				const {
					data: res
				} = await this.$httpRequest({
					url: '/api/yq/notRead?userNo=' + this.form.userNo +
						"&orgNo=" + this.form.orgNo,
					method: 'get',
					urlType: this.$getUrlType()
				})
				if (res.code == 200) {
					var num = res.data
					if (0 < num) {
						uni.setTabBarBadge({
							index: 1,
							text: num + ""
						})
					}
				} else {

				}
			},
			async getNoice() {
				const {
					data: res
				} = await this.$httpRequest({
					url: '/app/notice?orgNo=' + this.form.orgNo,
					method: 'get',
					urlType: this.$getUrlType()
				})
				if (res.code == 200) {
					this.noticeList = res.data
					this.noticeList.forEach(n => {
						n.createTime = n.createTime.slice(0, 10)
					})
				} else {

				}
			},
			takeMeal() {
				var that = this;
				uni.scanCode({
					scanType: ['qrCode'],
					success: function(res) {
						uni.showModal({
							content: JSON.stringify(res) + "1",
						})
					},
					fail: function(a) {},
					complete: function(b) {}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.uni-tabbar__icon {
		width: 48px;
		height: 24px;
	}

	page {
		background-color: #c6c6c6;
		padding: 0;
	}

	.topToolsBody {
		box-shadow: 0 0 4px 0px #828282;
		padding: 0 10px;
		border-radius: 2ch;
		margin: 10rpx 10rpx 20rpx;
		background-color: #f9f9f9;

		image {
			width: 40px;
			height: 40px;
		}

	}

	.topTools {
		align-items: center; //垂直居中
		padding: 2.5%;
		font-size: 30rpx;
		color: #717171;
	}

	.warp {
		height: 100%;
		padding-bottom: constant(safe-area-inset-bottom);
		padding-bottom: env(safe-area-inset-bottom);
	}

	.modules {
		// margin: 15rpx;
		// padding: 10upx;
	}

	.count {
		bottom: 110rpx;

	}
</style>
