<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="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="createName"
								  :rules="[
								]">
						<el-input v-model="inputForm.createName" placeholder="请填写发起人" readonly="true" disabled="true"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="所属部门" prop="officeName"
								  :rules="[
								  ]">
						<el-input v-model="inputForm.officeName" placeholder="请填写所属部门" readonly="true" disabled="true"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="发起时间" prop="createTime"
								  :rules="[
								  ]">
						<el-date-picker
							style="width:100%;"
							v-model="inputForm.createTime"
							type="date"
							readonly="true"
							disabled="true"
							format="YYYY-MM-DD"
							value-format="YYYY-MM-DD"
							placeholder="请选择日期"
							:size="default"
							:default-value="new Date()"
						/>
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item label="备注" prop="reason"
								  :rules="[]">
						<el-input :disabled="status === 'audit' || status === 'taskFormDetail' || method === 'view'" type="textarea" placeholder="请输入备注"  maxlength="500"  style="width:100%" v-model="inputForm.reason" show-word-limit></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="15" v-if="status === 'audit' || status === 'taskFormDetail'">
				<el-col :span="12">
					<el-form-item label="是否通过" prop="isPass"
								  :rules="[{required: true, message:'请选择是否通过', trigger:'blur'}
								]">
						<el-radio-group v-model="inputForm.isPass" :disabled="!isBmzr && (status === 'audit' || status === 'taskFormDetail') ">
							<el-radio label="0"  >通过</el-radio> <el-radio label="1" >不通过</el-radio>
						</el-radio-group>
					</el-form-item>
				</el-col>
				<el-col :span="12" v-show="inputForm.isPass === '1'">
					<el-form-item label="是否留用" prop="isEmploy"
								  :rules="[{required: true, message:'请选择是否留用', trigger:'blur'}
								]">
						<el-radio-group v-model="inputForm.isEmploy" :disabled="!isBmzr && (status === 'audit' || status === 'taskFormDetail') ">
							<el-radio label="0"  >留用</el-radio> <el-radio label="1" >不留用</el-radio>
						</el-radio-group>
					</el-form-item>
				</el-col>
				<el-col :span="12" v-show="inputForm.isEmploy === '0'">
					<el-form-item label="是否顺延一个月" prop="isPostpone"
								  :rules="[{required: true, message:'请选择是否留用', trigger:'blur'}
								]">
						<el-radio-group v-model="inputForm.isPostpone" :disabled="!isBmzr && (status === 'audit' || status === 'taskFormDetail') ">
							<el-radio label="0"  >顺延</el-radio> <el-radio label="1" >不顺延</el-radio>
						</el-radio-group>
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<UpLoadComponent ref="uploadComponent"></UpLoadComponent>

	</div>
</template>

<script>
	import XEUtils from 'xe-utils'
	import EntryService from '@/api/jy/EntryService'
	import { ElDatePicker } from 'element-plus';
	import UpLoadComponent from '@/views/common/UpLoadComponent'
	import CommonApi from '@/api/cw/common/CommonApi'
	import userService from '@/api/sys/userService'
	export default {
		props: {
			businessId: {
				type: String,
				default: ''
			},
			formReadOnly: {
				type: Boolean,
				default: false
			},
			status: {
				type: String,
				default: ''
			}
		},
		data () {
			return {
				title: '',
				method: '',
				loading: false,
				isBmzr:false,
				inputForm: {
					createName:this.$store.state.user.name,
					officeName:this.$store.state.user.office.name,
					id: '',
					reason: '',
					createTime:this.moment(new Date()).format('YYYY-MM-DD'),
					procInsId: '',
					isPass:'',
					isEmploy:'',
					isPostpone:'',
					files:[]
				},
				baseKey: '',
				keyWatch: '',
				dateList:[],
				programRow:'',
				days:'',

			}
		},
		EntryService:null,
		commonApi: null,
		created () {
			this.entryService=new EntryService()
			this.commonApi = new CommonApi()
		},
		mounted () {
		},
		activated () {
		},
		components: {
			ElDatePicker,
			UpLoadComponent,
		},
		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
				this.inputForm={
					createName:'',
					officeName:'',
					id: '',
					reason: '',
					createTime:'',
					procInsId: '',
					isPass:'',
					isEmploy:'',
					isPostpone:'',
					files:[]
				}
				this.inputForm.id = id
				this.loading = false
				this.$nextTick(() => {
					this.$refs.inputForm.resetFields()
					this.loading = true
					// this.$refs.uploadComponent.clearUpload()
					if (this.commonJS.isNotEmpty(this.inputForm.id)) {
						console.log('this.inputForm.id', this.inputForm.id)
						this.entryService.queryByApplyId(this.inputForm.id).then((data)=>{
							this.inputForm = this.recover(this.inputForm, data)
							if ('5' === data.status){
								this.inputForm.reason = ''
								this.inputForm.files = []
								this.inputForm.isPass = ''
								this.inputForm.isEmploy = ''
								this.inputForm.isPostpone = ''
							}

							console.log('s',this.inputForm)
							if (this.commonJS.isEmpty(this.inputForm.createName)){
								this.inputForm.createName = this.$store.state.user.name
							}
							if (this.commonJS.isEmpty(this.inputForm.officeName)){
								this.inputForm.officeName = this.$store.state.user.office.name
							}
							if (this.commonJS.isEmpty(this.inputForm.createTime)){
								this.inputForm.createTime=this.moment(new Date()).format('YYYY-MM-DD')
							}
							if (this.commonJS.isEmpty(this.inputForm.files)){
								this.inputForm.files=[]
							}
							if (this.status === 'audit' || this.status === 'taskFormDetail') {
								method = 'view'
							}
							console.log('me',method)
							this.$refs.uploadComponent.newUpload(method, this.inputForm.files, 'regular',null,null,null,null,null)
							this.loading = false
						})
						userService.isBmzr().then((data)=>{
							this.isBmzr = data
						})

					}
				})
			},
			saveForm(callback) {
				this.doSubmit('save', callback)
			},
			startForm(callback) {
				this.doSubmit('start', callback)
				// this.loading = true
				// if (this.commonJS.isNotEmpty(this.inputForm.id)) {
				// 	this.entryService.queryByApplyId(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 {
				//
				// }
			},
			async agreeForm(callback) {
				console.log('进入方法')
				this.loading = true
				if (this.commonJS.isEmpty(this.inputForm.isPass)){
					this.loading = false
					this.$message.error('请选择是否通过')
					throw new Error()
				}else {
					if (this.inputForm.isPass === '1'){
						if (this.commonJS.isEmpty(this.inputForm.isEmploy)){
							this.loading = false
							this.$message.error('请选择是否留用')
							throw new Error()
						}else {
							if (this.inputForm.isEmploy === '0'){
								if (this.commonJS.isEmpty(this.inputForm.isPostpone)){
									this.loading = false
									this.$message.error('请选择是否顺延')
									throw new Error()
								}
							}
						}

					}
				}
				await this.entryService.queryByApplyId(this.inputForm.id).then((data) => {
					console.log('data',data.status)
					if (data.status !== '2') { // status的值不等于“审核中”，就弹出提示
						if (data.status === '4'){
							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.entryService.saveApplyForm(this.inputForm).then((item) => {
									console.log('this.inputForm',this.inputForm)
									callback(data.businessTable, data.businessId, this.inputForm,'',item.isPass,item.isEmploy)
									this.loading = false
								}).catch(() => {
									this.loading = false
								})
							})
						}else {
							this.loading = false
							this.$message.error('任务数据已发生改变或不存在，请在待办任务中确认此任务是否存在')
							throw new Error()
						}
					} else {
						this.inputForm.status= '2'
						this.inputForm.createTime = ''
						if (this.inputForm.isPass ==='0'){
							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.entryService.saveApplyForm(this.inputForm).then((item) => {
									callback(data.businessTable, data.businessId, this.inputForm,'',item.isPass,item.isEmploy)
									this.loading = false
								}).catch(() => {
									this.loading = false
								})
							})
						}else {
							if (this.inputForm.isEmploy === '0'){
								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.entryService.saveApplyForm(this.inputForm).then((item) => {
										callback(data.businessTable, data.businessId, this.inputForm,'',item.isPass,item.isEmploy)
										this.loading = false
									}).catch(() => {
										this.loading = false
									})
								})
							}else {
								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.entryService.saveApplyForm(this.inputForm).then((item) => {
										callback(data.businessTable, data.businessId, this.inputForm,'',item.isPass,item.isEmploy)
										this.loading = false
									}).catch(() => {
										this.loading = false
									})
								})
							}

						}

					}
				})
			},
			reapplyForm(callback) {
				this.loading = true
				this.entryService.queryByApplyId(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
				if (this.$refs.uploadComponent.checkProgress()) {
					this.loading = false
					return
				}
				console.log('5',this.inputForm)
				if (status === 'save') {
					// 暂存
					this.loading = true
					this.inputForm.status = '1'
					this.inputForm.createTime = ''
					this.inputForm.files = this.$refs.uploadComponent.getDataList()
					this.entryService.saveApplyForm(this.inputForm).then((data) => {
						callback(data.businessTable, data.businessId, this.inputForm,data.isPass,data.isEmploy)
						this.loading = false
					}).catch(() => {
						this.loading = false
					})
					return
				} else if (status === 'start') {
					this.inputForm.createTime = ''
					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.entryService.saveApplyForm(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
					}
				})
			},
			close() {
				this.inputForm = {
					id:'',
					projectList:[],
					name:'',
					no:'',
					projectLeader:'',
					projectCreateDate:'',
					createById:'',
					createByName:'',
					createDate:'',
					remarks:'',
					files: [], // 附件信息
					types:[], //用印类型
					number:'', //用印份数
					projectId:'',
				}
				this.visible = false
			},
			async updateStatusById(type, callback) {
				this.loading = true
				if (type === 'reject' || type === 'reback') {
					this.entryService.queryByApplyId(this.inputForm.id).then((data) => {
						if (data.status !== '2') { // status的值不等于“审核中”，就弹出提示
							if (data.status === '4'){
								this.inputForm.status = '4'
								let param = {status: this.inputForm.status, id: this.inputForm.id}
								this.entryService.updateStatusByApplyId(param).then(() => {
									this.loading = false
									callback()
								})
							}
							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.entryService.updateStatusByApplyId(param).then(() => {
									this.loading = false
									callback()
								})
							}
						}
					})
				} else if (type === 'hold') {
					this.entryService.queryByApplyId(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.entryService.saveApplyForm(this.inputForm).then(() => {
								this.loading = false
								callback(data.businessTable, data.businessId, this.inputForm)
							})
						}
					})
				}

			},

		}

	}
</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>
