<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>
			<span v-if="showFlag" style="color: red">若对欠款金额无疑问，请在点击确认后，到财务侯媛媛处结清欠款！（仅可用现金）</span>
			<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-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="disabledTest"
					 label-width="100px" @submit.native.prevent>
				<el-row :gutter="26">
					<el-col :span="12">
						<el-form-item label="社保款项欠款金额" prop="imprestAmountArrears" :rules="[{required: true, message: '社保款项欠款金额不能为空', trigger: 'blur'}]">
							<el-input v-model="inputForm.imprestAmountArrears" maxlength="15" @input="inputForm.imprestAmountArrears = twoDecimalPlaces(inputForm.imprestAmountArrears)"></el-input>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
		</el-form>
	</div>
</template>

<script>
	import SelectTree from '@/components/treeSelect/treeSelect.vue'
	import handoverSocialSecurityService from '@/api/human/depart/HandoverSocialSecurityService'
	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 {
				disabledTest: false,
				showFlag: false,
				title: '',
				method: '',
				visible: false,
				loading: false,
				inputForm: {
					userId: '',
					name: '',
					imprestAmountArrears: '',
					procInsId: '',
					assigneeId: '',
					type: '',
					department: '',
					handoverId: '',
				},
				keyWatch: '',
			}
		},
		commonApi: null,
		created () {
			this.commonApi = new CommonApi()
		},
		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: {
			getKeyWatch (keyWatch) {
				this.keyWatch = keyWatch
			},
			getUpload () {
			},
			init (method, id) {
				this.method = method
				this.inputForm = {
					userId: '',
					name: '',
					imprestAmountArrears: '',
					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
					handoverSocialSecurityService.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.determineDisabled()

						this.loading = false
					})
				})
			},
			// 表单提交
			doSubmit () {
				this.loading = true
				this.inputForm.id = this.businessId
				handoverSocialSecurityService.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
				handoverSocialSecurityService.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'
				handoverSocialSecurityService.save(this.inputForm).then((data) => {
					this.inputForm.title = `${this.$store.state.user.name} 发起了 [社保款项检查]`
					this.inputForm.id = data.businessId
					callback(data.businessTable, data.businessId, this.inputForm)
					this.loading = false
				}).catch(() => {
					this.loading = false
				})
			},

			// 通过
			async agreeForm (callback) {
				this.loading = true
				handoverSocialSecurityService.findById(this.inputForm.id).then((data) => {
					if (data.type !== '2') { // 审核状态不是“审核中”，就弹出提示
						this.loading = false
						this.$message.error('任务数据已发生改变或不存在，请在待办任务中确认此任务是否存在')
						throw new Error('任务数据已发生改变或不存在，请在待办任务中确认此任务是否存在')
					} else {

						this.$refs['inputForm'].validate(async (valid) => {
							if (valid) {
								this.loading = true
								if (!this.inputForm.imprestAmountArrears) {
									this.loading = false
									this.$message.error('社保款项欠款金额不能为空')
									throw new Error('社保款项欠款金额不能为空')
								}
								this.commonApi.getTaskNameByProcInsId(this.inputForm.procInsId).then((data) => {
									if (this.commonJS.isNotEmpty(data)) {
										if (data === '欠款回收') {
											this.inputForm.type = '5'
										}
										if (data === '社保账款检查' || data === '欠款校核') {
											this.inputForm.assignee = this.inputForm.assigneeId
										}
									}
									if (data === '社保账款检查' & this.inputForm.imprestAmountArrears === '0') {
										this.inputForm.type = '5'
									}
									handoverSocialSecurityService.save(this.inputForm).then((data) => {
										callback(data.businessTable, data.businessId, this.inputForm)
										this.loading = false
									}).catch(() => {
										this.loading = false
									})
								})
							}
						})

					}
				})
			},
			// 修改状态
			updateStatusById (type, callback) {
				if (this.inputForm.imprestAmountArrears != 0) {
					if (type === 'reject') {
						this.loading = true
						handoverSocialSecurityService.findById(this.inputForm.id).then((data) => {
							if (data.type !== '2') { // 审核状态不是“审核中”，就弹出提示
								this.loading = false
								this.$message.error('任务数据已发生改变或不存在，请在待办任务中确认此任务是否存在')
								throw new Error('任务数据已发生改变或不存在，请在待办任务中确认此任务是否存在')
							} else {
								this.commonApi.getTaskNameByProcInsId(this.inputForm.procInsId).then((data) => {
									if (this.commonJS.isNotEmpty(data)) {
										if (data === '社保账款检查') {
											this.inputForm.type = '4'
										} else {
											this.inputForm.type = '2'
										}
									}
								})
								handoverSocialSecurityService.updateStatusById(this.inputForm).then(() => {
									this.loading = false
									callback()
								})
							}
						})
					} else if (type === 'hold') {
						this.loading = true
						handoverSocialSecurityService.findById(this.inputForm.id).then((data) => {
							if (data.type !== '4') { // 审核状态不是“驳回”，就弹出提示
								this.loading = false
								this.$message.error('任务数据已发生改变或不存在，请在待办任务中确认此任务是否存在')
								throw new Error('任务数据已发生改变或不存在，请在待办任务中确认此任务是否存在')
							} else {
								this.inputForm.type = '1'
								handoverSocialSecurityService.updateStatusById(this.inputForm).then(() => {
									this.loading = false
									callback()
								})
							}
						})
					}
				} else {
					callback()
				}

			},
			twoDecimalPlaces (num) {
				let str = num.toString()
				str=str.replace(/\s+/g,"");
				var len1 = str.substr(0, 1)
				var len2 = str.substr(1, 1)
				// eslint-disable-next-line eqeqeq
				if (str.length > 1 && len1 == 0 && len2 != '.') {
					str = str.substr(1, 1)
				}
				// eslint-disable-next-line eqeqeq
				if (len1 == '.') {
					str = ''
				}
				// eslint-disable-next-line eqeqeq
				if (str.indexOf('.') != -1) {
					var str_ = str.substr(str.indexOf('.') + 1)
					// eslint-disable-next-line eqeqeq
					if (str_.indexOf('.') != -1) {
						str = str.substr(0, str.indexOf('.') + str_.indexOf('.') + 1)
					}
					if (str_.length > 2) {
						this.$message.warning(`金额小数点后只能输入两位，请正确输入！`)
						return (str = '')
					}
				}
				// eslint-disable-next-line no-useless-escape
				str = str.replace(/[^\d^\.]+/g, '') // 保留数字和小数点
				return str
			},
			determineDisabled(){
				this.commonApi.getTaskNameByProcInsId(this.inputForm.procInsId).then((data) => {
					if (this.commonJS.isNotEmpty(data)) {
						console.log('data', data)
						if (data === '欠款回收' || data === '欠款确认') {
							this.disabledTest = true
						} else if (data === '社保账款检查' || data === '欠款校核') {
							if (this.status !== 'audit' ){
								this.disabledTest = true
							}else {
								this.disabledTest = false
							}
						}
						if (data === '欠款确认') {
							this.showFlag = true;
						} else {
							this.showFlag = false;
						}
					} else {
						this.disabledTest = true
					}

				})
			}
		}
	}
</script>
