<template>
    <div v-loading="loading" element-loading-text="拼命加载中......请稍等">
        <br />
        <br />
        <el-row :gutter="20">
            <el-col :span="16">
                <el-form
                    :model="form"
                    :rules="rules"
                    ref="refForm"
                    label-width="150px"
                >
                    <el-form-item label="手机号码：">
                        <span>{{ form.mobile }}</span>
                    </el-form-item>
                    <el-form-item prop="smsCode" label="短信验证码：">
                        <el-input
                            v-model.trim="form.smsCode"
                            style="width: 40%; margin-right: 10px"
                        ></el-input>
                        <send-sms-code-button
                            :mobile="form.mobile"
                        ></send-sms-code-button>
                    </el-form-item>
                    <el-form-item prop="newPassword" label="新密码：">
                        <el-input v-model.trim="form.newPassword"></el-input>
                    </el-form-item>
                    <el-form-item prop="newPassword2" label="确认密码：">
                        <el-input v-model.trim="form.newPassword2"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <p>提示：修改成功后需要重新登录账户</p>
                        <el-button
                            type="primary"
                            icon="el-icon-check"
                            @click="handleSubmit('refForm')"
                            >修 改
                        </el-button>
                        <el-button @click="handleCancelEmit">取 消</el-button>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>
    </div>
</template>

<script>
import SendSMSCodeButton from "@/components/SendSMSCodeButton";

import USER_MANAGEMENT from "@/api/user-management";
import cache from "@/utils/cache";

export default {
    name: "AccountModifyPassword",
    components: {
        "send-sms-code-button": SendSMSCodeButton,
    },
    data() {
        return {
            loading: false,
            form: {
                mobile: "",
                smsCode: "",
                newPassword: "",
                newPassword2: "",
            },
            rules: {
                smsCode: [
                    {
                        required: true,
                        message: "请输入短信验证码",
                        trigger: "change",
                    },
                ],
                newPassword: [
                    {
                        required: true,
                        message: "请输入新密码",
                        trigger: "change",
                    },
                ],
                newPassword2: [
                    {
                        required: true,
                        message: "请输入确认密码",
                        trigger: "change",
                    },
                ],
            },
        };
    },
    methods: {
        handleCancelEmit() {
            this.$emit("cancel");
        },
        handleSubmit(formName) {
            const _this = this;

            this.$refs[formName].validate((valid) => {
                if (valid) {
                    _this.loading = true;
                    USER_MANAGEMENT.forgotUserPassword({
                        mobile: _this.form.mobile,
                        password: _this.form.newPassword,
                        confirmPassword: _this.form.newPassword2,
                        smsCode: _this.form.smsCode,
                    })
                        .then((val) => {
                            _this.$message.success("修改成功！");
                            cache.removeSession("user");
                            cache.removeSession("company");
                            cache.removeToken();
                            _this.$store.dispatch("login/logout");
                        })
                        .catch((err) => {
                            _this.$message.error(err);
                        })
                        .finally(() => {
                            _this.loading = false;
                        });
                }
            });
        },
    },
    created() {
        this.form.mobile = JSON.parse(cache.getSession("user")).account;
    },
};
</script>

<style scoped>
</style>