<template>
	<el-form
		label-width="140px"
		:model="ruleForm"
		:rules="rules"
		ref="ruleForm"
	>
		<el-row>
			<el-col :span="12">
				<el-form-item label="招标备案编号:" prop="xmzbbabh">
					<el-input
						placeholder="请输入招标备案编号"
						v-model="ruleForm.xmzbbabh"
						disabled
					></el-input>
				</el-form-item>
			</el-col>
			<el-col :span="12">
				<el-form-item label="招标方式:" prop="xmzbfs">
					<el-input
						placeholder="请输入招标方式"
						v-model="ruleForm.xmzbfs"
						disabled
					></el-input>
				</el-form-item>
			</el-col>
		</el-row>
		<el-row>
			<el-col :span="12">
				<el-form-item label="招标价格:" prop="xmzbjg">
					<el-input
						placeholder="请输入招标价格"
						v-model="ruleForm.xmzbjg"
						disabled
						oninput="value=value.replace(/[^0-9.]/g,'')"
						maxlength="10"
					></el-input>
				</el-form-item>
			</el-col>
			<el-col :span="12">
				<el-form-item label="中标金额:" prop="zbjg">
					<el-input
						placeholder="请输入中标金额"
						v-model="ruleForm.zbjg"
						disabled
						oninput="value=value.replace(/[^0-9.]/g,'')"
						maxlength="10"
					></el-input>
				</el-form-item>
			</el-col>
		</el-row>
		<el-row>
			<el-col :span="12">
				<el-form-item label="中标单位:" prop="zbdw">
					<el-input
						placeholder="请输入中标单位"
						v-model="ruleForm.zbdw"
						disabled
					></el-input>
				</el-form-item>
			</el-col>
			<el-col :span="12">
				<el-form-item label="中标时间:" prop="zbsj">
					<el-date-picker
						v-model="ruleForm.zbsj"
						type="date"
						placeholder="选择日期"
						disabled
					></el-date-picker>
				</el-form-item>
			</el-col>
		</el-row>
		<el-row>
			<el-form-item label="项目招标情况附件:" required>
				<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.xmzbqkList"
								:key="index"
								v-show="xmzbqkLength != 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-show="item2.status == 1"
									>
										<span
											class="name"
											@click="downLoadFile(item2.fileId)"
										>
											{{ item2.fileName }}
										</span>
									</div>
								</td>
							</tr>
							<tr v-if="xmzbqkLength == 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 iconchucun iconbaocun"
				@click="saveInfo(0, 'ruleForm')"
				v-if="fatherObj.type != 1 && xmsqIdForm.xmzbzt < 3"
				>保存</el-button
			>
			<el-button
				type="primary"
				class="iconfont icontijiao"
				@click="saveInfo(1, 'ruleForm')"
				v-if="fatherObj.type != 1 && xmsqIdForm.xmzbzt < 3"
				>提交</el-button
			>
			<el-button
				type="primary"
				class="iconfont iconfanhui"
				@click="dialogVisible"
				>返回</el-button
			>
		</el-row>
	</el-form>
</template>
<script>
import { FileApi } from '@/api'
import {
	projectBidding,
	projectBiddingDetail
} from '@/api/projectManagement/index'
export default {
	name: 'shejidbiao',
	props: {
		// 父组件方法
		showDialog2: {
			type: Function,
			default: null
		},
		getList2: {
			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: '',
			// 表单验证
			rules: {
				xmzbbabh: [
					{
						required: true,
						message: '请输入招标备案编号',
						trigger: 'blur'
					}
				],
				xmzbfs: [
					{
						required: true,
						message: '请输入招标方式',
						trigger: 'blur'
					}
				],
				xmzbjg: [
					{
						required: true,
						message: '请输入招标价格',
						trigger: 'blur'
					}
				],
				zbjg: [
					{
						required: true,
						message: '请输入中标金额',
						trigger: 'blur'
					}
				],
				zbdw: [
					{
						required: true,
						message: '请输入中标单位',
						trigger: 'blur'
					}
				],
				zbsj: [
					{
						required: true,
						message: '请输入中标时间',
						trigger: 'blur'
					}
				],
				xmzbqkList: [
					{
						required: true,
						message: '请上传项目申请材料',
						trigger: 'change'
					}
				]
			},
			// 提交form
			ruleForm: {
				xmzbFileVOList: []
			},
			// 文件上传路径
			actionUrl: FileApi + '/file/upload',
			// 项目申请附件
			xmzbqkList: [],
			// 项目申请附件长度
			xmzbqkLength: '',
			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.xmzbqkList.forEach((item, index) => {
				console.log(this.buttonIndex, index)
				if (this.buttonIndex == index) {
					item.xmclFileList.push(obj)
				}
			})
			this.ruleForm.xmzbqkList = this.xmzbqkList
			console.log('上传.res:', res)
			console.log('上传.obj:', obj)
			console.log('上传.this.xmzbqkList:', this.xmzbqkList)
		},
		// 文件下载
		downLoadFile(id) {
			window.open(`${FileApi}/file/download?id=${id}`)
		},
		// 删除文件
		deleteFile(index, index2) {
			this.xmzbqkList[index].xmclFileList[index2].status = 0
		},
		// 取消按钮
		dialogVisible() {
			this.$emit('showDialog2', 'false')
			this.$emit('getList2')
		},
		getInfo() {
			let data = {
				xmsqId: this.fatherObj.xmsqId,
				xmzblx: 1
			}
			projectBiddingDetail(data).then((res) => {
				if (res.data && res.infoCode === 200) {
					this.ruleForm = res.data
					if (res.data.zbsj) {
						this.ruleForm.zbsj = res.data.zbsj.slice(0, 10)
					}
					console.log(this.ruleForm.zbsj)
					this.xmzbqkList = res.data.xmzbqkList
					this.xmzbqkLength = res.data.xmzbqkList.length
					console.log('设计招标this.ruleForm:', this.ruleForm)
				}
			})
		},
		// 0保存 1提交
		saveInfo(type, formName) {
			this.$refs[formName].validate((valid) => {
				if (valid) {
					let data = this.ruleForm
					data.dataStatus = type
					data.xmzblx = 1
					data.xmsqId = this.fatherObj.xmsqId
					console.log('设计招标data:', data)
					projectBidding(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: msg
							})
						}
						this.dialogVisible()
					})
				} else {
					console.log('error submit!!')
					return false
				}
			})
		},
		addList() {
			let obj = {
				sjId: '',
				xmsqId: '',
				xmcllx: 3,
				xmclmc: '',
				xmclsbsj: this.nowTime,
				xmclFileList: []
			}
			this.xmzbqkList.push(obj)
			this.xmzbqkLength = this.xmzbqkList.length
			this.ruleForm.xmzbqkList = this.xmzbqkList
			console.log('addList方法this.xmzbqkList:', this.xmzbqkList)
		},
		removeList() {
			this.ruleForm.xmzbqkList.pop()
			this.xmzbqkLength = this.xmzbqkList.length
		},
		// 获取上传按钮在表格中的索引
		getIndex(index) {
			this.buttonIndex = index
			console.log('getIndex方法index:', index)
		},
		deleteInfo(row) {
			let data = {
				xmId: row.xmId
			}
			deleteProject(data).then((res) => {
				if (res.infoCode === 200) {
					this.$message({
						type: 'success',
						message: '删除成功!'
					})
					this.getList()
				} else {
					this.$message({
						type: 'error',
						message: '删除失败!'
					})
				}
			})
		}
	},
	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;
}
.el-date-editor {
	&.el-input,
	&.el-input__inner {
		width: 100%;
	}
}

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