<template>
	<view>
		<!-- 首页风格切换-1-切换风格 -->
		<view class="top_bg_color" v-if="index_type == 1">
			<!-- 背景虚化轮播图 -->
			<!-- <view class="bg_img" :style="{backgroundImage:'url('+top_bg_color_img+')'}"></view> -->
			<!-- 固定安全色 -->
			<view class="bg_img" :style="{backgroundColor:top_bg_color}"></view>

			<t-i-navbar :mine_code="mine_code" :navbar_bg_color="navbar_bg_color"></t-i-navbar>
			<t-i-notice :text="text"></t-i-notice>
			<t-i-banner :banner="banners" :mine_code="mine_code" @change_top_bg_color_img="change_top_bg_color_img">
			</t-i-banner>
		</view>
		<!-- 首页风格切换-2-默认风格 -->
		<view v-if="index_type == 2">
			<t-i-navbar-2 :mine_code="mine_code"></t-i-navbar-2>
			<t-i-notice-2 :text="text"></t-i-notice-2>
			<t-i-banner-2 :banner="banners" :mine_code="mine_code" @change_top_bg_color_img="change_top_bg_color_img">
			</t-i-banner-2>
		</view>

		<view v-if="mine_code == 'ningdongyunying'">
			<t-i-ice :iceList="ice_list" :mine_code="mine_code"></t-i-ice>
		</view>

		<view v-if="mine_code == 'yangchangwan'">
			<t-i-ice :iceList="ice_list" :mine_code="mine_code"></t-i-ice>
		</view>

		<view v-if="mine_code == 'shicaocun'">
			<t-i-ice :iceList="ice_list" :mine_code="mine_code"></t-i-ice>
		</view>

		<t-i-icon v-if="mine_code == 'ningmeijituan' || 'wuyegongsi'" :iconList="iconList"
			:mine_code="mine_code"></t-i-icon>

		<!-- 新闻列表 -->
		<t-i-news :newsList="newsList" :mine_code="mine_code"></t-i-news>

		<!-- 固定入口 -->
		<t-i-icon v-if="mine_code != 'ningmeijituan' && iconList.length > 0" :iconList="iconList"
			:mine_code="mine_code"></t-i-icon>

		<!-- 常用功能 -->
		<t-i-common v-if="mine_code == 'ningdongyunying'"></t-i-common>

		<!-- 开屏通告 -->
		<openingNotice></openingNotice>

		<!-- 宁东运营 -->
		<view v-if="mine_code == 'ningdongyunying'">
			<t-i-ningdongyunying :mine_code="mine_code" :home_link="home_link" v-if="home_link.length > 0">
			</t-i-ningdongyunying>
		</view>

		<!-- 宁煤集团 -->
		<view v-if="mine_code == 'ningmeijituan'">
			<t-i-ningmeijituan :mine_code="mine_code" :home_link="home_link" v-if="home_link.length > 0">
			</t-i-ningmeijituan>
		</view>

		<!-- 金家渠 -->
		<view v-if="mine_code == 'jinjiaqu' || mine_code == 'jinjiaqu_neiwang'">
			<t-i-jinjiaqu :mine_code="mine_code" :home_link="home_link" v-if="home_link.length > 0"></t-i-jinjiaqu>
		</view>

		<!-- 石槽村 -->
		<view v-if="mine_code == 'shicaocun'">
			<t-i-shicaocun :mine_code="mine_code" :home_link="home_link" v-if="home_link.length > 0"></t-i-shicaocun>
		</view>

		<!-- 金凤 -->
		<view v-if="mine_code == 'jinfeng' || mine_code == 'jinfeng_neiwang'">
			<t-i-jinfeng :mine_code="mine_code" :home_link="home_link" v-if="home_link.length > 0"></t-i-jinfeng>
		</view>

		<!-- 羊场湾 -->
		<view v-if="mine_code == 'yangchangwan'">
			<t-i-yangchangwan :mine_code="mine_code" :home_link="home_link" v-if="home_link.length > 0">
			</t-i-yangchangwan>
		</view>

		<!-- 枣泉 -->
		<view v-if="mine_code == 'zaoquan'">
			<t-i-zaoquan :mine_code="mine_code" :home_link="home_link" v-if="home_link.length > 0"></t-i-zaoquan>
		</view>


	</view>
</template>

<script>
	import {
		set_base_url
	} from '@/common/set_base_url.js'
	import {
		goToLink
	} from '@/common/common.js'
	

	export default {
		data() {
			return {
				// 根_mine_code
				app_mine_code: "",
				// 当前煤矿编码
				mine_code: "",

				// 首页接口的基础请求路径  默认为当前矿编码的基础路径
				base_url: " ",


				// 轮播图
				banners: [],
				// 新闻列表
				newsList: "",
				// 值班公告
				text: "",
				// 固定入口
				iconList: [],
				iconList_arr: [],
				iconList_data: [],
				// 首页链接
				home_link: [],

				// 背景虚化图片
				top_bg_color_img: "",
				// 背景安全色
				top_bg_color: "#7372c2",
				// top_bg_color_list: ['#3e88c6', '#e75038', '#305471', '#f8bf3c', '#37ba73', '#7372c2'],
				top_bg_color_list: ['#d62b2b', '#00a2e8', '#b08654'],
				// 标题栏背景色
				navbar_bg_color: "",

				index_type: 1,

				// 信息平台链接
				ice_list: []
			}
		},
		onPullDownRefresh() {
			uni.reLaunch({
				url: "./index"
			})
			setTimeout(function() {
				uni.stopPullDownRefresh();
			}, 1000);

		},
		onLoad() {
			this.app_mine_code = uni.getStorageSync('mine_code')
			// 初始化当前煤矿编码
			this.mine_code = uni.getStorageSync('mine_code')
			// 根据矿编码切换首页接口不同的请求基础路径
			this.base_url = set_base_url(this.mine_code)

			// 风格定义
			// this.$api.home_style_setting({

			// }).then((res) => {
			// 	console.log(res.data.data)

			// 	this.index_type = res.data.data.value
			// })

			// #ifdef H5
			if (this.app_mine_code == 'ningmeijituan') {
				if (window.location.href.indexOf('token') != -1) {
					this.$api.oauth_getNoPasswordToken({
						token: window.location.href.split('token=')[1].split('&')[0],
						caller: window.location.href.split('token=')[1].split('&')[1].split('=')[1]
					}).then((res) => {

						uni.setStorageSync('Authorization', res.data.data.accessToken)
						uni.setStorageSync('token_type', res.data.data.token_type)
						// 存储用户基本信息
						uni.setStorageSync('user', res.data.data.user);
						uni.setStorageSync('people_code', res.data.data.user.people_code);
						// 存储电话号码
						uni.setStorageSync('mobile', res.data.data.user.mobile);
					})
				} else {
					this.check_token()
				}
			} else {
				this.check_token()
			}
			// #endif

			// #ifdef APP-PLUS
			this.check_token()
			// #endif


			// 获取轮播图
			this.get_banner()
			// 获取首页新闻列表
			this.getNews()
			// 值班公告
			this.get_notice()
			// 固定入口
			this.get_list()
			this.get_home_link()
			// 获取信息平台
			this.get_ICE_list()
		},
		onPageScroll: function(e) {
			if (e.scrollTop > 300) {
				this.navbar_bg_color = "#009fe8"
			} else {
				this.navbar_bg_color = ""
			}
		},
		onShow() {
			// E信-切换矿
			uni.$on('update_kuang', (data) => {
				this.mine_code = data.msg

				// 根据矿编码切换首页接口不同的请求基础路径
				this.base_url = set_base_url(this.mine_code)
				// uni.setStorageSync('base_url',this.base_url)


				// 获取轮播图
				this.get_banner()
				// 获取首页新闻列表
				this.getNews()
				// 值班公告
				this.get_notice()
				// 固定入口
				this.get_list()
				this.get_home_link()
				// 获取信息平台
				this.get_ICE_list()
				this.$forceUpdate()
			})
			uni.$on('common_update_kuang', (data) => {
				this.mine_code = data.msg

				// 根据矿编码切换首页接口不同的请求基础路径
				this.base_url = set_base_url(this.mine_code)
				// uni.setStorageSync('base_url',this.base_url)


				// 获取轮播图
				this.get_banner()
				// 获取首页新闻列表
				this.getNews()
				// 值班公告
				this.get_notice()
				// 固定入口
				this.get_list()
				this.get_home_link()
				// 获取信息平台
				this.get_ICE_list()
				this.$forceUpdate()
			})

			if (this.app_mine_code == 'ningdongyunying') {
				this.setTabBarBadge()
			}
		},
		methods: {
			// 单点登录及token过期验证
			check_token() {
				// token过期验证
				this.$api.worksheet_classify_list({

				}).then((res) => {
					// console.log(res)
					if (res.data.code == 401) {
						uni.showToast({
							icon: "none",
							title: "登录失效、请重新登录"
						})

						uni.clearStorageSync('Authorization');

						setTimeout(function() {
							uni.redirectTo({
								url: "../../my/login/login"
							})
						}, 1500)
					} else {
						// console.log(res)
					}
				})
			},

			// 获取轮播图
			get_banner() {
				if (this.mine_code != "zaoquan") {
					uni.request({
						url: this.base_url + "/scrollImg/list",
						method: "GET",
						header: {
							'accesskey': "b364b449a18af327867f7edc3431b541"
						},
						success: (res) => {
							// console.log(res.data.data.data)
							this.banners = res.data.data.data
							this.top_bg_color_img = this.banners[0].imgURL
						}
					})
				} else {
					this.$api.zaoquan_article_list({
						type: "mkxw",
						page_num: 1,
						page_size: 10
					}).then((res) => {
						
						let newArray = []
						res.data.content.forEach((item,index)=>{
							let banner = {}
							banner.imgURL = item.main_img
							banner.title = item.title
							banner.text = item.text
							newArray.push(banner)
						})
						this.banners = newArray
					})
				}
				
			},
			// 请求新闻动态
			getNews() {
				if (this.mine_code != "zaoquan") {
					uni.request({
						url: this.base_url + "/article/list",
						method: "GET",
						header: {
							'accesskey': "b364b449a18af327867f7edc3431b541"
						},
						data: {
							pageSize: 4
						},
						success: (res) => {
							this.newsList = res.data.data.data
						}
					})
				} else {
					this.$api.zaoquan_article_list({
						type: "mkxw",
						page_num: 1,
						page_size: 4
					}).then((res) => {
						this.newsList = res.data.content
					})
				}

			},
			// 信息平台
			get_ICE_list() {
				this.ice_list = []
				uni.request({
					url: this.base_url + "/home/links",
					header: {
						'accesskey': "b364b449a18af327867f7edc3431b541"
					},
					success: (res) => {
						// console.log(res.data.data)

						if (this.mine_code == 'jinfeng') {
							this.ice_list = res.data.data[2].children
						} else if (this.mine_code == 'ningdongyunying') {
							this.ice_list = res.data.data[1].children
						} else if (this.mine_code == 'ningmeijituan') {
							this.ice_list = res.data.data[1].children
						} else if (this.mine_code == 'shicaocun') {
							this.ice_list = res.data.data[0].children
						} else if (this.mine_code == 'yangchangwan') {
							this.ice_list = res.data.data[1].children
						} else if (this.mine_code == 'shicaocun') {
							this.ice_list = res.data.data[0].children
						}
					}
				})
			},
			// 值班公告
			get_notice() {
				uni.request({
					url: this.base_url + "/notice/list",
					method: "GET",
					header: {
						'accesskey': "b364b449a18af327867f7edc3431b541"
					},
					success: (res) => {
						if (!res.data.data.content) {
							this.text = res.data.data.message
						} else {
							this.text = res.data.data.content.replace(/<br /g, " ").replace(/>/g, " ").replace(
								/\//g, " ")
						}
					}
				})
			},
			// 固定入口
			get_list() {
				this.iconList = []
				// 加分组
				uni.request({
					url: this.base_url + "/homeNav/listByCategory",
					header: {
						'accesskey': "b364b449a18af327867f7edc3431b541"
					},
					method: "GET",
					success: (res) => {
						// console.log(res.data.data)
						this.iconList = res.data.data
					}
				})
			},
			// 首页链接
			get_home_link() {
				this.home_link = []
				uni.request({
					url: this.base_url + "/home/links",
					method: "GET",
					header: {
						'accesskey': "b364b449a18af327867f7edc3431b541"
					},
					success: (res) => {
						// console.log(res)
						this.home_link = res.data.data
					}
				})
			},

			// 切换背景图片
			change_top_bg_color_img(index) {
				// this.top_bg_color_img = this.banners[index].imgURL
				if (index > 2) {
					index = parseInt(3 * Math.random())
				}

				this.top_bg_color = this.top_bg_color_list[index]
			},

			setTabBarBadge() {
				// 闭环管理
				this.$api.five_fixed_fiveFixedNewCategory({

				}).then((res) => {
					// console.log(res.data.content.data)

					let data = res.data.content.data
					let num = 0

					data.forEach((item, index) => {
						num = num + item.no_num
					})

					if (this.mine_code == 'ningdongyunying') {
						if (num > 0) {
							uni.showTabBarRedDot({
								index: 2,
							})
						}
					}
				})

				this.$api.workflow_todo_query({
					staff_num: uni.getStorageSync('user').staff_num,
					query_type: 2
				}).then((res) => {
					let num = 0
					num = res.data.content.data

					if (this.mine_code == 'ningdongyunying') {
						if (num > 0) {
							uni.showTabBarRedDot({
								index: 2,
							})
						}
					}
				})

				this.$api.workflow_todo_query({
					staff_num: uni.getStorageSync('user').staff_num,
					query_type: 1
				}).then((res) => {
					let num = 0
					num = res.data.content.data

					if (this.mine_code == 'ningdongyunying') {
						if (num > 0) {
							uni.showTabBarRedDot({
								index: 2,
							})
						}
					}


				})

			}

		}

	}
</script>

<style lang="scss">
	page {
		background-color: #F6FAF6;
	}

	.top_bg_color {
		position: relative;

		width: 750rpx;
		overflow: hidden;

		.bg_img {
			width: 750rpx;
			height: 440rpx;

			position: absolute;
			top: 0;
			left: 0;
			z-index: 0;
			background-position: center;
			background-size: 100% 100%;
			filter: blur(50px);
			transform: scale(2);
			opacity: 0.9;
		}
	}
</style>