<template>
	<div>
		<div class="crumbs">
			<el-breadcrumb separator="/">
				<el-breadcrumb-item>
					<i class="el-icon-lx-global"></i> For Author
				</el-breadcrumb-item>
			</el-breadcrumb>
		</div>
		<div class="container">
			<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" row-key="jfa_id" border class="table" ref="multipleTable" header-cell-class-name="table-header"
			 :default-expand-all="true" :tree-props="{children: 'children', hasChildren: true}">
				<el-table-column prop="jfa_title" label="标题"></el-table-column>
				<el-table-column prop="sort" label="权重" width="80" align="center"></el-table-column>
				<el-table-column label="操作" width="140" 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="800px">
			<el-form ref="add_Form" :model="addForm" :rules="rules" label-width="85px">
				<el-form-item label="分组" prop="pid">
					<el-select v-model="addForm.pid" filterable value-key="groupID">
						<el-option :key="0" label="None" :value="0"></el-option>
						<el-option v-for="item in options" :key="item.jfa_id" :label="item.jfa_title" :value="item.jfa_id"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="标题" prop="jfa_title">
					<el-input v-model="addForm.jfa_title"></el-input>
				</el-form-item>
				<el-form-item label="内容">
					<quill-editor ref="myTextEditor" v-model="addForm.content" :options="editorOption"></quill-editor>
				</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="800px">
			<el-form ref="edit_Form" :model="editForm" :rules="rules" label-width="85px">
				<el-form-item label="分组">
					<p style="margin: 0 0 0 15px;" v-if="editForm.pid==0">None</p>
					<p style="margin: 0 0 0 15px;" v-if="editForm.pid!=0">{{editForm.p_title}}</p>
				</el-form-item>
				<el-form-item label="标题" prop="jfa_title">
					<el-input v-model="editForm.jfa_title"></el-input>
				</el-form-item>
				<el-form-item label="内容">
					<quill-editor ref="myTextEditor" v-model="editForm.content" :options="editorOption"></quill-editor>
				</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 {
				IMGdo_Url: this.IMGdoURL,
				IMGup_Url: this.IMGupURL,
				addVisible: false,
				editVisible: false,
				addForm: {
					journal_id: 0,
					sort: 0,
					pid: 0,
					content:''
				},
				editForm: {},
				tableData: [],
				editorOption: {
					placeholder: 'Please enter...'
				},
				options: [],
				rules: {
					jfa_title: [{
						required: true,
						message: '请输入标题',
						trigger: 'blur'
					}],
					pid: [{
						required: true,
						message: '请选择分组',
						trigger: 'blur'
					}],
					content: [{
						required: true,
						message: '请输入消息内容',
						trigger: 'blur'
					}]
				}
			};
		},
		created() {
			this.getData();
		},
		methods: {
			// 获取footer列表数据
			getData() {
				this.$api
					.post('master/Journal/getForAuthorList', {
						journal_id: 0
					})
					.then(res => {
						if (res.code == 0) {
							this.tableData = res.data.forAuthors
							this.options = res.data.forAuthors
						} else {
							this.$message.error(res.msg);
						}
					})
					.catch(err => {
						this.$message.error(err);
					});
			},
			// 添加操作
			add_Selection(index, row) {
				this.addVisible = true;
			},
			// 保存添加
			saveAdd(addForm) {
				this.$refs.add_Form.validate((valid) => {
					if (valid) {
						this.$api
							.post('master/Journal/addForAuthor', this.addForm)
							.then(res => {
								if (res.code == 0) {
									this.addVisible = false;
									this.$refs.add_Form.resetFields();
									this.addForm.sort = 0;
									this.$message.success(`添加成功`);
									this.getData();
								} else {
									this.$message.error(res.msg);
								}
							})
							.catch(err => {
								this.$message.error(err);
							});
					} else {
						return false;
					}
				});

			},
			// 编辑操作
			handleEdit(index, row) {
				this.editForm = Object.assign({}, row);
				this.editVisible = true;
			},
			// 保存编辑
			saveEdit(editForm) {
				this.$refs.edit_Form.validate((valid) => {
					if (valid) {
						this.$api
							.post('master/Journal/editForAuthor', this.editForm)
							.then(res => {
								if (res.code == 0) {
									this.editVisible = false;
									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/delForAuthor', 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(() => {});
			},
			// 富文本编辑器
			onEditorChange({
				editor,
				html,
				text
			}) {
				this.content = html;
			}
		},

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

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

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

	.red {
		color: #e71313;
	}
</style>
