<template>
	<div>
		<div class="crumbs">
			<el-breadcrumb separator="/">
				<el-breadcrumb-item><i class="el-icon-lx-cascades"></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>
			<el-table :data="tableData" border class="table" ref="multipleTable" header-cell-class-name="table-header">
				<el-table-column prop="title" label="标题">
					<template slot-scope="scope">
						<div v-html="scope.row.title"></div>
					</template>
				</el-table-column>
				<el-table-column label="封面" align="center" width="160">
					<template slot-scope="scope">
						<el-image class="table-td-thumb" :src="IMGdo_Url+'journalline/'+scope.row.img" v-if="scope.row.img!=''"></el-image>
					</template>
				</el-table-column>
				<el-table-column prop="time" label="时间" width="220px"></el-table-column>
				<el-table-column label="操作" width="200" align="left">
					<template slot-scope="scope">
						<el-button type="text" icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
						<el-button type="text" icon="el-icon-delete" class="red" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
						<router-link :to="{path:'/joutiactile',query: {top_num: scope.row.journal_id,time_id: scope.row.journal_line_id}}"
						 style="margin-left: 10px;">
							<el-button type="text" icon="el-icon-lx-text" class="orange">文章</el-button>
						</router-link>
					</template>
				</el-table-column>
			</el-table>
		</div>

		<!-- 添加弹出框 -->
		<el-dialog title="添加" :visible.sync="addVisible" width="40%">
			<el-form ref="add_Form" :model="addForm" :rules="rules" label-width="85px">
				<el-form-item label="标题" prop="title">
					<el-input v-model="addForm.title"></el-input>
				</el-form-item>
				<el-form-item label="时间" prop="time">
					<el-input v-model="addForm.time"></el-input>
				</el-form-item>
				<el-form-item label="距离" prop="dist">
					<el-input v-model="addForm.dist"></el-input>
				</el-form-item>
				<el-form-item label="封面">
					<el-upload class="avatar-uploader" :action="IMGup_Url+'master/Journal/up_line_file'" :show-file-list="false" name="journalline"
					 :on-success="handleAvatarSuccess" :on-error="handleAvatarError" :before-upload="beforeAvatarUpload">
						<img v-if="imageUrl" :src="imageUrl" class="avatar">
						<i v-else class="el-icon-plus avatar-uploader-icon"></i>
					</el-upload>
				</el-form-item>
				<el-form-item label="简介" prop="intro">
					<el-input v-model="addForm.intro" type="textarea" rows="6"></el-input>
				</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>

		<!-- 编辑弹出框 -->
		<el-dialog title="编辑" :visible.sync="editVisible" width="40%">
			<el-form ref="edit_Form" :model="editForm" :rules="rules" label-width="85px">
				<el-form-item label="标题" prop="title">
					<el-input v-model="editForm.title"></el-input>
				</el-form-item>
				<el-form-item label="时间" prop="time">
					<el-input v-model="editForm.time"></el-input>
				</el-form-item>
				<el-form-item label="距离" prop="dist">
					<el-input v-model="editForm.dist"></el-input>
				</el-form-item>
				<el-form-item label="封面">
					<el-upload class="avatar-uploader" :action="IMGup_Url+'master/Journal/up_line_file'" :show-file-list="false" name="journalline"
					 :on-success="handleEditSuccess" :on-error="handleAvatarError" :before-upload="beforeAvatarUpload">
						<img v-if="imageUrl" :src="imageUrl" class="avatar">
						<i v-else class="el-icon-plus avatar-uploader-icon"></i>
					</el-upload>
				</el-form-item>
				<el-form-item label="简介" prop="intro">
					<el-input v-model="editForm.intro" type="textarea" rows="4"></el-input>
				</el-form-item>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button @click="editVisible = false">取 消</el-button>
				<el-button type="primary" @click="saveEdit(editForm)">确 定</el-button>
			</span>
		</el-dialog>
	</div>
</template>

<script>
	export default {
		data() {
			return {
				addForm: {},
				tableData: [],
				editVisible: false,
				addVisible: false,
				aid: -1,
				editForm: {},
				title_header: "",
				idx: -1,
				id: -1,
				IMGdo_Url: this.IMGdoURL,
				IMGup_Url: this.IMGupURL,
				imageUrl: '',
				rules: {
					title: [{
						required: true,
						message: '请输入标题',
						trigger: 'blur'
					}],
					time: [{
						required: true,
						message: '请输入时间',
						trigger: 'blur'
					}],
					dist: [{
						required: true,
						message: '请输入距离',
						trigger: 'blur'
					}],
					intro: [{
						required: true,
						message: '请输入简介',
						trigger: 'blur'
					}]
				}
			};
		},
		created() {
			this.getData();
		},
		methods: {
			// 获取时间轴文章列表数据
			getData() {
				this.addForm.journal_id = this.$route.query.top_id;
				this.$api
					.post('master/Journal/getJournalLines', {
						'journal_id': this.$route.query.top_id
					})
					.then(res => {
						if (res.code == 0) {
							this.title_header = res.data.journal.title;
							this.tableData = res.data.lines;
						} else {
							this.$message.error(res.msg);
						}
					})
					.catch(err => {
						this.$message.error(err);
					});
			},

			// 添加操作
			add_Selection(index, row) {
				this.aid = index;
				this.addVisible = true;
				this.imageUrl = "";
			},
			// 保存添加
			saveAdd(addForm) {
				this.$refs.add_Form.validate((valid) => {
					if (valid) {
						this.$api
							.post('master/Journal/addJournalLine', this.addForm)
							.then(res => {
								if (res.code == 0) {
									this.addVisible = false;
									this.$refs.add_Form.resetFields();
									this.$message.success(`添加成功`);
									this.imageUrl = "";
									this.getData();
								} else {
									this.$message.error(res.msg);
								}
							})
							.catch(err => {
								this.$message.error(err);
							});

					} else {
						this.$message.error('error submit!!');
						return false;
					}
				});
			},
			// 编辑操作
			handleEdit(index, row) {
				this.idx = index;
				this.editForm = Object.assign({}, row);
				this.editVisible = true;
				this.imageUrl = this.IMGdo_Url + 'journalline/' + row.img;
			},
			// 保存编辑
			saveEdit(editForm) {
				this.$refs.edit_Form.validate((valid) => {
					if (valid) {
						this.$api
							.post('master/Journal/editJournalLine', this.editForm)
							.then(res => {
								if (res.code == 0) {
									this.editVisible = false;
									this.$message.success(`更改第 ${this.idx + 1} 行成功`);
									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/delJournalLine', 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.img = res.upurl;
				} else {
					this.$message.error(res.msg);
				}
				this.imageUrl = URL.createObjectURL(file.raw);
			},
			handleEditSuccess(res, file) {
				if (res.code == 0) {
					this.editForm.img = res.upurl;
				} else {
					this.$message.error(res.msg);
				}
				this.imageUrl = URL.createObjectURL(file.raw);
			},
			handleAvatarError(res, file) {
				// this.$message.error(res);
			},
			beforeAvatarUpload(file) {
				const isLt2M = file.size / 1024 / 1024 < 1;
				if (!isLt2M) {
					this.$message.error('图片大小不能超过 1M!');
				}
				return isLt2M;
			}
			
		}
	};
</script>

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

	.orange {
		color: #e6a23c;
	}


	.table {
		width: 100%;
		font-size: 14px;
	}

	.red {
		color: #ff0000;
	}

	.table-td-thumb {
		display: block;
		margin: auto;
		width: 40px;
		height: 40px;
	}
	
	.avatar-uploader .el-upload {
		border: 1px dashed #d9d9d9;
		border-radius: 6px;
		cursor: pointer;
		position: relative;
		overflow: hidden;
		width: 140px;
		height: 140px;
	}
	
	.avatar-uploader_small {
		height: 140px;
	}
	
	.avatar-uploader .el-upload:hover {
		border-color: #409EFF;
	}
	
	.avatar-uploader-icon {
		font-size: 28px;
		color: #8c939d;
		width: 140px;
		height: 140px;
		line-height: 140px;
		text-align: center;
	}
	
	.avatar-uploader_small .el-upload {
		width: 80px;
		height: 80px;
	}
	.avatar-uploader_small .avatar-uploader-icon {
		line-height: 80px;
		margin-left: -30px;
	}
	
	.avatar {
		width: 140px;
		height: 140px;
		display: block;
	}
</style>
