<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(0)" class="tab">
					<view class="t">
						<text class="name" :style="{color: currentTab === 0 ? '#004F9D' : ''}">任务</text>
						<u-badge type="error" absolute max="99" :value="0" :offset="[-6, -10]"></u-badge>
					</view>
					<view v-if="currentTab === 0" class="bottom-widget"></view>
				</view>
				<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="88" :offset="[-6, -10]"></u-badge>
					</view>
					<view v-if="currentTab === 1" 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 :key="key" :dataSource="item" v-for="(item, key) in lists" icon @click="taskClisk">
				</common-card>
			</view>

		</view>
	</view>
</template>

<script>
	import {
		getOrgList,
		getTodoData,
		FullGet
	} from '../../services/app'
	import {
		initFilter,
		extendRule,
		extendInclude
	} from '../../utils/common'
	export default {
		data() {
			return {
				currentTab: 0,
				lists: [],
				banners: [
					'https://shaxian.0598qq.com/assets/img_cate/img_zhuanti@2x.png',
					'https://shaxian.0598qq.com/assets/img_cate/img_zhuanti@2x.png',
					'https://shaxian.0598qq.com/assets/img_cate/img_zhuanti@2x.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
			// debugger
			// 判断刷新
			this.handleGetTodoData()
		},
		onLoad() {
			uni.getSystemInfo({
				success: e => {
					this.screenWidth = e.screenWidth
					// #ifndef H5
					this.statusBar = e.statusBarHeight
					// #endif
				}
			})
			// this.handleGetTodoData()
			// 设置tab红点
			// uni.showTabBarRedDot({
			//   index: 0
			// })

			// getOrgList({
			//   Sort: 'CODE',
			//   Order: 0,
			// Parameter10: window.location.host,
			//   Parameter11: '0'
			// }).then(res => {
			//   console.log('org list:', res)
			// })
		},
		methods: {
			handleTab(t) {
				this.currentTab = t
			},
			handleGetTodoData() {
				const orgId = uni.getStorageSync('orgId')
				const json = initFilter(orgId, "", "", '', this.pageIndex)
				// TODO: 这个参数待定
				json.Parameter1 = 1; // 1.待办事项 2.已办事项 3.今日新增 4.待办超时
				getTodoData(json).then(res => {
					// this.lists = (res.newData || []).map(i => {
					// 	return {
					// 		...i,
					// 		title: i.NOTICE_TITLE,
					// 		startTime: i.TASK_STARTDT,
					// 		endTime: i.TASK_ENDDT,
					// 		status: i.NOTICE_STATUS,
					// 	}
					// })
					this.total = res.TotalCount
					const newRes = (res.newData || []).map(i => {
						return {
							...i,
							title: i.NOTICE_TITLE,
							startTime: i.TASK_STARTDT,
							endTime: i.TASK_ENDDT,
							status: i.NOTICE_STATUS,
							textStatus: i.OVER_TIME
						}
					})
					this.lists = this.lists.concat(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) {
				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 'BS032_SHOWPRINT': //制表审批
								linkToUrl = '/pages/apply/subPages/BS/checkPlanNotice'
								break;
							case 'BS034_SHOWPRINT': //检查确认
								linkToUrl = '/pages/apply/subPages/BS/safeCheckCheck'
								break;
						}

						//特殊处理
						if (okApi === 'BS/BSSafeCheck/CheckAuditEnd' || okApi === 'BS/BSRiskSubmit/Notice' ||
							okApi === 'BS/BSRiskSubmit/DealEnd' || okApi ===
							'BS/BSRiskSubmitDelayApply/DealEnd' || okApi === 'FO/FOJobEventRecord/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 'FO003_SHOWPRINT': //检查确认
							linkToUrl = '../apply/subPages/FO/2preShiftMeetingShow'
							break;
						case 'BS006': //检查 隐患上报
							linkToUrl = '../apply/subPages/BS/riskSumbit'
							break;
						case 'FO003_SHOWPRINT': //班前会议签到
							linkToUrl = '../apply/subPages/1preShiftMeetingShow'
							break;
						case 'FO003': //班前会议编辑
							linkToUrl = '../apply/subPages/1preShiftMeetingEdit'
							break;
						case 'FO005_SHOWPRINT': //岗位当班签到
							linkToUrl = '../apply/subPages/2currentShiftClassShow'
							break;
						case 'FO005': //岗位当班编辑
							linkToUrl = '../apply/subPages/2currentShiftClassEdit'
							break;
						case 'FO013_SHOWPRINT': //岗位交接班签到
							linkToUrl = '../apply/subPages/3changeShiftRecordShow'
							break;
						case 'FO012': //岗位交接班编辑
							linkToUrl = '../apply/subPages/3changeShiftRecordEdit'
							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')
					}
				}


				// else {  //对于App端不开发的页面 提示
				// 	uni.$showMsg('请移至电脑端操作!', 'error')
				// }
			},
		},

		onReachBottom() {
			if (this.total >= this.pageIndex * 10) {
				this.pageIndex++
				this.handleGetTodoData()
			}
		}
	}
</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 {
		display: flex;
		align-items: center;
		position: relative;
		margin-right: 20px;
		padding-bottom: 6px;
	}

	.tabs-bar .tab .t {
		position: relative
	}

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

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

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

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