<template>
    <div class="forget-box">
        <login-header></login-header>
        <main class="wrap">
            <section>
                <div class="forget-title">重置登录密码</div>
                <div class="forget-form">
                    <el-form :model="ruleForm" :rules="rules" ref="ruleForm">
                        <el-form-item prop="pass">
                            <h3>新的登录密码</h3>
                            <el-input
                                type="password"
                                v-model.trim="ruleForm.pass"
                                autocomplete="off"
                                placeholder="请设置登录密码"
                                v-if="showPassword"
                            ></el-input>
                            <el-input
                                type="text"
                                v-model.trim="ruleForm.pass"
                                autocomplete="off"
                                placeholder="请设置登录密码"
                                v-else
                            ></el-input>
                            <div
                                :class="showPassword ? 'show-password' : 'hide-password'"
                                @click="toggleShowPassword"
                            ></div>
                        </el-form-item>
                        <el-form-item prop="checkPass">
                            <h3>确认新的登录密码</h3>
                            <el-input
                                type="password"
                                v-model.trim="ruleForm.checkPass"
                                autocomplete="off"
                                placeholder="确认登录密码"
                                v-if="showCheckPassword"
                            ></el-input>
                            <el-input
                                type="text"
                                v-model.trim="ruleForm.checkPass"
                                autocomplete="off"
                                placeholder="确认登录密码"
                                v-else
                            ></el-input>
                            <div
                                :class="showCheckPassword ? 'show-password' : 'hide-password'"
                                @click="toggleShowCheckPassword"
                            ></div>
                        </el-form-item>
                        <el-form-item class="btn-reg">
                            <el-button
                                type="primary"
                                @click="submitForm('ruleForm')"
                                :loading="submitLoading"
                            >确定</el-button>
                        </el-form-item>
                    </el-form>
                </div>
            </section>
        </main>
        <login-footer></login-footer>
    </div>
</template>

<script>
import { mapState } from "vuex";
import { api_changeNewPwd } from "api/login.js";
import utils from "utils/utils.js";
import loginHeader from "./components/header";
import loginFooter from "./components/footer";
export default {
    name: "reset",
    components: {
        loginHeader,
        loginFooter
    },
    computed: {
        ...mapState(["forgetLoginInfo"])
    },
    data() {
        var validatePass = (rule, value, callback) => {
            if (value === "") {
                callback(new Error("请输入密码"));
            } else if (!utils.isvalidPass(value)) {
                callback(
                    new Error(
                        "密码位数最少8位且必须含有数字、小写字母、大写字母"
                    )
                );
            } else {
                if (this.ruleForm.checkPass !== "") {
                    this.$refs.ruleForm.validateField("checkPass");
                }
                callback();
            }
        };
        var validatePass2 = (rule, value, callback) => {
            if (value === "") {
                callback(new Error("请再次输入密码"));
            } else if (value !== this.ruleForm.pass) {
                callback(new Error("两次输入密码不一致!"));
            } else {
                callback();
            }
        };
        return {
            ruleForm: {
                pass: "",
                checkPass: ""
            },
            rules: {
                pass: [{ validator: validatePass, trigger: "blur" }],
                checkPass: [{ validator: validatePass2, trigger: "blur" }]
            },
            showPassword: true,
            showCheckPassword: true,
            submitLoading: false
        };
    },
    methods: {
        toggleShowPassword() {
            this.showPassword = !this.showPassword;
        },
        toggleShowCheckPassword() {
            this.showCheckPassword = !this.showCheckPassword;
        },
        submitForm(formName) {
            this.submitLoading = true;
            this.$refs[formName].validate(valid => {
                if (valid) {
                    let changeNewPwdData = {
                        data: {
                            phoneNo: this.forgetLoginInfo.forgetLoginInfo
                                .mobile,
                            newPwd: this.ruleForm.pass,
                            confirmPwd: this.ruleForm.checkPass
                        }
                    };
                    api_changeNewPwd(changeNewPwdData).then(res => {
                        if (res.success) {
                            this.$message.success("登录密码重置成功！");
                            this.$router.replace({
                                path: "/login"
                            });
                        } else {
                            this.$message.error(res.msg);
                        }
                        this.submitLoading = false;
                    });
                } else {
                    this.submitLoading = false;
                    return false;
                }
            });
        }
    },
    beforeRouteEnter(to, from, next) {
        if (from.path !== "/forget/verification") {
            next("/forget/verification");
        } else {
            next();
        }
    }
};
</script>

<style lang="less" scoped>
@import "../../assets/styles/common.css";
.forget-box /deep/ .btn-reg {
    margin-top: 40px;
    .el-button--primary {
        width: 100%;
    }
}
.forget-box {
    background: #f7f7f7;
}
.wrap {
    min-height: calc(100vh - 224px);
    margin: 50px 0;
    section {
        overflow: hidden;
        width: 1080px;
        margin: 0 auto;
        background: #fff;
        .forget-form {
            width: 330px;
            margin: 0 auto;
            padding: 30px 0 38px;
            text-align: left;
            h3 {
                line-height: 40px;
                font-size: 16px;
                font-weight: normal;
            }
        }
        .show-password {
            position: absolute;
            bottom: 10px;
            right: 10px;
            width: 24px;
            height: 12px;
            background: url(~assets/images/icon-close-eye.png) no-repeat;
            cursor: pointer;
        }
        .hide-password {
            position: absolute;
            bottom: 14px;
            right: 10px;
            width: 24px;
            height: 12px;
            background: url(~assets/images/icon-open-eye.png) no-repeat;
            cursor: pointer;
        }
    }
}
</style>
