<template xmlns:v-slot="http://www.w3.org/1999/XSL/Transform">
	<div>
		<el-dialog
			:title="title"
			:close-on-click-modal="false"
			draggable
			width="1400px"
			height="500px"
			@close="close"
			append-to-body
			v-model="visible">
			<el-row>
				<el-row>
					<el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''" :disabled="status === 'audit' || status === 'taskFormDetail'|| method==='view'"
							 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="workName" :rules="[]">
									<el-input v-model="inputForm.workName"></el-input>
								</el-form-item>
							</el-col>
							<el-col :span="12">
								<el-form-item label="开始时间" prop="startTime" :rules="[{required: true, message: '开始时间不能为空', trigger: 'blur'}]">
									<el-date-picker
										style="width: 100%;"
										v-model="inputForm.startTime"
										type="date"
										value-format="YYYY-MM-DD"
										placeholder="选择开始时间">
									</el-date-picker>
								</el-form-item>
							</el-col>
							<el-col :span="12">
								<el-form-item label="结束时间" prop="endTime" :rules="[{required: true, message: '结束时间不能为空', trigger: 'blur'}]">
									<el-date-picker
										style="width: 100%;"
										v-model="inputForm.endTime"
										type="date"
										value-format="YYYY-MM-DD"
										placeholder="选择开始时间">
									</el-date-picker>
								</el-form-item>
							</el-col>
							<el-col :span="12">
								<el-form-item label="实习天数" prop="internshipDays" :rules="[]">
									<el-input
										v-on:input="inputForm.internshipDays=inputForm.internshipDays.replace(/[^\d.]/g,'')
							  .replace(/^\./g,'')
							  .replace(/\.{2,}/g,'.')
							  .replace('.','$#$').replace(/\./g,'').replace('$#$','.')
							  .replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3')"
										v-model="inputForm.internshipDays"
										controls-position="right"
										:controls="false"
										style="width:100%;"
										placeholder="请输入实习天数"
										clearable
									>
									</el-input>
								</el-form-item>
							</el-col>
							<el-col :span="12">
								<el-form-item label="项目经理" prop="projectManager"
											  :rules="[
								  	{required: true, message: '项目经理不能为空', trigger: 'change'}
                   ]">
<!--									<UserSelectSignatory style="width: 100%" ref="userSelect1" :disabled="status === 'audit' || status === 'taskFormDetail'"-->
<!--														 :readonly="true" :limit='1' :modelValue="inputForm.projectManager"-->
<!--														 @update:modelValue='(value, label) => {inputForm.projectManager = value}'></UserSelectSignatory>-->
									<UserSelect2 :limit='1' :modelValue="inputForm.projectManager" @update:modelValue='(value, label) => {inputForm.projectManager = value}'></UserSelect2>
								</el-form-item>
							</el-col>
						</el-row>
						<el-row :gutter="26">
							<el-col :span="12">
								<el-form-item label="备注" prop="remarks" :rules="[]">
									<el-input type="textarea"
											  maxlength="200" placeholder="请输入备注"
											  show-word-limit v-model="inputForm.remarks"></el-input>
								</el-form-item>
							</el-col>
						</el-row>
						<el-divider content-position="left"><i class="el-icon-document"></i>
							日志明细
							<el-button style="margin-left: 20px" type="primary" :disabled="method==='view' || status === 'audit' || status === 'taskFormDetail' " @click="insertEvent('amount')" plain>
								新增
							</el-button>
						</el-divider>
						<el-row  :gutter="15" >
							<el-col :span="24">
								<vxe-table
									border
									show-overflow
									ref="amountTable"
									class="vxe-table-element"
									:data="inputForm.amountInfos"
									style="margin-left: 5em"
									:key="amountKey"
									highlight-current-row
									:edit-rules="tableRules"
									:edit-config="{trigger: 'click', mode: 'row', showStatus: true, autoClear: true, icon:'_'}"
								>
									<vxe-table-column field="year" title="年" :edit-render="{}">
										<template v-slot:edit="scope">
											<el-input oninput ="value=value.replace(/\D|^/g,'')" maxlength="4" v-model="scope.row.year" ></el-input>
										</template>
									</vxe-table-column>
									<vxe-table-column field="month" title="月" :edit-render="{}">
										<template v-slot:edit="scope">
											<el-input oninput ="value=value.replace(/\D|^/g,'')" maxlength="4" v-model="scope.row.month" ></el-input>
										</template>
									</vxe-table-column>
									<vxe-table-column field="day" title="日" :edit-render="{}">
										<template v-slot:edit="scope">
											<el-input oninput ="value=value.replace(/\D|^/g,'')" maxlength="4" v-model="scope.row.day" ></el-input>
										</template>
									</vxe-table-column>
									<vxe-table-column field="week" title="星期" :edit-render="{}">
										<template v-slot:edit="scope">
											<el-input v-model="scope.row.week" ></el-input>
										</template>
									</vxe-table-column>
									<vxe-table-column field="auditProjectUnitName" title="审计项目单位名称" :edit-render="{}">
										<template v-slot:edit="scope">
											<el-input v-model="scope.row.auditProjectUnitName" ></el-input>
										</template>
									</vxe-table-column>
									<vxe-table-column field="location" title="地点" :edit-render="{}">
										<template v-slot:edit="scope">
											<el-input v-model="scope.row.location" @focus="locationListForm(scope.$rowIndex)"></el-input>
										</template>
									</vxe-table-column>
									<vxe-table-column field="auditWorkContent" title="审计工作内容" :edit-render="{}">
										<template v-slot:edit="scope">
											<el-input v-model="scope.row.auditWorkContent" ></el-input>
										</template>
									</vxe-table-column>
									<vxe-table-column title="操作" width="100">
										<template v-slot="scope">
											<el-button size="small" type="danger" @click="removeEvent(scope.row,scope.$rowIndex,'amount')">删除</el-button>
										</template>
									</vxe-table-column>
								</vxe-table>
							</el-col>
						</el-row>

					</el-form>
				</el-row>
			</el-row>
			<template #footer>
			<span class="dialog-footer">
			  <el-button @click="close()" icon="el-icon-circle-close">关闭</el-button>
			  <el-button  v-if="method === 'edit'" type="primary" icon="el-icon-circle-check" @click="doSubmit()">确定</el-button>
			</span>
			</template>
		</el-dialog>
	</div>
	<LocationPullForm ref="locationPullForm" @getProgramForUser="getProgramForUser"></LocationPullForm>
</template>

<script>
	import workLogService from '@/api/human/register/WorkLogService'
	import UserSelect2 from '@/components/userSelect'
	import LocationPullForm from './LocationPullForm'

	export default {
		props: {
			businessId: {
				type: String,
				default: ''
			},
			formReadOnly: {
				type: Boolean,
				default: false
			},
			status: {
				type: String,
				default: ''
			}
		},
		data () {
			return {
				tableRules: {
					year: [
						{ required: true, message: '请填写“年”' }
					],
					month: [
						{ required: true, message: '请填写“月”' }
					],
					day: [
						{ required: true, message: '请填写“日”' }
					],
					week: [
						{ required: true, message: '请填写“星期”' }
					],
					auditProjectUnitName: [
						{ required: true, message: '请填写“审计项目单位名称”' }
					],
					location: [
						{ required: true, message: '请填写“地点”' }
					]
				},
				title: '',
				method: '',
				visible: false,
				loading: false,
				inputForm: {
					userId: '',
					workName: '',
					startTime: '',
					endTime: '',
					internshipDays: '',
					remarks: '',
					projectManager: '',
					amountInfos: [],
				},
				keyWatch: '',
				amountKey: ''
			}
		},
		created () {
		},
		components: {
			LocationPullForm,
			UserSelect2,
		},
		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: '',
					workName: '',
					startTime: '',
					endTime: '',
					internshipDays: '',
					remarks: '',
					projectManager: '',
					amountInfos: [],
				}
				if (method === 'add') {
					this.title = `新建实习工作日志`
				} else if (method === 'edit') {
					this.title = '修改实习工作日志'
				} else if (method === 'view') {
					this.title = '查看实习工作日志'
				}
				this.inputForm.id = id
				this.visible = true
				this.loading = false
				this.$nextTick(() => {
					this.loading = true
					this.$refs.inputForm.resetFields()
					workLogService.findById(this.inputForm.id).then((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.inputForm.amountInfos.forEach((item, index) => {
							console.log('item', item)
							if (this.commonJS.isNotEmpty(item.checkType)) {
								if (item.checkType === '1') {
									this.inputForm.amountInfos[index].location = '所内'
								}
							}
						});

						this.loading = false
					})
				})
			},
			// 表单提交
			doSubmit () {
				this.$refs['inputForm'].validate((valid) => {
					if (valid) {
						this.loading = true

						if (this.inputForm.amountInfos.length < this.inputForm.internshipDays) {
							this.loading = false
							this.$message.error('日志明细行数要与实习天数一致')
							throw new Error('日志明细行数要与实习天数一致')
						}
						this.inputForm.amountInfos.forEach((item, index) => {
							if (this.commonJS.isEmpty(item.year)) {
								this.loading = false
								this.$message.warning('日志明细中第 ' + (index + 1) + ' 条数据的 “年” 为空')
								throw new Error()
							}
							if (this.commonJS.isEmpty(item.month)) {
								this.loading = false
								this.$message.warning('日志明细中第 ' + (index + 1) + ' 条数据的 “月” 为空')
								throw new Error()
							}
							if (this.commonJS.isEmpty(item.day)) {
								this.loading = false
								this.$message.warning('日志明细中第 ' + (index + 1) + ' 条数据的 “日” 为空')
								throw new Error()
							}
							if (this.commonJS.isEmpty(item.week)) {
								this.loading = false
								this.$message.warning('日志明细中第 ' + (index + 1) + ' 条数据的 “星期” 为空')
								throw new Error()
							}
							if (this.commonJS.isEmpty(item.auditProjectUnitName)) {
								this.loading = false
								this.$message.warning('日志明细中第 ' + (index + 1) + ' 条数据的 “审计项目单位名称” 为空')
								throw new Error()
							}
							if (this.commonJS.isEmpty(item.location)) {
								this.loading = false
								this.$message.warning('日志明细中第 ' + (index + 1) + ' 条数据的 “地点” 为空')
								throw new Error()
							}
							// 在这里执行对每个元素的操作
						});

						workLogService.save(this.inputForm).then((data) => {
							this.close()
							this.$message.success(data)
							this.$emit('refreshList')
							this.loading = false
						}).catch(() => {
							this.loading = false
						})
					}
				})
			},
			close () {
				this.$refs.inputForm.resetFields()
				this.visible = false
			},
			// 新增
			async insertEvent (type) {
				if (type === 'amount') {
					let d = {}
					if (this.commonJS.isEmpty(this.inputForm.amountInfos)) {
						this.inputForm.amountInfos = []
					}
					this.$refs.amountTable.insertAt(d)
					this.inputForm.amountInfos.push(d)
					this.amountKey = Math.random()
				}
			},
			// 删除
			removeEvent (row, rowIndex, type) {
				if (type === 'amount') {
					this.$refs.amountTable.remove(row)
					this.inputForm.amountInfos.splice(rowIndex, 1)
				}
			},
			// 地点下拉弹窗
			locationListForm (rowIndex) {
				this.indexRow = rowIndex
				this.$refs.locationPullForm.init()
			},
			getProgramForUser (rows) {

				if (rows.bj1 == 'bj1') {
					this.inputForm.amountInfos[this.indexRow].location = '所内'
					this.inputForm.amountInfos[this.indexRow].checkType = rows.checkType
				} else {
					this.inputForm.amountInfos[this.indexRow].locationId = rows[0].id
					this.inputForm.amountInfos[this.indexRow].location = rows[0].name
					this.inputForm.amountInfos[this.indexRow].checkType = rows.checkType
				}
				this.$forceUpdate()
				this.indexRow = ''
			},
		}
	}
</script>
