<template>
	<div class="updateInformation">
		<el-row class="addBtnBg" style="margin-bottom: 10px" v-if="!isSj">
			<el-button
				type="primary"
				class="iconfont iconxiugai1"
				size="small"
				@click="updateFun()"
			>
				编辑</el-button
			>
			<el-button
				type="primary"
				class="iconfont iconbaocun"
				size="small"
				@click="saveFun()"
			>
				保存</el-button
			>
			<el-button
				type="primary"
				class="iconfont iconquxiao"
				size="small"
				@click="resetFun()"
			>
				取消</el-button
			>
		</el-row>
		<el-form
			:label-width="formLabelWidth"
			:model="formData"
			:rules="rules"
			ref="formData"
		>
			<el-row :gutter="10">
				<el-col :span="12">
					<el-form-item label="单位名称：">
						<el-input
							v-model="formData.orgname"
							placeholder="请输入单位名称"
							disabled
							clearable
						></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="统一社会信用代码：" prop="tyshxydm">
						<el-input
							v-model="formData.tyshxydm"
							placeholder="请输入统一社会信用代码"
							clearable
							:disabled="isLook"
						></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="10">
				<el-col :span="24">
					<el-form-item label="地址：" prop="dz">
						<el-input
							v-model="formData.dz"
							placeholder="请输入地址"
							clearable
							:disabled="isLook"
						></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="10">
				<el-col :span="12">
					<el-form-item label="管辖区域：">
						<el-input
							v-model="formData.gxqy"
							placeholder="请输入管辖区域"
							:disabled="isLook"
							clearable
						></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="行政级别：">
						<el-input
							v-model="formData.xzjb"
							placeholder="请输入行政级别"
							:disabled="isLook"
							clearable
						></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="10">
				<el-col :span="12">
					<el-form-item label="行政机构设置情况：" prop="xzjgslqk">
						<el-select
							v-model="formData.xzjgslqk"
							placeholder="请选择"
							clearable
							:disabled="isLook"
						>
							<el-option
								v-for="item in xzjgList"
								: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="执法督查机构名称：">
						<el-input
							v-model="formData.zfdcjgmc"
							placeholder="请输入执法督查机构名称"
							clearable
							:disabled="isLook"
						></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="10">
				<el-col :span="12">
					<el-form-item label="执法督查机构情况：" prop="zfdcjgqk">
						<el-select
							v-model="formData.zfdcjgqk"
							placeholder="请选择"
							clearable
							:disabled="isLook"
						>
							<el-option
								v-for="item in zfdcList"
								: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="ndjfje"
					>
						<el-input
							v-model="formData.ndjfje"
							placeholder="请输入年度落实经费金额"
							clearable
							:disabled="isLook"
						></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="10">
				<el-col :span="12">
					<el-form-item label="行政编制人数：">
						<el-input
							v-model="formData.xzbzrs"
							:disabled="isLook"
						></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="事业编制人数：">
						<el-input
							v-model="formData.sybzrs"
							:disabled="isLook"
						></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="10">
				<el-col :span="12">
					<el-form-item label="国家证人数：">
						<el-input
							v-model="formData.gjzrs"
							:disabled="isLook"
						></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="市级证人数：">
						<el-input
							v-model="formData.sjzrs"
							:disabled="isLook"
						></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="10">
				<el-col :span="12">
					<el-form-item label="是否有执法队：" prop="sfyzfd">
						<el-radio-group
							v-model="formData.sfyzfd"
							:disabled="isLook"
						>
							<el-radio :label="0">是</el-radio>
							<el-radio :label="1">否</el-radio>
						</el-radio-group>
					</el-form-item>
				</el-col>
				<el-col :span="12" v-if="formData.sfyzfd === 0">
					<el-form-item label="执法队情况：" prop="zfdqk">
						<el-radio-group
							v-model="formData.zfdqk"
							:disabled="isLook"
						>
							<el-radio :label="0">编办批准成立</el-radio>
							<el-radio :label="1">内部组建</el-radio>
						</el-radio-group>
					</el-form-item>
				</el-col>
			</el-row>
			<div v-if="formData.sfyzfd === 0">
				<el-row :gutter="10" v-if="formData.zfdqk === 0">
					<el-col :span="12">
						<el-form-item label="行政编制类数量：" prop="xzbzlsl">
							<el-input
								v-model="formData.xzbzlsl"
								clearable
								@change="changeSl(1)"
								:disabled="isLook"
							></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="事业编制类数量：" prop="sybzlsl">
							<el-input
								v-model="formData.sybzlsl"
								clearable
								@change="changeSl(2)"
								:disabled="isLook"
							></el-input>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row :gutter="10" v-if="formData.zfdqk === 1">
					<el-col :span="12">
						<el-form-item label="专职机构数量：" prop="zzjgsl">
							<el-input
								v-model="formData.zzjgsl"
								clearable
								@change="changeSl(3)"
								:disabled="isLook"
							></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="非专职机构数量：" prop="fzzjgsl">
							<el-input
								v-model="formData.fzzjgsl"
								clearable
								@change="changeSl(4)"
								:disabled="isLook"
							></el-input>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row :gutter="10">
					<el-col :span="24">
						<el-form-item label="附件：" required>
							<el-upload
								class="upload-demo"
								:action="actionUrl"
								:on-preview="handlePreview"
								:on-remove="handleRemove"
								:before-remove="beforeRemove"
								:on-success="handleSuccess"
								:on-change="changeFj"
								multiple
								:disabled="isLook"
								:on-exceed="handleExceed"
								:file-list="formData.fileVOs"
							>
								<el-button
									size="small"
									type="primary"
									plain
									:disabled="isLook"
									>选择文件</el-button
								>
								<div slot="tip" class="el-upload__tip"></div>
							</el-upload>
						</el-form-item>
					</el-col>
				</el-row>
			</div>
			<div style="text-align: center" v-if="isSj">
				<el-button
					type="primary"
					class="iconfont iconbaocun"
					@click="btnSave(1, 'formData')"
					v-if="isUpdate"
				>
					确认</el-button
				>
				<el-button
					type="primary"
					class="iconfont iconquxiao"
					@click="btnSave(0, 'formData')"
					v-if="isUpdate"
				>
					取消</el-button
				>
				<el-button
					type="primary"
					class="iconfont iconfanhui"
					@click="btnSave(0, 'formData')"
					v-if="isLook"
				>
					返回</el-button
				>
			</div>
		</el-form>
	</div>
</template>

<script>
import { FileApi } from '@/api'
import {
	getbaseinfo,
	getList,
	updatebaseinfo,
	getbaseinfocha,
} from '@/api/unitBasicInformationMaintenance/index'
export default {
	name: 'updateInformation',
	data() {
		const blurText = async (rule, value, callback) => {
			if (!Number.isInteger(Number(value))) {
				callback(new Error('请输入大于或等于0的整数'))
			} else {
				if (value < 0) {
					callback(new Error('请输入大于或等于0的整数'))
				} else {
					callback()
				}
			}
		}
		const blurNumber = async (rule, value, callback) => {
			if (value != '') {
				if (
					/^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+)$/.test(value) == false
				) {
					callback(new Error('请输入大于或等于0的数字'))
				} else {
					callback()
				}
			} else {
				callback()
			}
		}
		return {
			isSj: false,
			isLook: false,
			isUpdate: false,
			jczListClone: [],
			jczList: [],
			isDcsjrwmc: false,
			lxrList: [],
			bjdwList: [
				{
					value: 1,
					label: '所在地',
				},
				{
					value: 2,
					label: '注册地',
				},
				{
					value: 3,
					label: '其他',
				},
			],
			formData: {
				orgname: null,
				tyshxydm: null,
				dz: null,
				gxqy: null,
				xzjb: null,
				xzjgslqk: null,
				zfdcjgmc: '监督检查处',
				zfdcjgqk: null,
				ndjfje: null,
				sfyzfd: null,
				zfdqk: null,
				xzbzlsl: null,
				sybzlsl: null,
				zzjgsl: null,
				fzzjgsl: null,
				fileVOs: [],
			},
			rules: {
				tyshxydm: [
					{
						min: 1,
						max: 50,
						message: '长度在 1 到 50 个字符',
						trigger: 'blur',
					},
				],
				dz: [
					{
						min: 1,
						max: 50,
						message: '长度在 1 到 50 个字符',
						trigger: 'blur',
					},
				],
				xzjgslqk: [
					{ required: true, message: '请选择', trigger: 'change' },
				],
				zfdcjgmc: [
					{
						min: 1,
						max: 50,
						message: '长度在 1 到 50 个字符',
						trigger: 'blur',
					},
				],
				zfdcjgqk: [
					{ required: true, message: '请选择', trigger: 'change' },
				],
				ndjfje: [{ validator: blurNumber }],
				sfyzfd: [
					{ required: true, message: '请选择', trigger: 'change' },
				],
				zfdqk: [
					{ required: true, message: '请选择', trigger: 'change' },
				],
				xzbzlsl: [{ required: true, validator: blurText }],
				sybzlsl: [{ required: true, validator: blurText }],
				zzjgsl: [{ required: true, validator: blurText }],
				fzzjgsl: [{ required: true, validator: blurText }],
			},
			xzjgList: [
				{
					value: '0',
					label: '独立机构',
				},
				{
					value: '1',
					label: '加挂粮食和物资储备局牌子',
				},
				{
					value: '2',
					label: '职能整合入其他部门',
				},
			],
			zfdcList: [
				{
					value: '0',
					label: '独立设立执法督查（监督检查）处（科、股）',
				},
				{
					value: '1',
					label: '加挂执法督查（监督检查）处（科、股）牌子',
				},
				{
					value: '2',
					label: '职能整合入其他处（科、股）',
				},
			],
			formLabelWidth: '210px',
			form: {},
			fileList: [],
			actionUrl: FileApi + '/file/upload',
		}
	},
	methods: {
		getBaseinfo(data) {
			if (this.isUpdate) {
				getbaseinfo(data).then((res) => {
					if (res.infoCode == 200) {
						if (res.data) {
							this.formData = res.data
							if (
								this.formData.fileVOs &&
								this.formData.fileVOs.length
							) {
								this.formData.fileVOs.forEach((item) => {
									item.name = item.remark
									item.url = this.getAddress(item.fileId)
								})
							} else {
								this.formData.fileVOs = []
							}
							// 天津市局登陆时，默认管辖区域：天津市，行政级别：省级；
							// 各区粮办登陆时，默认管辖区域：各区粮办名称，行政级别：市级；
							if (!this.formData.xzjb || !this.formData.gxqy) {
								if (
									sessionStorage.getItem('orgCode').length ==
									5
								) {
									//市局
									this.formData.gxqy = '天津市'
									this.formData.xzjb = '省级'
								} else {
									//区粮办
									this.formData.gxqy =
										sessionStorage.getItem('orgName')
									this.formData.xzjb = '市级'
								}
							}
						}
					} else {
						this.$message({
							type: 'error',
							message: res.msg,
						})
					}
				})
			} else {
				getbaseinfocha(data).then((res) => {
					if (res.infoCode == 200) {
						if (res.data) {
							this.formData = res.data
							if (
								this.formData.fileVOs &&
								this.formData.fileVOs.length
							) {
								this.formData.fileVOs.forEach((item) => {
									item.name = item.remark
									item.url = this.getAddress(item.fileId)
								})
							} else {
								this.formData.fileVOs = []
							}
						}
					} else {
						this.$message({
							type: 'error',
							message: res.msg,
						})
					}
				})
			}
		},
		getAddress(id) {
			return `${FileApi}/file/download?id=${id}`
		},
		//编辑
		updateFun() {
			this.isLook = false
			this.isUpdate = true
		},
		//保存
		saveFun() {
			if (!this.isUpdate) {
				return
			}
			this.$refs['formData'].validate((valid, dom) => {
				if (valid) {
					if (
						this.formData.sfyzfd === 0 &&
						!this.formData.fileVOs.length
					) {
						this.$message.error('请先上传附件！')
						return
					}
					let params = this.formData
					updatebaseinfo(params).then((res) => {
						if (res.infoCode == 200) {
							this.$message.success('保存成功')
						} else {
							this.$message({
								type: 'error',
								message: res.msg,
							})
						}
					})
					this.isLook = true
					this.isUpdate = false
				} else {
					for (let item in dom) {
						this.$message.error(dom[item][0].message)
						return false
					}
				}
			})
		},
		//取消
		resetFun() {
			if (!this.isUpdate) {
				return
			}
			this.$message.success('已取消')
			this.getBaseinfo(sessionStorage.getItem('orgCode'))
			this.isLook = true
			this.isUpdate = false
		},
		changeSl(type) {
			let unRequired = [
				{
					min: 1,
					max: 50,
					message: '长度在 1 到 50 个字符',
					trigger: 'blur',
				},
				{
					validator: (rule, value, callback) => {
						if (!Number.isInteger(Number(value))) {
							callback(new Error('请输入大于或等于0的整数'))
						} else {
							if (value < 0) {
								callback(new Error('请输入大于或等于0的整数'))
							} else {
								callback()
							}
						}
					},
					trigger: 'blur',
				},
			]
			let required = [
				{
					min: 1,
					max: 50,
					message: '长度在 1 到 50 个字符',
					trigger: 'blur',
				},
				{
					required: true,
					validator: (rule, value, callback) => {
						if (!Number.isInteger(Number(value))) {
							callback(new Error('请输入大于或等于0的整数'))
						} else {
							if (value < 0) {
								callback(new Error('请输入大于或等于0的整数'))
							} else {
								callback()
							}
						}
					},
					trigger: 'blur',
				},
			]
			if (this.rules.xzbzlsl && this.rules.xzbzlsl != '0' && type == 1) {
				this.rules.xzbzlsl = required
				this.rules.sybzlsl = unRequired
			}
			if (this.rules.sybzlsl && this.rules.sybzlsl != '0' && type == 2) {
				this.rules.xzbzlsl = unRequired
				this.rules.sybzlsl = required
			}
			if (this.rules.zzjgsl && this.rules.zzjgsl != '0' && type == 3) {
				this.rules.fzzjgsl = unRequired
				this.rules.zzjgsl = required
			}
			if (this.rules.fzzjgsl && this.rules.fzzjgsl != '0' && type == 4) {
				this.rules.zzjgsl = unRequired
				this.rules.fzzjgsl = required
			}
		},
		//保存 取消
		btnSave(type, formName) {
			if (type) {
				this.$refs[formName].validate((valid, dom) => {
					if (valid) {
						if (
							this.formData.sfyzfd === 0 &&
							!this.formData.fileVOs.length
						) {
							this.$message.error('请先上传附件！')
							return
						}
						let params = this.formData
						updatebaseinfo(params).then((res) => {
							if (res.infoCode == 200) {
								this.$message.success('保存成功')
							} else {
								this.$message({
									type: 'error',
									message: res.msg,
								})
							}
						})
						this.$router.go(-1)
					} else {
						for (let item in dom) {
							this.$message.error(dom[item][0].message)
							return false
						}
					}
				})
			} else {
				this.$router.go(-1)
			}
		},
		//转时间方法
		formatDateTime(date) {
			let y = date.getFullYear()
			let m = date.getMonth() + 1
			m = m < 10 ? '0' + m : m
			let d = date.getDate()
			d = d < 10 ? '0' + d : d
			let h = date.getHours()
			h = h < 10 ? '0' + h : h
			let minute = date.getMinutes()
			minute = minute < 10 ? '0' + minute : minute
			let second = date.getSeconds()
			second = second < 10 ? '0' + second : second
			return y + '-' + m + '-' + d
		},
		//转时间方法
		formatDateTimes(date) {
			let y = date.getFullYear()
			let m = date.getMonth() + 1
			m = m < 10 ? '0' + m : m
			let d = date.getDate()
			d = d < 10 ? '0' + d : d
			let h = date.getHours()
			h = h < 10 ? '0' + h : h
			let minute = date.getMinutes()
			minute = minute < 10 ? '0' + minute : minute
			let second = date.getSeconds()
			second = second < 10 ? '0' + second : second
			return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second
		},
		//文件上传及删除的方法
		//附件上传 接口报fail时
		changeFj(file, fileList) {
			if (file.status == 'fail') {
				if (file.size > 200000000) {
					this.$message({
						message: '上传失败，文件最大不超过200MB',
					})
				} else {
					this.$message({
						message: '上传失败',
					})
				}
			}
		},
		//文件上传成功
		handleSuccess(response) {
			if (response && response.infoCode === 200) {
				this.fileId = response.data.id
				if (this.formData.fileVOs == null) {
					this.formData.fileVOs = []
				}
				this.formData.fileVOs.push({
					fileId: response.data.id,
					fileType: 0,
					remark: response.data.name,
					name: response.data.name,
				})
				this.formData.fileVOs.forEach((item) => {
					item.status = 2
				})
				this.$message.success('上传成功')
			}
		},
		//下载文件
		handlePreview(file, fileList) {
			document.location.href = file.url
		},
		handleExceed(files, fileList) {
			this.$message.warning('附件最大数量为9个！')
		},
		//文件删除之前，弹出确认删除文件
		beforeRemove(file, fileList) {
			return this.$confirm(`确定移除 ${file.name}？`)
		},
		//文件删除
		handleRemove(file, fileList) {
			this.$message.success('移除成功')
			for (let i = 0; i < this.formData.fileVOs.length; i++) {
				if (file.response.data.id == this.formData.fileVOs[i].fileId) {
					this.formData.fileVOs.splice(i, 1)
					return
				}
			}
		},
	},
	mounted() {
		if (this.$route.query.type == 'look') {
			this.isLook = true
			this.isUpdate = false
		} else if (this.$route.query.type == 'update') {
			this.isLook = false
			this.isUpdate = true
		} else {
			this.isLook = true
			this.isUpdate = false
		}
		if (sessionStorage.getItem('orgCode').length > 5) {
			this.isSj = false
			this.getBaseinfo(sessionStorage.getItem('orgCode'))
		} else {
			this.isSj = true
			this.getBaseinfo(this.$route.query.orgCode)
		}
	},
}
</script>
<style scoped>
.updateInformation {
	position: relative;
	overflow-y: auto;
	margin: 10px;
	border: 1px solid #f4f5f8;
	padding: 10px;
}
.text {
	float: right;
	margin-right: 10px;
}
.text span {
	font-size: 14px !important;
}
</style>
