<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="yhName">
							<el-input size="mini" placeholder="请输入养护名称" v-model="params.yhName"></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="8">
						<el-form-item label="养护类型：" prop="yhType">
							<el-select style="width: 100%" size="mini" v-model="params.yhType" clearable
								placeholder="请选择养护类型">
								<el-option v-for="item in yhTypes" :key="item.id" :label="item.name"
									:value="item.value"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row>
					<el-col :span="8">
						<el-form-item label="养护人员：" prop="yhUser">
							<el-input size="mini" placeholder="请输入养护人员" v-model="params.yhUser"></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="8">
						<el-form-item label="养护内容：" prop="yhContent">
							<el-input size="mini" placeholder="请输入养护内容" v-model="params.yhContent"></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="8">
						<el-form-item label="优先级别：" prop="yhLevel">
							<el-select style="width: 100%" size="mini" v-model="params.yhLevel" clearable
								placeholder="请选择优先级别">
								<el-option v-for="item in yhLevels" :key="item.id" :label="item.name"
									:value="item.value"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row>
					<el-col :span="8">
						<el-form-item label="养护开始时间：" prop="yhSDate">
							<el-date-picker style="width: 100%" size="mini" value-format="yyyy-MM-dd"
								v-model="params.yhSDate" type="date" placeholder="请选择养护开始时间">
							</el-date-picker>
						</el-form-item>
					</el-col>
					<el-col :span="8">
						<el-form-item label="养护结束时间：" prop="yhEDate">
							<el-date-picker style="width: 100%" size="mini" value-format="yyyy-MM-dd"
								v-model="params.yhEDate" type="date" placeholder="请选择养护结束时间	">
							</el-date-picker>
						</el-form-item>
					</el-col>
					<el-col :span="8">
						<el-form-item label="经费(元)：" prop="yhMoney">
							<el-input size="mini" placeholder="请输入经费(元)" v-model="params.yhMoney"></el-input>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row>
					<el-col :span="8">
						<el-form-item label="责任人员：" prop="yhDuty">
							<el-input size="mini" placeholder="请输入责任人员" v-model="params.yhDuty"></el-input>
						</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>
							</el-upload>
							<div slot="tip" class="el-upload__tip">只能上传pdf文件</div>
						</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 req from '@/request.js'
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,
				yhContent: null,
				yhDuty: null,
				yhEDate: null,
				yhLevel: '1',
				yhMoney: null,
				yhName: null,
				yhSDate: null,
				yhType: '1',
				yhUser: null,
				filePaths: null, //文件路径
			},
			yhLevels: [{
				name: '高',
				value: '1'
			},
			{
				name: '中',
				value: '2'
			},
			{
				name: '低',
				value: '3'
			}
			],
			yhTypes: [{
				name: '日常养护',
				value: '1'
			}, {
				name: '专项养护',
				value: '2'
			},],
			fileList: [],
			waterList: [],
			rules: {
				yhName: [{
					required: true,
					message: '请输入护养名称',
					trigger: 'blur',
				},],
				yhType: [{
					required: true,
					message: '请输入护养类型',
					trigger: 'blur',
				},],
			}
		}
	},
	created() {
		this.getWaters()
		this.getDetailInfo()
		this.getSysDict()
		// 添加上传文件参数
		this.resPDF = dictUtils.getDictItemsFromCache('skPDFlx')
		console.log(this.resPDF);
		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.addSaveMain(this.params, (response) => {
							if (response.state) {
								this.$notify({
									type: 'success',
									message: '新增成功',
									duration: 2000,
								})
								this.$emit('closeFormDiao')
								this.$emit('refList')
							} else { }
						})
					}
					if (this.title === '修改') {
						reinforcement.upDataMain(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.detailIdMain(this.plId, (res) => {
					if (res.state) {
						this.params.rscd = res.value.rscd
						this.params.rsnm = res.value.rsnm
						this.params.yhContent = res.value.yhContent
						this.params.yhDuty = res.value.yhDuty
						this.params.yhEDate = res.value.yhEDate
						this.params.yhLevel = res.value.yhLevel.toString()
						this.params.yhMoney = res.value.yhMoney
						this.params.yhName = res.value.yhName
						this.params.yhSDate = res.value.yhSDate
						this.params.yhType = res.value.yhType
						this.params.yhUser = res.value.yhUser
						this.params.id = res.value.id
						this.params.pkVal = res.value.pkVal
						this.params.filePaths = res.value.filePaths
						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 {

			}
		},
		//获取字典数据
		getSysDict() {
			let portal = window.context.portal
			return new Promise((resolve, reject) => {
				req.post(portal + '/sys/sysDictionary/v1/queryAllDictItems').then(
					(response) => {
						resolve(response.data)
					},
					(error) => {
						reject(error)
					}
				)
			})
		},
	}
}
</script>

<style>
</style>
