<template>
	<el-form label-width="140px" :model="ruleForm" ref="ruleForm">
		<el-row>
			<el-form-item label="项目验收申请:">
				<el-input
					type="textarea"
					:row="3"
					placeholder="请输入项目验收申请"
					v-model="ruleForm.xmsqspys"
					disabled
				></el-input>
			</el-form-item>
		</el-row>
		<el-row>
			<el-form-item label="拟验收地点:" :rules="rules('拟验收地点')">
				<el-input v-model="ruleForm.nysdd" disabled></el-input>
			</el-form-item>
		</el-row>
		<el-row>
			<el-form-item label="项目验收材料:">
				<el-table
					border
					style="margin-bottom: 20px;"
					:data="ruleForm.xmlxysList"
				>
					<el-table-column
						type="index"
						label="序号"
						width="80"
						align="center"
					>
						<template slot-scope="scope">
							<span>{{ scope.$index + 1 }}</span>
						</template>
					</el-table-column>
					<el-table-column
						align="center"
						show-overflow-tooltip
						label="验收材料"
						width="400"
					>
						<template slot-scope="scope">
							<div class="tableText">{{ scope.row.xmclmc }}</div>
						</template>
					</el-table-column>
					<el-table-column
						align="center"
						show-overflow-tooltip
						label="类型"
						prop="xmyscllxMc"
					></el-table-column>
					<el-table-column
						align="center"
						show-overflow-tooltip
						label="材料附件"
					>
						<template slot-scope="scope">
							<div
								v-if="scope.row.xmclFileList"
								v-for="(elem, index) in scope.row.xmclFileList"
								:key="index"
							>
								<span
									style="cursor: pointer;"
									@click="downLoad(elem.fileId)"
									>{{ elem.fileName }}</span
								>
							</div>
						</template>
					</el-table-column>
				</el-table>
			</el-form-item>
		</el-row>

		<div style="padding-top: 20px; border-top: 1px #409eff solid;">
			<el-row>
				<el-form-item
					label="评审专家(组长):"
					prop="pszjzz"
					:rules="rules('评审专家(组长)')"
				>
					<el-input
						placeholder="请输入组长"
						:disabled="
							xmsqIdForm.xmlxzt > 14 || fatherObj.type == 1
						"
						v-model="ruleForm.pszjzz"
					></el-input>
				</el-form-item>
			</el-row>
			<el-row>
				<el-form-item
					label="评审专家(成员):"
					prop="pszjzy"
					:rules="rules('评审专家(成员)')"
				>
					<el-input
						placeholder="请输入成员"
						:disabled="
							xmsqIdForm.xmlxzt > 14 || fatherObj.type == 1
						"
						v-model="ruleForm.pszjzy"
					></el-input>
				</el-form-item>
			</el-row>
			<el-row>
				<el-form-item
					label="验收地点"
					prop="shysdd"
					:rules="rules('验收地点')"
				>
					<el-input
						placeholder="请输入验收地点"
						:disabled="
							xmsqIdForm.xmlxzt > 14 || fatherObj.type == 1
						"
						v-model="ruleForm.shysdd"
					></el-input>
				</el-form-item>
			</el-row>
			<el-row>
				<el-form-item
					label="验收时间"
					prop="shyssj"
					:rules="rules('验收时间')"
				>
					<el-date-picker
						v-model="ruleForm.shyssj"
						type="date"
						placeholder="选择日期"
						:disabled="
							xmsqIdForm.xmlxzt > 14 || fatherObj.type == 1
						"
					></el-date-picker>
				</el-form-item>
			</el-row>
			<el-row>
				<el-form-item
					label="验收结果:"
					prop="psjgzt"
					:rules="rules('验收结果')"
				>
					<el-radio-group
						:disabled="
							xmsqIdForm.xmlxzt > 14 || fatherObj.type == 1
						"
						v-model="ruleForm.psjgzt"
					>
						<el-radio :label="1">通过</el-radio>
						<el-radio :label="0">驳回</el-radio>
					</el-radio-group>
				</el-form-item>
			</el-row>
			<el-row>
				<el-form-item label="审批意见:">
					<el-input
						type="textarea"
						:rows="3"
						placeholder="请输入审批意见"
						:disabled="
							xmsqIdForm.xmlxzt > 14 || fatherObj.type == 1
						"
						v-model="ruleForm.psyj"
					></el-input>
				</el-form-item>
			</el-row>
			<el-row>
				<el-form-item label="验收意见附件:">
					<div class="fileTable">
						<table>
							<thead>
								<tr>
									<th width="10%">序号</th>
									<th width="25%">附件名称</th>
									<th width="25%">上传时间</th>
									<th width="30%">附件</th>
									<th
										width="10%"
										v-if="
											xmsqIdForm.xmlxzt <= 14 &&
												fatherObj.type != 1
										"
									>
										操作
									</th>
								</tr>
							</thead>
							<tbody>
								<tr
									v-for="(item, index) in attachFiles"
									:key="index"
									v-show="attachFiles.length != 0"
								>
									<td>{{ index + 1 }}</td>
									<td>
										<el-form-item>
											<el-input
												placeholder="请输入附件名称"
												v-model="item.xmclmc"
												:disabled="fatherObj.type == 1"
											></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>
										<el-upload
											:before-upload="beforeUpload"
											class="upload-demo"
											:action="actionUrl"
											:on-preview="handlePreview"
											:on-remove="
												(file) =>
													handleRemove(file, index)
											"
											:before-remove="beforeRemove"
											:limit="1"
											:on-exceed="handleExceed"
											:on-success="
												(res) =>
													handleSuccess(res, index)
											"
											:file-list="item.xmclFileList"
											v-if="fatherObj.type != 1"
										>
											<div @click="getIndex(index)">
												<el-button
													type="primary"
													size="small"
													:disabled="
														fatherObj.type == 1
													"
													>点击上传</el-button
												>
											</div>
										</el-upload>
										<div
											class="fileList"
											v-for="(item2,
											index2) in item.xmclFileList"
											:key="index2"
											v-show="fatherObj.type == 1"
										>
											<!-- <i
                        class="close"
                        @click="deleteFile(index, index2)"
                        v-if="fatherObj.type !== 1 && xmsqIdForm.xmlxzt < 15"
                      ></i> -->
											<span
												class="name"
												@click="
													downLoadFile(item2.fileId)
												"
											>
												{{ item2.fileName }}
											</span>
										</div>
									</td>
									<td
										align="center"
										v-if="fatherObj.type != 1"
									>
										<el-tooltip
											class="item"
											effect="light"
											content="删除"
											placement="top"
										>
											<el-button
												@click="removeList(index)"
												type="primary"
												class="operate_delete_btn iconfont iconshanchu"
												size="small"
											></el-button>
										</el-tooltip>
									</td>
								</tr>
								<tr v-if="attachFiles.length == 0">
									<td
										colspan="5"
										style="padding: 0; color: #909399; line-height: 60px;"
									>
										暂无数据
									</td>
								</tr>
							</tbody>
						</table>
					</div>
					<div class="check-icon-box" v-if="fatherObj.type !== 1">
						<!-- <span class="el-icon-remove check-icon" @click="removeList"></span> -->
						<span
							class="el-icon-circle-plus check-icon"
							@click="addList"
						></span>
					</div>
				</el-form-item>
			</el-row>
		</div>
		<el-row class="center">
			<el-button
				type="primary"
				class="iconfont iconshenhe"
				@click="saveInfo()"
				v-if="fatherObj.type != 1"
				>&nbsp;审核</el-button
			>
			<el-button
				type="primary"
				class="iconfont iconfanhui"
				@click="dialogVisible"
				>&nbsp;返回</el-button
			>
		</el-row>
	</el-form>
</template>
<script>
import { fileApi } from '@/api'
import {
	projectApprovalAcceptance,
	getProjectApprovalAcceptance,
	projectApprovalAcceptanceApproval
} from '@/api/projectManagement/index'
export default {
	name: 'jianyishu',
	props: {
		// 父组件方法
		showDialog10: {
			type: Function,
			default: null
		},
		getList10: {
			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 {
			attachFiles: [],

			buttonIndex: 0,
			xmysshList: [],
			nowTime: '',
			xmzbqkLength: 0,
			// 下拉框
			xmndValue: '',
			xmyscllxList: [
				'',
				'项目验收报告', // 1
				'文件类', // 2
				'合同类', // 3
				'项目变更情况', // 4
				'财务管理手续及票据' // 5
			],
			ruleForm: {
				xmlxlx: 2
			},
			// 文件上传路径
			actionUrl: fileApi + '/file/upload',
			fileList: []
		}
	},
	methods: {
		rules(msg) {
			return {
				required: true,
				message: '请输入' + msg,
				trigger: ['change', 'blur']
			}
		},
		getIndex(index) {
			this.buttonIndex = index
			console.log('getIndex方法index:', index)
		},
		beforeUpload(file) {
			const isLt200M = file.size / 1024 / 1024 < 200
			if (!isLt200M) {
				this.$message.error('上传文件大小不能超过 200MB!')
			}
			return isLt200M
		},
		handleRemove(file, index) {
			if (!file.fileId) {
				return
			}

			if (file.sjId) {
				this.attachFiles[index].xmclFileList[0].status = 0
				this.attachFiles[index]._file = [
					...this.attachFiles[index].xmclFileList
				]
			}
			this.attachFiles[index].xmclFileList.length = 0
		},
		handlePreview(file) {
			this.downLoadFile(file.fileId)
		},
		handleSuccess(res, thisIndex) {
			let obj = {
				fileId: res.data.id,
				fileName: res.data.name,
				name: res.data.name,
				status: 1
			}
			this.attachFiles[thisIndex].xmclFileList = []
			this.attachFiles[thisIndex].xmclFileList.push(obj)
			this.ruleForm.xmysshList = [...this.attachFiles]
		},

		handleExceed(files, fileList) {
			this.$message.warning(
				`当前限制选择 1 个文件，本次选择了 ${
					files.length
				} 个文件，共选择了 ${files.length + fileList.length} 个文件`
			)
		},
		beforeRemove(file, fileList) {
			if (!file.fileId) {
				return
			}

			return this.$confirm(`确定移除 ${file.name}？`, '提示')
		},
		addList() {
			let obj = {
				sjId: '',
				xmsqId: '',
				xmcllx: 12,
				xmclmc: '',
				xmclsbsj: this.nowTime,
				xmclFileList: []
			}
			this.attachFiles.push(obj)
			this.ruleForm.xmysshList = this.attachFiles
			console.log('this.ruleForm.xmysshList', this.ruleForm.xmysshList)
		},
		removeList(index) {
			this.attachFiles.splice(index, 1)
		},
		// 文件下载
		downLoadFile(id) {
			window.open(`${fileApi}/file/download?id=${id}`)
		},
		downLoad(id) {
			window.open(`${fileApi}/file/download?id=${id}`)
		},
		// 取消按钮
		dialogVisible() {
			this.$emit('showDialog10', 'false')
			this.$emit('getList10')
		},
		deleteFile(index, index2) {
			this.ruleForm.xmysshList[index].xmclFileList[index2].status = 0
		},
		// getInfo
		getInfo() {
			let data = {
				xmsqId: this.fatherObj.xmsqId,
				xmlxlx: 2
			}
			getProjectApprovalAcceptance(data).then((res) => {
				if (res.infoCode === 200) {
					if (res.data) {
						this.ruleForm = res.data
						this.attachFiles = res.data.xmysshList
						this.attachFiles.forEach((d) => {
							let file = d.xmclFileList[0]
							file.name = file.fileName
						})
						this.ruleForm.xmlxysList.forEach((d) => {
							d.xmyscllxMc = this.xmyscllxList[d.xmyscllx]
							if (d.xmclFileList.length) {
								d.xmclFileList.forEach((item) => {
									item.name = item.fileName
								})
							}
						})
						console.log('项目验收的this.ruleForm:', this.ruleForm)
					}
					this.$nextTick(() => {
						this.$refs.ruleForm.clearValidate()
					})
				} else {
					// this.$message({
					// 	type: 'error',
					// 	message: res.msg,
					// });
				}
			})
		},
		// saveInfo
		async saveInfo() {
			let flag = true
			await this.$refs.ruleForm.validate((valid) => {
				if (valid) {
					flag = false
				}
			})
			if (flag) {
				this.$message.warning('请先完成必填项')
				return
			}
			this.ruleForm.xmysshList = this.attachFiles.map((d) => {
				if (d._file)
					return {
						...d,
						xmclFileList: [...d.xmclFileList, ...d._file]
					}
				else return d
			})
			let data = this.ruleForm
			data.xmlxlx = 2
			data.xmsqId = this.fatherObj.xmsqId
			console.log('项目验收data:', data)
			try {
				projectApprovalAcceptanceApproval(data).then((res) => {
					if (res.infoCode === 200) {
						this.$message({
							type: 'success',
							message: '审核成功'
						})
					} else {
						// this.$message({
						// 	type: "error",
						// 	message: res.msg
						// });
					}
					this.dialogVisible()
				})
			} catch (error) {
				console.log('项目验收catch.error', error)
			}
		}
	},
	mounted() {
		this.getInfo()
		this.nowTime =
			String(new Date().getFullYear()) +
			'-' +
			String(new Date().getMonth() + 1) +
			'-' +
			String(new Date().getDate())
	}
}
</script>

<style lang="scss" scoped>
.center {
	text-align: center;
}
.operate_delete_btn {
	float: none !important;
}
.check-icon-box {
	width: 100%;
	text-align: right;
	margin-right: 25px;
	.check-icon {
		display: inline-block;
		width: 10%;
		text-align: center;
		line-height: 1.5em;
		margin: 0 auto 10px;
		vertical-align: top;
		color: #006bd5;
		font-size: 32px;
		cursor: pointer;
	}
}
.fileTable {
	width: 100%;
	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;
	}
}
.upload-demo {
	padding: 0 0 10px;
	height: auto;
	line-height: initial;
}

.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>
