<template xmlns:v-slot="http://www.w3.org/1999/XSL/Transform">
	<div>
		<el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method === 'view' ? 'readonly' : ''"
			label-width="100px" @submit.native.prevent>

			<el-divider content-position="left"><i class="el-icon-document"></i> 基本信息</el-divider>
			<el-row :gutter="26">
				<el-col :span="12">
					<el-form-item label="姓名" prop="name"
						:rules="[{ required: true, message: '姓名不能为空', trigger: 'blur' }]">
						<el-input v-model="inputForm.name" :disabled="true"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="所属部门" prop="department" :rules="[
						{ required: true, message: '请选择所属部门', trigger: 'blur' }
					]">
						<SelectTree ref="officeTree" :props="{
							value: 'id',             // ID字段名
							label: 'name',         // 显示名称
							children: 'children'    // 子级字段名
						}" :url="`/system-server/sys/office/treeData?type=2`" :value="inputForm.department" :disabled="true"
							:accordion="true" size="default" @getValue="(value) => { inputForm.department = value }" />
					</el-form-item>
				</el-col>
			</el-row>
			<el-divider content-position="left"><i class="el-icon-document"></i> 未完成报告归档的项目信息</el-divider>
			<el-row>
				<el-col :span="24">
					<div style="height:300px;padding-bottom: 50px;">
						<vxe-table border="inner" auto-resize resizable height="auto" :loading="tableLoading1"
							size="small" ref="todoTable" show-header-overflow show-overflow highlight-hover-row
							:menu-config="{}" align="center" :print-config="{}" :import-config="{}" :export-config="{}"
							:data="dataList1" header-align="center" :checkbox-config="{}">
							<vxe-column title="项目编号" field="projectNumber">
							</vxe-column>
							<vxe-column title="项目名称" field="projectName"></vxe-column>
							<vxe-column title="报告号" field="reportNo"></vxe-column>
							<vxe-column title="创建人" field="createName"></vxe-column>
							<vxe-column title="创建时间" field="createTime"></vxe-column>
							<vxe-column title="当前所处节点" field="nodeLocation">
								<template #default="scope">
									<el-tag type="warning" v-if="scope.row.archiveSta === '超时未归档'">超时未归档</el-tag>
									<el-tag type="warning" v-if="scope.row.archiveSta === '暂时未归档'">暂时未归档</el-tag>
								</template>
							</vxe-column>
						</vxe-table>
						<vxe-pager v-if="inputForm.type != 5" background :current-page="tablePage1.currentPage"
							:page-size="tablePage1.pageSize" :total="tablePage1.total"
							:page-sizes="[10, 20, 100, 1000, { label: '全量数据', value: 1000000 }]"
							:layouts="['PrevPage', 'JumpNumber', 'NextPage', 'FullJump', 'Sizes', 'Total']"
							@page-change="currentChangeHandle1">
						</vxe-pager>
					</div>
				</el-col>
			</el-row>
			<el-divider content-position="left"><i class="el-icon-document"></i> 作废报告归档未完成的项目信息</el-divider>
			<el-row>
				<el-col :span="24">
					<div style="height:300px;padding-bottom: 50px;">
						<vxe-table border="inner" auto-resize resizable height="auto" :loading="tableLoading2"
							size="small" ref="todoTable" show-header-overflow show-overflow highlight-hover-row
							:menu-config="{}" :print-config="{}" :import-config="{}" :export-config="{}"
							:data="dataList2" align="center" header-align="center" :checkbox-config="{}">
							<vxe-column title="项目编号" field="projectNumber">
							</vxe-column>
							<vxe-column title="项目名称" field="projectName"></vxe-column>
							<vxe-column title="报告号" field="reportNo"></vxe-column>
							<vxe-column title="创建人" field="userName"></vxe-column>
							<vxe-column title="创建时间" field="createTime"></vxe-column>
							<vxe-column title="当前所处节点" field="">
								<template #default="scope">
									<el-tag type="warning">报告作废{{ $dictUtils.getDictLabel("filed_type",
										scope.row.applyFileType, '-') }}</el-tag>

								</template>
							</vxe-column>
						</vxe-table>
						<vxe-pager background :current-page="tablePage2.currentPage" :page-size="tablePage2.pageSize"
							:total="tablePage2.total"
							:page-sizes="[10, 20, 100, 1000, { label: '全量数据', value: 1000000 }]"
							:layouts="['PrevPage', 'JumpNumber', 'NextPage', 'FullJump', 'Sizes', 'Total']"
							@page-change="currentChangeHandle2">
						</vxe-pager>
					</div>
				</el-col>
			</el-row>
		</el-form>
	</div>
</template>

<script>
import SelectTree from '@/components/treeSelect/treeSelect.vue'
import handoverDraftService from '@/api/human/depart/HandoverDraftService'
import ProjectReportArchiveService from '@/api/cw/projectRecords/ProjectReportArchiveService'
import ReportCancellApplyArchivedService from '@/api/cw/reportCancellApplyArchived/ReportCancellApplyArchivedService'

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: '',
				assigneeId: '',
				type: '',
				department: '',
				handoverId: '',
			},
			keyWatch: '',
			// 获取1个月内完成的项目接口的参数
			tablePage1: {
				total: 0,
				currentPage: 1,
				pageSize: 10,
				orders: []
			},
			dataList1: [],
			tableLoading1: false,
			searchForm1: {
				cwProjectRecordsDTO: {
					projectName: '',
					projectMasterName: '',
					realHeaderName: ''
				},
				name: '',
				year: '',
				number: '',
				auditDates: [],
				status: '',
				createBy: {
					name: ''
				},
				createDates: [],
				reportNo: '',
				fileNumber: '',
				archiveSta: ''
			},
			// 作废报告接口参数
			tablePage2: {
				total: 0,
				currentPage: 1,
				pageSize: 10,
				orders: []
			},
			dataList2: [],
			tableLoading2: false,
			searchForm: {
				projectNumber: '',
				applyFileType: '',
				projectName: '',
				projectMasterId: '',
				projectMaster2Id: '',
				createBy: '',
				contractDates: [],
				projectMasterName: '', // 项目经理1
				projectMaster2Name: '' // 项目经理2
			},

		}
	},
	projectReportArchiveService: null,
	reportCancellApplyArchivedService: null,
	created() {
		this.projectReportArchiveService = new ProjectReportArchiveService()
		this.reportCancellApplyArchivedService = new ReportCancellApplyArchivedService()
	},
	components: {
		SelectTree,
	},
	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: {
		// 获取作废报告归档未完成信息
		getCancelProjectInfo() {
			this.tableLoading2 = true
			this.reportCancellApplyArchivedService.findNotCompList({
				'current': this.tablePage2.currentPage,
				'size': this.tablePage2.pageSize,
				'orders': this.tablePage2.orders,
				...this.searchForm2,
				createBy: this.inputForm.name,
				applyFileType: '-1'
			}).then((data) => {
				this.dataList2 = data.records
				this.tablePage2.total = data.total
				this.tableLoading2 = false
			})
		},
		// 当前页
		currentChangeHandle2({ currentPage, pageSize }) {
			this.tablePage2.currentPage = currentPage
			this.tablePage2.pageSize = pageSize
			this.getCancelProjectInfo()
		},
		// 获取未完成归档的数据列表
		getNotCompProjectInfo() {
			this.tableLoading1 = true
			this.projectReportArchiveService.findCompletedList({
				'current': this.tablePage1.currentPage,
				'size': this.tablePage1.pageSize,
				'orders': this.tablePage1.orders,
				...this.searchForm1,
				archiveSta: "-2",
				createBy: {
					name: this.inputForm.name
				}
			}).then((data) => {
				this.dataList1 = data.records
				this.tablePage1.total = data.total
				this.tableLoading1 = false
			})
		},

		// 当前页
		currentChangeHandle1({ currentPage, pageSize }) {
			this.tablePage1.currentPage = currentPage
			this.tablePage1.pageSize = pageSize
			this.getCompProjectInfo()
		},
		getKeyWatch(keyWatch) {
			this.keyWatch = keyWatch
		},
		getUpload() {
		},
		init(method, id) {
			this.method = method
			this.inputForm = {
				userId: '',
				name: '',
				procInsId: '',
				assigneeId: '',
				type: '',
				department: '',
				handoverId: '',
			}
			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
				handoverDraftService.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.getNotCompProjectInfo()
					// 获取作废归档未完成数据
					this.getCancelProjectInfo()
					this.loading = false
				})
			})
		},
		// 表单提交
		doSubmit() {
			this.loading = true
			this.inputForm.id = this.businessId
			handoverDraftService.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
			handoverDraftService.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'
			handoverDraftService.save(this.inputForm).then((data) => {
				this.inputForm.title = `${this.$store.state.user.name} 发起了 [底稿归档检查]`
				this.inputForm.id = data.businessId
				this.inputForm.assignee = this.inputForm.draftAdministrator
				callback(data.businessTable, data.businessId, this.inputForm)
				this.loading = false
			}).catch(() => {
				this.loading = false
			})
		},

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