<template>
	<view class="HTodo-v" :key="key" v-if="!loading">
		<view class="system-close" v-if="!hasAuth">您暂无该控件的访问权限</view>
		<view class="HTodo-box" v-else>
			<view class="HTodo-list u-flex" :style="{'flex-wrap':option.styleType==1?'nowrap':'wrap'}">
				<view class="u-flex-col HTodo-list-item" v-for="(item,index) in option.defaultValue" :key="index"
					@click="jump(item)" :style="option.style">
					<view class="u-m-b-8" style="position: relative">
						<view :class="item.icon" class="icon"
							:style="{'background-color':item.iconColor||item.iconBgColor || '#008cff'}"></view>
						<u-badge type="error" :offset="offset" :count="item.num" show-zero
							:bgColor="option.valueFontBgColor"
							:style="{'color':option.valueFontColor,'font-weight':option.valueFontWeight?700:400}">
						</u-badge>
					</view>
					<view class="u-line-1 title"
						:style="{'font-size':option.labelFontSize,'color':option.labelFontColor,'font-weight':option.labelFontWeight?700:400}">
						{{item.fullName}}
					</view>
				</view>
			</view>
		</view>
	</view>
</template>
<script>
	import {
		getFlowTodoCount
	} from "@/api/workFlow/flowEngine";
	export default {
		props: {
			config: {
				type: Object,
				default: () => {}
			}
		},
		data() {
			return {
				offset: ['-8', '-14'],
				option: {},
				propsApi: '',
				key: +new Date(),
				flowDone: [],
				toBeReviewed: [],
				flowCirculateType: [],
				hasAuth: true,
				loading: true,
			}
		},
		created() {
			this.init()
			uni.$off('proRefresh')
			uni.$on('proRefresh', () => {
				this.initData()
			})
		},
		methods: {
			getFlowTodoCount() {
				this.loading = true
				for (let i = 0; i < this.option.defaultValue.length; i++) {
					let defaultValue = this.option.defaultValue[i]
					if (defaultValue.category && defaultValue.category.length) this[defaultValue.id] = defaultValue
						.category
				}
				const query = {
					flowCirculateType: this.flowCirculateType,
					flowDoneType: this.flowDone,
					toBeReviewedType: this.toBeReviewed,
				}
				getFlowTodoCount(query).then(res => {
					let data = res.data || {}
					this.hasAuth = data.isToSign || data.isTodo || data.isDoing || data.isDone ||
						data.isCirculate;
					this.option.defaultValue.forEach(e => {
						if (e.id == 'flowToSign') {
							e.num = data.flowToSign || 0;
							if (!e.noShow) e.noShow = !data.isToSign;
						}
						if (e.id == 'flowTodo') {
							e.num = data.flowTodo || 0;
							if (!e.noShow) e.noShow = !data.isTodo;
						}
						if (e.id == 'flowDoing') {
							e.num = data.flowDoing || 0;
							if (!e.noShow) e.noShow = !data.isDoing;
						}
						if (e.id == 'flowDone') {
							e.num = data.flowDone || 0;
							if (!e.noShow) e.noShow = !data.isDone;
						}
						if (e.id == 'flowCirculate') {
							e.num = data.flowCirculate || 0;
							if (!e.noShow) e.noShow = !data.isCirculate;
						}
					})
					this.option.defaultValue = this.option.defaultValue.filter((o) => !o.noShow)
					this.key = +new Date()
					this.loading = false
				})
			},
			init() {
				this.initData()
				if (!this.config.allRefresh.autoRefresh && this.config.refresh.autoRefresh) {
					setInterval(this.initData, this.config.refresh.autoRefreshTime * 60000)
				}
			},
			initData() {
				this.option = JSON.parse(JSON.stringify(this.config.option))
				let style;
				style = {
					'width': '240rpx'
				}
				if (this.option.styleType == 2) {
					style = {
						'width': 100 / this.option.appRowNumber + '%'
					}
					if (this.option.appShowBorder) {
						style['border-right'] = '2rpx solid #f0f2f6'
						style['border-bottom'] = '2rpx solid #f0f2f6'
					}
				}
				this.option.style = style
				this.getFlowTodoCount()
				this.option.labelFontSize = this.option.labelFontSize * 2 + 'rpx'
				this.key = +new Date()
			},
			jump(item) {
				if (this.config.platform === 'mp') return
				const sysConfigInfo = uni.getStorageSync('sysConfigInfo')
				if (['flowToSign'].includes(item.id) && sysConfigInfo.flowSign == 0) return this.$u.toast(
					"系统未开启流程待签，无法跳转")
				if (['flowTodo'].includes(item.id) && sysConfigInfo.flowTodo == 0) return this.$u.toast(
					"系统未开启流程待办，无法跳转")
				let url = '/my/entrustAgent'
				let tabIndex = {
					'flowToSign': 0,
					'flowTodo': 1,
					'flowDoing': 2,
					'flowDone': 4,
					'flowCirculate': 5,
				}
				let data = {
					tabIndex: tabIndex[item.id]
				}
				if (['flowToSign', 'flowDoing', 'flowDone', 'flowCirculate', 'flowTodo'].includes(item.id)) url =
					'/workFlow/flowTodo'
				uni.navigateTo({
					url: `/pages${url}/index?data=${encodeURIComponent(JSON.stringify(data))}`
				})
				uni.navigateTo({
					url: url,
					fail: (err) => {
						// this.$u.toast("暂无此页面")
					}
				})
			}
		}
	}
</script>
<style lang="scss">
	.HTodo-v {
		width: 100%;
		height: 100%;

		.system-close {
			color: #8b949e;
			display: flex;
			align-items: center;
			justify-content: center;
			height: 150rpx;
			overflow: hidden;
			width: 100%;
		}

		.HTodo-box {
			width: 100%;
			overflow-x: scroll;

			.HTodo-list {


				.HTodo-list-item {
					align-items: center;
					padding: 16rpx 20rpx;

					.title {
						width: 100%;
						text-align: center;
					}

					.icon {
						width: 90rpx;
						height: 90rpx;
						font-size: 60rpx;
						color: #fff;
						text-align: center;
						line-height: 90rpx;
						border-radius: 50%;
					}
				}
			}
		}
	}
</style>