<template>
	<el-form label-width="140px" :model="ruleForm" ref="ruleForm">
		<!-- <el-row>
			<el-col :span="12">
				<el-form-item label="项目申请条件标题:">
					<el-input
						placeholder="请输入项目申请条件标题"
						v-model="xmIdForm.xmfabt"
						disabled
					></el-input>
				</el-form-item>
			</el-col>
			<el-col :span="12">
				<el-form-item label="年度:">
					<el-input
						placeholder="请输入项目年度"
						v-model="xmIdForm.xmnd"
						disabled
					></el-input>
				</el-form-item>
			</el-col>
		</el-row> -->
		<el-row>
			<el-form-item label="立项审批说明:">
				<el-input
					type="textarea"
					v-model="ruleForm.xmsqspys"
					:rows="3"
					placeholder="请输入立项审批说明"
					:disabled="
						this.xmsqIdForm.xmlxzt > 2 || fatherObj.type == 1
					"
				></el-input>
			</el-form-item>
		</el-row>
		<el-row>
			<el-form-item label="立项审批材料:">
				<div class="fileTable" style="width: 100%;">
					<table>
						<thead>
							<tr>
								<th width="10%">序号</th>
								<th width="25%">附件名称</th>
								<th width="25%">上传时间</th>
								<th width="40%">附件</th>
							</tr>
						</thead>
						<tbody>
							<tr
								v-for="(item, index) in ruleForm.xmlxysList"
								:key="index"
								v-if="xmlxysLength != 0"
							>
								<td>{{ index + 1 }}</td>
								<td>
									<el-form-item>
										<el-input
											placeholder="请输入附件名称"
											v-model="item.xmclmc"
											disabled
										></el-input>
									</el-form-item>
								</td>
								<td>
									<el-form-item>
										<el-input
											placeholder="请输入上传时间"
											v-model="item.xmclsbsj"
											disabled
										></el-input>
									</el-form-item>
								</td>
								<td>
									<div
										class="fileList"
										v-for="(item2,
										index2) in item.xmclFileList"
										:key="index2"
										v-if="item2.status == 1"
									>
										<span
											class="name"
											@click="downLoadFile(item2.fileId)"
										>
											{{ item2.fileName }}
										</span>
									</div>
								</td>
							</tr>
							<tr v-if="xmlxysLength == 0">
								<td
									colspan="4"
									style="padding: 0; color: #909399; line-height: 60px;"
								>
									暂无数据
								</td>
							</tr>
						</tbody>
					</table>
				</div>
			</el-form-item>
		</el-row>
		<el-row class="center">
			<el-button
				type="primary"
				class="iconfont iconfanhui"
				@click="dialogVisible"
				>返回</el-button
			>
		</el-row>
	</el-form>
</template>
<script>
import { FileApi } from '@/api'
import {
	projectApprovalAcceptance,
	getProjectApprovalAcceptance
} from '@/api/projectManagement/index'
export default {
	name: 'keyanbaogao',
	props: {
		// 父组件方法
		showDialog5: {
			type: Function,
			default: null
		},
		getList5: {
			type: Function,
			default: null
		},
		// 父组件参数
		fatherObj: {
			type: Object,
			default: null
		},
		fatherEchoForm: {
			type: Object,
			default: null
		},
		xmsqIdForm: {
			type: Object,
			default: null
		},
		xmIdForm: {
			type: Object,
			default: null
		}
	},
	data() {
		return {
			// 下拉框
			xmndValue: '',
			// 提交form
			ruleForm: {
				xmlxlx: 1
			},
			// 文件上传路径
			actionUrl: FileApi + '/file/upload',
			fileList: [],
			// 立项审批附件
			xmlxysList: [],
			// 立项审批数组长度
			xmlxysLength: '',
			buttonIndex: '',
			// 系统当前时间
			nowTime: '',
			editable: false
		}
	},
	methods: {
		beforeUpload(file) {
			console.log(file.name, file.type)
			var isLimitFile = [
				'.zip',
				'.rar',
				'.jpg',
				'.png',
				'.gif',
				'.jpeg',
				'.bmp',
				'.7z'
			].some((ext) => {
				file.name.endsWith(ext)
			})
			if (
				isLimitFile ||
				file.type.includes('image') ||
				file.type.includes('x-zip-compressed') ||
				file.type.includes('x-rar-compressed')
			) {
				this.$message.error('不能上传图片或者压缩文件')
				return false
			}
		},
		// 文件删除的方法
		handleRemove(file, fileList) {
			console.log('文件删除file:', file)
			console.log('文件删除fileList:', fileList)
		},
		handlePreview(file) {
			console.log(file)
		},
		handleExceed(files, fileList) {
			this.$message.warning(
				`当前限制选择 1 个文件，本次选择了 ${
					files.length
				} 个文件，共选择了 ${files.length + fileList.length} 个文件`
			)
		},
		beforeRemove(file, fileList) {
			return this.$confirm(`确定移除 ${file.name}？`, '提示')
		},
		// 获取上传返回信息
		handleAvatarSuccess(res, file) {
			let obj = {
				fileId: res.data.id,
				fileName: res.data.name,
				name: res.data.name,
				status: 1
			}
			this.xmlxysList.forEach((item, index) => {
				console.log(this.buttonIndex, index)
				if (this.buttonIndex == index) {
					item.xmclFileList.push(obj)
				}
			})
			this.ruleForm.xmlxysList = this.xmlxysList
			console.log('上传.res:', res)
			console.log('上传.obj:', obj)
			console.log('上传.this.xmlxysList:', this.xmlxysList)
		},
		// 文件下载
		downLoadFile(id) {
			window.open(`${FileApi}/file/download?id=${id}`)
		},
		// 删除文件
		deleteFile(index, index2) {
			this.xmlxysList[index].xmclFileList[index2].status = 0
		},
		dialogVisible() {
			this.$emit('showDialog5', 'false')
			this.$emit('getList5')
		},
		getDeatil() {
			let data = {
				xmsqId: this.fatherObj.xmsqId,
				xmlxlx: 1
			}
			getProjectApprovalAcceptance(data).then((res) => {
				if (res.data && res.infoCode === 200) {
					this.ruleForm = res.data
					this.xmlxysList = res.data.xmlxysList
					this.xmlxysLength = res.data.xmlxysList.legnth
					console.log('立项审批this.ruleForm:', this.ruleForm)
				}
			})
		},
		saveInfo(type) {
			let data = {
				type: type,
				data: this.ruleForm
			}
			data.data.xmsqId = this.xmsqIdForm.xmsqId
			data.data.xmlxFileList = this.xmlxFileList
			console.log('立项审批data:', data)
			projectApprovalAcceptance(data).then((res) => {
				if (res.infoCode === 200) {
					let msg = ''
					type == 0 ? (msg = '保存成功!') : (msg = '提交成功!')
					this.$message({
						type: 'success',
						message: msg
					})
				} else {
					// this.$message({
					// 	type: "error",
					// 	message: res.msg
					// });
				}
				this.dialogVisible()
			})
		},
		addList() {
			let obj = {
				sjId: '',
				xmsqId: '',
				xmcllx: 2,
				xmclmc: '',
				xmclsbsj: this.nowTime,
				xmclFileList: []
			}
			obj.xmclsbsj = this.nowTime
			this.xmlxysList.push(obj)
			this.xmlxysLength = this.xmlxysList.length
			this.ruleForm.xmlxysList = this.xmlxysList
			console.log('addList方法this.xmlxysList:', this.xmlxysList)
		},
		removeList() {
			this.ruleForm.xmlxysList.pop()
			this.xmlxysLength = this.submitForm.xmlxysList.length
		},
		// 获取上传按钮在表格中的索引
		getIndex(index) {
			this.buttonIndex = index
			console.log('getIndex方法index:', index)
		}
	},
	created() {
		this.getDeatil()
		this.nowTime =
			String(new Date().getFullYear()) +
			'-' +
			String(new Date().getMonth() + 1) +
			'-' +
			String(new Date().getDate())
	}
}
</script>

<style lang="scss" scoped>
.title {
	margin-bottom: 20px;
	h2 {
		font-size: 1.8em;
	}
}

.center {
	text-align: center;
}

.check-icon-box {
	float: left;
	width: 8%;
	text-align: center;
	.check-icon {
		display: block;
		margin: 0 auto 10px;
		vertical-align: top;
		color: #006bd5;
		font-size: 32px;
		cursor: pointer;
	}
}

.fileTable {
	float: left;
	width: 92%;
	table,
	th,
	td {
		border: 1px #eaeaea solid;
		border-collapse: collapse;
	}
	th,
	td {
		text-align: center;
		color: #333;
		font-size: 14px;
		height: 35px;
	}
	th,
	tr:hover td {
		background-color: #f6f7f9;
	}
	table {
		width: 100%;
	}
	th {
		font-weight: bold;
	}
	td {
		padding: 20px 10px;
		transition: background-color 0.25s ease;
	}
}

.fileList {
	position: relative;
	height: 25px;
	line-height: 25px;
	&:hover .name {
		color: #409eff;
	}
	.name {
		cursor: pointer;
		color: #606266;
		transition: color 0.3s;
	}
	.close {
		position: absolute;
		top: 50%;
		right: 7px;
		margin-top: -7px;
		width: 14px;
		height: 14px;
		cursor: pointer;
		z-index: 1;
		&:after,
		&:before {
			position: absolute;
			top: 50%;
			left: 50%;
			margin-top: -5px;
			content: '';
			width: 1px;
			height: 10px;
			background-color: #606266;
		}
		&:after {
			transform: rotate(45deg);
		}
		&:before {
			transform: rotate(-45deg);
		}
	}
}
</style>
