<template xmlns:v-slot="http://www.w3.org/1999/XSL/Transform">
	<div>
		<el-row>
			<el-col :span="24">

				<el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="status === 'audit' || status === 'taskFormDetail'"
						 label-width="135px" @submit.native.prevent>
					<el-divider content-position="left"><i class="el-icon-document"></i> 项目合同信息</el-divider>
					<el-row>
						<el-col :span="12">
							<el-form-item label="合同编号" prop="contractNo"
										  :rules="[
                   ]">
								<el-input placeholder="" :disabled="true" show-word-limit v-model="inputForm.contractNo"></el-input>
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item label="合同名称" prop="contractName"
										  :rules="[
                   ]">
								<el-input placeholder="" :disabled="true" show-word-limit v-model="inputForm.contractName"></el-input>
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item label="合同金额(元)" prop="contractAmount"
										  :rules="[
                   ]">
								<el-input placeholder="" :disabled="true" show-word-limit v-model="inputForm.contractAmount"></el-input>
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item label="委托方" prop="clientName"
										  :rules="[
                   ]">
								<el-input placeholder="" :disabled="true" show-word-limit v-model="inputForm.clientName"></el-input>
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item label="合同类型" prop="contractType"
										  :rules="[
              ]">
								<el-select :disabled="true" v-model="inputForm.contractType" placeholder="请选择" style="width:100%;">
									<el-option
										v-for="item in $dictUtils.getDictList('cw_contract_type')"
										:key="item.value"
										:label="item.label"
										:value="item.value">
									</el-option>
								</el-select>
							</el-form-item>
						</el-col>
					</el-row>
					<el-divider content-position="left"><i class="el-icon-document"></i> 项目基础信息</el-divider>
					<el-row  :gutter="0">
						<el-col :span="12">
							<el-form-item label="项目名称" prop="projectName"
										  :rules="[
                   ]">
								<el-input :disabled="true" v-model="inputForm.projectName" clearable></el-input>
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item label="项目编号" prop="projectNumber"
										  :rules="[
                   ]">
								<el-input :disabled="true" v-model="inputForm.projectNumber" clearable></el-input>
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item label="创建人" prop="projectCreateByName"
										  :rules="[
                   ]">
								<el-input :disabled="true" v-model="inputForm.projectCreateByName" clearable></el-input>
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item label="项目责任人" prop="projectCreateByName"
										  :rules="[
                   ]">
								<el-input :disabled="true" v-model="inputForm.projectCreateByName" clearable></el-input>
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item label="项目所属部门" prop="officeId"
										  :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.officeId"
									:accordion="true"
									size="large"
									:disabled="true"
									@getValue="(value) => {inputForm.officeId=value}"/>
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item label="现场负责人" prop="projectManager"
										  :rules="[
                   ]">
								<el-input :disabled="true" v-model="inputForm.projectManager" clearable></el-input>
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item label="项目类别" prop="reportType"
										  :rules="[
                                 {required: true, message:'项目类别不能为空', trigger:'blur'}
                  ]">
								<el-select :disabled="true" v-model="inputForm.reportType" placeholder="请选择项目类别" clearable style="width: 100%;">
									<el-option
										v-for="item in $dictUtils.getDictList('program_engineering_consultation_type')"
										:key="item.value"
										:label="item.label"
										:value="item.value">
									</el-option>
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item label="项目等级" prop="projectLevel"
										  :rules="[
                  {required: true, message:'请选择项目等级(500万及500万以上为A级，500万以下为B级)', trigger:'blur'}
               ]">
								<el-radio-group :disabled="true" v-model="inputForm.projectLevel">
									<el-radio  v-for="item in $dictUtils.getDictList('cw_project_level')" :label="item.value" >{{item.label}}</el-radio>
								</el-radio-group>
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item label="项目规模(元)" prop="projectMoney"
										  :rules="[{required: true, message:'项目规模不能为空', trigger:'blur'}
                   ]">
								<el-input-number
									:disabled="true"
									v-model="inputForm.projectMoney"
									controls-position="right"
									style="width:100%"
									:precision="2"
									:max="9999999999"
									:step="0.01"
									:min="0"
									placeholder="请填写项目规模"
									:controls="false"
									clearable>
								</el-input-number>
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item label="工作开始日期" prop="planStartDate"
										  :rules="[
                 ]">
								<el-date-picker
									:disabled="true"
									v-model="inputForm.planStartDate"
									type="date"
									value-format="YYYY-MM-DD"
									placeholder="选择工作开始日期"
									style="width:100%"
									placement="bottom-start"
									clearable>
								</el-date-picker>
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item label="工作结束日期" prop="planEndDate"
										  :rules="[
                 ]">
								<el-date-picker
									:disabled="true"
									v-model="inputForm.planEndDate"
									type="date"
									value-format="YYYY-MM-DD"
									placeholder="选择工作结束日期"
									style="width:100%"
									placement="bottom-start"
									clearable>
								</el-date-picker>
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item label="创建时间" prop="createDate"
										  :rules="[
                   ]">
								<el-date-picker
									:disabled="true"
									v-model="inputForm.createDate"
									type="date"
									value-format="YYYY-MM-DD"
									placeholder="选择创建时间"
									style="width:100%"
									placement="bottom-start"
									clearable>
								</el-date-picker>
							</el-form-item>
						</el-col>
					</el-row>
					<el-form v-if="status === 'audit'" :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"
							 label-width="135px" @submit.native.prevent>
						<el-row>
							<el-col  :span="12">
								<el-form-item label="案卷号" prop="filedNo"
											  :rules="[
                                {required: true, message:'案卷号不能为空', trigger:'blur'}
                   ]">
									<el-input :disabled="false" v-model="inputForm.filedNo" placeholder="请填写案卷号" clearable></el-input>
								</el-form-item>
							</el-col>
							<el-col  :span="12">
								<el-form-item label="确认案卷号" prop="confirmFiledNo"
											  :rules="[
                                {required: true, message:'确认案卷号不能为空', trigger:'blur'}
                   ]">
									<el-input :disabled="false" v-model="inputForm.confirmFiledNo" placeholder="请填写确认案卷号" clearable></el-input>
								</el-form-item>
							</el-col>
						</el-row>
					</el-form>
					<el-form v-if="inputForm.paperArchivingStatus === '5'" :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"
							 label-width="135px" @submit.native.prevent>
						<el-row>
							<el-col  :span="12">
								<el-form-item label="案卷号" prop="filedNo"
											  :rules="[
                                {required: true, message:'案卷号不能为空', trigger:'blur'}
                   ]">
									<el-input :disabled="true" v-model="inputForm.filedNo" placeholder="请填写案卷号" clearable></el-input>
								</el-form-item>
							</el-col>
							<el-col  :span="12">
								<el-form-item label="确认案卷号" prop="confirmFiledNo"
											  :rules="[
                                {required: true, message:'确认案卷号不能为空', trigger:'blur'}
                   ]">
									<el-input :disabled="true" v-model="inputForm.confirmFiledNo" placeholder="请填写确认案卷号" clearable></el-input>
								</el-form-item>
							</el-col>
						</el-row>
					</el-form>
					<el-divider content-position="left"><i class="el-icon-document"></i>
						委托方联系人信息
					</el-divider>
					<el-row  :gutter="15" >
						<el-col :span="24">
							<vxe-table
								border
								show-footer
								show-overflow
								ref="contactTable"
								class="vxe-table-element"
								:data="inputForm.contactInfos"
								style="margin-left: 5em"
								:key="amountKey"
								highlight-current-row
								:edit-config="{trigger: 'click', mode: 'row', showStatus: true, autoClear: true, icon:'_'}"
							>
								<vxe-table-column field="clientContactsName" title="委托方" :edit-render="{}" show-overflow="title">
									<template v-slot:edit="scope">
										<el-input maxlength="15" :readonly="true" v-model="scope.row.clientContactsName" ></el-input>
									</template>
								</vxe-table-column>
								<vxe-table-column field="name" title="联系人姓名" :edit-render="{}" show-overflow="title">
									<template v-slot:edit="scope">
										<el-input maxlength="15" :readonly="true" v-model="scope.row.name" ></el-input>
									</template>
								</vxe-table-column>
								<vxe-table-column field="contactFirst" title="联系方式1" :edit-render="{}" show-overflow="title">
									<template v-slot:edit="scope">
										<el-input maxlength="15" :readonly="true" v-model="scope.row.contactFirst" ></el-input>
									</template>
								</vxe-table-column>
								<vxe-table-column field="contactSecond" title="联系方式2" :edit-render="{}" show-overflow="title">
									<template v-slot:edit="scope">
										<el-input maxlength="15" :readonly="true" v-model="scope.row.contactSecond" ></el-input>
									</template>
								</vxe-table-column>
							</vxe-table>
						</el-col>
					</el-row>
					<el-divider content-position="left"><i class="el-icon-document"></i>
						施工方信息
					</el-divider>
					<el-row  :gutter="15" >
						<el-col :span="24">
							<vxe-table
								border
								show-footer
								show-overflow
								ref="contactTable"
								class="vxe-table-element"
								:data="inputForm.constructionParty"
								style="margin-left: 5em"
								:key="amountKey3"
								highlight-current-row
								:edit-config="{trigger: 'click', mode: 'row', showStatus: true, autoClear: true, icon:'_'}"
							>
								<vxe-table-column field="clientContactsName" title="施工方单位名称" :edit-render="{}" show-overflow="title">
									<template v-slot:edit="scope">
										<el-input maxlength="15" :readonly="true" v-model="scope.row.clientContactsName" ></el-input>
									</template>
								</vxe-table-column>
								<vxe-table-column field="name" title="联系人姓名" :edit-render="{}" show-overflow="title">
									<template v-slot:edit="scope">
										<el-input maxlength="15" :readonly="true" v-model="scope.row.name" ></el-input>
									</template>
								</vxe-table-column>
								<vxe-table-column field="contactFirst" title="联系方式1" :edit-render="{}" show-overflow="title">
									<template v-slot:edit="scope">
										<el-input maxlength="15" :readonly="true" v-model="scope.row.contactFirst" ></el-input>
									</template>
								</vxe-table-column>
								<vxe-table-column field="contactSecond" title="联系方式2" :edit-render="{}" show-overflow="title">
									<template v-slot:edit="scope">
										<el-input maxlength="15" :readonly="true" v-model="scope.row.contactSecond" ></el-input>
									</template>
								</vxe-table-column>
							</vxe-table>
						</el-col>
					</el-row>
					<el-divider content-position="left"><i class="el-icon-document"></i>
						项目报告
					</el-divider>
					<el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"
							 label-width="160px" @submit.native.prevent>
						<el-row  :gutter="15" >
							<el-col :span="24">
								<vxe-table
									border
									show-footer
									show-overflow
									ref="contactTable"
									class="vxe-table-element"
									:data="inputForm.projectReportInfos"
									style="margin-left: 5em"
									:key="amountKey2"
									highlight-current-row
									:edit-config="{trigger: 'click', mode: 'row', showStatus: true, autoClear: true, icon:'_'}"
								>
									<vxe-table-column field="reportNo" title="报告编号" :edit-render="{}" show-overflow="title">
										<template v-slot:edit="scope">
											<el-input maxlength="15" :readonly="true" v-model="scope.row.reportNo" @focus="openProgramPageForm(scope.$rowIndex, scope.row)"></el-input>
										</template>
									</vxe-table-column>
									<vxe-table-column field="projectName" title="报告名称" :edit-render="{}" show-overflow="title">
										<template v-slot:edit="scope">
											<el-input maxlength="15" :readonly="true" v-model="scope.row.projectName" ></el-input>
										</template>
									</vxe-table-column>
									<vxe-table-column field="createTime" title="创建日期" :edit-render="{}" show-overflow="title">
										<template v-slot:edit="scope">
											<el-input maxlength="15" :readonly="true" v-model="scope.row.createTime" ></el-input>
										</template>
									</vxe-table-column>
									<vxe-table-column field="status" title="状态" :edit-render="{}" show-overflow="title">
										<template v-slot:edit="scope">
											<el-input maxlength="15" :readonly="true" v-model="scope.row.status" ></el-input>
										</template>
									</vxe-table-column>
								</vxe-table>
							</el-col>
						</el-row>
					</el-form>
				</el-form >
			</el-col>
		</el-row>
	</div>
	<ReportManagementForm ref="reportManagementForm"></ReportManagementForm>
</template>

<script>
	import ReportManagementForm from '../ReportManagementFormNew'
	import SelectTree from '@/components/treeSelect/treeSelect.vue'
	import OSSSerivce, {
		// eslint-disable-next-line no-unused-vars
		beforeAvatarUpload,
		// eslint-disable-next-line no-unused-vars
		openWindowOnUrl,
		// eslint-disable-next-line no-unused-vars
		toHref
	} from '@/api/sys/OSSService'
	import reportPaperArchivingService from '@/api/cw/reportManagement/ReportPaperArchivingService'
	import projectReportService from '@/api/cw/reportManagement/ProjectReportService'
	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 {
				amountKey: '',
				amountKey2: '',
				amountKey3: '',
				title: '',
				method: '',
				loading: false,
				inputForm: {
					paperArchivingStatus: '',
					paperArchivingId: '',
					contactInfos: [],
					constructionParty: [],
					projectReportInfos: [],
					contractNo: '',
					contractName: '',
					contractAmount: '',
					clientName: '',
					contractType: '',
					projectName: '',
					projectNumber: '',
					location: '',
					projectCreateByName: '',
					officeId: '',
					projectManager: '',
					reportType: '',
					projectLevel: '',
					projectMoney: '',
					planStartDate: '',
					planEndDate: '',
					createDate: '',
					filedNo: '',
					confirmFiledNo: '',
					createBy: {
						id: '',
						name: this.$store.state.user.name
					},
				},
				keyWatch: '',
			}
		},
		commonApi: null,
		created () {
			this.commonApi = new CommonApi()
		},
		computed: {
			bus: {
				get () {
					return this.businessId
				},
				set (val) {
					this.businessId = val
				}
			}
		},
		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)
				}
			}
		},
		components: {
			ReportManagementForm,
			SelectTree,
		},
		methods: {
			getKeyWatch (keyWatch) {
				this.keyWatch = keyWatch
			},
			init (method, id) {
				this.method = method
				this.inputForm = {
					paperArchivingStatus: '',
					paperArchivingId: '',
					contactInfos: [],
					constructionParty: [],
					projectReportInfos: [],
					contractNo: '',
					contractName: '',
					contractAmount: '',
					clientName: '',
					contractType: '',
					projectName: '',
					projectNumber: '',
					location: '',
					projectCreateByName: '',
					officeId: '',
					projectManager: '',
					reportType: '',
					projectLevel: '',
					projectMoney: '',
					planStartDate: '',
					planEndDate: '',
					createDate: '',
					filedNo: '',
					confirmFiledNo: '',
					createBy: {
						id: '',
						name: this.$store.state.user.name
					},
				}
				this.inputForm.id = id
				this.loading = false
				this.$nextTick(() => {
					// this.$refs.inputForm.resetFields()

					this.loading = true
					projectReportService.queryByIdNew(this.inputForm.id).then((data) => {
						if (data) {
							this.inputForm = this.recover(this.inputForm, data)

							if (this.status === 'taskFormDetail' || this.status === 'audit') {
								this.method = 'view'
							} else {
								this.method = 'false'
							}
							this.loading = false
						} else {
							projectReportService.queryByPaperArchivingId(this.inputForm.id).then((data2) => {
								if (data2) {
									this.inputForm = this.recover(this.inputForm, data2)

									if (this.status === 'taskFormDetail' || this.status === 'audit') {
										this.method = 'view'
									} else {
										this.method = 'false'
									}
									this.loading = false
								}

							})
						}

					})
				})
			},
			saveForm (callback) {
				this.doSubmit('save', callback)
			},
			startForm (callback) {
				this.loading = true
				if (this.commonJS.isNotEmpty(this.inputForm.id)) {
					reportPaperArchivingService.queryByReportId(this.inputForm.id).then((data) => {
						if (this.commonJS.isNotEmpty(data.status) && data.status !== '0' && data.status !== '1' && data.status !== '3') { // 审核状态不是“未发起”或“暂存”或“撤回”，就弹出提示
							this.loading = false
							this.$message.error('任务数据已发生改变或不存在，请在待办任务中确认此任务是否存在')
							throw new Error('任务数据已发生改变或不存在，请在待办任务中确认此任务是否存在')
						} else {
							this.doSubmit('start', callback)
						}
					})
				} else {
					this.doSubmit('start', callback)
				}
			},
			async agreeForm (callback) {
				this.loading = true
				await reportPaperArchivingService.queryByReportId(this.inputForm.id).then((data) => {
					if (data.status !== '2') { // 审核状态不是“待审核”，就弹出提示
						this.loading = false
						this.$message.error('任务数据已发生改变或不存在，请在待办任务中确认此任务是否存在')
						throw new Error()
					} else {
						this.doSubmit('agree', callback)
					}
				})
			},
			reapplyForm (callback) {
				this.loading = true
				reportPaperArchivingService.queryByReportId(this.inputForm.id).then((data) => {
					if (data.status !== '4') { // 审核状态不是“驳回”，就弹出提示
						this.loading = false
						this.$message.error('任务数据已发生改变或不存在，请在待办任务中确认此任务是否存在')
						throw new Error('任务数据已发生改变或不存在，请在待办任务中确认此任务是否存在')
					} else {
						this.doSubmit('reapply', callback)
					}
				})
			},
			// 表单提交
			doSubmit (status, callback) {
				if (status === 'save') {
					// 暂存
					this.inputForm.status = '1'
					this.loading = true
					reportPaperArchivingService.saveForm(this.inputForm).then((data) => {
						callback(data.businessTable, data.businessId, this.inputForm)
						this.loading = false
					}).catch(() => {
						this.loading = false
					})
					return
				} else if (status === 'start') {
					// 送审  待审核
					this.inputForm.status = '2'
				} else if (status === 'agree') {
					// 审核同意
					this.inputForm.agreeDate = this.moment(new Date()).format('YYYY-MM-DD')
					this.inputForm.agreeUserId = this.$store.state.user.id
					// this.inputForm.status = '5'
				} else if (status === 'reapply') {
					this.inputForm.status = '2'
				}
				this.$refs['inputForm'].validate((valid) => {
					if (valid) {
						this.loading = true
						if (status === 'agree') {

							if (this.inputForm.filedNo !== this.inputForm.confirmFiledNo) {
								this.$message.error('“案卷号”与“确认案卷号”不一致，请重新填写')
								this.loading = false
								throw new Error('“案卷号”与“确认案卷号”不一致，请重新填写')
							} else {
								this.inputForm.status = '5'
								reportPaperArchivingService.saveForm(this.inputForm).then((data2) => {
									callback(data2.businessTable, data2.businessId, this.inputForm)
									this.loading = false
								}).catch(() => {
									this.loading = false
								})
							}



						}else{

							reportPaperArchivingService.saveForm(this.inputForm).then((data) => {
								callback(data.businessTable, data.businessId, this.inputForm)
								this.loading = false
							}).catch(() => {
								this.loading = false
							})

						}

					} else {
						this.loading = false
					}
				})
			},
			async updateStatusById (type, callback) {
				this.loading = true
				if (type === 'reject' || type === 'reback') {
					await reportPaperArchivingService.queryByReportId(this.inputForm.id).then((data) => {
						if (data.status !== '2') { // status的值不等于“审核中”就弹出提示
							this.loading = false
							this.$message.error('任务数据已发生改变或不存在，请在待办任务中确认此任务是否存在')
							throw new Error()
						} else {
							if (type === 'reject') {
								reportPaperArchivingService.queryByReportId(this.inputForm.id).then((review) => {
									console.log('review', review)
									this.rejectOn(callback)
								})
							}
							if (type === 'reback') {
								// 撤回
								let param = {status: '3', id: this.inputForm.paperArchivingId}
								reportPaperArchivingService.updateStatusByReportId(param)
							}
						}
					})
				} else if (type === 'hold') {
					reportPaperArchivingService.queryByReportId(this.inputForm.id).then((data) => {
						if (data.status !== '4') { // status的值不等于“驳回”就弹出提示
							this.loading = false
							this.$message.error('任务数据已发生改变或不存在，请在待办任务中确认此任务是否存在')
							throw new Error()
						} else {
							// 终止
							let param = {status: '1', id: this.inputForm.paperArchivingId}
							reportPaperArchivingService.updateStatusByReportId(param).then(() => {
								callback()
								this.loading = false
							})
						}
					})
				}
			},
			// 这个方法是这个之前这个页面的驳回方法，现在粘出来复用
			rejectOn (callback) {
				// 驳回
				let param
				this.inputForm.status = '4'
				param = {status: this.inputForm.status,
					id: this.inputForm.paperArchivingId,
					needUpdate2: this.inputForm.needUpdate2,
					needUpdate3: this.inputForm.needUpdate3,
					remark2: this.inputForm.remark2,
					remark3: this.inputForm.remark3,
					details: this.inputForm.details
				}
				reportPaperArchivingService.updateStatusByReportId(param).then(() => {
					this.loading = false
					callback()
				})
			},
			close () {
				this.inputForm = {
					paperArchivingStatus: '',
					paperArchivingId: '',
					contactInfos: [],
					constructionParty: [],
					projectReportInfos: [],
					contractNo: '',
					contractName: '',
					contractAmount: '',
					clientName: '',
					contractType: '',
					projectName: '',
					projectNumber: '',
					location: '',
					projectCreateByName: '',
					officeId: '',
					projectManager: '',
					reportType: '',
					projectLevel: '',
					projectMoney: '',
					planStartDate: '',
					planEndDate: '',
					createDate: '',
					filedNo: '',
					confirmFiledNo: '',
					createBy: {
						id: '',
						name: this.$store.state.user.name
					},
				}
			},
			openProgramPageForm (rowIndex, row) {
				this.$refs.reportManagementForm.init('view', this.inputForm.id, '')
			},
		}
	}
</script>
<style scoped>
	/deep/ .el-input-number .el-input__inner {
		text-align: left;
	}

	/deep/ .vxe-select-option {
		padding: 0 .6em;
		max-width: 600px;
		overflow: visible;
		text-overflow: ellipsis;
		white-space: nowrap;
		-webkit-user-select: none;
		-moz-user-select: none;
		-ms-user-select: none;
		user-select: none
	}
</style>
