<template>
	<div>
		<el-dialog
			:title="title"
			:close-on-click-modal="false"
			dialogDrag
			width="300px"
			append-to-body
			align-center="true"
			height="auto"
			v-loading="loading"
			@close="close"
			@keyup.enter.native="getWorkClient"
			v-model="visibleChoose">
			<el-icon style="color: #E6A23C;size:20px"><Warning /></el-icon>是否进行批量审核
			<template #footer>
				<div class="dialog-footer">
				<el-button v-loading="loading" size="primary"  @click="todo('1')"
						   v-noMoreClick>同意
				</el-button>
				<el-button v-loading="loading"  type="paimary"  @click="todo('0')"
						   v-noMoreClick>驳回
				</el-button>
				</div>
			</template>
		</el-dialog>
		<TaskForm ref="taskForm"></TaskForm>
	</div>
</template>

<script>
	import ZsReimbursementService from '@/api/zs/reimbursement/ReimbursementService'
	import CwReimbursementApprovalService from '@/api/cw/reimbursementApproval/ReimbursementApprovalService'
	import ZxReimbursementService from '@/api/consultancy/reimbursement/ReimbursementService'
	import XmReimbursementService from '@/api/ccpm/reimbursement/ReimbursementService'
	import CommonApi from '@/api/cw/common/CommonApi'
	import taskService from "@/api/flowable/taskService";
	import PgReimbursementService from '@/api/sys/ReimbursementService'
	// import pick from "lodash.pick";

	export default {
		data() {
			return {
				title: '',
				method: '',
				visibleChoose: false,
				loading: false,
				tablePage: {
					total: 0,
					currentPage: 1,
					pageSize: 10,
					orders: []
				},
				dataList: [],
				inputForm: {
					type: ''
				},
				procId: '',
				pre: '',
				id: '',
				taskFormData: [],
				auditForm: {
					message: "",
					type: "",
					status: "",
					userIds: null,
					assignee: null,
				},
				bxList:['咨询-所长报销-电子发票','咨询-所长报销','咨询-总经办报销','咨询-报销审批-电子发票','咨询-报销审批','项目-总经办报销','中审-总经办报销',
				'评估-总经办报销','会计-总经办报销','项目-报销审批-电子发票','项目-报销审批','评估-所长报销-电子发票','评估-所长报销','中审-所长报销-电子发票',
				'中审-所长报销','会计-所长报销','会计-所长报销-电子发票','中审-报销审批-电子发票','评估-报销审批-电子发票','会计-报销审批-电子发票','中审-报销审批',
				'评估-报销审批','会计-报销审批']
			}
		},
		ZsReimbursementService: null,
		CwReimbursementApprovalService: null,
		ZxReimbursementService: null,
		XmReimbursementService: null,
		commonApi: null,
		PgReimbursementService:null,
		created() {
			this.zsReimbursementService = new ZsReimbursementService()
			this.cwReimbursementApprovalService = new CwReimbursementApprovalService()
			this.zxReimbursementService = new ZxReimbursementService()
			this.xmReimbursementService = new XmReimbursementService()
			this.pgReimbursementService = new PgReimbursementService()
			this.commonApi = new CommonApi()
		},
		components: {
			//以注册未使用过
			/*      SelectUserTree,
				  InputNumber*/
		},
		methods: {
			init(info) {
				this.inputForm = {
					type: ''
				}
				this.title = '批量审核'
				this.visibleChoose = true
				this.dataList = info
				this.loading = false
			},
			// 表单提交
			async todo(type) {
				this.loading = true
				//同意
				if (type === '1') {
					for (let i = 0; i < this.dataList.length; i++) {
						//根据流程名称获取租户
						let dataInfo = this.dataList[i]
						this.id = ''
						let total=this.dataList.length
						this.procId = dataInfo.task.processInstanceId
						this.inputForm.type = '2'
						await taskService
							.getTaskDef({
								taskId: dataInfo.task.id,
								taskName: dataInfo.task.name,
								taskDefKey: dataInfo.task.taskDefinitionKey,
								procInsId: dataInfo.task.processInstanceId,
								procDefId: dataInfo.task.processDefinitionId,
								procDefKey: dataInfo.task.processDefKey,
								status: dataInfo.status,
							})
							.then(async (data) => {
								this.id = data.businessId
								this.auditForm.status = 'agree'
								data = {...data, agree: true}
								await taskService
									.audit({
										taskId: dataInfo.task.id,
										taskDefKey: dataInfo.task.taskDefinitionKey,
										procInsId: dataInfo.task.processInstanceId,
										procDefId: dataInfo.task.processDefinitionId,
										vars: data,
										comment: this.auditForm,
										assignee: this.auditForm.assignee,
										recordType: ''
									})
									.then(async (data) => {
										console.log('list',this.bxList)
										this.pre = dataInfo.processDefinitionName.substring(0, dataInfo.processDefinitionName.lastIndexOf("-"))
										if (this.commonJS.isNotEmpty(dataInfo.task.name)) {
											if (dataInfo.task.name === '所长报销审核人') {
												this.inputForm.type = '5'
											}
											if (dataInfo.task.name === '所长审批') {
												this.inputForm.type = '5'
											}
											if (dataInfo.task.name === '总经办人员审批') {
												this.inputForm.type = '5'
											}
										}
										if (this.bxList.includes(dataInfo.processDefinitionName)){
											if (this.pre === '中审') {
												await this.zsReimbursementService.findById(this.id).then(async (data) => {
													if (data.type !== '2') {
														this.$message.error('数据已发生改变或不存在，请刷新数据')
													} else {
														let param = {type: this.inputForm.type, id: data.id}
														await this.zsReimbursementService.updateStatusById(param).then(() => {
															// callback()
														})
														// this.reimbursementService.updateStatusById(this.inputForm)
													}
												})
											} else if (this.pre === '会计') {
												await this.cwReimbursementApprovalService.findById(this.id).then(async (data) => {
													if (data.type !== '2') {
														this.$message.error('数据已发生改变或不存在，请刷新数据')
													} else {
														let param = {type: this.inputForm.type, id: data.id}
														await this.cwReimbursementApprovalService.updateStatusById(param).then(() => {
															this.loading = false
															// callback()
														})
														// this.reimbursementService.updateStatusById(this.inputForm)
													}
												})
											} else if (this.pre === '咨询') {
												await this.zxReimbursementService.findById(this.id).then(async (data) => {
													if (data.type !== '2') {
														this.$message.error('数据已发生改变或不存在，请刷新数据')
													} else {
														let param = {type: this.inputForm.type, id: data.id}
														await this.zxReimbursementService.updateStatusById(param).then(() => {
															// callback()
														})
														// this.reimbursementService.updateStatusById(this.inputForm)
													}
												})

											} else if (this.pre === '项目') {
												await this.xmReimbursementService.findById(this.id).then(async (data) => {
													if (data.type !== '2') {
														this.$message.error('数据已发生改变或不存在，请刷新数据')
													} else {
														let param = {type: this.inputForm.type, id: data.id}
														await this.xmReimbursementService.updateStatusById(param).then(() => {
															// callback()
														})
														// this.reimbursementService.updateStatusById(this.inputForm)
													}
												})
											}else if (this.pre === '评估'){
												await this.pgReimbursementService.findById(this.inputForm.id).then(async (data)=>{
													if (data.type !== '2') {
														this.$message.error('数据已发生改变或不存在，请刷新数据')
													} else {
														let param = {type: this.inputForm.type, id: data.id}
														await this.pgReimbursementService.updateStatusById(param).then(() => {
															// callback()
														})
														// this.reimbursementService.updateStatusById(this.inputForm)
													}
												})
											}
											if ((i+1) === total){
												this.$message.success("提交成功");
											}
										}

									})
							});

					}
				} else {
					for (let i = 0; i < this.dataList.length; i++) {
						//根据流程名称获取租户
						let dataInfo = this.dataList[i]
						this.id = ''
						let total=this.dataList.length
						await taskService
							.getTaskDef({
								taskId: dataInfo.task.id,
								taskName: dataInfo.task.name,
								taskDefKey: dataInfo.task.taskDefinitionKey,
								procInsId: dataInfo.task.processInstanceId,
								procDefId: dataInfo.task.processDefinitionId,
								procDefKey: dataInfo.task.processDefKey,
								status: dataInfo.status,
							})
							.then(async (data) => {
								this.id = data.businessId
								this.auditForm.status = 'reject'
								data = {...data, agree: false}
								await taskService
									.audit({
										taskId: dataInfo.task.id,
										taskDefKey: dataInfo.task.taskDefinitionKey,
										procInsId: dataInfo.task.processInstanceId,
										procDefId: dataInfo.task.processDefinitionId,
										vars: data,
										comment: this.auditForm,
										assignee: this.auditForm.assignee,
										recordType: ''
									})
									.then(async (data) => {
										this.pre = dataInfo.processDefinitionName.substring(0, dataInfo.processDefinitionName.lastIndexOf("-"))
										// await setTimeout(this.updateStatusById("reject"),2000)
										if (this.bxList.includes(dataInfo.processDefinitionName)){
											if (this.pre === '中审') {
												this.zsReimbursementService.findById(this.id).then((data) => {
													if (data.type !== '2') {
														this.$message.error('数据已发生改变或不存在，请刷新数据')
													} else {
														let param = {type: '4', id: data.id}
														this.zsReimbursementService.updateStatusById(param).then(() => {
															// callback()
														})
														// this.reimbursementService.updateStatusById(this.inputForm)
													}
												})
											} else if (this.pre === '会计') {
												this.cwReimbursementApprovalService.findById(this.id).then((data) => {
													if (data.type !== '2') {
														this.$message.error('数据已发生改变或不存在，请刷新数据')
													} else {
														let param = {type: '4', id: data.id}
														this.cwReimbursementApprovalService.updateStatusById(param).then(() => {
															this.loading = false
															// callback()
														})
														// this.reimbursementService.updateStatusById(this.inputForm)
													}
												})
											} else if (this.pre === '咨询') {
												this.zxReimbursementService.findById(this.id).then((data) => {
													if (data.type !== '2') {
														this.$message.error('数据已发生改变或不存在，请刷新数据')
													} else {
														let param = {type: '4', id: data.id}
														this.zxReimbursementService.updateStatusById(param).then(() => {
															// callback()
														})
														// this.reimbursementService.updateStatusById(this.inputForm)
													}
												})

											} else if (this.pre === '项目') {
												this.xmReimbursementService.findById(this.id).then((data) => {
													if (data.type !== '2') {
														this.$message.error('数据已发生改变或不存在，请刷新数据')
													} else {
														let param = {type: '4', id: data.id}
														this.xmReimbursementService.updateStatusById(param).then(() => {
															// callback()
														})
														// this.reimbursementService.updateStatusById(this.inputForm)
													}
												})
											}else if (this.pre === '评估'){
												await this.pgReimbursementService.findById(this.inputForm.id).then(async (data)=>{
													if (data.type !== '2') {
														this.loading = false
														this.$message.error('数据已发生改变或不存在，请刷新数据')
													} else {
														let param = {type: this.inputForm.type, id: data.id}
														await this.pgReimbursementService.updateStatusById(param).then(() => {
															// callback()
														})
														// this.reimbursementService.updateStatusById(this.inputForm)
													}
												})
											}
										}
										if ((i+1) === total){
											this.$message.success("提交成功");
										}
									})

							})

					}
				}
				this.loading = false
				this.close()
				this.$emit('refreshDataList')
			},
			close() {
				// this.$refs.searchForm.resetFields()
				this.visibleChoose = false
			},
		}
	}
</script>
<style scoped>
	/deep/ .el-dialog__body {
		padding-top: 0;
	}

	/deep/ .el-form-item--mini.el-form-item, .el-form-item--small.el-form-item {
		margin-bottom: 0px;
	}
</style>
