<template>
	<view class="todo-page">
		<view class="status-bar" :style="{'width': screenWidth + 'px'}"></view>
		<view class="main">
			<view class="swiper-wrap">
				<u-swiper :list="banners" height="110" radius="8" circular indicator indicatorActiveColor="#004F9D"
					indicatorInactiveColor="#D7D7D7" indicatorMode="dot" indicatorStyle="right: 20px;"
					@change="e => currentBanner = e.current">
				</u-swiper>
			</view>
			<view class="tabs-bar">
				<view @click="handleTab('1')" class="tab">
					<view class="t">
						<text class="name" :style="{color: currentTab === '1' ? '#004F9D' : ''}">任务</text>
						<u-badge type="error" absolute max="99" :value="todoTotal" :offset="[0, 0]"></u-badge>
					</view>
					<view v-if="currentTab === '1'" class="bottom-widget"></view>
				</view>
				<view @click="handleTab('3')" class="tab">
					<view class="t">
						<text class="name" :style="{color: currentTab === '3' ? '#004F9D' : ''}">消息</text>
						<u-badge type="error" absolute max="99" :value="noticeTotal" :offset="[0, 0]"></u-badge>
					</view>
					<view v-if="currentTab === '3'" class="bottom-widget"></view>
				</view>
			</view>
			<view v-if="!lists.length" class="empty-wrap">
				<u-empty text="暂无任务" icon="/static/empty@2x.png">
				</u-empty>
			</view>
			<view v-else class="content-list">
				<common-card :dataSource="item" v-for="(item, key) in lists" :key="key" icon @click="taskClisk">
				</common-card>
			</view>
		</view>

	</view>
</template>

<script>
	import CommonCard from '@/components/custom/common-card.nvue';

	import {
		handleTaskStatus,
		getTodoData,
		FullGet
	} from '../../services/app'
	import {
		initFilter,
		extendRule,
		extendInclude
	} from '../../utils/common'

	// import '@/utils/showMsg.js'
	export default {
		components: {
			CommonCard
		},
		data() {
			return {
				timingGet: undefined,
				affairType: '1',
				currentTab: '1',
				todoTotal: 0,
				noticeTotal: 0,
				lists: [],
				banners: [
					'../../static/banner02.png',
					'../../static/banner03.png',
					'../../static/banner04.png'
				],
				currentBanner: 0,
				text: '',
				statusBar: '54px',
				IsBrush: true,
				pageIndex: 1,
				comPickerInfo: {
					showPicker: false,
					columns: [],
					title: '',
					name: ''
				}
			}
		},
		onShow() {
			// let allPages=getCurrentPages()
			// let lastPage=allPages-1
			// let option =allPages[lastPage].options
			this.pageIndex = 1
			this.handleGetTodoData()
			this.timingGet = setInterval(() => {
				const d = getApp().globalData.noticeAndTask
				if (d.Msg > this.noticeTotal || d.Task > this.todoTotal) {
					this.pageIndex = 1
					this.handleGetTodoData()
				}
			}, 6000)
		},
		onHide() {
			clearInterval(this.timingGet)
		},
		onLoad() {
			uni.getSystemInfo({
				success: e => {
					this.screenWidth = e.screenWidth
					// #ifndef H5
					this.statusBar = e.statusBarHeight
					// #endif
				}
			})
			this.handleGetTodoData()
		},
		onPullDownRefresh() {
			this.pageIndex = 1
			this.handleGetTodoData()
			setTimeout(function() {
				uni.stopPullDownRefresh();
			}, 1000);
		},
		methods: {
			handleTab(t) {
				this.currentTab = t
				this.affairType = t
				this.pageIndex = 1
			},
			handleGetTodoData(isAdd = false) {
				const orgId = uni.getStorageSync('orgId')
				const json = initFilter(orgId, "", "", '', this.pageIndex)
				json.Parameter1 = this.affairType; // 1.待办事项 2.已办事项 3.今日新增 4.待办超时
				getTodoData(json).then(res => {
					this.total = res.TotalCount
					this.noticeTotal = res.todayNewCount
					this.todoTotal = res.todoCount
					const newRes = (res.newData || []).map(i => {
						const obj = {
							...i,
							title: i.NOTICE_TITLE,
							startTime: i.TASK_STARTDT,
							endTime: i.TASK_ENDDT,
							// status: i.NOTICE_STATUS,
							// statusText: i.NOTICE_STATUS
						}
						if (this.affairType === '1') {
							obj.status = i.OVER_TIME === '正常' ? 0 : 1
							obj.statusText = i.OVER_TIME
						}
						if (this.affairType === '3') {
							obj.status = i.NOTICE_STATUS
							obj.statusText = i.NOTICE_STATUS === 0 ? '未读' : '已办'
						}
						return obj
					})
					if (isAdd)
						this.lists = this.lists.concat(newRes)
					else
						this.lists = newRes
				})
			},
			// 动态设置tabBar
			// onSetTabBar() {
			//   uni.setTabBarItem({
			//     index: 2,
			//     text: 'text',
			//     pagePath: 'pages/my/index',
			//     iconPath: 'static/tabbar_user@2x.png',
			//     selectedIconPath: 'static/tabbar_user_activited@2x.png'
			//   })
			// }

			taskClisk(item) {
				if (this.affairType === '3') {
					const modalOption = {
						title: '消息',
						content: item.NOTICE_TITLE,
						success: e => {
							const {
								confirm,
								cancel
							} = e
							if (confirm) {
								if (item.NOTICE_STATUS === 0) {
									// 标记为已读
									const orgId = uni.getStorageSync('orgId')
									const json = initFilter(orgId, item.ID)
									handleTaskStatus(json).then(res => {
										if (res) {
											uni.showToast({
												title: '标记为已读'
											})
											this.pageIndex = 1
											this.handleGetTodoData()
										}
									})
								} else {
									// null
								}
							}
							if (cancel) {
								// cancel
							}
						}
					}
					if (item.NOTICE_STATUS === 0) {
						modalOption.cancelText = '确认'
						modalOption.confirmText = '标记已读'
					} else {
						modalOption.showCancel = false
					}
					uni.showModal(modalOption)
					return
				}
				let linkToUrl = ''
				if (item.SOURCE_FORMCODE == 'PF119') {
					const orgId = uni.getStorageSync('orgId')
					const json = initFilter(orgId)
					extendRule(json, 'ID', 1, item.SOURCE_DATA_ID);
					extendInclude(json, "Nav_ApproveDetails")
					extendInclude(json, "Nav_ApproveDetails.Nav_ApproveUser")
					FullGet(json).then(res => {
						let okApi = res.CALLBACK_INTERFACE
						// api 请求 获取  APPROVE_CODE ID
						switch (res.APPROVE_CODE) {
							case 'BS006_SHOWPRINT': //隐患上报审批  XLK100635
								linkToUrl = '/pages/apply/subPages/BS/riskSumbitAudit'
								break;
							case 'BS032_SHOWPRINT': //制表审批
								linkToUrl = '/pages/apply/subPages/BS/checkPlanNotice'
								break;
							case 'BS034_SHOWPRINT': //检查确认
								linkToUrl = '/pages/apply/subPages/BS/safeCheckAudit' //待做
								break;
							case 'BS044_SHOWPRINT': //整改记录审批
								linkToUrl = '/pages/apply/subPages/BS/riskSumbitDealAudit'
								break;
							case 'BS046_SHOWPRINT': //延期申请审批
								linkToUrl = '/pages/apply/subPages/BS/delayApplyView'
								break;




							case 'HM061_SHOWPRINT': //危险源和风险辨识任务
								linkToUrl = '/pages/apply/subPages/HM/1riskTaskShow'
								break;
							case 'HM063_SHOWPRINT': //危险源和风险辨识任务
								linkToUrl = '/pages/apply/subPages/HM/2riskRecordShow'
								break;
							case 'HM101_SHOWPRINT': //职业危害辨识记录签到
								linkToUrl = '/pages/apply/subPages/HM/3hazardTaskShow'
								break;
							case 'HM099_SHOWPRINT': //职业危害辨识记录签到
								linkToUrl = '/pages/apply/subPages/HM/3hazardRecordShow'
								break;

							case 'FO015_SHOWPRINT': //一般作业活动记录审批
								linkToUrl = '/pages/apply/subPages/FO2/jobEventShow'
								break;
							case 'FO017_SHOWPRINT': //工作票审批
								linkToUrl = '/pages/apply/subPages/FO2/jobShow'
								break;
							case 'HM104_SHOWPRINT': //作业任务识别审批
								linkToUrl = '../apply/subPages/HM2/jobTaskIdentityShow'
								break;
							case 'HM087_SHOWPRINT': //作业任务分析审批
								linkToUrl = '../apply/subPages/HM2/jobTaskAnalyzeShow'
								break;
							case 'HM107_SHOWPRINT': //许可作业审批
								linkToUrl = '../apply/subPages/HM2/jobTaskLicenseShow'
								break;
							case 'HM109_SHOWPRINT': //关键作业审批
								linkToUrl = '../apply/subPages/HM2/jobTaskCrucialShow'
								break;
							case 'HM111_SHOWPRINT': //一般作业审批
								linkToUrl = '../apply/subPages/HM2/jobTaskOrdinaryShow'
								break;
							case 'PT020_SHOWPRINT': //安全生产目标与指标
								linkToUrl = '../apply/subPages/SCPT/formulationDepartmentShow'
								break;
							case 'PT024_SHOWPRINT': //考核方案审核
								linkToUrl = '../apply/subPages/SCPT/PlanAuditShow'
								break;
							case 'PT029_SHOWPRINT': // 监测与考核记录
								linkToUrl = '../apply/subPages/SCMT/monitorAssessmentShow'
								break;
						}
						console.log(linkToUrl)
						//特殊处理
						if (okApi === 'BS/BSSafeCheck/CheckAuditEnd' || okApi === 'BS/BSRiskSubmit/Notice' ||
							okApi === 'BS/BSRiskSubmit/DealEnd' || okApi ===
							'BS/BSRiskSubmitDelayApply/DealEnd' || okApi === 'FO/FOJobEventRecord/BackUpdate'|| 
							okApi === 'FO/FOCrucialLicenseJob/BackUpdate') {
							okApi = res.CALLBACK_INTERFACE + 'New'
						} else {
							okApi = 'PF/Approve/Submit'
						}
						if (linkToUrl != '') {
							linkToUrl += "?tableKey=1&ID=" + res.DATA_ID + "&ApproveID=" + item
								.SOURCE_DATA_ID + "&taskID=" +
								item.ID + '&isAudit=true&okApi=' + okApi + '&refuseApi=' + res
								.REJECT_INTERFACE
							this.$store.commit('updateAuditModel', res)
							uni.navigateTo({
								url: linkToUrl
							})
						} else {
							uni.$showMsg('功能待完善!', 'error')
						}
					})
				} else {
					switch (item.SOURCE_FORMCODE) {
						case 'BS032_SHOWPRINT': //检查通知 确认
							linkToUrl = '../apply/subPages/BS/checkPlanNotice'
							break;
						case 'BS057': //检查登记
							linkToUrl = '../apply/subPages/BS/safeCheckSign'
							break;
						case 'BS034': //检查确认
							linkToUrl = '../apply/subPages/BS/safeCheckCheck'
							break;
						case 'BS006': //检查 隐患上报
							linkToUrl = '../apply/subPages/BS/riskSumbit'
							break;
						case 'BS061': //检查 手动隐患上报指定整改部门
							linkToUrl = '../apply/subPages/BS/riskSumbitUserDep'
							break;
						case 'BS062': //检查 隐患上报（驳回重新上报）
							linkToUrl = '../apply/subPages/BS/riskSumbitBack'
							break;
						case 'BS042': //隐患整改通知
							linkToUrl = '../apply/subPages/BS/riskSumbitNotice'
							break;
						case 'BS042_SHOWPRINT': //隐患整改通知： 确认
							linkToUrl = '../apply/subPages/BS/riskSumbitNoticeCheck'
							break;
						case 'BS049': //指定整改落实人
							linkToUrl = '../apply/subPages/BS/riskSumbitActualDeal'
							break;
						case 'BS042_ACTUALCHECK': //整改落实人 确认
							linkToUrl = '../apply/subPages/BS/riskSumbitActualDealCheck'
							break;
						case 'BS044': //隐患整改单
							linkToUrl = '../apply/subPages/BS/riskSumbitDeal'
							break;
						case 'BS044_CHECK': //整改验收 责任人
							linkToUrl = '../apply/subPages/BS/riskSumbitDealCheck'
							break;
						case 'BS046': //隐患整改单
							linkToUrl = '../apply/subPages/BS/delayApply'
							break;
						case 'BS046_SHOWPRINT': //延期申请审批
							linkToUrl = '/pages/apply/subPages/BS/delayApplyView'
							break;
						case 'BS069': //检查记录确认退回
							linkToUrl = '/pages/apply/subPages/BS/riskSumbitDetailBack'
							break;

						case 'FO003_SHOWPRINT': //班前会议签到
							linkToUrl = '../apply/subPages/FO/1preShiftMeetingShow'
							break;
						case 'FO003': //班前会议编辑
							linkToUrl = '../apply/subPages/FO/1preShiftMeetingEdit'
							break;
						case 'FO005_SHOWPRINT': //岗位当班签到
							linkToUrl = '../apply/subPages/FO/2currentClassRecordShow'
							break;
						case 'FO005': //岗位当班编辑
							linkToUrl = '../apply/subPages/FO/2currentClassRecordEdit'
							break;
						case 'FO013_SHOWPRINT': //岗位交接班签到
							linkToUrl = '../apply/subPages/FO/3changeShiftRecordShow'
							break;
						case 'FO012': //岗位交接班编辑
							linkToUrl = '../apply/subPages/FO/3changeShiftRecordEdit'
							break;
						case 'FO010_SHOWPRINT': //班组安全活动签到
							linkToUrl = '../apply/subPages/FO/4teamActivityShow'
							break;
						case 'FO010': //班组安全活动编辑
							linkToUrl = '../apply/subPages/FO/4teamActivityEdit'
							break;

						case 'HM061_SHOWPRINT': //危险辨识任务签到
							linkToUrl = '../apply/subPages/HM/1riskTaskShow'
							break;
						case 'HM062': //危险辨识任务编辑
							linkToUrl = '../apply/subPages/HM/1riskTaskEdit'
							break;
						case 'HM064': //危险辨识记录编辑
							linkToUrl = '../apply/subPages/HM/2riskRecordEdit'
							break;
						case 'HM063_SHOWPRINT': //危险辨识记录签到
							linkToUrl = '../apply/subPages/HM/2riskRecordShow'
							break;
						case 'HM101_SHOWPRINT': //职业危害辨识记录签到
							linkToUrl = '../apply/subPages/HM/3hazardTaskShow'
							break;
						case 'HM100': //职业危害辨识记录签到
							linkToUrl = '../apply/subPages/HM/3hazardRecordEdit'
							break;
						case 'HM099_SHOWPRINT': //职业危害辨识记录签到
							linkToUrl = '../apply/subPages/HM/3hazardRecordShow'
							break;
						case 'SE013_SHOWPRINT': //培训通知
							linkToUrl = '../apply/subPages/SE/1trainNotifyShow'
							break;

						case 'FO015': //一般作业活动记录编辑
							linkToUrl = '../apply/subPages/FO2/jobEventEdit1'
							break;
						case 'FO015_SHOWPRINT': //一般作业活动记录签到
							linkToUrl = '../apply/subPages/FO2/jobEventShow'
							break;
						case 'FO017': //关键许可工作票编辑
							linkToUrl = '../apply/subPages/FO2/jobEdit'
							break;
						case 'FO017_SHOWPRINT': //关键许可工作票签到
							linkToUrl = '../apply/subPages/FO2/jobShow'
							break;
						case 'FO019': //技术交底表编辑
							linkToUrl = '../apply/subPages/FO2/techEdit'
							break;
						case 'FO019_SHOWPRINT': //技术交底表签到
							linkToUrl = '../apply/subPages/FO2/techShow'
							break;
						case 'FO008_SHOWPRINT': //作业方案讨论记录签到
							linkToUrl = '../apply/subPages/FO2/preOperSchShow'
							break;
						case 'FO021': //关键作业活动记录编辑
							linkToUrl = '../apply/subPages/FO2/jobActivityEdit'
							break;
						case 'FO021_SHOWPRINT': //关键作业活动记录签到
							linkToUrl = '../apply/subPages/FO2/jobActivityShow'
							break;
						case 'HM105': //作业任务识别编辑
							linkToUrl = '../apply/subPages/HM2/jobTaskIdentityEdit'
							break;
						case 'HM104_SHOWPRINT': //作业任务识别签到
							linkToUrl = '../apply/subPages/HM2/jobTaskIdentityShow'
							break;
						case 'HM088': //作业任务分析编辑
							linkToUrl = '../apply/subPages/HM2/jobTaskAnalyzeEdit'
							break;
						case 'HM113': //作业任务分析编辑
							linkToUrl = '../apply/subPages/HM2/jobTaskAnalyzeEdit'
							break;
						case 'HM087_SHOWPRINT': //作业任务分析签到
							linkToUrl = '../apply/subPages/HM2/jobTaskAnalyzeShow'
							break;
						case 'HM108': //许可作业编辑
							linkToUrl = '../apply/subPages/HM2/jobTaskLicenseEdit'
							break;
						case 'HM119': //许可作业编辑
							linkToUrl = '../apply/subPages/HM2/jobTaskLicenseEdit'
							break;
						case 'HM107_SHOWPRINT': //许可作业确认
							linkToUrl = '../apply/subPages/HM2/jobTaskLicenseShow'
							break;
						case 'HM109': //关键作业编辑
							linkToUrl = '../apply/subPages/HM2/jobTaskCrucialEdit'
							break;
						case 'HM115': //关键作业编辑
							linkToUrl = '../apply/subPages/HM2/jobTaskCrucialEdit'
							break;
						case 'HM109_SHOWPRINT': //关键作业确认
							linkToUrl = '../apply/subPages/HM2/jobTaskCrucialShow'
							break;
						case 'HM112': //一般作业编辑
							linkToUrl = '../apply/subPages/HM2/jobTaskOrdinaryEdit'
							break;
						case 'HM117': //一般作业编辑
							linkToUrl = '../apply/subPages/HM2/jobTaskOrdinaryEdit'
							break;
						case 'HM111_SHOWPRINT': //一般作业确认
							linkToUrl = '../apply/subPages/HM2/jobTaskOrdinaryShow'
							break;
						case 'FM202_SHOWPRINT': // 随手拍确认
							linkToUrl = '../apply/subPages/FM/snapshotConfirm'
							break;
						case 'HM124': //文件发布表
							linkToUrl = '../apply/subPages/HM2/filePublishEdit'
							break;
						case 'HM123_SHOWPRINT': //文件发布表
							linkToUrl = '../apply/subPages/HM2/filePublishShow'
							break;
						case 'PT014_SHOWPRINT': //安全生产方针征集表
							linkToUrl = '../apply/subPages/SCPT/policyCollectShow'
							break;
						case 'PT020': //安全生产目标与指标
							linkToUrl = '../apply/subPages/SCPT/formulationDepartmentEdit'
							break;
						case 'PT020_SHOWPRINT': //安全生产目标与指标
							linkToUrl = '../apply/subPages/SCPT/formulationDepartmentShow'
							break;
						case 'PT024': //考核方案审核
							linkToUrl = '../apply/subPages/SCPT/PlanAuditEdit'
							break;
						case 'PT024_SHOWPRINT': //考核方案审核
							linkToUrl = '../apply/subPages/SCPT/PlanAuditShow'
							break;
						case 'PT029': //监测与考核记录
							linkToUrl = '../apply/subPages/SCPT/monitorAssessmentEdit'
							break;
						case 'PT029_SHOWPRINT': //监测与考核记录
							linkToUrl = '../apply/subPages/SCPT/monitorAssessmentShow'
							break;
					}

					if (linkToUrl != '') {
						uni.navigateTo({
							url: linkToUrl += "?tableKey=1&ID=" + item.SOURCE_DATA_ID + "&taskID=" + item.ID
						})
					} else {
						uni.$showMsg('功能待完善!', 'error')
					}
				}
			},
		},
		watch: {
			affairType() {
				this.handleGetTodoData()
			}
		},
		onReachBottom() {
			if (this.total >= this.pageIndex * 10) {
				this.pageIndex++
				this.handleGetTodoData(true)
			}
		}
	}
</script>

<style>
	.todo-page {
		position: relative;
	}

	.todo-page .status-bar {
		height: 120px;
		background-image: linear-gradient(to right, #CFECFF, #6EB6FF);
		position: absolute;
		top: 0;
		left: 0;
		z-index: -10;
	}

	.todo-page .main {
		padding: 62px 8px;
		position: relative;
		z-index: 10;
		background-size: 100% 120px;
		background-repeat: no-repeat;
	}

	.todo-page .swiper-wrap {
		margin-bottom: 16px;
	}

	.tabs-bar {
		display: flex;
		flex-direction: row;
	}

	.tabs-bar .tab {
		height: 40px;
		display: flex;
		align-items: center;
		position: relative;
		margin-right: 6px;
		padding-bottom: 6px;
	}

	.tabs-bar .tab .t {
		width: 55px;
		height: 50px;
		position: relative;
		/* overflow: inherit; */
	}

	.tabs-bar .tab .t .name {
		padding-top: 7px;
		padding-left: 8px;
		font-size: 18px;
		line-height: 22px;
		color: #999;
	}

	.tabs-bar .tab .bottom-widget {
		width: 20px;
		height: 3px;
		background-color: #004F9D;
		position: absolute;
		bottom: 4px;
		left: 15px;
	}

	.empty-wrap {
		margin-top: 60px;
	}

	.content-list {
		padding: 10px 0;
	}
</style>