<template>
	<view class="nav-pages">
		<!-- 政务 -->
		<view v-show="[1,2].includes(pattern)">
			<view v-show="pattern === 1">
				<top ref="affairTopRef" :active-pattern="pattern" :statusBarHeight="statusBarHeight" :pattern="1"
					:scrollTop="scrollTop" :topKongLimList="topKongLimList" :toQyKongLimList="toQyKongLimList"
					:weatherInfo="weatherInfo" />
				<affair ref="affairRef" :active-pattern="pattern" :statusBarHeight="statusBarHeight" />
			</view>
			<!-- 生活 -->
			<view v-show="pattern === 2">
				<top ref="lifeTopRef" :active-pattern="pattern" :statusBarHeight="statusBarHeight" :pattern="2"
					:scrollTop="scrollTop" :topKongLimList="topKongLimList" :toQyKongLimList="toQyKongLimList"
					:weatherInfo="weatherInfo" />
				<life ref="lifeRef" :active-pattern="pattern" :statusBarHeight="statusBarHeight" />
			</view>
			<template v-if="showSopupSwiper">
				<popup :pattern="pattern" />
			</template>
		</view>

		<!-- 关怀模式 -->
		<view v-if="pattern === 3">
			<care-top :statusBarHeight="statusBarHeight" :scrollTop="scrollTop" />
			<showSolicitude :statusBarHeight="statusBarHeight" />
		</view>
	</view>

</template>

<script>
	import affair from "./home/affair.vue"
	import life from "./home/life.vue"
	import showSolicitude from "./home/showSolicitude.vue"
	import top from "./home/module/top.vue"
	import careTop from "./home/module/care-top.vue"
	import popup from "./home/module/popup.vue"
	import {
		http
	} from '@/common/request';
	import {
		getAPPList,
		versionNewest
	} from "@/api/government.js"
	import {
		getWeather_Fn,
		getWeatherInfo_Fn
	} from "@/utils/weather/index.js"
	export default {
		components: {
			affair,
			life,
			top,
			showSolicitude,
			careTop,
			popup
		},
		provide() {
			return {
				home: this,
			};
		},
		data() {
			return {
				pattern: 1, // 1：政务 ， 2：生活 ， 3：关怀模式
				statusBarHeight: 0,
				scrollTop: 0,
				topKongLimList: [],
				toQyKongLimList: [],
				showSopupSwiper: false,
				weatherObj: null,
				weatherInfo: null
			}
		},
		onLoad() {
			// setTimeout(() => {
			// 	uni.navigateTo({
			// 		url: '/pages/government/demo'
			// 	})
			// },3000)
			// return
			this.statusBarHeight = uni.getSystemInfoSync().statusBarHeight;
			// http.get('/protocol/?code=user_protocol').then(res => {})
			// #ifdef APP-PLUS
			if (plus.os.name == 'iOS') {
				// 首先获取一次网络状态
				uni.getNetworkType({
					success: (res) => {
						if (res.networkType != 'none') {
							this.getKongKim();
							this.getVersionNewest()
							this.getWeatherInfo();
						}
					}
				});
				// 监听网络变化
				uni.onNetworkStatusChange((res) => {
					console.log(res, 111111111)
					if (res.isConnected) {
						this.getKongKim();
						this.getVersionNewest()
						this.$refs.affairRef.init()
						this.$refs.affairTopRef.init();
						this.$refs.lifeRef.init()
						this.$refs.lifeTopRef.init();
					}
				});
			} else {
				this.getKongKim();
				this.getVersionNewest()
				this.getWeatherInfo();
			}
			// #endif

			// #ifdef H5
			this.getKongKim();
			this.getVersionNewest()
			this.getWeatherInfo();
			// #endif
		},

		onPullDownRefresh() {
			try {
				switch (this.pattern) {
					case 1:
						this.$refs.affairTopRef.init();
						this.$refs.affairRef.init();
						this.getKongKim();
						break;
					case 2:
						this.$refs.lifeTopRef.init();
						this.$refs.lifeRef.init();
						this.getKongKim();
						break;
				}
			} catch (e) {
				//TODO handle the exception
			}
			this.getWeatherInfo();
			setTimeout(() => {
				uni.stopPullDownRefresh();
			}, 2000)
		},
		onReachBottom() {
			if (this.pattern == 2) {
				this.$refs.lifeRef.moreGoods()
			}
		},
		methods: {
			getWeatherInfo() {
				getWeather_Fn().then(res => {
					this.weatherObj = res
				})
			},
			init(e) {
				this.pattern = e
			},
			getKongKim() {
				const keys = [this.$keys.SY_TOP_KONGKIM, this.$keys.SY_qiye_KONGKIM]
				keys.forEach(el => {
					getAPPList(el).then(res => {
						const list = res.data || []
						if (list.length > 0) {
							this.setService(list, el, keys)
						}
					}).catch(err => {
						this.setService([], el, keys)
					})
				})
			},

			setService(data, key, keys) {
				switch (key) {
					case keys[0]:
						// 首页顶金刚区
						this.topKongLimList = data || []
						break;
					case keys[1]:
						// 首页企业入口
						this.toQyKongLimList = data || []
						break;
				}

			},
			// 获取更新包
			getVersionNewest() {
				const systemInfo = uni.getSystemInfoSync();
				let version_number = systemInfo.appVersion;
				let platform = uni.getSystemInfoSync().platform;
				let type = 0
				if (platform == 'ios') {
					type = 1
				} else if (platform == 'android') {
					type = 0
				}
				versionNewest({
					type: type
				}).then(res => {
					this.newest = res.data;
					if (version_number < this.newest.version) {
						uni.navigateTo({
							url: "/components/appUpdatePop?type=1"
						})
					} else {
						this.showSopupSwiper = true
					}
				}).catch(err => {
					this.showSopupSwiper = true
				})
			}

		},
		watch: {
			weatherObj: {
				handler: function(newInfo) {
					this.weatherInfo = getWeatherInfo_Fn(newInfo)
				},
				immediate: true,
				deep: true
			}
		}

	}
</script>
<style>
	page {
		/* opacity: 0.2; */

		/* rgba(75,90,255,0.2), rgba(74,181,255,0.2) 8%, */
		background: #fcfcfc;
	}
</style>