<template xmlns:v-slot="http://www.w3.org/1999/XSL/Transform">
	<div>
		<span style="color: red;">因离职，需将门禁卡上交至信息部陈强处</span>
	</div>
</template>

<script>
import handoverRecoveryService from '@/api/human/depart/HandoverRecoveryService'
import CommonApi from '@/api/cw/common/CommonApi'

export default {
	props: {
		businessId: {
			type: String,
			default: ''
		},
		formReadOnly: {
			type: Boolean,
			default: false
		},
		status: {
			type: String,
			default: ''
		}
	},
	data() {
		return {
			title: '',
			method: '',
			visible: false,
			loading: false,
			inputForm: {
				userId: '',
				name: '',
				procInsId: '',
				handoverId: '',
				assigneeId: '',
				draftAdministrator: '',
				type: '',
			},
			keyWatch: '',
		}
	},
	commonApi: null,
	created() {
		this.commonApi = new CommonApi()
	},
	components: {
	},
	computed: {
		bus: {
			get() {
				return this.businessId
			},
			set(val) {
				this.businessId = val
			}
		},
		name() {
			return this.$store.state.user.name
		},
		userId() {
			return this.$store.state.user.id
		}
	},
	watch: {
		'keyWatch': {
			handler(newVal) {
				if (this.commonJS.isNotEmpty(this.bus)) {
					this.init('', this.bus)
				} else {
					this.$nextTick(() => {
						this.$refs.inputForm.resetFields()
					})
				}
			}
		},
		'loading': {
			handler(newVal) {
				this.$emit('changeLoading', newVal)
			}
		}
	},
	methods: {
		getKeyWatch(keyWatch) {
			this.keyWatch = keyWatch
		},
		getUpload() {
		},
		init(method, id) {
			this.method = method
			this.inputForm = {
				userId: '',
				name: '',
				procInsId: '',
				handoverId: '',
				assigneeId: '',
				draftAdministrator: '',
				type: '',
			}
			if (method === 'add') {
				this.title = `新建门卡回收`
			} else if (method === 'edit') {
				this.title = '修改门卡回收信息'
			}
			this.inputForm.id = id
			this.visible = true
			this.loading = false
			this.$nextTick(() => {
				this.loading = true
				handoverRecoveryService.findById(this.inputForm.id).then(async (data) => {
					if (this.status === 'audit' || this.status === 'taskFormDetail') {
						method = 'view'
					}
					this.inputForm = this.recover(this.inputForm, data)
					this.inputForm = JSON.parse(JSON.stringify(this.inputForm))

					this.loading = false
				})
			})
		},
		// 表单提交
		doSubmit() {
			this.loading = true
			this.inputForm.id = this.businessId
			handoverRecoveryService.save(this.inputForm).then((data) => {
				this.close()
				this.$message.success(data)
				this.$emit('refreshDataList')
				this.loading = false
			}).catch(() => {
				this.loading = false
			})
		},
		close() {
			this.fileList = []
			this.fileList2 = []
			this.visible = false
		},
		reapplyForm(callback) {
			this.loading = true
			handoverRecoveryService.findById(this.inputForm.id).then((data) => {
				if (data.type !== '4') { // 审核状态不是“驳回”，就弹出提示
					this.loading = false
					this.$message.error('任务数据已发生改变或不存在，请在待办任务中确认此任务是否存在')
					throw new Error('任务数据已发生改变或不存在，请在待办任务中确认此任务是否存在')
				} else {
					this.startForm(callback)
				}
			})
		},
		// 送审
		async startForm(callback) {
			this.loading = true
			this.inputForm.type = '2'
			handoverRecoveryService.save(this.inputForm).then((data) => {
				this.inputForm.title = `${this.$store.state.user.name} 发起了 [门卡回收]`
				this.inputForm.id = data.businessId
				callback(data.businessTable, data.businessId, this.inputForm)
				this.loading = false
			}).catch(() => {
				this.loading = false
			})
		},

		// 通过
		async agreeForm(callback) {
			this.loading = true
			handoverRecoveryService.findById(this.inputForm.id).then((data) => {
				if (data.type !== '2') { // 审核状态不是“审核中”，就弹出提示
					this.loading = false
					this.$message.error('任务数据已发生改变或不存在，请在待办任务中确认此任务是否存在')
					throw new Error('任务数据已发生改变或不存在，请在待办任务中确认此任务是否存在')
				} else {
					this.loading = true
					this.commonApi.getTaskNameByProcInsId(this.inputForm.procInsId).then((data) => {
						if (this.commonJS.isNotEmpty(data)) {
							if (data === '门卡注销') {
								this.inputForm.type = '5'

								if (this.$dictUtils.getDictValue('dd_status', 'dd_status') == 1) {
									//获取用户数据
									let userInfo = this.$TOOL.data.get("USER_INFO");
									if (userInfo.mobile && userInfo.mobile != "") {
										//调用删除钉钉数据接口
										// handoverRecoveryService.deleteDingTalkByTenantId({
										// 	mobilePhone: userInfo.mobile
										// })
									}
								}

							}
						}
						handoverRecoveryService.save(this.inputForm).then((data) => {
							callback(data.businessTable, data.businessId, this.inputForm)
							this.loading = false
						}).catch(() => {
							this.loading = false
						})
					})
				}
			})
		},
		// 修改状态
		updateStatusById(type, callback) {
			if (type === 'reject') {
				this.loading = true
				handoverRecoveryService.findById(this.inputForm.id).then((data) => {
					if (data.type !== '2') { // 审核状态不是“审核中”，就弹出提示
						this.loading = false
						this.$message.error('任务数据已发生改变或不存在，请在待办任务中确认此任务是否存在')
						throw new Error('任务数据已发生改变或不存在，请在待办任务中确认此任务是否存在')
					} else {
						handoverRecoveryService.updateStatusById(this.inputForm).then(() => {
							this.loading = false
							callback()
						})

					}
				})
			} else if (type === 'hold') {
				this.loading = true
				handoverRecoveryService.findById(this.inputForm.id).then((data) => {
					if (data.type !== '4') { // 审核状态不是“驳回”，就弹出提示
						this.loading = false
						this.$message.error('任务数据已发生改变或不存在，请在待办任务中确认此任务是否存在')
						throw new Error('任务数据已发生改变或不存在，请在待办任务中确认此任务是否存在')
					} else {
						this.inputForm.type = '1'
						handoverRecoveryService.updateStatusById(this.inputForm).then(() => {
							this.loading = false
							callback()
						})
					}
				})
			}
		},
	}
}
</script>
