<template>
	<div>
		<div class="crumbs">
			<el-breadcrumb separator="/">
				<el-breadcrumb-item>
					<i class="el-icon-coordinate"></i> Editor List
				</el-breadcrumb-item>
			</el-breadcrumb>
		</div>
		<div class="container">
			<el-table :data="tableData" border class="table" ref="multipleTable" header-cell-class-name="table-header" empty-text="New messages (0)">
				<el-table-column prop="account" label="Account"></el-table-column>
				<el-table-column prop="realname" label="Realname"></el-table-column>
				<el-table-column prop="email" label="Email"></el-table-column>
				<el-table-column label=" " width="180" align="center">
					<template slot-scope="scope">
						<el-button plain type="warning" icon="el-icon-key" @click="handleCipher(scope.row)">Change Password</el-button>
					</template>
				</el-table-column>
			</el-table>
		</div>

		<!-- 编辑用户密码弹出框 -->
		<el-dialog title="Change Password" :visible.sync="cipherVisible" width="600px" :close-on-click-modal="false">
			<el-form ref="cipherTab" :model="cipherForm" :rules="rules" label-width="160px">
				<el-form-item label="Account :" prop="account">
					{{cipherForm.account}}
				</el-form-item>
				<el-form-item label="Email :" prop="email">
					{{cipherForm.email}}
				</el-form-item>
				<el-form-item label="Realname :" prop="realname">
					{{cipherForm.realname}}
				</el-form-item>
				<el-form-item label="New password :" prop="new_password">
					<el-input type="password" placeholder="Please enter..." v-model="cipherForm.new_password"></el-input>
				</el-form-item>
				<el-form-item label="Confirm password :" prop="re_password">
					<el-input type="password" placeholder="Please enter..." v-model="cipherForm.re_password"></el-input>
				</el-form-item>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button @click="cipherVisible = false">Cancel</el-button>
				<el-button type="primary" @click="savCipher">Save</el-button>
			</span>
		</el-dialog>
	</div>
</template>

<script>
	export default {
		data() {
			return {
				tableData: [],
				cipherForm: {
					new_password: '',
					re_password: ''
				},
				cipherVisible: false,
				rules: {
					new_password: [{
						required: true,
						message: 'Please enter new password ',
						trigger: 'blur'
					}],
					re_password: [{
						required: true,
						message: 'Please enter the new password again',
						trigger: 'blur'
					}],
				}
			};
		},
		mounted() {

		},
		created() {
			this.getDate()
		},
		methods: {
			// 获取数据
			getDate() {
				this.$api
					.post('/api/User/getAllEditor')
					.then(res => {
						if (res.code == 0) {
							this.tableData = res.data.editors;
						} else {
							this.$message.error(res.msg);
						}
					})
					.catch(err => {
						this.$message.error(err);
					});

			},

			// 修改密码弹窗
			handleCipher(row) {
				this.cipherForm = Object.assign({}, row);
				this.cipherVisible = true;
			},

			// 保存修改密码操作
			savCipher() {
				this.$refs.cipherTab.validate((valid) => {
					if (valid) {
						if (this.cipherForm.new_password == this.cipherForm.re_password) {
							this.cipherForm.password = this.cipherForm.new_password
							this.$api
								.post('api/User/changeEditorPassword', this.cipherForm)
								.then(res => {
									if (res.code == 0) {
										this.$message.success('Password modified successfully!');
										this.cipherVisible = false;
										this.getDate();
									} else {
										this.$message.error(res.msg);
									}
								})
								.catch(err => {
									this.$message.error(err);
								});
						} else {
							this.$message.error("The two new password entries are inconsistent!");
						}
					} else {
						this.$message.error('error submit!!');
						return false;
					}
				});

			},
		},
		computed: {

		},
		watch: {

		}
	};
</script>

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

	.el-button--primary.is-plain:hover {
		background-color: #409EFF !important;
		color: #fff !important;
	}

	.ro_li_ku {
		border: 1px solid #419fcf;
		border-radius: 5px;
		color: #419fcf;
		display: inline-block;
		margin: 5px;
		padding: 0 5px;
		white-space: nowrap;
	}
</style>
