<template>
	<div>
		<div class="crumbs">
			<el-breadcrumb separator="/">
				<el-breadcrumb-item>
					<i class="el-icon-connection"></i> Editorial board list
				</el-breadcrumb-item>
			</el-breadcrumb>
		</div>
		<div class="container">
			<div class="handle-box">
				<el-button type="primary" icon="el-icon-circle-plus-outline" @click="addEditor">add</el-button>
			</div>
			<div class="handle-box" :rules="rules">
				<el-select v-model="query.journal_id" placeholder="请选择期刊" @change="chan_id">
					<el-option v-for="item in df_jour" :label="item.title" :key="item.journal_id" :value="item.journal_id"></el-option>
				</el-select>
			</div>
			<el-table :data="tableData" border stripe class="table" ref="multipleTable" header-cell-class-name="table-header">
				<el-table-column prop="account" label="username" align="center"></el-table-column>
				<el-table-column prop="realname" label="realname" align="center"></el-table-column>
				<el-table-column prop="phone" label="phone" align="center"></el-table-column>
				<el-table-column prop="email" label="email" align="center"></el-table-column>
				<el-table-column label="" width="100" align="center">
					<template slot-scope="scope">
						<el-button type="text" icon="el-icon-delete" class="red" @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="40%">
			<el-form ref="add_Form" :model="addForm" :rules="rules" label-width="150px">
				<el-form-item label="account" prop="account">
					<el-input v-model="addForm.account" @blur='saerNa_U()'></el-input>
				</el-form-item>
				<el-form-item label="email" prop="email">
					<el-input v-model="addForm.email" @blur='saerEm_U()'></el-input>
				</el-form-item>
				<el-form-item label="journal" prop="journal_id">
					<el-select v-model="addForm.journal_id" filterable placeholder="Please select a responsible Journal" value-key="groupID"
					 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-form-item>
				<el-form-item label="realname" prop="realname">
					<el-input v-model="addForm.realname"></el-input>
				</el-form-item>
				<el-form-item label="password" prop="password">
					<el-input v-model="addForm.password" type="password"></el-input>
				</el-form-item>
				<el-form-item label="Confirm password" prop="repassword">
					<el-input v-model="addForm.repassword" type="password"></el-input>
				</el-form-item>
				<el-form-item label="phone" prop="phone">
					<el-input v-model="addForm.phone"></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)" :disabled="this.btn_alert">OK</el-button>
			</span>
		</el-dialog>


		<!-- 已有信息弹出框 -->
		<el-dialog title="Add Editorial board member" :visible.sync="oldVisible" width="40%">
			<el-form ref="old_Form" :model="oldForm" :rules="rules" label-width="100px">
				<el-form-item label="account">
					{{oldForm.account}}
				</el-form-item>
				<el-form-item label="email">
					{{oldForm.email}}
				</el-form-item>
				<el-form-item label="journal" prop="journal_id">
					<el-select v-model="oldForm.journal_id" filterable placeholder="Please select a responsible Journal" value-key="groupID"
					 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-form-item>
				<el-form-item label="realname">
					{{oldForm.realname}}
				</el-form-item>
				<el-form-item label="phone">
					{{oldForm.phone}}
				</el-form-item>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button @click="oldVisible = false">Cancel</el-button>
				<el-button type="primary" @click="saveOld(oldForm)">OK</el-button>
			</span>
		</el-dialog>

	</div>
</template>

<script>
	export default {
		data() {
			return {
				edit_id: localStorage.getItem('U_id'),
				tableData: [],
				df_jour: [],
				addVisible: false,
				oldVisible: false,
				addForm: {},
				oldForm: {},
				aid: -1,
				id: -1,
				btn_alert: false,
				query: {
					journal_id: 0,
				},
				rules: {
					account: [{
						required: true,
						message: 'Please input account',
						trigger: 'blur'
					}],
					email: [{
						required: true,
						message: 'Please input email',
						trigger: 'blur'
					}, {
						validator: function(rule, value, callback) {
							if (/^[-.-_A-Za-z0-9]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/.test(value) == false) {
								callback(new Error("Please enter the correct email format"));
							} else {
								callback();
							}
						},
						trigger: "blur"
					}],
					journal_id: [{
						required: true,
						message: 'Please select a responsible Journal',
						trigger: 'blur'
					}],
					password: [{
						required: true,
						message: 'Please input password',
						trigger: 'blur'
					}],
					repassword: [{
						required: true,
						message: 'Please input confirm password',
						trigger: 'blur'
					}]
				}
			};
		},
		created() {
			this.getjour();
			// this.getdate();
		},
		methods: {
			// 获取编委和负责期刊列表数据
			getdate() {
				this.$api
					.post('api/Chief/getJournalBoard', this.query)
					.then(res => {
						this.tableData = res.data.boards;
					})
					.catch(err => {
						this.$message.error(err);
					});

			},
			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.query.journal_id = this.df_jour[0].journal_id
							this.$api
								.post('api/Chief/getJournalBoard', this.query)
								.then(res => {
									this.tableData = res.data.boards;
								})
								.catch(err => {
									this.$message.error(err);
								});
						} else {
							this.$message.error(res.msg);
						}
					})
					.catch(err => {
						this.$message.error(err);
					});
			},

			// 下拉分类搜索
			chan_id(event) {
				this.getdate();
			},

			// 添加操作
			addEditor(index, row) {
				this.aid = index;
				this.addVisible = true;
			},

			// 查找与他相同的账号
			saerNa_U() {
				if (this.addForm.account != undefined) {
					this.$api
						.post('api/Chief/checkEmailForUser', {
							'email': this.addForm.account
						})
						.then(res => {
							if (res.code == 0) {
								if (res.data.user_info != null) {
									this.$confirm('this account already exists. Do you want to operate under this account？', 'Tips', {
											type: 'warning'
										})
										.then(() => {
											this.addVisible = false;
											this.$refs.add_Form.resetFields();
											// this.id = index;
											this.oldVisible = true;
											this.oldForm = res.data.user_info;
										})
										.catch(() => {});
									this.btn_alert = true
								} else {
									this.btn_alert = false
								}
							} else {
								this.$message.error(res.msg);
							}
						})
						.catch(err => {
							this.$message.error(err);
						});
				}
			},
			// 查找与他相同的邮箱
			saerEm_U() {
				if (this.addForm.email != undefined) {
					this.$api
						.post('api/Chief/checkEmailForUser', {
							'email': this.addForm.email
						})
						.then(res => {
							if (res.code == 0) {
								if (res.data.user_info != null) {
									this.$confirm('this account already exists. Do you want to operate under this account？', 'Tips', {
											type: 'warning'
										})
										.then(() => {
											this.addVisible = false;
											this.$refs.add_Form.resetFields();
											// this.id = index;
											this.oldVisible = true;
											this.oldForm = res.data.user_info;
										})
										.catch(() => {});
									this.btn_alert = true
								} else {
									this.btn_alert = false
								}
							} else {
								this.$message.error(res.msg);
							}
						})
						.catch(err => {
							this.$message.error(err);
						});
				}
			},

			// 保存添加
			saveAdd(addForm) {
				if (addForm.password == addForm.repassword) {
					this.$refs.add_Form.validate((valid) => {
						if (valid) {
							this.$api
								.post('api/Chief/addJournalBoard', 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;
						}
					});
				} else {
					this.$message.error("The two passwords are inconsistent！");
				}
			},
			// 保存old添加
			saveOld(oldForm) {
				this.$refs.old_Form.validate((valid) => {
					if (valid) {
						this.$api
							.post('api/Chief/addJournalBoardHas', this.oldForm)
							.then(res => {
								if (res.code == 0) {
									this.oldVisible = false;
									this.$refs.old_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/Chief/delJournalBoard', 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>
