<template>
	<view style="background-color: #fff;">
		<view v-if="gwShow" style="padding: 20rpx;">
			<view style="font-size: 38rpx;font-weight: 600;">公文办理</view>
			<view style="margin-top: 40rpx;">
				<u-grid :border="false" col="4">
					<u-grid-item v-if="swBlock" @click="shouwen('收文','003,012',1)">
						<u-badge style="z-index: 2;" :type="type" max="99" :value="shouwenNum" :offset="[-10,10]"
							:showZero="false" absolute></u-badge>
						<image src="/static/swgl.png" mode="widthFix" style="width: 100rpx;"></image>
						<text class="grid-text">收文</text>
					</u-grid-item>
					<u-grid-item v-if="fwBlock" @click="shouwen('发文','002',2)">
						<u-badge style="z-index: 2;" :type="type" max="99" :value="numList['002']" :offset="[-10,10]"
							:showZero="false" absolute></u-badge>
						<image src="/static/fwgl.png" mode="widthFix" style="width: 100rpx;"></image>
						<text class="grid-text">发文</text>
					</u-grid-item>
					<!-- <u-grid-item @click="shouwen('其他处室收文','012',1)">
						<u-badge style="z-index: 2;" :type="type" max="99" :value="numList['012']" :offset="[-10,10]"
							:showZero="false" absolute></u-badge>
						<image src="/static/swgl.png" mode="widthFix" style="width: 100rpx;"></image>
						<text class="grid-text">收文-急件</text>
					</u-grid-item> -->
					<!-- 
					<u-grid-item @click="shouwen('其他处室发文','005',2)">
						<u-badge style="z-index: 2;" :type="type" max="99" :value="numList['005']" :offset="[-10,10]"
							:showZero="false" absolute></u-badge>
						<image src="/static/fwgl.png" mode="widthFix" style="width: 100rpx;"></image>
						<text class="grid-text">其他处室发文</text>
					</u-grid-item> -->
				</u-grid>
			</view>
		</view>
		<u-divider v-if="gwShow"></u-divider>
		<view v-if="rcbgShow" style="padding: 20rpx;">
			<view style="font-size: 38rpx;font-weight: bold;">日常办公</view>
			<view style="margin-top: 40rpx;">
				<u-grid :border="false" col="4">
					<!-- 	<u-grid-item v-if="ccbaBlock" @click="dailyWork('中层出差备案','006',1)">
						<u-badge style="z-index: 2;" :type="type" max="99" :value="numList['006']" :offset="[-10,10]"
							:showZero="false" absolute></u-badge>
						<image src="/static/ccba.png" mode="widthFix" style="width: 100rpx;"></image>
						<text class="grid-text">出差备案</text>
					</u-grid-item>
					<u-grid-item v-if="wcskBlock" @click="dailyWork('外出授课备案','007',2)">
						<u-badge style="z-index: 2;" :type="type" max="99" :value="numList['007']" :offset="[-10,10]"
							:showZero="false" absolute></u-badge>
						<image src="/static/skba.png" mode="widthFix" style="width: 100rpx;"></image>
						<text class="grid-text">授课备案</text>
					</u-grid-item> -->
					<u-grid-item v-if="qjBlock" @click="dailyWork('职工请 ( 休 ) 假申请','021',3)">
						<u-badge style="z-index: 2;" :type="type" max="99" :value="numList['021']" :offset="[-10,10]"
							:showZero="false" absolute></u-badge>
						<image src="/static/qingjia.png" mode="widthFix" style="width: 100rpx;"></image>
						<text class="grid-text">请假申请</text>
					</u-grid-item>
					<!-- 	<u-grid-item v-if="xxfbshBlock" @click="shouwen('信息发布审核','010',3)">
						<u-badge style="z-index: 2;" :type="type" max="99" :value="numList['010']" :offset="[-10,10]"
							:showZero="false" absolute></u-badge>
						<image src="/static/xxfbsh.png" mode="widthFix" style="width: 100rpx;"></image>
						<text class="grid-text">信息发布审核</text>
					</u-grid-item> -->
					<u-grid-item v-if="sqycBlock" @click="dailyWork('用车申请','015',4)">
						<u-badge style="z-index: 2;" :type="type" max="99" :value="numList['015']" :offset="[-10,10]"
							:showZero="false" absolute></u-badge>
						<image src="/static/usecar.png" mode="widthFix" style="width: 100rpx;"></image>
						<text class="grid-text">用车申请</text>
					</u-grid-item>
					<!-- 	<u-grid-item v-if="tbsqBlock" @click="dailyWork('调班申请','020',5)">
						<u-badge style="z-index: 2;" :type="type" max="99" :value="numList['020']" :offset="[-10,10]"
							:showZero="false" absolute></u-badge>
						<image src="/static/tiaoban.png" mode="widthFix" style="width: 100rpx;"></image>
						<text class="grid-text">调班申请 </text>
					</u-grid-item> -->
					<u-grid-item v-if="yzsqBlock" @click="dailyWork('用章申请','017',6)">
						<u-badge style="z-index: 2;" :type="type" max="99" :value="numList['017']" :offset="[-10,10]"
							:showZero="false" absolute></u-badge>
						<image src="/static/yongzhang.png" mode="widthFix" style="width: 99rpx;"></image>
						<text class="grid-text">用章申请 </text>
					</u-grid-item>
					<!-- <u-grid-item v-if="htyzBlock" @click="dailyWork('合同用章申请','018',7)">
						<u-badge style="z-index: 2;" :type="type" max="99" :value="numList['018']" :offset="[-10,10]"
							:showZero="false" absolute></u-badge>
						<image src="/static/htyongzhang.png" mode="widthFix" style="width: 99rpx;"></image>
						<text class="grid-text">合同用章申请 </text>
					</u-grid-item>
					<u-grid-item v-if="zbtjBlock" @click="weekly('周报')">
						<u-badge style="z-index: 2;" :type="type" max="99" :offset="[-10,10]" :showZero="false"
							absolute></u-badge>
						<image src="/static/weekly.png" mode="widthFix" style="width: 99rpx;"></image>
						<text class="grid-text">周报 </text>
					</u-grid-item>
					<u-grid-item v-if="dbsxBlock" @click="dailyWork('督办事项','016',8)">
						<u-badge style="z-index: 2;" :type="type" max="99" :value="totalValue" :offset="[-10,10]"
							:showZero="false" absolute></u-badge>
						<image src="/static/duban.png" mode="widthFix" style="width: 100rpx;"></image>
						<text class="grid-text">督办事项 </text>
					</u-grid-item> -->
				</u-grid>
			</view>
			<u-divider></u-divider>
		</view>
		<view v-if="qtyyShow" class="">
			<view style="padding:10rpx 20rpx;">
				<view style="font-size:38rpx;font-weight: bold;">其他应用</view>
				<view style="margin-top: 40rpx;">
					<u-grid :border="false" col="4">
						<u-grid-item v-if="kq != 0&&kqglShow" @click="kaoqin">
							<image src="/static/kqgl.png" mode="widthFix" style="width: 100rpx;"></image>
							<text class="grid-text">考勤管理</text>
						</u-grid-item>

						<u-grid-item v-if="zbglShow" @click="zhiban">
							<image src="/static/zhiban.png" mode="widthFix" style="width: 100rpx;"></image>
							<text class="grid-text">值班安排</text>
						</u-grid-item>
					</u-grid>
				</view>
			</view>
		</view>

		<!-- <u-divider></u-divider>
		<view style="padding: 20rpx;">
			<view style="font-size:38rpx;font-weight: bold;">值班安排</view>
			<view style="margin-top: 40rpx;">
				<u-grid :border="false" col="4">
					<u-grid-item @click="zhiban">
						<image src="/static/zhiban.png" mode="widthFix" style="width: 100rpx;"></image>
						<text class="grid-text">值班安排</text>
					</u-grid-item>
				</u-grid>
			</view>
		</view> -->
	</view>
</template>

<script>
	export default {
		data() {
			return {
				//待办相关
				db: {
					pageNum: 1,
					pageSize: 20,
					fkFlow: null,
					title: null,
					token: uni.getStorageSync('jtoken')
				},
				numList: {},
				type: "error",
				value: 100,
				num: 0,
				kq: false,
				gwShow: false,
				rcbgShow: false,
				fwBlock: false,
				swShow: false,
				fwShow: false,
				qjBlock: false,
				ccbaBlock: false,
				wcskBlock: false,
				xxfbshBlock: false,
				xslwspBlock: false,
				tbsqBlock: false,
				dbsxBlock: false,
				zbtjBlock: false,
				yzsqBlock: false,
				sqycBlock: false,
				htyzBlock: false,
				qtyyShow: false,
				kqglShow: false,
				zbglShow: false,
				totalValue: null,
				shouwenNum: null
			}
		},
		onLoad() {
			uni.$u.http.get('/getInfo').then(res => {
				console.log('res', res)
				res.permissions.filter((item) => {
					if (
						item == "lcqd:fw:start" ||
						item == "lcqd:sw:item" ||
						item == "*:*:*"
					) {
						this.gwShow = true;
					}
					if (
						item == "lcqd:qj:start" ||
						item == "lcqd:ccba:start" ||
						item == "lcqd:wcsk:start" ||
						item == "lcqd:xxfbsh:start" ||
						item == "lcqd:xslwsp:start" ||
						item == "lcqd:tbsq:start" ||
						item == "lcqd:sqyc:start" ||
						item == "lcqd:dbsx:start" ||
						item == "lcqd:zbtj:start" ||
						item == "lcqd:yzsq:start" ||
						item == "lcqd:htyz:start" ||
						item == "*:*:*"
					) {
						this.rcbgShow = true;
					}
					if (item == 'oa:attendance:index' || item == 'duty:dutyl:list' ||
						item == "*:*:*") {
						this.qtyyShow = true
					}
					if (item == 'oa:attendance:index' ||
						item == "*:*:*") {
						this.kqglShow = true
					}
					if (item == 'duty:dutyl:list' ||
						item == "*:*:*") {
						this.zbglShow = true
					}
					if (item == "lcqd:fw:start" ||
						item == "*:*:*") {
						this.fwBlock = true
					}
					if (item == 'lcqd:sw:item' ||
						item == "*:*:*") {
						this.swBlock = true
					}
					if (item == 'lcqd:qj:start' ||
						item == "*:*:*") {
						this.qjBlock = true
					}
					if (item == 'lcqd:ccba:start' ||
						item == "*:*:*") {
						this.ccbaBlock = true
					}
					if (item == 'lcqd:wcsk:start' ||
						item == "*:*:*") {
						this.wcskBlock = true
					}
					if (item == 'lcqd:xxfbsh:start' ||
						item == "*:*:*") {
						this.xxfbshBlock = true
					}
					if (item == 'lcqd:xslwsp:start' ||
						item == "*:*:*") {
						this.xslwspBlock = true
					}
					if (item == 'lcqd:tbsq:start' ||
						item == "*:*:*") {
						this.tbsqBlock = true
					}
					if (item == 'lcqd:dbsx:start' ||
						item == "*:*:*") {
						this.dbsxBlock = true
					}
					if (item == 'lcqd:sqyc:start' ||
						item == "*:*:*") {
						this.sqycBlock = true
					}
					if (item == 'lcqd:yzsq:start' ||
						item == "*:*:*") {
						this.yzsqBlock = true
					}
					if (item == 'lcqd:zbtj:start' ||
						item == "*:*:*") {
						this.zbtjBlock = true
					}
					if (item == 'lcqd:htyz:start' ||
						item == "*:*:*") {
						this.htyzBlock = true
					}
				});
			})
			this.needUpdateApp()
		},
		onShow() {
			this.shouwenNum = null
			this.kq = uni.getStorageSync('type')
			this.getdbNum()
			this.getUnreadEmail()
			// this.getdbTotal()
			console.log('75555555555')
			// uni.$off()
			uni.$on('query', (query) => {
				console.log('query', query)
				if (query.a == 'stag') {
					console.log('55555557')
					this.getdbNum()
				}
			})
		},
		watch: {
			'num': {
				handler: function(v, o) {
					if (this.num > 0) {
						this.$forceUpdate()
					}
				},
				deep: true
			},
		},
		methods: {
			//升级
			needUpdateApp(i) {
				var _this = this;
				plus.runtime.getProperty(plus.runtime.appid, function(wgtinfo) {
					uni.showLoading({
						title: "检查中...",
						mask: true
					})
					uni.$u.http.post('/app/version/advance/' + wgtinfo.versionCode).then(res => {
						if (res.code == 200) {
							if (res.data != undefined) {
								uni.hideLoading()
								uni.showModal({
									title: "提示",
									content: "有新版本是否进行更新",
									success(qwe) {

										if (qwe.confirm) {
											_this.upgradeApp(getApp().globalData.updateUrl + res
												.data.url)
										}
									}
								})
							} else {
								uni.hideLoading()
								// uni.showToast({
								// 	title: "当前为最新版本",
								// 	icon: "none"
								// })
							}
						}
					}).catch(res => {
						uni.hideLoading()
					})
				})
			},
			upgradeApp(url) {
				uni.showLoading({
					title: '更新中……'
				});
				uni.downloadFile({
					url: url,
					success: downloadResult => {
						if (downloadResult.statusCode === 200) {
							try {
								var filePath = downloadResult.tempFilePath;
								var ext = filePath.substring(
									filePath.lastIndexOf('.') + 1
								);
								plus.runtime.install(
									downloadResult.tempFilePath, {},
									function() {
										uni.hideLoading()
										plus.runtime.restart();
									},
									function(e) {
										// console.log(e);
									}
								);
							} catch (e) {
								// console.log(e);
							}
						} else {
							uni.hideLoading()
							uni.showToast({
								title: "下载失败",
								icon: 'none'
							})
						}
					},
					fail: res => {

					}
				});
			},
			getdbTotal() {
				uni.$u.http.get('/offcial/supervision/dblist', {
					params: this.db
				}).then(res => {
					console.log('res', res)
					this.totalValue = res.total
					// uni.hideLoading()
					// this.dbList = [...this.dbList, ...res.rows]
					// if (res.total <= this.db.pageSize) {
					// 	this.statusList[0] = "nomore"
					// } else {
					// 	this.statusList[0] = "loadmore"
					// }
					// this.$forceUpdate()
					// this.dbTotal = Math.ceil(res.total / this.db.pageSize)
				})
			},
			// 周报
			weekly() {
				uni.navigateTo({
					url: "/pages/weekly/weekly"
				})
			},
			//收发文
			shouwen(v, i, t) {
				uni.navigateTo({
					url: `/pages/shouwen/shouwen?title=${v}&fkFlow=${i}&type=${t}`
				})
			},
			//考勤
			kaoqin() {
				uni.navigateTo({
					url: "/pages/kaoqin/kaoqin"
				})
			},
			// 值班
			zhiban() {
				uni.navigateTo({
					url: "/pages/zhibananpai/zhibananpai"
				})
			},
			//日常办公
			dailyWork(v, i, t) {
				uni.navigateTo({
					url: `/pages/dailyWork/dailyWork?title=${v}&fkFlow=${i}&type=${t}`
				})
			},
			//查询待办数量
			getdbNum() {
				let that = this
				uni.$u.http.get('/jflow/restful/DB_Todolist_Num?token=' + uni.getStorageSync('jtoken')).then(res => {
					if (res.code == 200) {
						this.num = 0
						this.numList = {}
						if (res.list.length > 0) {
							let qwe = 0
							for (let i in res.list) {
								if (res.list[i].FK_Flow != '008' & res.list[i].FK_Flow != '016') {
									qwe = res.list[i].Total + qwe
									this.numList[res.list[i].FK_Flow] = res.list[i].Total
									this.num++
								}
								console.log('565', this.numList['003'])
								if (this.numList['003'] != null && this.numList['012'] != null && this.numList[
										'003'] != undefined && this.numList['012']) {
									this.shouwenNum = this.numList['003'] + this.numList['012']
								} else if (this.numList['003'] != null && this.numList['003'] != undefined) {
									this.shouwenNum = this.numList['003']
								} else if (this.numList['012'] != null && this.numList['012'] != undefined) {
									this.shouwenNum = this.numList['012']
								}

								console.log('qwe', qwe)
								console.log('total', that.totalValue)
								if (qwe == '0') {
									qwe = that.totalValue
								} else {
									qwe = qwe + that.totalValue
								}

								console.log('qwe', qwe)
								if (i == res.list.length - 1) {
									this.$forceUpdate()
									uni.setTabBarBadge({ //显示数字
										index: 0, //tabbar下标
										text: '' + qwe + '' //数字
									})
								}
							}
						} else {
							this.$forceUpdate()
							uni.removeTabBarBadge({ //显示数字
								index: 0, //tabbar下标
							})
						}
						console.log('this。', this.numList)
					}

				})
			},
			//获取未读邮件
			getUnreadEmail() {
				uni.$u.http.get('/oa/mail/count/unread').then(res => {
					if (res == 0) {
						this.$forceUpdate()
						uni.removeTabBarBadge({ //显示数字
							index: 1, //tabbar下标
						})
					} else {
						this.$forceUpdate()
						uni.setTabBarBadge({ //显示数字
							index: 1, //tabbar下标
							text: '' + res + '' //数字
						})
					}
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.grid-text {
		font-size: 28rpx;
		color: #606266;
		padding: 10rpx 0 20rpx 0rpx;
		/* #ifndef APP-PLUS */
		box-sizing: border-box;
		/* #endif */
	}

	::v-deep .u-badge {
		width: 36rpx;
		height: 36rpx;
		display: flex;
		font-size: 28rpx;
		align-items: center;
		justify-content: center;
		// font-weight: bold;
	}
</style>