<template>
    <el-dialog :title="Title" :visible.sync="isShow" width="30%" :before-close="handleClose" @close="closeDialog">
        <div class="content">
            <el-form :model="form" ref="form" :rules="rules" label-width="0">
                <el-form-item
                    prop="phone"
                    :rules="{
                        pattern: /^1[3-9]\d{9}$/,
                        message: '请输入正确的手机号',
                        trigger: 'blur'
                    }"
                >
                    <span class="el-icon-mobile-phone"></span>
                    <el-input style="width: 300px" v-model="form.phone" placeholder="请输入手机号"></el-input>
                </el-form-item>
                <el-form-item prop="code">
                    <el-form-item prop="code" :rules="{ required: true, message: '请输入', trigger: 'blur' }">
                        <span class="icon">
                            <img src="../assets/img/login_icon_yanzhengma@3x (2).png" alt="" />
                        </span>
                        <el-input style="width: 300px" v-model="form.code" placeholder="请输入短信验证码">
                            <span
                                @click="send"
                                style="color: #ff4f50; cursor: pointer; font-size: 13px"
                                slot="suffix"
                                >{{ time === 0 ? '发送验证码' : `重新发送${time}s` }}</span
                            >
                        </el-input>
                    </el-form-item>
                </el-form-item>
                <el-form-item prop="password">
                    <span class="el-icon-unlock"></span>
                    <el-input
                        style="width: 300px"
                        v-model="form.password"
                        type="password"
                        autocomplete="off"
                        placeholder="请输入8-16位密码，建议数字、大小写字母组合"
                    ></el-input>
                </el-form-item>
                <el-form-item prop="password2">
                    <span class="el-icon-unlock"></span>
                    <el-input
                        style="width: 300px"
                        v-model="form.password2"
                        type="password"
                        autocomplete="off"
                        placeholder="请再次输入密码"
                    ></el-input>
                </el-form-item>
            </el-form>
            <template>
                <el-checkbox v-model="checked" v-if="isShow2">
                    <el-link style="margin-bottom: 2px">点击同意注册协议</el-link>
                </el-checkbox>
            </template>
            <div class="sub-btn" style="width: 300px" v-if="isShow2">
                <div class="btn1" @click="doRegister">立即注册</div>
                <el-button style="background: #f2f3f5; color: #292c33" type="info" class="btn-block" @click="Login"
                    >已有账号，立即登录</el-button
                >
            </div>
            <div class="sub-btn" style="width: 300px" v-else>
                <el-button type="primary" class="btn-block" @click="passwordFn2">确定</el-button>
                <el-button style="background: #f2f3f5; color: #292c33" type="info" class="btn-block" @click="Login"
                    >想起密码，立即登录</el-button
                >
            </div>
        </div>
    </el-dialog>
</template>
<script>
import phone from '../mixins/phone';
export default {
    mixins: [phone],
    props: {
        isShow: {
            type: Boolean,
            default: false
        },
        isShow2: {
            type: Boolean,
            default: false
        }
    },
    data() {
        return {
            rules: {
                password: [{ min: 8, max: 16, message: '密码长度在 8 到 16 个字符', trigger: 'blur' }],
                password2: [
                    {
                        validator: (rule, value, callback) => {
                            if (value !== this.form.password) {
                                callback(new Error('两次密码输入不一致'));
                            } else {
                                callback();
                            }
                        },
                        trigger: 'blur'
                    },
                    { required: true, message: '请再次输入密码', trigger: 'blur' }
                ]
            },
            form: {},
            time: 0,
            checked: false
        };
    },
    computed: {
        Title() {
            if (this.isShow2) {
                return '注册';
            } else {
                return '修改密码';
            }
        }
    },
    methods: {
        Login() {
            this.$emit('init');
        },
        // 验证码
        send() {
            this.sendMsg(this.form.phone);
        },
        handleClose() {
            this.$emit('refreash');
        },
        // 注册
        doRegister() {
            this.$http;
            this.verifyMsg(this.form.phone, this.form.code).then(() => {
                return (
                    this.$http
                        .post('/auth/phoneRegister', {
                            phone: this.form.phone,
                            password: this.form.password,
                            code: this.form.code
                        })
                        // })
                        .then(res => {
                            console.log(res);
                            localStorage.setItem('webToken', res);
                            return this.$store.dispatch('getUserInfo');
                        })
                        .then(() => {
                            this.$message.success('注册成功');
                            this.$emit('init');
                        })
                        .catch(e => {
                            if (e) {
                                this.$message.error(e.error);
                            }
                        })
                );
            });
        },
        // 修改密码
        passwordFn2() {
            this.verifyMsg(this.form.phone, this.form.code)
                .then(() => {
                    this.$http
                        .post('/auth/forgotPassword', {
                            phone: this.form.phone,
                            password: this.form.password,
                            code: this.form.code
                        })
                        .then(() => {
                            this.$toast.success('修改成功');
                            this.$emit('init');
                        })
                        .catch(e => {
                            if (e) {
                                this.$message.error(e.error);
                            }
                        });
                })
                .catch(e => {
                    if (e) {
                        this.$message.error(e.error);
                    }
                });
        },
        closeDialog() {
            this.$emit('close1');
        }
    }
};
</script>
<style lang="less" scoped>
/deep/ .el-dialog {
    width: 480px !important;
    padding-bottom: 55px;
    background: #ffffff;
    border-radius: 8px;
    .el-dialog__title {
        font-size: 20px;
        font-weight: 500;
        color: #292c33;
        line-height: 28px;
    }
}
/deep/ .el-dialog__header {
    padding: 30px 0 45px 80px;
}
/deep/ .el-dialog__headerbtn {
    margin: 14px 50px 0 0;
    &::before {
        width: 24px;
        height: 24px;
    }
}
/deep/ .el-dialog__close {
    width: 24px;
    height: 24px;
}
/deep/ .el-dialog__body {
    padding: 0;
}
/deep/ .el-checkbox {
    margin-left: -127px;
}
/deep/ .el-checkbox__inner {
    border-radius: 50%;
}
/deep/ .el-form-item__error {
    left: 110px;
}
/deep/ .el-form-item__content {
    display: flex;
    justify-content: center;
}
.content {
    .el-form {
        /deep/ .el-input {
            .el-input__inner {
                border-width: 0px;
                border-bottom: 1px solid #f2f3f5;
                padding: 0 0 0;
                height: 50px;
                background: none;
                line-height: 50px;
                margin-left: 10px;
            }
            .el-input__prefix {
                color: #292c33;
                font-size: 18px;
            }
            .el-input-group__append {
                background-color: transparent;
                border-width: 0;
            }
        }
    }
    text-align: center;
    .el-icon-mobile-phone {
        color: #000;
        font-size: 24px;
        margin-top: 13px;
    }
    .el-icon-unlock {
        color: #000;
        font-size: 24px;
        margin-top: 12px;
    }
    .icon {
        color: #000;
        img {
            width: 28px;
            height: 28px;
            padding-top: 10px;
        }
    }
    .btn {
        display: flex;
        align-items: center;
        width: 330px;
        justify-content: space-between;
        margin-left: 80px;
    }
    .sub-btn {
        padding-top: 62px;
        text-align: center;
        width: 300px;
        margin-left: 78px;
        .btn1 {
            width: 330px;
            height: 40px;
            line-height: 40px;
            background: linear-gradient(135deg, #00ffcb 0%, #006eff 100%);
            border-radius: 4px;
            font-size: 13px;
            color: #fff;
            text-align: center;
        }
        .el-button {
            font-size: 12px;
            line-height: 16px;
            width: 330px;
            border-width: 0px;
            margin-top: 20px;
        }
    }
}
</style>
