<template>
	<el-card class="box-card">
		<template #header>
			<div class="card-header">
				<span>我的待办
					<el-badge class="mark" v-if="backlogCount !== 0" :value="backlogCount" :max="99"
						style="line-height: 0;margin-top: 8px" />
				</span>
				<div style="float:right;">
					<el-button type="text" @click="refreshList()">刷新</el-button>
					<el-button type="text" @click="toPendingList()">更多>></el-button>
				</div>
			</div>
		</template>
		<div style="height: 250px;margin-top: 5px">
			<vxe-table border="inner" :loading="loading" size="small" ref="todoTable" show-header-overflow show-overflow
				highlight-hover-row :menu-config="{}" :print-config="{}" :import-config="{}" :export-config="{}"
				:data="dataList" header-align="center" :checkbox-config="{}">
				<vxe-column title="实例标题" field="vars.title">
				</vxe-column>
				<vxe-column title="流程名称" field="processDefinitionName"></vxe-column>
				<vxe-column title="当前环节" field="task.name">
					<template #default="scope">
						<el-tag>{{ scope.row.task.name }}</el-tag>
					</template>
				</vxe-column>
				<vxe-column title="流程发起人" field="vars.userName"></vxe-column>
				<vxe-column field="task.createTime" title="发起时间">
				</vxe-column>
				<vxe-column title="操作" width="100px" fixed="right" align="center">
					<template #default="scope">
						<div>
							<el-button text type="primary" @click="todo(scope.row)">办理</el-button>
							<el-button text type="primary" @click="trace(scope.row)">进度</el-button>
						</div>
					</template>
				</vxe-column>
			</vxe-table>
			<vxe-pager background style="margin-top: 5px" size="small" :current-page="tablePage.currentPage"
				:page-size="tablePage.pageSize" :total="tablePage.total"
				:page-sizes="[10, 20, 100, 1000, { label: '全量数据', value: 1000000 }]"
				:layouts="['PrevPage', 'JumpNumber', 'NextPage', 'FullJump', 'Sizes', 'Total']"
				@page-change="currentChangeHandle">
			</vxe-pager>
		</div>
	</el-card>
	<v-dialog title="查看进度" :close-on-click-modal="true" append-to-body v-model="visible" width="70%" height="600px">
		<flow-chart ref="preview" :processInstanceId="processInstanceId"></flow-chart>
	</v-dialog>

	<UpdatePassword ref="updatePassword" :innerVisible="innerVisible" v-on:innerDialog="getInnerStatus($event)">
	</UpdatePassword>
</template>

<script>
// import FlowChart from '../modeler/FlowChart'
import pick from "lodash.pick";
import UserSelect from "@/components/userSelect/UserSelectDialog";
import taskService from "@/api/flowable/taskService";
import formService from "@/api/flowable/formService";
import userService from "@/api/sys/UserService";
import UpdatePassword from "@/views/layout/UpdatePassword2.vue";
export default {
	// title: "我的待办",
	// icon: "wodedaiban",
	// description: "我的待办任务",
	// layout: {
	// 	w: 6,
	// 	h: 15,
	// },
	data() {
		return {
			dataList: [],
			tablePage: {
				total: 0,
				currentPage: 1,
				pageSize: 10,
				orders: [],
			},
			loading: false,
			visible: false,
			currentTask: null,
			processInstanceId: "",
			timer: null,
		};
	},
	computed: {
		backlogCount() {
			return this.$store.state.global.backlogCount
		},
	},
	watch: {
		$route(to, from) {
			if (to.fullPath == "/home") {
				this.refreshList()
			}
		}
	},
	mounted() {
		// 页面加载后
		// 1.在执行定时器前先执行一次获取接口数据的操作函数, 否则接口会1秒钟后才调用
		// 2.为了避免退出当前页面后,在其他页面也继续调用接口,退出前需要清除定时器.
		this.refreshList();
		if (this.$store.state.user.timer2 == null) {
			this.$store.state.user.timer2 = setInterval(() => {
				this.refreshList()
			}, 60000)
		}
		// 判断当前用户是否为管理员
		userService.is().then((data) => {
			if (!data) {
				// 如果当前用户不是管理员，则判断是否修改过密码
				userService.isUpdatePassword().then((data) => {
					if (!data) {
						// 如果没有修改过密码，则弹出密码修改窗口
						this.innerVisible = true
						this.$refs.updatePassword.init(!data)
						//
					}
				})
			}
		})

	},
	beforeDestroy() {
		if (this.$store.state.user.timer2) {
			clearInterval(this.$store.state.user.timer2); // 如果是 setInterval
			this.$store.state.user.timer2 = null;
		}
	},
	components: {
		UserSelect,
		UpdatePassword
		// FlowChart
	},
	methods: {
		getInnerStatus(status) {
			this.innerVisible = status
		},
		// 获取数据列表
		refreshList() {
			this.loading = true;
			taskService
				.todoList({
					current: this.tablePage.currentPage,
					size: this.tablePage.pageSize,
					orders: this.tablePage.orders,
					...this.searchForm,
				})
				.then((data) => {
					this.dataList = data.records;
					this.tablePage.total = data.total;
					this.loading = false;
					this.$store.state.global.backlogCount = data.total
				});
		},
		// 当前页
		currentChangeHandle({ currentPage, pageSize }) {
			this.tablePage.currentPage = currentPage;
			this.tablePage.pageSize = pageSize;
			this.refreshList();
		},
		todo(row) {
			taskService
				.getTaskDef({
					taskId: row.task.id,
					taskName: row.task.name,
					taskDefKey: row.task.taskDefinitionKey,
					procInsId: row.task.processInstanceId,
					procDefId: row.task.processDefinitionId,
					procDefKey: row.task.processDefKey,
					status: row.status,
				})
				.then(async (data) => {
					// 如果是需要处理的流程
					if (this.isReimbursementProcess(data.procDefKey)) {
						// 根据流程名称获取对应的表单地址
						let urlObj = this.isReimbursementUrl(row.processDefinitionName)
						if (urlObj) {
							let approvalTime = null
							// 判断是否为文件补充流程和报销流程
							if (urlObj.type && urlObj.type === 'file') {
								approvalTime = await taskService.getCreateTimeByProcInsId(row.task.processInstanceId)
							} else {
								approvalTime = await taskService.getApprovalTimeByProcInsId(row.task.processInstanceId)

							}
							if (approvalTime) {
								// 根据发起审批的时间判断是否为新版本报销页面
								let newStartDate = this.$dictUtils.getDictLabel("new_version_reimbursement_date", '1', '-')
								const compareResult = this.validateXG.compareDate(approvalTime, newStartDate);
								const newVersionFlag = compareResult == 'same' || compareResult == 'after';
								if (newVersionFlag) {
									data.formUrl = urlObj.url
								}
							}
						}
					}
					// 读取流程表单
					var cs = "cs2"
					this.$router.push({
						path: "/flowable/task/TaskForm",
						query: {
							wareHouseId: row.processDefinitionName === '物资管理-入库修改' ? data.businessId : '',
							cwArchiveId: row.processDefinitionName === '会计-项目报告归档' ? data.businessId : '',
							returnId: row.processDefinitionName === '物资管理-领用退回申请' ? data.businessId : '',
							formTitle: `${row.vars.title}`,
							title: `审批【${row.task.name || ""}】`,
							cs: cs,
							...pick(
								data,
								"formType",
								"formReadOnly",
								"formUrl",
								"procDefKey",
								"taskDefKey",
								"procInsId",
								"procDefId",
								"taskId",
								"status",
								"title",
								"businessId"
							),
						},
					});
				});
		},
		// 判断是否为报销流程
		// Process_1669271524486 会计-报销审批
		// Process_1669271524487 会计-报销审批-电子发票
		// Process_1704851888818 会计-所长报销1
		// Process_1705370333755 会计-所长报销-电子发票1
		// Process_1705389994103 中审-所长报销1
		// Process_1705390592348 中审-所长报销-电子发票1
		// Process_1673924346231 中审-报销审批1
		// Process_1673924346232 中审-报销审批-电子发票1
		// Process_1705391439287 评估-所长报销1
		// Process_1705391725816 评估-所长报销-电子发票1
		// Process_1665383385070 评估-报销申请1
		// Process_1665383385071 评估-报销申请-电子发票1
		// Process_1705454007925 项目-报销审批1
		// Process_1705454863808 项目-报销审批-电子发票1
		// Process_1706170102059 会计-总经办报销1
		// Process_1706175399469 评估-总经办报销1
		// Process_1706176397312 中审-总经办报销1
		// Process_1706176397323 中审-苏州分公司-智永祥部门1
		// Process_1706176937876 项目-总经办报销1
		// Process_1706671221048 咨询-报销审批1
		// Process_1706679472488 咨询-报销审批-电子发票1
		// Process_1706680073642 咨询-总经办报销1
		// Process_1706680331785 咨询-所长报销1
		// Process_1706680660857 咨询-所长报销-电子发票1
		// Process_1709102298565 盛所会计-报销申请1
		// Process_1709104787957 盛所项目-报销申请1
		// Process_1711418224962 物资-项目报销1
		// Process_1713769833657 苏州-业务操作报销
		isReimbursementProcess(procDefKey) {
			const reimbursementProcesses = [
				"Process_1669271524486",
				"Process_1669271524487",
				"Process_1704851888818",
				"Process_1705370333755",
				"Process_1705389994103",
				"Process_1705390592348",
				"Process_1673924346231",
				"Process_1673924346232",
				"Process_1705391439287",
				"Process_1705391725816",
				"Process_1665383385070",
				"Process_1665383385071",
				"Process_1705454007925",
				"Process_1705454863808",
				"Process_1706170102059",
				"Process_1706175399469",
				"Process_1706176397312",
				"Process_1706176397323",
				"Process_1706176937876",
				"Process_1706671221048",
				"Process_1706679472488",
				"Process_1706680073642",
				"Process_1706680331785",
				"Process_1706680660857",
				"Process_1709102298565",
				"Process_1709104787957",
				"Process_1711418224962",
				"Process_1722305645394",
				"Process_1722412442950",
				"Process_1722416114655",
				"Process_1722404901733",
				"Process_1722389642543",
				"Process_1713769833657"
			];
			return reimbursementProcesses.includes(procDefKey);
		},
		isReimbursementUrl(name) {
			// 会计-文件补充
			let cwFile = ["会计-报销文件补充"]
			//项目-报销文件补充
			let ccpmFile = ["项目-报销文件补充"]
			//咨询-报销文件补充
			let zxFile = ["咨询-报销文件补充"]
			//中审-文件补充
			let zsFile = ["中审-报销文件补充"]
			//评估-文件补充
			let pgFile = ["评估-报销文件补充"]
			// 会计
			let cw = ["会计-所长报销", "会计-所长报销-电子发票", "会计-总经办报销", "盛所会计-报销申请", "会计-报销审批", "会计-报销审批-电子发票"]
			// 中审
			let zs = ["中审-所长报销", "中审-所长报销-电子发票", "中审-总经办报销", "中审-报销审批", "中审-报销审批-电子发票"]
			// 评估
			let pg = ["评估-总经办报销", "评估-所长报销", "评估-所长报销-电子发票", "评估-报销申请", "评估-报销审批-电子发票"]
			// 项目
			let ccpm = ["物资-项目报销", "项目-总经办报销", "项目-报销审批", "项目-报销审批-电子发票", "盛所项目-报销申请",]
			// 咨询
			let zx = ["咨询-总经办报销", "咨询-所长报销", "咨询-所长报销-电子发票", "咨询-报销审批", "咨询-报销审批-电子发票"]
			// 苏州
			let sz = ["苏州-业务操作报销"]
			if (sz.includes(name)) {
				return {
					url: '/cw/szfbReimbursementApproval/info/NewReimbursementForm',
					type: 'reimbursement'
				}
			}

			if (cw.includes(name)) {
				return {
					url: '/cw/reimbursementApproval/info/NewReimbursementForm',
					type: 'reimbursement'
				}
			}
			if (zs.includes(name)) {
				return {
					url: '/zs/reimbursement/info/NewReimbursementForm',
					type: 'reimbursement'
				}
			}
			if (pg.includes(name)) {
				return {
					url: '/reimbursement/info/NewReimbursementForm',
					type: 'reimbursement'
				}
			}
			if (ccpm.includes(name)) {
				return {
					url: '/ccpm/reimbursement/info/NewReimbursementForm',
					type: 'reimbursement'
				}
			}
			if (zx.includes(name)) {
				return {
					url: '/consultancy/reimbursement/info/NewReimbursementForm',
					type: 'reimbursement'
				}
			}
			if (cwFile.includes(name)) {
				return {
					url: "/cw/reimbursementApproval/info/NewReimbursementFileSupplementForm",
					type: 'file'
				}
			}
			if (ccpmFile.includes(name)) {
				return {
					url: "/ccpm/reimbursement/info/NewReimbursementFileSupplementForm",
					type: 'file'
				}
			}
			if (zxFile.includes(name)) {
				return {
					url: "/consultancy/reimbursement/info/NewReimbursementFileSupplementForm",
					type: 'file'
				}
			}
			if (zsFile.includes(name)) {
				return {
					url: "/zs/reimbursement/info/NewReimbursementFileSupplementForm",
					type: 'file'
				}
			}
			if (pgFile.includes(name)) {
				return {
					url: "/reimbursement/info/NewReimbursementFileSupplementForm",
					type: 'file'
				}
			}
			return null;
		},
		toPendingList() {
			// this.$router.push('./PendingList')
			this.$router.push({
				path: '/sys/dashboard/workBench/PendingList',
				query: {
					num: 3
				}
			})
		},
		trace(row) {
			this.processInstanceId = row.task.processInstanceId;
			this.visible = true;
			this.$nextTick(() => {
				this.$refs.preview.init();
			});
		},
	},
};
</script>
<style scoped>
.box-card {
	height: 100%;
}
</style>
