<template xmlns:v-slot="http://www.w3.org/1999/XSL/Transform">
	<div>
		<el-dialog
			:title="title"
			:close-on-click-modal="false"
			draggable
			width="1300px"
			@close="close"
			@keyup.enter.native=""
			v-model="visible">
		<el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method === 'view'"
				 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="officeName"
								  :rules="[]">
						<el-input v-model="inputForm.officeName" class="m-2" placeholder="请输入部门" clearable></el-input>

					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="企业名称" prop="businessName"
								  :rules="[]">
						<el-input v-model="inputForm.businessName" class="m-2" placeholder="请输入企业名称" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="项目名称" prop="project"
								  :rules="[]">
						<el-input v-model="inputForm.project" class="m-2" placeholder="请输入项目名称" clearable></el-input>
					</el-form-item>
				</el-col>

				<el-col :span="12">
					<el-form-item label="业务类型" prop="businessType"
								  :rules="[]">
						<el-select style="width:100%;" v-model="inputForm.businessType" class="m-2" placeholder="请选择" clearable>
							<el-option
								v-for="item in $dictUtils.getDictList('business_type')"
								:key="item.value"
								:label="item.label"
								:value="item.value">
							</el-option>

						</el-select>
					</el-form-item>
				</el-col>

				<el-col :span="12">
					<el-form-item label="报告文号类型" prop="reportNoType"
								  :rules="[]">
						<el-select style="width:100%;" @change="getReportNo()" v-model="inputForm.reportNoType" class="m-2" placeholder="请选择" clearable>
							<el-option
								v-for="item in $dictUtils.getDictList('report_no_type')"
								:key="item.value"
								:label="item.label"
								:value="item.value">
							</el-option>

						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="报告文号" prop="reportNo"
								  :rules="[{required: true, message:'报告文号不能为空', trigger:'blur'}]">
						<el-input @change="getFileBoxNumber()" v-model="inputForm.reportNo" class="m-2" placeholder="请输入报告文号" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="报告涵盖期间" prop="reportingPeriod"
								  :rules="[]">
						<el-input v-model="inputForm.reportingPeriod" class="m-2" placeholder="请输入报告涵盖期间" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="归档经办人" prop="fillingManager"
								  :rules="[]">
						<el-input v-model="inputForm.fillingManager" class="m-2" placeholder="请输入归档经办人" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="项目经理" prop="projectLeader"
								  :rules="[]">
						<el-input v-model="inputForm.projectLeader" class="m-2" placeholder="请输入项目经理" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="项目负责人" prop="projectMaster"
								  :rules="[]">
						<el-input v-model="inputForm.projectMaster" class="m-2" placeholder="请输入项目负责人" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="签字注册会计师" prop="signatureCpa"
								  :rules="[]">
						<el-input v-model="inputForm.signatureCpa" class="m-2" placeholder="请输入签字注册会计师" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="签字合伙人" prop="signatureParter"
								  :rules="[]">
						<el-input v-model="inputForm.signatureParter" class="m-2" placeholder="请输入签字合伙人" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="项目等级" prop="projectLevel"
								  :rules="[]">
						<el-input v-model="inputForm.projectLevel" class="m-2" placeholder="请输入项目等级" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="报告日期" prop="reportDate"
								  :rules="[]">
						<el-date-picker
							style="width: 100%"
							v-model="inputForm.reportDate"
							type="date"
							value-format="YYYY-MM-DD"
							placeholder="请选择日期"
							:clearable="true"
						/>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="盖章日期" prop="stampDate"
								  :rules="[]">
						<el-date-picker
							style="width: 100%"
							v-model="inputForm.stampDate"
							type="date"
							value-format="YYYY-MM-DD"
							placeholder="请选择日期"
							:clearable="true"
						/>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="归档日期" prop="archieveDate"
								  :rules="[]">
						<el-date-picker
							style="width: 100%"
							v-model="inputForm.archieveDate"
							type="date"
							value-format="YYYY-MM-DD"
							placeholder="请选择日期"
							:clearable="true"
						/>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="盒号" prop="fileBoxNumber"
								  :rules="[]">
						<el-input @blur="checkFileBoxNumber()" v-model="inputForm.fileBoxNumber" class="m-2" placeholder="请输入盒号" clearable></el-input>
						<span style="color: #E6A23C; font-weight: bold;">{{usedReportNo}}</span>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="盒数" prop="boxNumber"
								  :rules="[]">
						<el-input v-model="inputForm.boxNumber" class="m-2" placeholder="请输入盒数" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="本数" prop="bookNumbers"
								  :rules="[]">
						<el-input v-model="inputForm.bookNumbers" class="m-2" placeholder="请输入本数" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="合同原件" prop="originalContract"
								  :rules="[]">
						<el-input v-model="inputForm.originalContract" class="m-2" placeholder="请输入合同原件" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="资产总额(万元)" prop="totalAssets"
								  :rules="[]">
						<el-input v-model="inputForm.totalAssets" v-on:input="inputForm.totalAssets = inputForm.totalAssets.replace(/[^\d.]/g, '')
                      .replace(/^\./g, '')
                      .replace(/\.{2,}/g, '.')
                      .replace('.', '$#$').replace(/\./g, '').replace('$#$', '.')
                      .replace(/(?<=\.\d{6})\d+/g, '')
                      .replace(/^(\-)*(\d+)\.(\d{6}).*$/, '$1$2.$3').replace(/^0+/, '0')" class="m-2" placeholder="请输入资产总额" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="存放地点(区位码)" prop="storagePlaces"
								  :rules="[]">
						<el-select multiple style="width:100%;" v-model="inputForm.storagePlaces" class="m-2" placeholder="请选择" clearable>
							<el-option
								v-for="item in $dictUtils.getDictList('storage_place')"
								:key="item.value"
								:label="item.label"
								:value="item.value">
							</el-option>

						</el-select>
						<span style="color: #E6A23C; font-weight: bold;">{{storagePlace}}</span>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="存放地点(特殊)" prop="specialPlace"
								  :rules="[]">
						<el-input v-model="inputForm.specialPlace" class="m-2" placeholder="请输入存放地点(特殊)" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item label="备注" prop="remarks"
								  :rules="[ ]">
						<el-input  type="textarea" placeholder="请输入说明"  maxlength="500"  style="width:100%" v-model="inputForm.remarks" show-word-limit></el-input>
					</el-form-item>
				</el-col>

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

<script>
	import XEUtils from 'xe-utils'
	import ReportArchieveService from '@/api/zs/reportArchieve/ReportArchieveService'
	import { ElDatePicker } from 'element-plus';
	export default {
		props: {
			businessId: {
				type: String,
				default: ''
			},
			formReadOnly: {
				type: Boolean,
				default: false
			},
			status: {
				type: String,
				default: ''
			}
		},
		data () {
			return {
				title: '',
				method: '',
				loading: false,
				visible: false,
				inputForm: {
					id: '',
					officeName:'',
					businessName:'',
					project:'',
					businessType:'',
					reportNoType:'',
					reportNo:'',
					reportingPeriod:'',
					fillingManager:'',
					projectLeader:'',
					projectMaster:'',
					signatureCpa:'',
					signatureParter:'',
					projectLevel:'',
					reportDate:'',
					stampDate:'',
					archieveDate:'',
					fileBoxNumber:'',
					boxNumber:'',
					bookNumbers:'',
					originalContract:'',
					totalAssets:'',
					storagePlaces:'',
					remarks:'',
					specialPlace:''
				},
				baseKey: '',
				keyWatch: '',
				dateList:[],
				usedReportNo:'',
				storagePlace:'',
			}
		},
		ReportArchieveService:null,
		created () {
			console.log('jld ')
			this.reportArchieveService=new ReportArchieveService()
		},
		mounted () {
		},
		activated () {
		},
		components: {
			ElDatePicker
		},
		computed: {
			bus: {
				get () {
					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)
				}
			}
		},
		methods: {
			getKeyWatch(keyWatch) {
				this.keyWatch = keyWatch
			},
			init(method, id) {
				this.method = method
				this.inputForm={
					id: '',
					officeName:'',
					businessName:'',
					project:'',
					businessType:'',
					reportNoType:'',
					reportNo:'',
					reportingPeriod:'',
					fillingManager:'',
					projectLeader:'',
					projectMaster:'',
					signatureCpa:'',
					signatureParter:'',
					projectLevel:'',
					reportDate:'',
					stampDate:'',
					archieveDate:'',
					fileBoxNumber:'',
					boxNumber:'',
					bookNumbers:'',
					originalContract:'',
					totalAssets:'',
					storagePlaces:'',
					remarks:'',
					specialPlace:''
				}
				if (method === 'add') {
					this.title = `新建报告归档信息`
				} else if (method === 'edit') {
					this.inputForm.id = id
					this.title = '修改报告归档信息'
				} else if (method === 'view') {
					this.inputForm.id = id
					this.title = '查看报告归档信息'
				}
				this.loading = false
				this.visible= true
				this.$nextTick(() => {
					this.$refs.inputForm.resetFields()
					this.loading = true
					if (this.commonJS.isNotEmpty(this.inputForm.id)) {
						console.log('this.inputForm.id', this.inputForm.id)
						this.reportArchieveService.queryById(this.inputForm.id).then((data)=>{
							console.log('s',data)
							this.inputForm = this.recover(this.inputForm, data)
							this.reportArchieveService.getReportNo(this.inputForm.reportNoType).then((data1)=>{
								if (this.commonJS.isNotEmpty(data1.fileBoxNumber)){
									let fileBoxNumber = ''
									let updateName = ''
									let reportNo = ''
									if (this.commonJS.isNotEmpty(data1.fileBoxNumber)){
										fileBoxNumber = data1.fileBoxNumber
									}
									if (this.commonJS.isNotEmpty(data1.updateName)){
										updateName = data1.updateName
									}
									if (this.commonJS.isNotEmpty(data1.reportNo)){
										reportNo = data1.reportNo
									}
									this.usedReportNo = "盒号("+fileBoxNumber+")填写人("+updateName+")报告文号("+reportNo+")"
								}else {
									this.usedReportNo = ''
								}
								if(this.commonJS.isNotEmpty(data1.storagePlace)) {
									this.storagePlace = "上一文件存放地点：" + data1.storagePlace
								}else{
									this.storagePlace = ''
								}
							})
						})
						this.loading = false
					}
				})
			},
			// 表单提交
			 doSubmit() {
				try {
					this.$refs['inputForm'].validate((valid) => {
						if (valid) {
							this.loading = true
							if (this.commonJS.isEmpty(this.inputForm.reportNoType)){
								this.loading = false
								this.$message.error('报告文号类型不能为空')
								throw new Error('报告文号类型不能为空')
							}
							if (this.commonJS.isEmpty(this.inputForm.reportNo)){
								this.loading = false
								this.$message.error('报告文号不能为空')
								throw new Error('报告文号不能为空')
							}
							if (this.commonJS.isEmpty(this.inputForm.storagePlaces) && this.commonJS.isEmpty(this.inputForm.specialPlace)){
								this.loading = false
								this.$message.error('存放地区区位码和特殊最少填一个')
								throw new Error('存放地区区位码和特殊最少填一个')
							}
							if (this.commonJS.isNotEmpty(this.inputForm.fileBoxNumber) && this.commonJS.isNotEmpty(this.inputForm.reportNoType)){
								this.reportArchieveService.checkFileBoxNumber(this.inputForm.fileBoxNumber,this.inputForm.reportNoType).then((data)=>{
									if (data){
										this.loading = false
										this.inputForm.fileBoxNumber = ''
										this.$message.error('该盒号已被使用,请重新输入')
										throw new Error(`该盒号已被使用,请重新输入`)
									}
								})
							}
							this.reportArchieveService.checkReportTypeAndReportNo(this.inputForm.reportNoType,this.inputForm.reportNo).then((data) =>{
								if (!data){
									this.loading = false
									this.$message.error('报告文号类型与报告文号不一致,请重新输入')
									throw new Error('报告文号类型与报告文号不一致,请重新输入')
								}
								this.reportArchieveService.saveForm(this.inputForm).then((da) => {
									this.visible = false
									this.$emit('refreshList')
								}).catch(() => {
									this.$refs.inputForm.resetFields()
									this.loading = false
								})
							})

						} else {
							this.loading = false
						}
					})
				}catch (e) {
					console.log('123123')
					this.$message.error(e.message)
				}

			},
			close() {
				this.inputForm = {
					id: '',
					officeName:'',
					businessName:'',
					project:'',
					businessType:'',
					reportNoType:'',
					reportNo:'',
					reportingPeriod:'',
					fillingManager:'',
					projectLeader:'',
					projectMaster:'',
					signatureCpa:'',
					signatureParter:'',
					projectLevel:'',
					reportDate:'',
					stampDate:'',
					archieveDate:'',
					fileBoxNumber:'',
					boxNumber:'',
					bookNumbers:'',
					originalContract:'',
					totalAssets:'',
					storagePlace:'',
					remarks:'',
				}
				this.visible = false
				this.usedReportNo = ''
				this.storagePlace = ''
			},
			getReportNo(){
				console.log('id',this.inputForm.reportNoType)
				this.reportArchieveService.getReportNo(this.inputForm.reportNoType).then((data)=>{
					if (this.commonJS.isNotEmpty(data.fileBoxNumber)){
						let fileBoxNumber = ''
						let updateName = ''
						let reportNo = ''
						if (this.commonJS.isNotEmpty(data.fileBoxNumber)){
							fileBoxNumber = data.fileBoxNumber
						}
						if (this.commonJS.isNotEmpty(data.updateName)){
							updateName = data.updateName
						}
						if (this.commonJS.isNotEmpty(data.reportNo)){
							reportNo = data.reportNo
						}
						this.usedReportNo = "盒号("+fileBoxNumber+")填写人("+updateName+")报告文号("+reportNo+")"
					}else {
						this.usedReportNo = ''
					}
					if(this.commonJS.isNotEmpty(data.storagePlace)) {
						this.storagePlace = "上一文件存放地点：" + data.storagePlace
					}else{
						this.storagePlace = ''
					}
				})
			},
			getFileBoxNumber(){
				console.log('id',this.inputForm.reportNo)
				this.reportArchieveService.getFileBoxNumber(this.inputForm.reportNo).then((data)=>{
					if (this.commonJS.isNotEmpty(data.fileBoxNumber)){
						let fileBoxNumber = ''
						let updateName = ''
						let reportNo = ''
						if (this.commonJS.isNotEmpty(data.fileBoxNumber)){
							fileBoxNumber = data.fileBoxNumber
						}
						if (this.commonJS.isNotEmpty(data.updateName)){
							updateName = data.updateName
						}
						if (this.commonJS.isNotEmpty(data.reportNo)){
							reportNo = data.reportNo
						}
						this.usedReportNo = "盒号("+fileBoxNumber+")填写人("+updateName+")报告文号("+reportNo+")"
					}else {
						this.usedReportNo = ''
					}
					if(this.commonJS.isNotEmpty(data.storagePlace)) {
						this.storagePlace = "上一文件存放地点：" + data.storagePlace
					}else{
						this.storagePlace = ''
					}
				})
			},
			checkFileBoxNumber(){
				if (this.commonJS.isNotEmpty(this.inputForm.fileBoxNumber) && this.commonJS.isNotEmpty(this.inputForm.reportNoType)){
					this.reportArchieveService.checkFileBoxNumber(this.inputForm.fileBoxNumber,this.inputForm.reportNoType).then((data)=>{
						if (data){
							this.loading = false
							this.$message.error(`该盒号已被使用,请重新输入`)
							this.inputForm.fileBoxNumber = ''
						}
					})
				}

			},


		}

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