<template>
	<view style="overflow: hidden;">
		<page-meta :root-font-size="getFontSizeValue"></page-meta>
		<view class="uni-margin-wrap">
			<swiper class="swiper" circular :indicator-dots="indicatorDots" :autoplay="autoplay" :interval="interval"
				:duration="duration">
				<swiper-item v-for="item in bannnerData" :key="item.id">
					<image :src="item.imgPath" mode=""></image>
				</swiper-item>
			</swiper>
		</view>

		<view class="orderManage">
			<view class="orderTit" style="padding-left: 15px;color: rgb(126, 131, 125);">
				<text
					style="width: 4px;background-color: rgb(26, 188, 156);height: 20px;margin-top: 10px;margin-right: 5px;"></text>
				<text>工单模块</text>
			</view>

			<view class="orderCon">
				<view class="orderList" v-for="item in orderListData" :key="item.id" @click="workOrderListClick(item)">
					<image :style="{ background: item.imageBg }" :src="item.imagePath" mode=""></image>
					<text>{{ item.text }}</text>
				</view>
			</view>

			<view class="orderTit" @click="changeStore" v-if="noticState">
				<image style="background: #ff9600" src="/static/icon_noticefi.png"></image>
				<text>公告通知</text>
			</view>

			<view class="notification">
				<view class="notifiList" v-for="item in noticListData" :key="item.id">
					<image :style="{ background: item.imageBg }" :src="item.imagePath" mode=""></image>
					<view class="">
						<text>{{ item.textTit }}</text>
						<text>{{ item.textCon }}</text>
					</view>
				</view>
			</view>
		</view>
		<!-- 弹框 -->
		<view>
			<uni-popup ref="inputDialogs" type="dialog">
				<uni-popup-dialog ref="inputClose" mode="input" :title="title" @confirm="dialogInputConfirm">
					<view style="width:100%;">
						<uni-table ref="table" border stripe type="selection" emptyText="暂无更多数据"
							@selection-change="selectionChange">
							<uni-tr>
								<uni-th align="center">科室</uni-th>
							</uni-tr>
							<uni-tr v-for="(item, index) in allowData" :key="index">
								<uni-td align="center">{{ $mHelper.findParents(departList, item.deptid) }}</uni-td>
							</uni-tr>
						</uni-table>
					</view>
				</uni-popup-dialog>
			</uni-popup>
		</view>
	</view>
</template>
<script>
	import {
		mapGetters
	} from "vuex"
	import store from '@/store';
	import {
		getToken
	} from '@/utils/auth'
	import listUrl  from '@/utils/listUrl.js'
	import pageData from '../myTask/repairList/addRepair/pageData.js'
	export default {
		data() {
			return {
				indicatorDots: true,
				getFontSizeValue: '1rem',
				autoplay: true,
				interval: 2000,
				noticState: false,
				duration: 500,
				bannnerData: [],
				orderListData: [],
				noticListData: [],
				departList: [],
				allowData: [],
				title: "允用科室",
				selectedIndexs:[],
				onchangedept: ''
			}
		},
		computed: {
			...mapGetters(["State"]),
		},
		onLoad() {
			this.init()
			this.saveDept()
			this.getFontSizeValue = uni.getStorageSync('fontSizeValue') + 'rem'
			// this.getFontSizeValue = '1.5rem'
			if (uni.getStorageSync("roleCode") === "APRY") {
				uni.hideTabBar()
			}
			if (uni.getStorageSync("isAllow") === "1") {
				var str = uni.getStorageSync('storageAllowUserDept') //获取允用科室ids
				var arr = []
				if (str.indexOf(',') >= 0) {
					for (var i = 0; i < str.split(",").length; i++) {
						arr.push({
							deptname: "",
				  	deptid: str.split(",")[i]
						})
					}
					this.allowData = arr
				}
				this.$nextTick(function() {
					// DOM 更新了
					this.$refs.inputDialogs.open()
				})

			}
			// uni.hideTabBar()
		},
		methods: {
			selectionChange(e){
				this.selectedIndexs = e.detail.index
				this.onchangedept = this.allowData[e.detail.index].deptid
				console.log(this.allowData[e.detail.index])
			},
			dialogInputConfirm(){
				if(this.selectedIndexs.length!=1){
					this.$mHelper.toast("请选择一行数据");
					return;
				}
				const params={
					deptId:this.onchangedept,
					userid:uni.getStorageSync('userId')
				}
				this.$http.get('UserAccount/ChangeDeptId', params).then(res=>{
					if(res.state == "success"){
						this.$mHelper.toast(res.message);
						this.$refs.inputDialogs.close()
						this.$mStore.dispatch("GetInfo").then((e)=>{
							if(e.data.Dept){
								uni.setStorageSync('deptId', e.data.Dept.F_DeptId)
								uni.setStorageSync('deptName', e.data.Dept.F_DeptName)
							}
						})
					}
				})
			},
			init() {
				// #ifdef APP-PLUS
				let timer
				timer = setInterval(() => {
					plus.push.getClientInfoAsync((info) => {
						let cid = info["clientid"];
						if (cid) {
							clearInterval(timer)
							this.clienGet(cid) //获取cid 
						}
					});
				}, 5000)

				this.upData()
				// #endif
				this.bannnerData = this.$mConstDataConfig.bannnerData
				this.getMenuPermissions() //获取用户权限
			},
			saveDept() {
				pageData.getDet((res, data) => {
					this.departList = data
					uni.setStorageSync('deptTreeData', JSON.stringify(res))
					uni.setStorageSync('deptAllList', JSON.stringify(data))
				})
			},
			changeStore() {
				this.$mStore.dispatch("STATE", 3)
			},
			workOrderListClick(item) {
				uni.navigateTo({
					url: item.navigateToUrl,
				})
			},
			clienGet(cid) {

				let apptype
				if (plus.device.model.indexOf('iPhone') > -1) {
					apptype = 2;
				} else {
					apptype = 1;
				}
				const params = {
					apptype: apptype,
					clientid: cid,
					token: uni.getStorageSync("token"),
				}
				this.$http.get("PushMessage/PutAppClientIdAsync", params).then((response) => {
						if (response.state.toLowerCase() === "success") {
							let data = response.message
						}

					})
					.catch((e) => {
						console.log(e)
					})

			},
			upData() {
				plus.runtime.getProperty(plus.runtime.appid, function(wgtinfo) {
					uni.setStorageSync('versionCode', wgtinfo.version)
				});
				let typeUp = ''
				if (plus.device.model.indexOf('iPhone') > -1) {
					typeUp = 1;
				} else {
					typeUp = 0;
				}
				const params = {
					type: typeUp,
					isNew: true,
				}
				this.$http.get('/ApplicationsVersion/GetAndroid', params).then((response) => {
					if (response.state == "success") {
						let data = response.data
						console.log(data)
						//NewCode_ = data.data.F_VersionCode; 
						const nowVersionCode = uni.getStorageSync('versionCode')
						const serveVersionCode = data.F_VersionCode
						console.log(nowVersionCode + "---" + serveVersionCode)
						if (nowVersionCode !== serveVersionCode) {
							if (typeUp === 0) {
								
								uni.showModal({
									title: '更新',
									content: '是否更新',
									showCancel:false,
									success: function (res) {
										if (res.confirm) { 
											plus.runtime.openURL(data.FileUrl[0].F_FileUrl, err => {
												plus.runtime.openURL(data.FileUrl[0].F_FileUrl);
											}, 'com.android.browser');
										} else if (res.cancel) {
											this.$mHelper.toast("取消更新");
										}
									}
								});
							} else if (typeUp === 1) {
								uni.showModal({
									title: '更新',
									content: '是否更新',
									showCancel:false,
									success: function (res) {
										if (res.confirm) {
											plus.runtime.openURL(data.F_Url);
										} else if (res.cancel) {
											this.$mHelper.toast("取消更新");
										}
									}
								});

							}


						}

					}
				}).catch((e) => {
					console.log(e);
				})
			},

			getMenuPermissions() {
				let code = uni.getStorageSync("roleCode")
				this.$http.get("InternalMessages/GetCount").then((response) => {
						if (response.state.toLowerCase() === "success") {
							if (response.IsRead == 1) {
								uni.showTabBarRedDot({
									index: 2
								})
							} else {
								uni.hideTabBarRedDot({
									index: 2
								})
							}
						}

				})
				.catch((e) => {
					console.log(e)
				})
				this.$http.get("Index/GetMenu").then((response) => {
					if (response.state.toLowerCase() === "success") {
						if (response.data.length>0) {

							this.orderListData = listUrl.getMenuData(response.data,code)
							// console.log(this.orderListData)
						}
					}
				})
				.catch((e) => {
					console.log(e)
				})
			},
		},
	}
</script>

<style lang="scss">
	.equipmentInfo {
		width: 100%;
		margin-top: 15px;
		border: 1px solid rgb(229, 229, 229);
		box-shadow: 0px 4px 16px 0px rgba(69, 91, 99, 0.05);
		margin-bottom: 20px;
		border-radius: 5px;

		.trCon {
			width: 100%;
			// font-size: 14px;

			.tdCon {
				display: inline-block;
				padding: 10px 0px;
				color: rgb(102, 102, 102);
				width: 48%;
				padding-right: 5px;
				text-align: center;
			}
		}
	}

	.uni-margin-wrap {
		width: 690rpx;
		width: 100%;
	}

	.swiper {
		height: 225px;

		image {
			width: 100%;
			height: 100%;
		}
	}

	.orderManage {
		.orderTit {
			margin-top: 15rpx;
			width: 100%;
			height: 80rpx;
			line-height: 80rpx;
			background: #ffffff;
			display: flex;
			border-bottom: 1rpx solid #eeeeee;

			text {
				font-size: 1rem;
			}

			image {
				width: 24rpx;
				height: 24rpx;
				padding: 8rpx;
				border-radius: 12px;
				background: #1972c2;
				margin: 20rpx 10rpx 20rpx 26rpx;
			}
		}

		.orderCon {
			background: #ffffff;
			display: flex;
			flex-wrap: wrap;

			.orderList {
				width: 186rpx;
				height: 206rpx;
				text-align: center;
				border-right: 2rpx solid #eeeeee;
				border-bottom: 2rpx solid #eeeeee;

				image {
					width: 40rpx;
					height: 40rpx;
					margin: 0 auto;
					display: block;
					border-radius: 35rpx;
					padding: 15rpx;
					margin-top: 45rpx;
				}

				text {
					line-height: 60rpx;
				}
			}

			.orderList:nth-child(4n + 4) {
				border-right: none;
			}

			.orderList:last-child {
				border-right: none;
			}
		}

		.notification {
			background: #ffffff;
			margin-bottom: 100rpx;
			display: flex;
			flex-wrap: wrap;
			border-bottom: 1rpx solid #eeeeee;

			.notifiList {
				width: 375rpx;
				height: 172rpx;
				padding: 46rpx 30rpx 46rpx 30rpx;
				text-align: left;
				border-right: 1rpx solid #eeeeee;
				border-bottom: 1rpx solid #eeeeee;
				box-sizing: border-box;
				display: flex;

				image {
					width: 48rpx;
					height: 48rpx;
					box-sizing: border-box;
					border-radius: 24rpx;
					padding: 10rpx;
					margin: 19rpx;
				}

				text {
					font-size: 24rpx;
					line-height: 30rpx;
				}
			}

			.notifiList:nth-child(2n + 2) {
				border-right: none;
			}
		}
	}
</style>
