<template>
	<div>
		<div class="crumbs">
			<el-breadcrumb separator="/">
				<el-breadcrumb-item><i class="el-icon-lx-cascades"></i> 期刊管理</el-breadcrumb-item>
				<el-breadcrumb-item>Call for Paper管理</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="标题"></el-table-column>
				<el-table-column label="图片" align="center" width="160">
					<template slot-scope="scope">
						<el-image class="table-td-thumb" :src="IMGdo_Url+'journalCfp/'+scope.row.icon"></el-image>
					</template>
				</el-table-column>
				<el-table-column align="center" label="时间" width="200px" prop="showtime"></el-table-column>
				<el-table-column align="center" label="权重" width="100px" prop="sort"></el-table-column>
				<el-table-column label="操作" width="180" align="center">
					<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>
					</template>
				</el-table-column>
			</el-table>
		</div>

		<!-- 添加弹出框 -->
		<el-dialog title="添加" :visible.sync="addVisible" width="50%">
			<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="showtime">
					<el-input v-model="addForm.showtime" placeholder="eg : 1990-01-01"></el-input>
				</el-form-item>
				<el-form-item label="内容" prop="content">	
					<quill-editor ref="myTextEditor" v-model="addForm.content" :options="editorOption"></quill-editor>
					<el-upload class="avatar-uploader_" :action="IMGup_Url+'master/Journal/up_cfp_file'" name="journalCfp"
					 :show-file-list="false" :on-success="uploadSuccess">
					</el-upload>
				</el-form-item>
				<el-form-item label="图片" label-width="85px">
					<el-upload class="avatar-uploader" :action="IMGup_Url+'master/Journal/up_cfp_file'" :show-file-list="false" name="journalCfp"
					 :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="权重">
					<el-input-number v-model="addForm.sort" :min="0"></el-input-number>
				</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="50%">
			<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="showtime">
					<el-input v-model="editForm.showtime" placeholder="eg : 1990-01-01"></el-input>
				</el-form-item>
				<el-form-item label="内容" prop="content">
					<quill-editor ref="myTextEditor" v-model="editForm.content" :options="editorOption"></quill-editor>
					<el-upload class="avatar-uploader_" :action="IMGup_Url+'master/Journal/up_cfp_file'" name="journalCfp"
					 :show-file-list="false" :on-success="uploadSuccess">
					</el-upload>
				</el-form-item>
				<el-form-item label="图片" label-width="85px">
					<el-upload class="avatar-uploader" :action="IMGup_Url+'master/Journal/up_cfp_file'" :show-file-list="false" name="journalCfp"
					 :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="权重">
					<el-input-number v-model="editForm.sort" :min="0"></el-input-number>
				</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>
	import 'quill/dist/quill.core.css';
	import 'quill/dist/quill.snow.css';
	import 'quill/dist/quill.bubble.css';
	import {
		quillEditor
	} from 'vue-quill-editor';
	export default {
		data() {
			return {
				addForm: {

				},
				tableData: [],
				editVisible: false,
				addVisible: false,
				IMGdo_Url: this.IMGdoURL,
				IMGup_Url: this.IMGupURL,
				IMGqi_Url: this.IMGqiURL,
				imageUrl: '',
				aid: -1,
				editForm: {},
				title_header: "",
				idx: -1,
				id: -1,
				content: '',
				editorOption: {
					placeholder: 'Please enter...',
					modules: {
						toolbar: {
							container: [
								['bold', 'italic', 'underline', 'strike'],
								['blockquote', 'code-block'],
								[{
									'header': 1
								}, {
									'header': 2
								}],
								[{
									'list': 'ordered'
								}, {
									'list': 'bullet'
								}],
								[{
									'script': 'sub'
								}, {
									'script': 'super'
								}],
								[{
									'indent': '-1'
								}, {
									'indent': '+1'
								}],
								[{
									'direction': 'rtl'
								}],
								[{
									'size': ['small', false, 'large', 'huge']
								}],
								[{
									'header': [1, 2, 3, 4, 5, 6, false]
								}],
								[{
									'color': []
								}, {
									'background': []
								}],
								[{
									'font': []
								}],
								[{
									'align': []
								}],
								['link', 'image', 'video']
							],
							// 工具栏
							handlers: {
								image: function(value) {
									if (value) {
										// upload点击上传事件
										document.querySelector('.avatar-uploader_ input').click()
									} else {
										this.quill.format('image', false)
									}
								}
							}
						}
					}
				},
				rules: {
					title: [{
						required: true,
						message: '请输入标题',
						trigger: 'blur'
					}],
					showtime: [{
						required: true,
						message: '请输入时间',
						trigger: 'blur'
					}],
					content: [{
						required: true,
						message: '请输入内容',
						trigger: 'blur'
					}]
				}
			};
		},
		created() {
			this.getData();
		},
		methods: {
			// 获取Call for Paper列表数据
			getData() {
				this.addForm.journal_id = this.$route.query.top_id;
				this.$api
					.post('master/Journal/getJournalCfps', {
						'journal_id': this.$route.query.top_id
					})
					.then(res => {
						if (res.code == 0) {
							this.title_header = res.data.journal.title;
							this.tableData = res.data.cfps;
						} 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) {
				if (this.addForm.icon == undefined) {
					this.$message.error("请插入图片");
				} else {
					this.$refs.add_Form.validate((valid) => {
						if (valid) {
							this.$api
								.post('master/Journal/addJournalCfp', 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 + 'journalCfp/' + row.icon;
			},
			// 保存编辑
			saveEdit(editForm) {
				this.$refs.edit_Form.validate((valid) => {
					if (valid) {
						this.$api
							.post('master/Journal/editJournalCfp', 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/delJournalCfp', 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.icon = res.upurl;
				} else {
					this.$message.error(res.msg);
				}
				this.imageUrl = URL.createObjectURL(file.raw);
			},
			handleEditSuccess(res, file) {
				if (res.code == 0) {
					this.editForm.icon = res.upurl;
				} else {
					this.$message.error(res.msg);
				}
				this.imageUrl = URL.createObjectURL(file.raw);
			},
			handleAvatarError(res, file) {

			},
			beforeAvatarUpload(file) {
				const isLt2M = file.size / 1024 / 1024 < 1;
				if (!isLt2M) {
					this.$message.error('图片大小不能超过 1M!');
				}
				return isLt2M;
			},
			// 富文本编辑器
			onEditorChange({
				editor,
				html,
				text
			}) {
				this.content = html;
			},
			
			// 上传成功
			uploadSuccess(res) {
				let quill = this.$refs.myTextEditor.quill;
			
				// 获取光标所在位置
				let length = quill.getSelection().index
				// 插入图片 res.upurl为服务器返回的图片地址
				quill.insertEmbed(length, 'image', this.IMGqi_Url + 'journalCfp/' + res.upurl)
				// 调整光标到最后
				quill.setSelection(length + 1)
			
			}
		},

		components: {
			quillEditor
		},
	};
</script>

<style>
	.title_header {
		margin-bottom: 20px;
	}
	
	.ql-editor {
		height: 320px;
	}

	.avatar-uploader .el-upload {
		border: 1px dashed #d9d9d9;
		border-radius: 6px;
		cursor: pointer;
		position: relative;
		overflow: hidden;
		width: 140px;
		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 {
		width: 140px;
		height: 140px;
		display: block;
	}

	.handle-box {
		margin-bottom: 20px;
	}

	.handle-input {
		width: 300px;
		display: inline-block;
	}

	.handle-pwd {
		cursor: pointer;
	}

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

	.red {
		color: #ff0000;
	}

	.mr10 {
		margin-right: 10px;
	}

	.table-td-thumb {
		display: block;
		margin: auto;
		width: 40px;
		height: 40px;
	}
	
	.avatar-uploader_ {
		height: 0;
	}
	
	.avatar-uploader_ .el-upload--text {
		height: 0 !important;
		border: 0;
	}
	
</style>
