<template>
	<div>
		<div class="crumbs">
			<el-breadcrumb separator="/">
				<el-breadcrumb-item>
					<i class="el-icon-connection"></i> Chief editor 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" style="width: 300px;">
					<el-option label="All" :key="0" :value="0"></el-option>
					<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" empty-text="New messages (0)">
				<el-table-column prop="account" label="username" align="center"></el-table-column>
				<el-table-column prop="journal.title" label="journal" 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" width="150px"></el-table-column>
				<el-table-column prop="email" label="email" align="center"></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 class="pagination">
				<el-pagination background layout="total, prev, pager, next" :current-page="query.pageIndex" :page-size="query.pageSize"
				 :total="Total" @current-change="handlePageChange"></el-pagination>
			</div>
		</div>

		<!-- 添加弹出框 -->
		<el-dialog title="Add Chief Editor" :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 Chief Editor" :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 {
				tableData: [],
				df_jour: [],
				addVisible: false,
				oldVisible: false,
				addForm: {},
				oldForm: {},
				aid: -1,
				id: -1,
				btn_alert: false,
				Total: 0,
				query: {
					journal_id: 0,
					pageIndex: 1,
					pageSize: 10
				},
				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.getdate();
		},
		methods: {
			getdate() {
				// 获取主编列表数据
				this.$api
					.post('api/Chief/getChiefList', this.query)
					.then(res => {
						this.tableData = res.data.chiefs;
						this.Total = res.data.count || 0;
					})
					.catch(err => {
						this.$message.error(err);
					});
				// 获取全部期刊
				this.$api
					.post('api/Chief/getAllJournals')
					.then(res => {
						if (res.code == 0) {
							this.df_jour = res.data.journals;
						} else {
							this.$message.error(res.msg);
						}
					})
					.catch(err => {
						this.$message.error(err);
					});
			},

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

			// 分页导航
			handlePageChange(val) {
				this.$set(this.query, 'pageIndex', val);
				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 user already exists, do you want to add this user as journal editor？', 'Tips', {
											type: 'warning'
										})
										.then(() => {
											this.addVisible = false;
											this.$refs.add_Form.resetFields();
											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 user already exists, do you want to add this user as journal editor？', 'Tips', {
											type: 'warning'
										})
										.then(() => {
											this.addVisible = false;
											this.$refs.add_Form.resetFields();
											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/addChief', 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/addChiefHas', 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/delChiefToJournal ', 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>
