<template>
	<!-- 除险加固弹框 -->
	<div>
		<el-dialog custom-class="bbsl-location-dialog" top="10vh" :close-on-click-modal="false"
			:title="title + '（除险加固）'" :visible.sync="showFormDia" width="80%" :before-close="handleClose">
			<el-form ref="formInfo" :rules="rules" :model="params" label-width="150px">
				<el-row>
					<el-col :span="8">
						<el-form-item label="水库名称：" prop="waterName">
							<el-select style="width: 100%" size="mini" disabled v-model="params.rscd" clearable
								placeholder="请选择水库名称">
								<el-option v-for="item in waterList" :key="item.id" :label="item.rsnm"
									:value="item.rscd"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="8">
						<el-form-item label="竣工验收时间：" prop="completeDate">
							<el-date-picker style="width: 100%" size="mini" value-format="yyyy-MM-dd"
								v-model="params.completeDate" type="date" placeholder="请选择竣工验收时间">
							</el-date-picker>
						</el-form-item>
					</el-col>
					<el-col :span="8">
						<el-form-item label="投入使用时间：" prop="useDate">
							<el-date-picker style="width: 100%" size="mini" value-format="yyyy-MM-dd"
								v-model="params.useDate" type="date" placeholder="请选择投入使用时间">
							</el-date-picker>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row>
					<el-col :span="8">
						<el-form-item label="最后一次加固开工时间：" prop="lastDate">
							<el-date-picker style="width: 100%" size="mini" value-format="yyyy-MM-dd"
								v-model="params.lastDate" type="date" placeholder="请选择最后一次加固开工时间">
							</el-date-picker>
						</el-form-item>
					</el-col>
					<el-col :span="8">
						<el-form-item label="上传文件：">
							<el-upload name="files" class="avatar-uploader" :action="uploadUrl"
								:on-success="uploadSuccess" :before-upload="beforeUpload" :headers="header"
								:accept="accept" :file-list="fileList" :data="UploadData">
								<el-button size="mini" icon="el-icon-upload" type="success">上传</el-button>
								<div slot="tip" class="el-upload__tip">只能上传pdf文件</div>
							</el-upload>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button type="primary" @click="goInfo">保 存</el-button>
				<el-button @click="handleClose">取 消</el-button>
			</span>
		</el-dialog>
	</div>

</template>

<script>
import reinforcement from '@/api/reservoirManagement/resFrom.js'
import dictUtils from '@/components/dict/DictSelectUtils.js'
import {
	mapState
} from 'vuex'
export default {
	props: {
		rscd: {
			type: String,
			default: '',
		},
		showFormDia: {
			type: Boolean,
			default: false,
		},
		title: {
			type: String,
			default: '',
		},
		wId: {
			type: String,
			default: '',
		},
		plId: {
			type: String,
			default: '',
		},
	},
	data() {
		return {
			name: "除险加固",
			UploadData: {
				bizCode: "ReservoirPdf",
				bizId: this.rscd,
				bizType: "",
			},
			uploadUrl: window.context.portal + '/file/v1/uploadFileForConfig', // 服务器上传地址
			accept: '.pdf',
			previewUrl: window.context.portal + '/file/onlinePreviewController/v1/getFileById_',
			params: {
				rscd: null,
				rsnm: null,
				completeDate: null,
				useDate: null,
				lastDate: null,
				filePaths: null, //文件路径
			},
			fileList: [],
			waterList: [],
			rules: {
				damName: [{
					required: true,
					message: '请输入大坝名称',
					trigger: 'blur',
				},],
			}
		}
	},
	created() {
		this.getWaters()
		this.getDetailInfo()
		// 添加上传文件参数
		this.resPDF = dictUtils.getDictItemsFromCache('skPDFlx')
		this.resPDF.forEach(res => {
			if (res.name == this.name) {
				this.UploadData.bizType = res.value
			}
		})
	},
	computed: mapState({
		header: (state) => {
			return {
				Authorization: `Bearer ${state.login.currentUser.token}`
			}
		},
	}),
	methods: {
		handleClose() {
			this.params = {}
			this.$emit('closeFormDiao')
		},
		//获取水库
		getWaters() {
			reinforcement.getWater({}, (res) => {
				this.waterList = res.value
				this.waterList.forEach((ee) => {
					if (this.wId == ee.id) {
						this.params.rscd = ee.rscd
						this.params.rsnm = ee.rsnm
					}
				})
			})
		},
		beforeUpload(file) {
			return true
		},
		uploadSuccess(res) {
			if (res) {
				let arr = []
				arr.push({
					fileName: res.fileName,
					fileId: res.fileId
				})
				this.params.filePaths = JSON.stringify(arr)
			} else {
				// 提示信息，需引入Message
				this.$message.error('上传失败！')
			}
		},
		//保存
		goInfo(e) {
			this.$refs.formInfo.validate((valid) => {
				if (valid) {
					if (this.title === '新增') {
						let arr = JSON.parse(JSON.stringify(this.params))
						reinforcement.addSaveRes(this.params, (response) => {
							if (response.state) {
								this.$notify({
									type: 'success',
									message: '新增成功',
									duration: 2000,
								})
								this.$emit('closeFormDiao')
								this.$emit('refList')
							} else { }
						})
					}
					if (this.title === '修改') {
						reinforcement.upDataRes(this.params, (response) => {
							if (response.state) {
								this.$notify({
									type: 'success',
									message: '修改成功',
									duration: 2000,
								})
								this.$emit('closeFormDiao')
								this.$emit('refList')
							} else { }
						})
					}
				} else {
					return false
				}
			})
		},
		//修改查询
		getDetailInfo() {
			if (this.title === '修改') {
				reinforcement.detailIdRes(this.plId, (res) => {
					if (res.state) {
						this.params.id = res.value.id
						this.params.rscd = res.value.rscd
						this.params.rsnm = res.value.rsnm
						this.params.lastDate = res.value.lastDate
						this.params.pkVal = res.value.pkVal
						this.params.useDate = res.value.useDate
						this.params.completeDate = res.value.completeDate
						let aaa = JSON.parse(res.value.filePaths)
						if (res.value.filePaths) {
							aaa.forEach(e => {
								this.fileList.push({
									name: e.fileName,
									url: this.previewUrl + e.fileId
								})
							})
						}

					}
				})
			} else {

			}
		},
	}
}
</script>

<style>
</style>
