<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-select v-model="query.journal_id" filterable placeholder="Please select a responsible Journal" value-key="groupID"
				 style="width: 300px;" @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 class="table" ref="multipleTable" header-cell-class-name="table-header">
				<el-table-column prop="matching" label="match" width="70" align="center">
					<template slot-scope="scope">
						<el-button icon="el-icon-check" type="text" style="color: #0fa31d;" v-if="scope.row.tuser_id!=0"></el-button>
						<el-button icon="el-icon-close" type="text" style="color: #ff0000" v-if="scope.row.tuser_id==0"></el-button>
					</template>
				</el-table-column>
				<el-table-column prop="name" label="name"></el-table-column>
				<el-table-column prop="dr" label="degree" width="100"></el-table-column>
				<el-table-column prop="title" label="title" width="210"></el-table-column>
				<el-table-column prop="address" label="company"></el-table-column>
				<el-table-column prop="country" label="country" width="180"></el-table-column>
				<!-- <el-table-column prop="group_name" label="grouping" align="center">
					<template slot-scope="scope">
						<el-tag v-if="scope.row.type=='2' && scope.row.group_name==null" effect="plain">None</el-tag>
						<el-tag v-if="scope.row.type=='2' && scope.row.group_name!=null" effect="plain">{{scope.row.group_name}}</el-tag>
						<span v-if="scope.row.type!='2'" effect="plain"></span>
					</template>
				</el-table-column> -->
				<el-table-column prop="user.account" label="Match user name"></el-table-column>
				<el-table-column prop="user.email" label="Match user mailbox"></el-table-column>
				<el-table-column label=" " width="100" align="center">
					<template slot-scope="scope">
						<el-button type="text" icon="el-icon-link" @click="matchEdit(scope.$index, scope.row)" style="color: #e6a23c;">Matching</el-button>
					</template>
				</el-table-column>
			</el-table>
		</div>

		<!-- 添加弹出框 -->
		<el-dialog title="Add Editorial board member" :visible.sync="marVisible" width="40%" v-loading="loading"
		 element-loading-text="Loading..." element-loading-spinner="el-icon-loading" element-loading-background="rgba(0, 0, 0, 0.8)"
		 @close="backGood()">
			<el-tabs tab-position="top">
				<el-tab-pane label="User matching">
					<el-form ref="name_Form" :model="nameForm" :rules="rules" label-width="70px">
						<el-form-item label="name" prop="account">
							<el-input v-model="nameForm.account" style="width: 300px;display: inline-block;"></el-input>
							<el-button type="warning" icon="el-icon-link" @click="matchNat(nameForm)" style="margin-left: 20px;">Matching</el-button>
						</el-form-item>
					</el-form>
					<el-table :data="tableData_name" border 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-link" @click="matchSub(scope.$index, scope.row)" style="color: #e6a23c;">Matching</el-button>
							</template>
						</el-table-column>
					</el-table>
				</el-tab-pane>
				<el-tab-pane label="Mailbox matching">
					<el-form ref="email_Form" :model="emailForm" :rules="rules" label-width="70px">
						<el-form-item label="email" prop="email">
							<el-input v-model="emailForm.email" style="width: 300px;display: inline-block;"></el-input>
							<el-button type="warning" icon="el-icon-link" @click="matchMit(emailForm)" style="margin-left: 20px;">Matching</el-button>
						</el-form-item>
					</el-form>
				</el-tab-pane>
			</el-tabs>

		</el-dialog>
	</div>
</template>

<script>
	export default {
		data() {
			return {
				loading: false,
				edit_id: localStorage.getItem('U_id'),
				tableData: [],
				tableData_name: [],
				marchForm: {},
				nameForm: {},
				emailForm: {},
				df_jour: [],
				marVisible: false,
				query: {
					journal_id: ''
				},
				rules: {
					account: [{
						required: true,
						message: 'Please input account',
						trigger: 'blur'
					}],
					email: [{
						required: true,
						message: 'Please input email',
						trigger: 'blur'
					}],
				}
			};
		},
		created() {
			this.getTate();
		},
		methods: {
			// 获取编委和负责期刊列表数据
			getTate() {
				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.getDate();
						} else {
							this.$message.error(res.msg);
						}
					})
					.catch(err => {
						this.$message.error(err);
					});
			},

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

			// 关闭模态框
			backGood() {
				this.nameForm.account = ''
				this.emailForm.email = ''
				this.tableData_name = [];
			},

			getDate() {
				this.$api
					.post('api/Board/getOffwebBoard', this.query)
					.then(res => {
						if (res.code == 0) {
							this.tableData = res.data.boards;
						} else {
							this.$message.error(res.msg);
						}
					})
					.catch(err => {
						this.$message.error(err);
					});
			},

			// 匹配操作
			matchEdit(index, row) {
				this.marchForm = Object.assign({}, row);
				this.marVisible = true;
			},

			// 用户名搜索
			matchNat() {
				this.$refs.name_Form.validate((valid) => {
					if (valid) {
						this.$api
							.post('api/Board/searchUserByAccount', this.nameForm)
							.then(res => {
								if (res.code == 0) {
									this.tableData_name = res.data.users;
									if (this.tableData_name == '') {
										this.$message({
											message: 'No Result!',
											type: 'warning'
										});
									}
								} else {
									this.$message.error(res.msg);
								}
							})
							.catch(err => {
								this.$message.error(err);
							});

					} else {
						return false;
					}
				});

			},


			// 用户名匹配
			matchSub(index, row) {
				this.$refs.name_Form.validate((valid) => {
					if (valid) {
						this.loading = true;
						this.$api
							.post('api/Board/relationUser', {
								'email': row.email,
								'user_id': row.user_id,
								'journal_id': this.query.journal_id,
								'jboard_id': this.marchForm.board_id,
							})
							.then(res => {
								if (res.code == 0) {
									this.loading = false;
									// this.$refs.name_Form.resetFields();
									// this.$refs.email_Form.resetFields();
									this.tableData_name = [];
									this.$message.success(`Match successful！`);
									this.marVisible = false;
									this.getDate();
								} else {
									this.loading = false;
									this.$message.error(res.msg);
								}
							})
							.catch(err => {
								this.loading = false;
								this.$message.error(err);
							});
					} else {
						return false;
					}
				});

			},

			// 邮箱匹配
			matchMit(emailForm) {
				this.$refs.email_Form.validate((valid) => {
					if (valid) {
						this.loading = true;
						this.$api
							.post('api/Board/searchUserByEmail', emailForm)
							.then(res => {
								if (res.code == 0) {
									let aco_email = res.data.user.email
									let aco_uesid = res.data.user.user_id
									this.$api
										.post('api/Board/relationUser', {
											'email': aco_email,
											'user_id': aco_uesid,
											'journal_id': this.query.journal_id,
											'jboard_id': this.marchForm.board_id,
										})
										.then(res => {
											if (res.code == 0) {
												this.loading = false;
												// this.$refs.name_Form.resetFields();
												// this.$refs.email_Form.resetFields();
												this.tableData_name = [];
												this.$message.success(`Match successful！`);
												this.marVisible = false;
												this.getDate();
											} else {
												this.loading = false;
												this.$message({
													message: 'No Result!',
													type: 'warning'
												});
												// this.$message.error(res.msg);
											}
										})
										.catch(err => {
											this.loading = false;
											this.$message.error(err);
										});
								} else {
									this.loading = false;
									this.$message.error(res.msg);
								}
							})
							.catch(err => {
								this.loading = false;
								this.$message.error(err);
							});

					} else {
						return false;
					}
				});
			}

		}
	};
</script>

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

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

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