<template>
	<div>
		<div class="crumbs">
			<el-breadcrumb separator="/">
				<el-breadcrumb-item><i class="el-icon-lx-punch"></i> 数据库</el-breadcrumb-item>
				<el-breadcrumb-item>数据库资料</el-breadcrumb-item>
			</el-breadcrumb>
		</div>
		<div class="container">
			<div class="title_header">
				<span>{{title_header}}</span>
			</div>
			<div class="handle-box">
				<el-button type="primary" icon="el-icon-plus" class="handle-del" @click="add_Selection">添加</el-button>
			</div>
			<ul class="up_list">
				<li v-for="item in listData">
					<h4>{{ item.title }}</h4>
					<p>{{ item.ctime }}</p>
					<div>{{ item.url }} <i @click="loadfile(item, $event)" class="el-icon-lx-down"></i></div>
					<span v-if="role_pd == '0'"><i @click="deletfile(item, $event)" class="el-icon-lx-delete red"></i></span>
				</li>
			</ul>
			<div class="pagination">
				<el-pagination background layout="total, prev, pager, next" :current-page="query.pageIndex" :page-size="query.pageSize"
				 :total="link_Total" @current-change="handlePageChange"></el-pagination>
			</div>
		</div>

		<!-- 添加弹出框 -->
		<el-dialog title="添加" :visible.sync="addVisible" width="30%">
			<el-form ref="add_Form" :model="addForm" :rules="rules" label-width="85px">
				<el-form-item label="标题" prop="name">
					<el-input v-model="addForm.name" type="textarea" rows="4"></el-input>
				</el-form-item>
				<el-form-item label="文件上传">
					<el-upload class="upload-demo" :action="IMGup_Url+'master/Datebase/up_file'" :on-preview="handlePreview"
					 :on-remove="handleRemove" :before-remove="beforeRemove" :on-success="handleAvatarSuccess" multiple name="dbdata"
					 :on-exceed="handleExceed" :file-list="fileList" :limit="1">
						<el-button size="small" type="primary">点击上传</el-button>
					</el-upload>
				</el-form-item>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button @click="addVisible = false">取 消</el-button>
				<el-button type="primary" @click="saveAdd(addForm)">确 定</el-button>
			</span>
		</el-dialog>

	</div>
</template>

<script>
	export default {
		data() {
			return {
				query: {
					db_id: this.$route.query.db_num,
					pageIndex: 1,
					pageSize: 20
				},
				addForm: {
					db_id: this.$route.query.db_num,
				},
				role_pd: localStorage.getItem('admin_role'),//身份判断
				IMGdo_Url: this.IMGdoURL,
				IMGup_Url: this.IMGupURL,
				fileList: [],
				listData: [],
				multipleSelection: [],
				delList: [],
				addVisible: false,
				link_Total: 0,
				aid: -1,
				title_header: "",
				id: -1,
				rules: {
					name: [{
						required: true,
						message: '请输入标题',
						trigger: 'blur'
					}]
				}
			};
		},
		created() {
			this.getData();
		},
		methods: {
			// 获取资料库列表数据
			getData() {
				this.$api
					.post('master/Datebase/getDatas', this.query)
					.then(res => {
						if (res.code == 0) {
							this.title_header = res.data.dbinfo.name;
							for (let i = 0; i < res.data.dataList.length; i++) {
								let date = new Date(parseInt(res.data.dataList[i].ctime) * 1000);
								let Y = date.getFullYear() + '-';
								let M = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) + '-' : date.getMonth() + 1 + '-';
								let D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
								res.data.dataList[i].ctime = Y + M + D;
							}
							this.listData = res.data.dataList;
							this.link_Total = res.data.dataList.length || 0;

						} else {
							this.$message.error(res.msg);
						}
					})
					.catch(err => {
						this.$message.error(err);
					});
			},
			// 下载
			loadfile(item, event) {
				window.open(this.IMGdo_Url + 'dbdata/' + item.url);
			},
			// 删除
			deletfile(item, event) {
				this.$confirm('确定要删除吗？', '提示', {
						type: 'warning'
					})
					.then(() => {
						this.$api
							.post('master/Datebase/delData', item)
							.then(res => {
								if (res.code == 0) {
									this.$message.success('删除成功');
									this.getData();
								} else {
									this.$message.error(res.msg);
								}
							})
							.catch(err => {
								this.$message.error(err);
							});
					})
					.catch(() => {});
			},
			// 添加操作
			add_Selection(index, row) {
				this.aid = index;
				this.addVisible = true;
			},
			// 保存添加
			saveAdd(addForm) {
				if (this.addForm.url == undefined || this.addForm.url == "") {
					this.$message.error("请上传附件！");
				} else {
					this.$refs.add_Form.validate((valid) => {
						if (valid) {
							this.$api
								.post('master/Datebase/addData', this.addForm)
								.then(res => {
									if (res.code == 0) {
										this.addVisible = false;
										this.$refs.add_Form.resetFields();
										this.$message.success(`添加成功`);
										this.getData();
									} else {
										this.$message.error(res.msg);
									}
								})
								.catch(err => {
									this.$message.error(err);
								});

						} else {
							this.$message.error('error submit!!');
							return false;
						}
					});
				}

			},

			// 删除操作
			handleDelete(index, row) {
				// 二次确认删除
				this.$confirm('确定要删除吗？', '提示', {
						type: 'warning'
					})
					.then(() => {
						this.$api
							.post('master/Journal/delJournalAbs', row)
							.then(res => {
								if (res.code == 0) {
									this.$message.success('删除成功');
									this.getData();
								} else {
									this.$message.error(res.msg);
								}
							})
							.catch(err => {
								this.$message.error(err);
							});
					})
					.catch(() => {});
			},

			// 文件上传
			handleAvatarSuccess(res, file) {
				if (res.code == 0) {
					this.addForm.url = res.upurl;
				} else {
					this.$message.error(res.msg);
				}
			},
			handleRemove(file, fileList) {
				this.addForm.url = "";
			},
			handlePreview(file) {
				// console.log(file);
			},
			handleExceed(files, fileList) {
				this.$message.warning(`当前限制选择只能上传 1 个文件，请先删除`);
			},
			beforeRemove(file, fileList) {
				return this.$confirm(`确定移除 ${ file.name }？`);
			},

			// 分页导航
			handlePageChange(val) {
				this.$set(this.query, 'pageIndex', val);
				this.getData();
			}
		}
	};
</script>

<style>
	.title_header {
		margin-bottom: 20px;
	}
	.handle-box {
		margin-bottom: 20px;
	}

	.upload-demo .el-upload {
		text-align: left;
		width: 200px;
		height: 35px;
	}

	.el-upload-list__item:first-child {
		margin-top: 0;
	}

	.el-upload--text {
		border: 0;
	}

	ul.up_list li {
		list-style: none;
		color: #606266;
		font-size: 14px;
		border-bottom: 1px solid #dcdfe6;
		padding-bottom: 20px;
		margin-bottom: 20px;
	}

	ul.up_list li .red {
		color: #ff0000;
		font-weight: bolder;
	}

	ul.up_list li {
		position: relative;
	}
	ul.up_list li span{
		position: absolute;
		top: -8px;
		right: 8px;
		cursor: pointer;
		
	}
	ul.up_list li h4 {
		padding: 0 40px 0 10px;
	}

	ul.up_list li p {
		padding: 0 10px;
		text-align: right;
	}

	ul.up_list li div {
		padding: 10px 10px 0 10px;;
	}

	ul.up_list li div i {
		cursor: pointer;
		font-weight: bolder;
		color: #0175ec;
		margin-left: 10px;
	}
</style>
