<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">
                        <h3>登录账号</h3>
                        <el-form-item prop="phoneNum">
                            <el-input
                                type="text"
                                v-model.number="ruleForm.phoneNum"
                                placeholder="请输入手机号"
                                class="phone"
                                maxlength="11"
                            ></el-input>
                        </el-form-item>
                        <h3>图形验证码</h3>
                        <el-form-item prop="picture">
                            <el-input
                                type="text"
                                v-model.trim="ruleForm.picture"
                                placeholder="请输入图形验证码"
                                maxlength="5"
                            ></el-input>
                            <img
                                :src="imageCode"
                                alt="图形验证码"
                                class="verification-code"
                                @click="changeImageCode"
                            />
                        </el-form-item>
                        <el-form-item class="btn-reg">
                            <el-button
                                type="primary"
                                :loading="loading"
                                @click="toChoose('ruleForm')"
                            >下一步</el-button>
                        </el-form-item>
                    </el-form>
                </div>
            </section>
        </main>
        <login-footer></login-footer>
    </div>
</template>

<script>
import { mapMutations } from "vuex";
import {
    api_iamgeCode,
    api_validateImage,
    api_verifyMobileExists
} from "api/login.js";
import utils from "utils/utils.js";
import loginHeader from "./components/header";
import loginFooter from "./components/footer";
export default {
    name: "forget",
    components: {
        loginHeader,
        loginFooter
    },
    data() {
        var checkPhone = (rule, value, callback) => {
            if (value === "") {
                callback(new Error("请输入手机号"));
            } else if (!utils.isvalidPhone(value)) {
                callback(new Error("手机号格式错误"));
            } else {
                let phoneNum = {
                    params: {
                        phoneNo: value
                    }
                };
                api_verifyMobileExists(phoneNum)
                    .then(res => {
                        if (res.success) {
                            callback(new Error("用户名不存在，请重新输入"));
                        } else {
                            callback();
                        }
                    })
                    .catch(err => {});
            }
        };
        var pictureCode = (rule, value, callback) => {
            if (value === "") {
                callback(new Error("请输入图形验证码"));
            } else {
                callback();
            }
        };
        return {
            ruleForm: {
                phoneNum: "",
                picture: ""
            },
            rules: {
                phoneNum: [{ validator: checkPhone, trigger: "blur" }],
                picture: [{ validator: pictureCode, trigger: "blur" }]
            },
            imageCode: "",
            markStr: "",
            loading: false
        };
    },
    methods: {
        ...mapMutations(["setForgetLoginInfo"]),
        toChoose(formName) {
            this.loading = true;
            this.$refs[formName].validate(valid => {
                if (valid) {
                    let validateImageData = {
                        data: {
                            phoneNo: this.ruleForm.phoneNum,
                            markStr: this.markStr,
                            imageCode: this.ruleForm.picture
                        }
                    };
                    api_validateImage(validateImageData).then(res => {
                        if (res.success) {
                            let forgetUserInfo = {
                                mobile: this.ruleForm.phoneNum
                            };
                            this.$store.commit(
                                "setForgetLoginInfo",
                                forgetUserInfo
                            );
                            this.$router.push({
                                path:
                                    "/forget/choose?phoneNum=" +
                                    this.ruleForm.phoneNum
                            });
                        } else {
                            this.$message.error(res.msg);
                            if (res.code == -13) {
                                this.getImageCode();
                            }
                        }
                        this.loading = false;
                    });
                } else {
                    this.loading = false;
                    return false;
                }
            });
        },
        getImageCode() {
            let iamgeCodeData = {
                data: {}
            };
            api_iamgeCode(iamgeCodeData).then(res => {
                this.imageCode =
                    "data:image/png;base64," + res.data.imgByBase64Str;
                this.markStr = res.data.markStr;
            });
        },
        changeImageCode() {
            this.getImageCode();
        }
    },
    created() {
        this.getImageCode();
    }
};
</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 {
    margin: 30px 0;
    section {
        width: 1080px;
        min-height: calc(100vh - 184px);
        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;
            }
        }
        .verification-code {
            position: absolute;
            right: 2px;
            top: 3px;
            height: 38px;
            cursor: pointer;
        }
    }
}
</style>
