<template xmlns:v-slot="http://www.w3.org/1999/XSL/Transform">
	<div>
		<div v-if="(inputForm.status === '5' ||inputForm.status === '7')&&(tabName === '一级校审'||tabName === '二级校审'||tabName === '三级校审')">
			<el-row type="flex" justify="end">
				<el-button @click="downloadFirstAuditTpl">下载</el-button>
			</el-row>
		</div>
		<el-form size="large" :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"
				 label-width="170px" @submit.native.prevent>

			<el-row  :gutter="15">
				<el-col :span="24" >
					<el-divider content-position="left"><i class="el-icon-document"></i>
						合同信息
					</el-divider>
				</el-col>
				<el-col :span="12">
					<el-form-item label="合同名称" prop="contractName"
								  :rules="[
			 ]">
						<el-input :disabled="true" v-model="inputForm.contractName" placeholder="请填写合同名称"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="合同金额(元)" prop="contractAmount"
								  :rules="[
			 ]">
						  <el-input :disabled="true" v-model="inputForm.contractAmount" placeholder="请填写合同金额(元)"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="委托方" prop="primaryLinkman"
								  :rules="[
			 ]">
						<el-input :disabled="true" v-model="inputForm.primaryLinkman" placeholder="请填写委托方"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="合同类型(大类)" prop="contractTypeFirst"
								  :rules="[
			 ]">
						<el-input :disabled="true" v-model="inputForm.contractTypeFirst" placeholder="请填写合同类型(大类)"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="合同类型(小类)" prop="contractType"
								  :rules="[
			 ]">
						<el-input :disabled="true" v-model="inputForm.contractType" placeholder="请填写合同类型(小类)"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row  :gutter="15">
				<el-col :span="24" >
					<el-divider content-position="left"><i class="el-icon-document"></i>
						项目基本信息
					</el-divider>
				</el-col>
				<el-col :span="12">
					<el-form-item label="项目名称" prop="projectName"
								  :rules="[
			 ]">
						<el-input :disabled="true" v-model="inputForm.projectName" placeholder="请填写项目名称"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="项目所在地" prop="projectPlace"
								  :rules="[
			 ]">
						  <el-input :disabled="true" v-model="inputForm.projectPlace" placeholder="请填写项目所在地"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="建设地点" prop="buildPlace"
								  :rules="[
			 ]">
						<el-input :disabled="true" v-model="inputForm.buildPlace" placeholder="请填写建设地点"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="项目负责人" prop="projectLeader"
								  :rules="[
			 ]">
						<el-input :disabled="true" v-model="inputForm.projectLeader" placeholder="请填写项目负责人"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="创建人" prop="createByName"
								  :rules="[
			 ]">
						<el-input :disabled="true" v-model="inputForm.createByName" placeholder="请填写创建人"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="创建时间" prop="createTime"
								  :rules="[
			 ]">
						<el-input :disabled="true" v-model="inputForm.createTime" placeholder="请填写创建时间"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12" >
					<el-form-item label="审核人" prop="auditPeople"
								  :rules="[
			 ]">
						<UserSelectSignatory style="width: 100%" ref="userSelect2" :disabled="status === 'audit' || status === 'taskFormDetail'|| method ==='view' || isEdit"
											 :readonly="true" :limit='1' :modelValue="inputForm.auditPeople"
											 @update:modelValue='(value) => {inputForm.auditPeople = value}'></UserSelectSignatory>
					</el-form-item>
				</el-col>

			</el-row>
			<el-row  :gutter="15">
				<el-col :span="24" v-show="inputForm.status === '2' && status ==='audit'?true:false">
					<el-divider content-position="left"><i class="el-icon-document"></i>
						审核人及审核和修改意见
					</el-divider>
				</el-col>

				<el-col :span="24" v-if="commonJS.isNotEmpty(inputForm.initialAuditOpinion)">
					<el-form-item label="审核意见" prop="initialAuditOpinion"
								  :rules="[
			 ]">
						<el-input type="textarea" id="auditData"  :rows="4" :disabled="true" show-word-limit maxlength="500" v-model="inputForm.initialAuditOpinion" placeholder="请填写审核意见"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="24" v-if="commonJS.isNotEmpty(inputForm.initialEditOpinion)">
					<el-form-item label="修改意见" prop="initialEditOpinion"
								  :rules="[
			 ]">
						  <el-input type="textarea" id="editData"  :rows="4" :disabled="true" show-word-limit maxlength="500" v-model="inputForm.initialEditOpinion" placeholder="请填写修改意见"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="24" v-show="inputForm.status === '2' && status ==='audit'?true:false">
					<el-form-item label="审核意见" prop="auditOpinion"
								  :rules="[
			 ]">
						<el-input :rows="4"  show-word-limit maxlength="500" type="textarea" v-model="inputForm.auditOpinion" placeholder="请填写审核意见"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="24" v-show="inputForm.status === '4' && status !=='audit'?true:false">
					<el-form-item label="修改意见" prop="editOpinion"
								  :rules="[
			 ]">
						<el-input :rows="4"  show-word-limit maxlength="500" type="textarea" v-model="inputForm.editOpinion" placeholder="请填写修改意见"></el-input>
					</el-form-item>
				</el-col>
			</el-row>

		</el-form>
		<!--        附件-->
		<UpLoadComponent ref="uploadComponent"></UpLoadComponent>


	</div>
</template>

<script>
	import ProjectInfoService from "@/api/jy/ProjectInfoService";
	import UserSelectSignatory from '../workClientInfo/clientUserSelect'
	import UpLoadComponent from '@/views/common/UpLoadComponent'
	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: {
					auditId1: '',
					auditId2: '',
					auditId3: '',
					id: '',
					contractName:'',
					contractAmount:'',
					primaryLinkman:'',
					contractTypeFirst:'',
					contractType:'',
					projectName:'',
					projectPlace:'',
					buildPlace:'',
					projectLeader:'',
					createByName:'',
					createTime:'',
					initialAuditOpinion:'',
					initialEditOpinion:'',
					auditOpinion:'',
					editOpinion:'',
					workAttachmentDtoList:[],
					auditPeople:'',
					status:'',
					projectId:'',
					no:'',
					auditLevel:''
				},
				keyWatch: '',
				tabName:'',
				isEdit:false

			}
		},
		ProjectInfoService:null,
		created () {
			this.projectInfoService=new ProjectInfoService()
		},
		computed: {
			bus: {
				get () {
					this.$refs.uploadComponent.setDividerName('附件')
					return this.businessId

				},
				set (val) {
					this.businessId = val
				}
			}
		},
		watch: {
			'keyWatch': {
				handler (newVal) {
					console.log('dsad')
					if (this.bus) {
						console.log('dsad',this.bus)
						if (this.bus !== 'false') {
							this.init('', this.bus)
						}
					} else {
						this.$nextTick(() => {
							this.$refs.inputForm.resetFields()
						})
					}
				}
			},
			'loading': {
				handler (newVal) {
					this.$emit('changeLoading', newVal)
					this.$refs.uploadComponent.changeLoading(newVal)
				}
			}
		},
		components: {
			UserSelectSignatory,
			UpLoadComponent,
		},
		methods: {
			getKeyWatch (keyWatch) {
				this.keyWatch = keyWatch
			},
			init (method, id,tabName) {
				this.tabName = tabName
				this.method = method
				console.log('method', method)
				this.inputForm = {
					auditId1: '',
					auditId2: '',
					auditId3: '',
					id: '',
					contractName:'',
					contractAmount:'',
					primaryLinkman:'',
					contractTypeFirst:'',
					contractType:'',
					projectName:'',
					projectPlace:'',
					buildPlace:'',
					projectLeader:'',
					createByName:'',
					createTime:'',
					initialAuditOpinion:'',
					initialEditOpinion:'',
					auditOpinion:'',
					editOpinion:'',
					workAttachmentDtoList:[],
					auditPeople:'',
					status:'',
					projectId:'',
					no:'',
					auditLevel:''
				}
				this.inputForm.id = id
				this.visible = true
				if (method === 'add') {
					// this.inputForm.id = 'add'
					this.title = `新建项目`
				} else if (method === 'edit') {
					this.title = '修改项目'
				} else if (method === 'view') {
					this.title = '查看项目详情'
				}
				this.loading = false
				this.$nextTick(() => {
					this.$refs.inputForm.resetFields()
					this.loading = true
					this.$refs.uploadComponent.clearUpload()
					// this.$refs.archiveFile.clearUpload()
					console.log('id',this.inputForm.id)
						this.projectInfoService.findByIdAudit(this.inputForm.id).then((data) => {
							this.inputForm = this.recover(this.inputForm, data)
							this.inputForm.contractName=data.jyProjectList.contractName
							this.inputForm.contractAmount=data.jyProjectList.contractAmount
							this.inputForm.primaryLinkman=data.jyProjectList.primaryLinkman
							this.inputForm.contractTypeFirst=data.jyProjectList.contractTypeFirst
							this.inputForm.contractType=data.jyProjectList.contractType
							this.inputForm.projectName=data.jyProjectList.name
							this.inputForm.projectPlace=data.jyProjectList.place
							this.inputForm.buildPlace=data.jyProjectList.buildPlace
							this.inputForm.createByName=data.jyProjectList.createById
							this.inputForm.createTime=data.jyProjectList.createTime
							this.inputForm.projectLeader=data.jyProjectList.leader
							this.inputForm.projectId=data.projectId
							this.inputForm.auditLevel=data.auditLevel
							this.inputForm.no = data.jyProjectList.no
							console.log('this.inputForm',this.inputForm)
							if (this.commonJS.isNotEmpty(this.inputForm.auditPeople)){
								this.isEdit=true
							}

							if (this.commonJS.isNotEmpty(data.initialAuditOpinion)){
								this.inputForm.initialAuditOpinion=data.initialAuditOpinion.replace(/<br\/>/g, "\n")
							}
							if (this.commonJS.isNotEmpty(data.initialEditOpinion)){
								this.inputForm.initialEditOpinion=data.initialEditOpinion.replace(/<br\/>/g, "\n")
							}
							console.log('data', data)
							if (this.status === 'audit' || this.status === 'taskFormDetail') {
								method = 'view'
							}
							console.log('method2',method)
							this.$refs.uploadComponent.newUpload(method, this.inputForm.workAttachmentDtoList, 'jyProjectAudit', null, null, null, null, false)
							// this.$refs.archiveFile.newUpload('view', [], 'cwWorkContract', null, null, null, null, false)
							this.loading = false
						})
				})
			},
			close () {
				this.$refs.uploadComponent.clearUpload()
				this.inputForm = {
					id1: '',
					id2: '',
					id3: '',
					id: '',
					contractName:'',
					contractAmount:'',
					primarkLinkman:'',
					contractTypeFirst:'',
					contractType:'',
					projectName:'',
					projectPlace:'',
					buildPlace:'',
					projectLeader:'',
					createByName:'',
					createTime:'',
					initialAuditOpinion:'',
					initialEditOpinion:'',
					auditOpinion:'',
					editOpinion:'',
					workAttachmentDtoList:[],
					status:'',
				}
				this.$refs.inputForm.resetFields()
				this.visible = false
			},
			reapplyForm (callback) {
				this.loading = true
				this.projectInfoService.findByIdAudit(this.inputForm.id).then((data) => {
					if (data.status !== '4') { // 审核状态不是“驳回”，就弹出提示
						this.loading = false
						this.$message.error('任务数据已发生改变或不存在，请在待办任务中确认此任务是否存在')
						throw new Error('任务数据已发生改变或不存在，请在待办任务中确认此任务是否存在')
					} else {
						this.startFormTrue(callback)
					}
				})
			},
			saveForm (callback) {
				this.doSubmit('save', callback)
			},
			startForm (callback) {
				this.loading = true
				if (this.commonJS.isNotEmpty(this.inputForm.id)) {
					this.projectInfoService.findByIdAudit(this.inputForm.id).then((data) => {
						if (this.commonJS.isNotEmpty(data.status) && data.status !== '0' && data.status !== '1' && data.status !== '3' && data.status !== '6') { // 审核状态不是“未发起”或“暂存”或“撤回”或"超期未发起"，就弹出提示
							this.loading = false
							this.$message.error('任务数据已发生改变或不存在，请在待办任务中确认此任务是否存在')
							throw new Error('任务数据已发生改变或不存在，请在待办任务中确认此任务是否存在')
						} else {
							this.startFormTrue(callback)
						}
					})
				} else {
					this.startFormTrue(callback)
				}
			},
			startFormTrue (callback) {
				this.doSubmit('start', callback)
			},
			async agreeForm (callback) {
				this.loading = true
				await this.projectInfoService.findByIdAudit(this.inputForm.id).then((data) => {
					if (data.status !== '2') { // status的值不等于“审核中”，就弹出提示
						this.loading = false
						this.$message.error('任务数据已发生改变或不存在，请在待办任务中确认此任务是否存在')
						throw new Error()
					} else {
						this.doSubmit('agree', callback)
					}
				})
			},
			// 表单提交
			async doSubmit (status, callback) {
				this.loading = true
				if (await this.$refs.uploadComponent.checkProgress()) {
					this.loading = false
					throw new Error()
				}
				if (status === 'save') {
					// 暂存
					this.loading = true
					this.inputForm.status = '1'
					this.inputForm.workAttachmentDtoList = this.$refs.uploadComponent.getDataList()
					if (this.commonJS.isEmpty(this.inputForm.auditPeople)) {
						this.$message.error('请选择审核人')
						this.loading = false
						throw new Error('请选择审核人')
					}
					this.projectInfoService.saveFormThree(this.inputForm).then((data) => {
						this.inputForm.assignee = this.inputForm.auditPeople
						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.status = '5'
					this.inputForm.auditDate = this.moment(new Date()).format('YYYY-MM-DD HH:mm:ss')
				}
				this.$refs['inputForm'].validate((valid) => {
					if (valid) {
						this.loading = true
						this.inputForm.workAttachmentDtoList = this.$refs.uploadComponent.getDataList()
						if (this.commonJS.isEmpty(this.inputForm.auditPeople)) {
							this.$message.error('请选择审核人')
							this.loading = false
							throw new Error('请选择审核人')
						}
						this.projectInfoService.saveFormThree(this.inputForm).then((data) => {
							this.inputForm.assignee = this.inputForm.auditPeople
							this.inputForm.id = data.businessId
							let processTitle = ''
							if (this.inputForm.auditLevel === '1'){
								processTitle = `${this.$store.state.user.name} 发起了 ${this.inputForm.projectName} - ${this.inputForm.no} ` + `[嘉溢-一级校审]`
							}else if (this.inputForm.auditLevel === '2'){
								processTitle = `${this.$store.state.user.name} 发起了 ${this.inputForm.projectName} - ${this.inputForm.no} ` + `[嘉溢-二级校审]`
							}else if (this.inputForm.auditLevel === '3'){
								processTitle = `${this.$store.state.user.name} 发起了 ${this.inputForm.projectName} - ${this.inputForm.no} ` + `[嘉溢-三级校审]`
							}
							this.inputForm.title = processTitle
							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 (await this.$refs.uploadComponent.checkProgress()) {
					this.loading = false
					throw new Error()
				} else {
					if (type === 'reject' || type === 'reback') {
						this.projectInfoService.findByIdAudit(this.inputForm.id).then((data) => {
							if (data.status !== '2') { // status的值不等于“审核中”，就弹出提示
								this.loading = false
								this.$message.error('任务数据已发生改变或不存在，请在待办任务中确认此任务是否存在')
								throw new Error()
							} else {
								// if (type === 'agree') {
								//   // 同意
								//   this.inputForm.status = '5'
								// }
								if (type === 'reject') {
									// 驳回
									this.inputForm.status = '4'
									this.loading = true
									this.inputForm.workAttachmentDtoList = this.$refs.uploadComponent.getDataList()
									this.projectInfoService.saveFormThree(this.inputForm).then((data) => {
										this.loading = false
									}).catch(() => {
										this.loading = false
									})
									// return
								}
								if (type === 'reback') {
									// 撤回
									this.inputForm.status = '3'
								}
								if (type === 'reject' || type === 'reback') {
									let param = {status: this.inputForm.status, id: this.inputForm.id}
									this.loading = true
									this.projectInfoService.updateStatusByAuditId(param).then(() => {
										this.loading = false
										callback()
									})
								}
							}
						})
					} else if (type === 'hold') {
						this.projectInfoService.findByIdAudit(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.id}
								this.projectInfoService.updateStatusByAuditId(param).then(() => {
									this.loading = false
									callback()
								})
							}
						})
					}
				}
			},
			// 下载一级校审
			downloadFirstAuditTpl () {
				// this.$utils.downloadExcel('/sys/project/import/template')
				this.loading = true
				if (this.tabName === '一级校审') {
					this.projectInfoService.downloadFirstAuditTpl(this.inputForm.id, this.inputForm.projectId).then((res) => {
						console.log('hh',res)
						// 将二进制流文件写入excel表，以下为重要步骤
						this.tabName=this.inputForm.projectName+" 报告质量内审单"
						this.$utils.downloadWord(res, this.tabName)
						this.loading = false
					}).catch(function (err) {
						this.loading = false
						if (err.response) {
							console.log(err.response)
						}
					})
				}
				if (this.tabName === '二级校审') {
					this.projectInfoService.downloadFirstAuditTpl(this.inputForm.id, this.inputForm.projectId).then((res) => {
						// 将二进制流文件写入excel表，以下为重要步骤
						this.tabName=this.inputForm.projectName+" 报告质量内审单"
						this.$utils.downloadWord(res, this.tabName)
						this.loading = false
					}).catch(function (err) {
						this.loading = false
						if (err.response) {
							console.log(err.response)
						}
					})
				}
				if (this.tabName === '三级校审') {
					this.projectInfoService.downloadFirstAuditTpl(this.inputForm.id, this.inputForm.projectId).then((res) => {
						// 将二进制流文件写入excel表，以下为重要步骤
						this.tabName=this.inputForm.projectName+" 报告质量内审单"
						this.$utils.downloadWord(res, this.tabName)
						this.loading = false
					}).catch(function (err) {
						this.loading = false
						if (err.response) {
							console.log(err.response)
						}
					})
				}
			},

		}
	}
</script>
<style lang="scss">
	.el-input-number .el-input__inner {
		text-align: left;
	}
	.el-divider__text {
		font-size: 16px;
		font-weight: bold;
	}

</style>
