<template>
	<div>
		<div class="crumbs">
			<el-breadcrumb separator="/">
				<el-breadcrumb-item>
					<i class="el-icon-paperclip"></i> Editorial Group list
				</el-breadcrumb-item>
			</el-breadcrumb>
		</div>
		<div class="container">
			<div class="handle-box" :rules="rules">
				<el-select v-model="addForm.journal_id" placeholder="Please select a journal" @change="getJourDate" style="width: 300px;">
					<el-option v-for="item in df_jour" :label="item.title" :key="item.journal_id"
						:value="item.journal_id"></el-option>
				</el-select>
				<el-button type="primary" icon="el-icon-plus" @click="addGroup" style="float: right;">
					Add Group</el-button>
			</div>
			<el-table :data="tableData" border stripe class="table" ref="multipleTable"
				header-cell-class-name="table-header" empty-text="New messages (0)">
				<el-table-column prop="group_name" label="Group name"></el-table-column>
				<el-table-column prop="person_num" label="Person num" align="center" width="150"></el-table-column>
				<el-table-column label="" width="110" align="center">
					<template slot-scope="scope">
						<el-button size="mini" type="danger" plain icon="el-icon-delete"
							@click="handleDelete(scope.$index, scope.row)">Delete</el-button>
					</template>
				</el-table-column>
			</el-table>
		</div>

		<!-- 添加弹出框 -->
		<el-dialog title="Add Editorial board member" :visible.sync="addVisible" width="500px">
			<el-form ref="add_Form" :model="addForm" :rules="rules" label-width="120px">
				<el-form-item label="Jouranl :">
					{{addForm.journal_title}}
				</el-form-item>
				<el-form-item label="Group name :" prop="group_name">
					<el-input v-model="addForm.group_name"></el-input>
				</el-form-item>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button @click="addVisible = false">Cancel</el-button>
				<el-button type="primary" @click="saveAdd(addForm)">OK</el-button>
			</span>
		</el-dialog>

	</div>
</template>

<script>
	export default {
		data() {
			return {
				edit_id: localStorage.getItem('U_id'),
				tableData: [],
				df_jour: [],
				addVisible: false,
				addForm: {
					journal_id: 0,
					journal_title: '',
					group_name: ''
				},
				journalId: 0,
				rules: {
					group_name: [{
						required: true,
						message: 'Please input group name',
						trigger: 'blur'
					}],
				}
			};
		},
		created() {
			this.getJour();
		},
		methods: {
			// 获取期刊列表
			getJour() {
				this.$api
					.post('api/Chief/getJournalsByEditor', {
						'user_id': this.edit_id
					})
					.then(res => {
						if (res.code == 0) {
							this.df_jour = res.data.journals;
							this.addForm.journal_id = this.df_jour[0].journal_id
							this.addForm.journal_title = this.df_jour[0].title
							this.getDate();
						} else {
							this.$message.error(res.msg);
						}
					})
					.catch(err => {
						this.$message.error(err);
					});
			},

			// 获取分组数据
			getDate() {
				this.$api
					.post('api/Board/getBoardGroupList', {
						journal_id: this.addForm.journal_id
					})
					.then(res => {
						if (res.code == 0) {
							this.tableData = res.data.groups
						} else {
							this.$message.error(res.msg);
						}
					})
					.catch(err => {
						this.$message.error(err);
					});
			},

			// 期刊下拉
			getJourDate() {
				for (var i = 0; i < this.df_jour.length; i++) {
					if (this.df_jour[i].journal_id == this.addForm.journal_id) {
						this.addForm.journal_title = this.df_jour[i].title
					}
				}
				this.getDate()
			},

			// 添加弹出
			addGroup() {
				this.addVisible = true
			},

			// 保存添加
			saveAdd(addForm) {
				this.$refs.add_Form.validate((valid) => {
					if (valid) {
						this.$api
							.post('api/Board/addBoardGroup', this.addForm)
							.then(res => {
								if (res.code == 0) {
									this.addVisible = false;
									this.$refs.add_Form.resetFields();
									this.$message.success(`Added successfully！`);
									this.getDate();
								} else {
									this.$message.error(res.msg);
								}
							})
							.catch(err => {
								this.$message.error(err);
							});
					} else {
						return false;
					}
				});

			},

			// 删除操作
			handleDelete(index, row) {
				// 二次确认删除
				this.$confirm('Are you sure you want to delete？', 'Tips', {
						type: 'warning'
					})
					.then(() => {
						this.$api
							.post('api/Board/delBoardGroup', row)
							.then(res => {
								if (res.code == 0) {
									this.$message.success('Delete succeeded');
									this.getDate();
								} else {
									this.$message.error(res.msg);
								}
							})
							.catch(err => {
								this.$message.error(err);
							});
					})
					.catch(() => {});
			}
		}
	};
</script>

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

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

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