<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':''" :disabled="status === 'taskFormDetail' "
				 label-width="160px" @submit.native.prevent>
			<el-divider content-position="left"><i class="el-icon-document"></i>
				基本信息
			</el-divider>
			<el-row  :gutter="15">
				<el-col :span="12">
					<el-form-item label="交接人" prop="heir" :rules="[
                        {required: true, message:'交接人不能为空', trigger:'change'}
               ]">
						<el-select :disabled="isLeader " v-model="inputForm.heir" placeholder="请选择交接人" style="width: 100%;" clearable>
							<el-option
								v-for="item in userList"
								:key="item.id"
								:label="item.name"
								:value="item.id">
							</el-option>
						</el-select>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row  :gutter="15">
				<el-divider content-position="left"><i class="el-icon-document"></i>
					项目进度信息
				</el-divider>
				<el-col :span="24">
					<vxe-table
						border
						:footer-method="footerMethod2"
						show-overflow
						show-footer
						ref="baseTable"
						:key="baseKey"
						class="vxe-table-element"
						:data="inputForm.processList"
						style="margin-left: 5em"
						@cell-click=""
						@edit-closed=""
						keep-source
						:readonly="true"
						highlight-current-row
						:edit-config="{trigger: 'click', mode: 'cell', showStatus: true, autoClear: true, icon: '-'}"
					>
						<vxe-table-column field="projectName" align="center" title="项目名称" :edit-render="{}" show-overflow="title">
							<template v-slot:edit="scope">
								<el-input  :disabled="true" placeholder="请填写项目名称" :readonly="true" v-model="scope.row.projectName"/>
							</template>
						</vxe-table-column>
						<vxe-table-column field="no" align="center" title="报告号" :edit-render="{}">
							<template v-slot:edit="scope">
								<el-input :disabled="true" :readonly="true" placeholder="请填写报告号" v-model="scope.row.no"/>
							</template>
						</vxe-table-column>
						<vxe-table-column  field="projectLeader" align="center" title="项目负责人" :edit-render="{}">
							<template v-slot:edit="scope">
								<el-input :disabled="true" placeholder="请填写项目负责人" v-model="scope.row.projectLeader"/>
							</template>
						</vxe-table-column>
						<vxe-table-column  field="officeName" align="center" title="部门" :edit-render="{}">
							<template v-slot:edit="scope">
								<el-input :disabled="true" placeholder="请填写部门" v-model="scope.row.officeName"/>
							</template>
						</vxe-table-column>
						<vxe-table-column  field="process" align="center" title="当前项目进度" :edit-render="{}">
							<template v-slot:edit="scope">
								<el-input :disabled="true" placeholder="请填写项目负责人" v-model="scope.row.process"/>
							</template>
						</vxe-table-column>
						<vxe-table-column field="processTime" align="center" title="进度完成时间" :edit-render="{}">
							<template v-slot:edit="scope">
								<el-input :disabled="true" placeholder="请填写创建时间" v-model="scope.row.processTime"/>
							</template>
						</vxe-table-column>
					</vxe-table>
				</el-col>
			</el-row>
		</el-form>
	</div>
</template>

<script>
	import XEUtils from 'xe-utils'
	import DimissionService from '@/api/jy/DimissionService'
	import { ElDatePicker } from 'element-plus';
	import SelectUserTree from '@/views/utils/treeUserSelect'
	import userService from "@/api/sys/userService";
	import CommonApi from '@/api/cw/common/CommonApi'
	// 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: '',
				loading: false,
				inputForm: {
					id: '',
					createById:'',
					heir:'',
					processList:[],
					procInsId:'',
				},
				baseKey: '',
				keyWatch: '',
				programRow:'',
				userList:[],
				isLeader:false,

			}
		},
		DimissionService:null,
		commonApi: null,
		created () {
			this.dimissionService=new DimissionService()
			this.commonApi = new CommonApi()
		},
		mounted () {
		},
		activated () {
		},
		components: {
			ElDatePicker,
			// UpLoadComponent,
			SelectUserTree,
		},
		computed: {
			bus: {
				get () {
					// this.$refs.uploadComponent.setDividerName('附件')
					return this.businessId
				},
				set (val) {
					this.businessId = val
				}
			}
		},
		watch: {
			'keyWatch': {
				handler (newVal) {
					if (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)
				}
			}
		},
		methods: {
			getKeyWatch(keyWatch) {
				this.keyWatch = keyWatch
			},
			init(method, id) {
				this.method = method
				console.log('method',this.method)
				this.inputForm={
					id: '',
					heir:'',
					processList:[],
					procInsId:'',
				}
				this.inputForm.id = id
				console.log('this.inputForm.id', this.status)
				this.loading = false
				this.$nextTick(() => {
					this.$refs.inputForm.resetFields()
					this.loading = true
					// this.$refs.uploadComponent.clearUpload()
						this.dimissionService.findHeirById(this.inputForm.id).then((data)=>{
							this.inputForm = this.recover(this.inputForm, data)
							userService.getUserByOffice(data.createById).then((da)=>{
								this.userList=da
							})
							this.commonApi.getTaskNameByProcInsId(this.inputForm.procInsId).then((da)=> {
								if (this.commonJS.isNotEmpty(da)) {
									if ((da === '部门主任审核' && this.status === 'audit') || ( this.status !== 'taskFormDetail' && this.status !== 'audit') ) {
										this.isLeader = false
									}else {
										if (this.status === 'taskFormDetail' || this.status === 'audit' || da !== '部门主任审核' || method === 'view'){
											this.isLeader = true
										}
									}
								}
							})
							if (this.commonJS.isEmpty(this.inputForm.createById)){
								this.inputForm.createById = data.createById
							}
							console.log('s',this.inputForm)
							// this.$refs.uploadComponent.newUpload(method, this.inputForm.files, 'monthly',null,null,null,null,null)
							this.loading = false
						})
				})
			},
			saveForm(callback) {
				this.doSubmit('save', callback)
			},
			startForm(callback) {
				this.loading = true
				if (this.commonJS.isNotEmpty(this.inputForm.id)) {
					this.dimissionService.findHeirById(this.inputForm.id).then((data) => {
						if (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) {
				console.log('进入方法')
				this.loading = true
				 this.dimissionService.findHeirById(this.inputForm.id).then((data) => {
					if (data.status !== '2' ) { // status的值不等于“审核中”，就弹出提示
						this.loading = false
						this.$message.error('任务数据已发生改变或不存在，请在待办任务中确认此任务是否存在')
						throw new Error()
					} else {
						this.inputForm.status= '2'
						this.commonApi.getTaskNameByProcInsId(this.inputForm.procInsId).then((da)=>{
							if (this.commonJS.isNotEmpty(da)) {
								if (da === '人事审核') {
									this.inputForm.status = '5'
								}
							}
							let param = {status: this.inputForm.status, id: this.inputForm.id}
							this.inputForm=JSON.stringify(this.inputForm)
							this.dimissionService.updateHeirStatusById(this.inputForm).then(() => {
								callback(data.businessTable, data.businessId, this.inputForm)
								this.loading = false
							}).catch(() => {
								this.loading = false
							})
						})
					}
				})
			},
			reapplyForm(callback) {
				this.loading = true
				this.dimissionService.findHeirById(this.inputForm.id).then((data) => {
					if (data.status !== '4') { // 审核状态不是“驳回”，就弹出提示
						this.loading = false
						this.$message.error('任务数据已发生改变或不存在，请在待办任务中确认此任务是否存在')
						throw new Error('任务数据已发生改变或不存在，请在待办任务中确认此任务是否存在')
					} else {
						this.doSubmit('reapply', callback)
					}
				})
			},
			// 表单提交
			async doSubmit(status, callback) {
				this.loading = true
				console.log('5',this.inputForm)
				if (status === 'save') {
					// 暂存
					this.loading = true
					this.inputForm.status = '1'
					// if (this.$refs.uploadComponent.checkProgress()) {
					// 	this.loading = false
					// 	return
					// }
					// this.inputForm.files = this.$refs.uploadComponent.getDataList()
					this.dimissionService.saveHeir(this.inputForm).then((data) => {
						callback(data.businessTable, data.businessId, this.inputForm,data.days)
						this.loading = false
					}).catch(() => {
						this.loading = false
					})
					return
				} else if (status === 'start') {

					if (this.commonJS.isEmpty(this.inputForm.heir)){
						this.$message.error('请选择交接人')
						this.loading = false
					}
					// if (this.$refs.uploadComponent.checkProgress()) {
					// 	this.loading = false
					// 	return
					// }
					// this.inputForm.files = this.$refs.uploadComponent.getDataList()
					// 送审  待审核
					this.inputForm.status = '2'
				} else if (status === 'agree') {
					this.inputForm.status= '5'
				} else if (status === 'reapply') {
					this.inputForm.status = '2'
				}
				this.$refs['inputForm'].validate((valid) => {
					if (valid) {
						this.loading = true
						console.log('form',this.inputForm)
						this.inputForm=JSON.parse(JSON.stringify(this.inputForm))
						this.dimissionService.saveHeir(this.inputForm).then((data) => {
							callback(data.businessTable, data.businessId, this.inputForm)
							this.$refs.inputForm.resetFields()
							this.loading = false
						}).catch(() => {
							this.$refs.inputForm.resetFields()
							this.loading = false
						})
					} else {
						this.loading = false
					}
				})
			},
			async updateStatusById(type, callback) {
				this.loading = true
				if (type === 'reject' || type === 'reback') {
					this.dimissionService.findHeirById(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'
							}
							if (type === 'reback') {
								// 撤回
								this.inputForm.status = '3'
							}
							if (type === 'reject' || type === 'reback') {
								let param = {status: this.inputForm.status, id: this.inputForm.id}
								this.dimissionService.updateHeirStatusById(param).then(() => {
									this.loading = false
									callback()
								})
							}
						}
					})
				} else if (type === 'hold') {
					this.dimissionService.findHeirById(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.dimissionService.updateHeirStatusById(param).then(() => {
								this.loading = false
								callback()
							})
						}
					})
				}

			},
			close() {
				this.inputForm = {
					id: '',
					heir:'',
					processList:[],
					procInsId:'',
				}
				this.visible = false
			},



		}

	}
</script>
<style scoped>
	/deep/ .el-input-number .el-input__inner {
		text-align: left;
	}
	/deep/ .vxe-footer--row .vxe-footer--column:nth-child(1) .vxe-cell--item {
		font-weight:700;
	}
</style>
