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

			<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="riceNumber" :rules="[{required: true, message: '餐补卡号不能为空', trigger: 'blur'}]">
						<el-input v-model="inputForm.riceNumber"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
	</div>
</template>

<script>
	import enrollmentRegistrationService from '@/api/human/enrollment/EnrollmentRegistrationService'
	import OSSSerivce, {
		httpRequest,
		fileName,
		toHref,
	} from '@/api/sys/OSSService'

	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: {
					userId: '',
					riceNumber: '',
					procInsId: '',
				},
				keyWatch: '',
			}
		},
		created () {
			this.ossService = new OSSSerivce()
		},
		components: {
		},
		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: '',
					procInsId: '',
				}
				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
					enrollmentRegistrationService.findRiceCardById(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.loading = false
					})
				})
			},
			// 表单提交
			doSubmit () {
				this.$refs['inputForm'].validate((valid) => {
					if (valid) {
						this.loading = true
						this.inputForm.id = this.businessId
						enrollmentRegistrationService.saveRiceCard(this.inputForm).then((data) => {
							this.close()
							this.$message.success(data)
							this.$emit('refreshDataList')
							this.loading = false
						}).catch(() => {
							this.loading = false
						})
					}
				})
			},
			close () {
				this.$refs.inputForm.resetFields()
				this.fileList = []
				this.fileList2 = []
				this.visible = false
			},
			reapplyForm (callback) {
				this.loading = true
				enrollmentRegistrationService.findRiceCardById(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.$refs['inputForm'].validate((valid) => {
					if (valid) {
						this.loading = true
						this.inputForm.type = '2'
						enrollmentRegistrationService.saveRiceCard(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.$refs.inputForm.resetFields()
							this.loading = false
						}).catch(() => {
							this.$refs.inputForm.resetFields()
							this.loading = false
						})
					}
				})
			},

			// 通过
			async agreeForm (callback) {
				this.loading = true
				enrollmentRegistrationService.findRiceCardById(this.inputForm.id).then((data) => {
					if (data.type !== '2') { // 审核状态不是“审核中”，就弹出提示
						this.loading = false
						this.$message.error('任务数据已发生改变或不存在，请在待办任务中确认此任务是否存在')
						throw new Error('任务数据已发生改变或不存在，请在待办任务中确认此任务是否存在')
					} else {
						this.$refs['inputForm'].validate((valid) => {
							if (valid) {
								this.loading = true
								this.inputForm.type = '5'
								enrollmentRegistrationService.saveRiceCard(this.inputForm).then((data) => {
									callback(data.businessTable, data.businessId, this.inputForm)
									this.loading = false
								}).catch(() => {
									this.loading = false
								})
							}
						})
					}
				})
			},
			// 修改状态
			updateStatusById (type, callback) {
				if (type === 'reject') {
					this.loading = true
					enrollmentRegistrationService.findRiceCardById(this.inputForm.id).then((data) => {
						if (data.type !== '2') { // 审核状态不是“审核中”，就弹出提示
							this.loading = false
							this.$message.error('任务数据已发生改变或不存在，请在待办任务中确认此任务是否存在')
							throw new Error('任务数据已发生改变或不存在，请在待办任务中确认此任务是否存在')
						} else {
							this.inputForm.type = '4'
							enrollmentRegistrationService.updateRiceCardStatusById(this.inputForm).then(() => {
								this.loading = false
								callback()
							})
						}
					})
				} else if (type === 'hold') {
					this.loading = true
					enrollmentRegistrationService.findRiceCardById(this.inputForm.id).then((data) => {
						if (data.type !== '4') { // 审核状态不是“驳回”，就弹出提示
							this.loading = false
							this.$message.error('任务数据已发生改变或不存在，请在待办任务中确认此任务是否存在')
							throw new Error('任务数据已发生改变或不存在，请在待办任务中确认此任务是否存在')
						} else {
							this.inputForm.type = '1'
							enrollmentRegistrationService.updateRiceCardStatusById(this.inputForm).then(() => {
								this.loading = false
								callback()
							})
						}
					})
				}
			},
		}
	}
</script>
