<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="Please select a journal" @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" empty-text="New messages (0)">
				<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 prop="" label="WOS H index" width="120px" align="center">
					<template slot-scope="scope">
						<p v-if="scope.row.indexs_show!=0" style="display: inline-block;width: 90px;">
							<b :style="{color:scope.row.indexs_color}">{{scope.row.indexs[0].h_index}}</b>
							({{now_year}})
							<i class="el-icon-edit" @click="HIndexEdit(scope.row)"
								style="margin-left: 10px;color: #006699;cursor: pointer;"></i>
						</p>
						<p v-if="scope.row.indexs_show==0" style="color: #aaa;display: inline-block;width: 90px;">
							Null
							<i class="el-icon-edit" @click="HIndexEdit(scope.row)"
								style="margin-left: 10px;color: #006699;cursor: pointer;"></i>
						</p>
						<!-- <el-button size="mini" type="text" @click="HIndexList(scope.row)">More ></el-button> -->
					</template>
				</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="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>

		<!-- H指数列表弹出框 -->
		<el-dialog title="Edit H Index" :visible.sync="HListVisible" width="400px">
			<div style="margin: 0 0 20px 10px;font-weight: bold;">{{HIxForm.realname}}</div>
			<div class="HIList HIListTit">
				<p>Year</p>
				<p>WOS H index</p>
			</div>
			<div v-for="item in HIxData" class="HIList">
				<p>{{item.year}}</p>
				<p>{{item.h_index}}</p>
			</div>
			<span slot="footer" class="dialog-footer">
				<el-button type="primary" @click="HListVisible=false">OK</el-button>
			</span>
		</el-dialog>

		<!-- H指数修改弹出框 -->
		<el-dialog title="Edit H Index" :visible.sync="HEditVisible" width="400px">
			<div style="margin: 0 0 20px 10px;font-weight: bold;">{{HIxForm.realname}}</div>
			<el-form ref="old_Form" :model="HIxForm" :rules="rules" label-width="150px">
				<el-form-item>
					<span slot="label">
						WOS H index ({{this.now_year}})
					</span>
					<el-input v-model="HIxForm.index"></el-input>
				</el-form-item>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button type="primary" @click="saveHIx(HIxForm)">OK</el-button>
			</span>
		</el-dialog>

	</div>
</template>

<script>
	export default {
		data() {
			return {
				edit_id: localStorage.getItem('U_id'),
				now_year: 0,
				tableData: [],
				df_jour: [],
				addVisible: false,
				oldVisible: false,
				HListVisible: false,
				HEditVisible: false,
				addForm: {},
				oldForm: {},
				HIxForm: {
					index: ''
				},
				HIxData: [],
				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;
						for (var i = 0; i < this.tableData.length; i++) {
							if (this.tableData[i].indexs.length != 0) {
								if (this.tableData[i].indexs[0].year == this.now_year) {
									this.tableData[i].indexs_show = this.tableData[i].indexs[0].h_index
									if (this.tableData[i].indexs_show < 10) {
										this.tableData[i].indexs_color = "#cb160a"
									} else if (this.tableData[i].indexs_show < 15) {
										this.tableData[i].indexs_color = "#cbb504"
									} else {
										this.tableData[i].indexs_color = "#0cbc15"
									}

								} else {
									this.tableData[i].indexs_show = 0
								}
							} else {
								this.tableData[i].indexs_show = 0
							}
						}
					})
					.catch(err => {
						this.$message.error(err);
					});

			},
			getjour() {
				this.now_year = new Date().getFullYear()
				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;
									for (var i = 0; i < this.tableData.length; i++) {
										if (this.tableData[i].indexs.length != 0) {
											if (this.tableData[i].indexs[0].year == this.now_year) {
												this.tableData[i].indexs_show = this.tableData[i].indexs[0].h_index
												if (this.tableData[i].indexs_show < 10) {
													this.tableData[i].indexs_color = "#cb160a"
												} else if (this.tableData[i].indexs_show < 15) {
													this.tableData[i].indexs_color = "#cbb504"
												} else {
													this.tableData[i].indexs_color = "#0cbc15"
												}
											} else {
												this.tableData[i].indexs_show = 0
											}
										} else {
											this.tableData[i].indexs_show = 0
										}
									}
								})
								.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;
					}
				});
			},
			// H指数列表
			HIndexList(row) {
				this.HListVisible = true
				this.HIxForm.realname = row.realname
				this.HIxData = row.indexs
			},
			// H指数修改
			HIndexEdit(row) {
				this.HEditVisible = true
				this.HIxForm.user_id = row.user_id
				this.HIxForm.realname = row.realname
				this.HIxForm.year = this.now_year
				if (row.indexs_show != 0) {
					this.HIxForm.index = row.indexs_show
				} else {
					this.HIxForm.index = ''
				}
			},
			// 保存index
			saveHIx() {
				this.$api
					.post('api/Board/editIndex', this.HIxForm)
					.then(res => {
						if (res.code == 0) {
							this.$message.success('Edit H Index succeeded');
							this.HEditVisible = false
							this.getdate();
						} else {
							this.$message.error(res.msg);
						}
					})
					.catch(err => {
						this.$message.error(err);
					});
			},
			// 删除操作
			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;
	}

	.HIList {
		margin: 0 0 10px 10px;
	}

	.HIList>p {
		display: inline-block;
		width: 120px;
	}

	.HIListTit>p {
		font-weight: bold;
	}
</style>
