<template>
	<div>
		<div class="crumbs">
			<el-breadcrumb separator="/">
				<el-breadcrumb-item>
					<i class="el-icon-lx-profile"></i> 个人中心
				</el-breadcrumb-item>
			</el-breadcrumb>
		</div>
		<div class="container">
			<div class="form-box">
				<el-form :model="coreForm" :rules="rules" ref="core_Form" label-width="80px">
					<el-form-item label="用户名" prop="account">
						<span style="padding-left: 15px;">{{coreForm.account}}</span>
					</el-form-item>
					<el-form-item label="姓名" prop="realname">
						<el-input type="text" placeholder="请输入姓名" v-model="coreForm.realname"></el-input>
					</el-form-item>
					<el-form-item label="密码" prop="password">
						<el-button type="warning" @click="psw_word">修改密码</el-button>
					</el-form-item>
					<el-form-item label="联系方式" prop="phone">
						<el-input type="text" placeholder="请输入联系方式" v-model="coreForm.phone"></el-input>
					</el-form-item>
					<el-form-item>
						<el-button type="primary" @click="onSubmit(coreForm)">确定保存</el-button>
						<!-- <el-button>取消</el-button> -->
					</el-form-item>
				</el-form>
			</div>
		</div>

		<!-- 修改密码弹出框 -->
		<el-dialog title="修改密码" :visible.sync="pass_word" width="30%">
			<el-form :model="pwdform" :rules="rules" ref="pwd_form" label-width="100px">
				<el-form-item label="旧密码" prop="password">
					<el-input type="password" placeholder="请输入旧密码" v-model="pwdform.password"></el-input>
				</el-form-item>
				<el-form-item label="新密码" prop="new_password">
					<el-input type="password" placeholder="请输入新密码" v-model="pwdform.new_password"></el-input>
				</el-form-item>
				<el-form-item label="确认密码" prop="re_password">
					<el-input type="password" placeholder="请再次输入新密码" v-model="pwdform.re_password"></el-input>
				</el-form-item>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button @click="pass_word = false">取 消</el-button>
				<el-button type="primary" @click="save_pwd(pwdform)">确 定</el-button>
			</span>
		</el-dialog>
	</div>
</template>

<script>
	export default {
		data() {
			return {
				ABase: {
					account: localStorage.getItem('admin_username'),
				},
				coreForm: {},
				pwdform: {
					account: localStorage.getItem('admin_username'),
					password: '',
					new_password: '',
					re_password: ''
				},
				pass_word: false,
				rules: {
					realname: [{
						required: true,
						message: '请输入姓名',
						trigger: 'blur'
					}],
					phone: [{
							required: true,
							message: '请输入联系方式',
							trigger: 'blur'
						},
						{
							validator: function(rule, value, callback) {
								if (/^1[34578]\d{9}$/.test(value) == false) {
									callback(new Error("请输入正确的手机格式"));
								} else {
									callback();
								}
							},
							trigger: "blur"
						}
					],
					password: [{
						required: true,
						message: '请输入旧密码',
						trigger: 'blur'
					}],
					new_password: [{
						required: true,
						message: '请输入新密码',
						trigger: 'blur'
					}],
					re_password: [{
						required: true,
						message: '请再次输入新密码',
						trigger: 'blur'
					}]
				}
			};
		},
		created() {
			this.getData();
		},
		methods: {
			// 点击修改密码
			psw_word() {
				this.pass_word = true;
			},
			// 点击提交密码
			save_pwd(pwdform) {
				this.$refs.pwd_form.validate((valid) => {
					if (valid) {
						if (pwdform.new_password == pwdform.re_password) {
							this.$api
								.post('master/Admin/changepasswd', this.pwdform)
								.then(res => {
									if (res.code == 0) {
										this.$message.success('修改密码成功!');
										this.pass_word = false;
									} else {
										this.$message.error(res.msg);
									}
								})
								.catch(err => {
									this.$message.error(err);
								});
						} else {
							this.$message.error("两次新密码输入不一致");
						}
					} else {
						this.$message.error('error submit!!');
						return false;
					}
				});
			},
			// 获取个人信息
			getData() {
				this.$api
					.post('master/Admin/getAdminBase', this.ABase)
					.then(res => {
						if (res.code == 0) {
							this.coreForm = res.data;
						} else {
							this.$message.error(res.msg);
						}
					})
					.catch(err => {
						this.$message.error(err);
					});
			},
			// 点击提交个人信息
			onSubmit(coreForm) {
				this.$refs.core_Form.validate((valid) => {
					if (valid) {
						this.$api
							.post('master/Admin/editAdminBase', this.coreForm)
							.then(res => {
								if (res.code == 0) {
									this.$message.success('修改个人信息成功！');
									this.getData();
								} else {
									this.$message.error(res.msg);
								}
							})
							.catch(err => {
								this.$message.error(err);
							});
					} else {
						this.$message.error('error submit!!');
						return false;
					}
				});
			}
		}
	};
</script>
