<template>
	<div>
		<div class="crumbs">
			<el-breadcrumb separator="/">
				<el-breadcrumb-item>
					<i class="el-icon-user"></i> Reviewer list
				</el-breadcrumb-item>
			</el-breadcrumb>
		</div>
		<div class="container">
			<div class="handle-box">
				<el-select v-model="query.journalId" @change="getDate" placeholder="Please select journal">
					<el-option :key="0" label="All journal" :value="0"></el-option>
					<el-option v-for="item in journalList" :key="item.journal_id" :label="item.title" :value="item.journal_id"></el-option>
				</el-select>
				<!-- <el-button type="primary" icon="el-icon-circle-plus-outline" @click="addJournal">add</el-button> -->
			</div>
			<el-table :data="tableData" border stripe class="table" ref="multipleTable" header-cell-class-name="table-header">
				<!-- <el-table-column prop="user_id" label="No." align="center"></el-table-column> -->
				<el-table-column prop="account" label="Account" align="center"></el-table-column>
				<el-table-column prop="realname" label="Name" align="center"></el-table-column>
				<el-table-column prop="email" label="Email" align="center"></el-table-column>
				<el-table-column prop="country" label="Country" align="center"></el-table-column>
				<el-table-column prop="company" label="Affiliation" align="center"></el-table-column>
				<el-table-column prop="title" label="Journal" align="center"></el-table-column>
				<el-table-column prop="board" label="Youth editorial board" align="center">
					<template slot-scope="scope">
						<el-button icon="el-icon-check" type="text" style="color: #0fa31d" v-if="scope.row.is_yboard=='1'"></el-button>
						<el-button icon="el-icon-close" type="text" style="color: #ff0000" v-if="scope.row.is_yboard=='0'"></el-button>
					</template>
				</el-table-column>
				<el-table-column label="" width="350" align="center">
					<template slot-scope="scope">
						<el-button size="mini" type="success" plain icon="el-icon-sort-up" @click="gradeup(scope.row)" v-if="scope.row.is_yboard=='0'">Upgrade</el-button>
						<el-button size="mini" type="warning" plain icon="el-icon-sort-down" @click="gradedown(scope.row)" v-if="scope.row.is_yboard=='1'">downgrade</el-button>
						<el-button size="mini" type="primary" plain icon="el-icon-tickets" @click="showdetail(scope.row)">Detail</el-button>
						<el-button size="mini" type="danger" plain icon="el-icon-delete" @click="showDelete(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="删除" :visible.sync="delOpen" width="40%">
			<div v-for="item in lineOptions" :name='item.journal_id' :value='item.reviewer_id' style="margin-left: 60px;">
				{{item.journal.title}} - {{item.reviewer.account}}
				<el-button type="text" icon="el-icon-delete" @click="lastDelete($event)" style="color: #FF0000;margin-left: 20px;">Delete</el-button>
			</div>
			<span slot="footer" class="dialog-footer">
				<el-button @click="delOpen = false;" type="primary">取 消</el-button>
			</span>
		</el-dialog>
		<!-- <el-dialog title="change" :visible.sync="editVisible" width="40%">
            <el-form ref="changform" :model="change" label-width="80px">
                <el-form-item label="editormsg">
                    <span>{{change.title}}</span>
                </el-form-item>
                <el-form-item label="state">
                    <el-select v-model="change.editorId" placeholder="Please select">
                        <el-option :key="0" label="Please select" :value="0"></el-option>
                        <el-option
                            v-for="item in editorList"
                            :key="item.user_id"
                            :label="item.account"
                            :value='item.user_id'
                        ></el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">cancel</el-button>
                <el-button type="primary" @click="">save</el-button>
            </span>
        </el-dialog> -->
	</div>
</template>

<script>
	export default {
		name: 'reviewerList',
		data() {
			return {
				query: {
					username: localStorage.getItem('U_name'),
					journalId: 0,
					pageIndex: 1,
					pageSize: 10
				},
				tableData: [],
				journalList: [],
				Total: 0,
				// editVisible: false,
				delForm: {
					line: []
				},
				delOpen: false,
				addForm: {},
				lineOptions: [],
				mess_age: {}
			};
		},
		created() {
			this.getDate();
			this.initselect();
		},
		methods: {
			// 获取编辑列表数据
			getDate() {
				this.$api
					.post('api/User/getreviewerList', this.query)
					.then(res => {
						this.Total = res.total;
						this.tableData = res.data;
					})
					.catch(err => {
						console.log(err);
					});
			},
			// 分页导航
			handlePageChange(val) {
				this.$set(this.query, 'pageIndex', val);
				this.getDate();
			},
			// 升级
			gradeup(row) {
				 // 二次确认
				 this.$confirm('确定要升级'+row.account+'为青年编委吗？', '提示', {
				 		type: 'warning'
				 	})
				 	.then(() => {
				 		this.$api
				 			.post('api/User/upReviewerToYboard', row)
				 			.then(res => {
				 				if (res.code == 0) {
				 					this.$message.success('升级'+row.account+'为青年编委成功');
				 					this.getDate();
				 				} else {
				 					this.$message.error(res.msg);
				 				}
				 			})
				 			.catch(err => {
				 				this.$message.error(err);
				 			});
				 	})
				 	.catch(() => {});
			},
			// 降级
			gradedown(row) {
				 // 二次确认
				 this.$confirm('确定要降级'+row.account+'为审稿人吗？', '提示', {
				 		type: 'warning'
				 	})
				 	.then(() => {
				 		this.$api
				 			.post('api/User/downReviewerToYboard ', row)
				 			.then(res => {
				 				if (res.code == 0) {
				 					this.$message.success('降级为'+row.account+'审稿人成功');
				 					this.getDate();
				 				} else {
				 					this.$message.error(res.msg);
				 				}
				 			})
				 			.catch(err => {
				 				this.$message.error(err);
				 			});
				 	})
				 	.catch(() => {});
			},
			// 查看详情
			showdetail(row) {
				this.$router.push({
					path: 'reviewerDetail',
					query: {
						id: row.user_id
					}
				});
			},
			// 删除弹窗
			showDelete(index, row) {
				this.aid = index;
				this.delOpen = true;
				this.$api
					.post('api/User/getReviewerForDel', {
						reviewer_id: index.reviewer_id,
						editor_account: localStorage.getItem('U_name')
					})
					.then(res => {
						this.lineOptions = res.data;
					})
					.catch(err => {
						console.log(err);
					});
			},
			// 删除操作
			lastDelete(e) {
				this.mess_age.reviewer_id = e.currentTarget.parentElement.getAttribute('value')
				this.mess_age.journal_id = e.currentTarget.parentElement.getAttribute('name')
				// 二次确认删除
				this.$confirm('确定要删除吗？', '提示', {
						type: 'warning'
					})
					.then(() => {
						this.$api
							.post('api/User/deleteArticleReviewer',this.mess_age)
							.then(res => {
								if (res.code == 0) {
									this.delOpen = false;
									this.$message.success('删除成功');
									this.getDate();
								} else {
									this.$message.error(res.msg);
								}
							})
							.catch(err => {
								this.$message.error(err);
							});
					})
					.catch(() => {});
			},
			//初始化期刊选项
			initselect() {
				this.$api
					.post('api/Article/getJournal', {
						username: this.query.username
					})
					.then(res => {
						this.journalList = res;
					})
					.catch(err => {
						console.log(err);
					});
			}
		}
	};
</script>

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

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

	.table-td-thumb {
		display: block;
		margin: auto;
		width: 40px;
		height: 40px;
	}

	.el-table .warning-row {
		background: #f3ca7f;
	}

	.el-table .success-row {
		background: #bcfc9a;
	}

	.el-table .normol-row {
		background: #d8f1c7
	}

	.el-table .red-row {
		background: #f05555;
	}
</style>
