<template>
    <div>
        <el-dialog
            :title="Title"
            append-to-body
            :visible.sync="Show"
            width="30%"
            :before-close="handleClose"
            @close="closeDialog"
        >
            <div class="content">
                <el-form v-if="!phoneLogin" :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>
                    <div @keyup.enter="Login">
                        <el-form-item prop="password" :rules="{ required: true, message: '请输入', trigger: 'blur' }">
                            <span class="el-icon-unlock"></span>
                            <el-input
                                style="width: 300px"
                                v-model="form.password"
                                placeholder="请输入密码"
                                type="password"
                            ></el-input>
                        </el-form-item>
                    </div>
                </el-form>
                <el-form v-if="phoneLogin" :model="form" ref="form" 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 v-model="form.phone" style="width: 300px" placeholder="请输入手机号"> </el-input>
                    </el-form-item>
                    <div @keyup.enter="loginPhone">
                        <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>
                    </div>
                </el-form>
                <div class="btn">
                    <el-link @click="phoneLogin = !phoneLogin">{{
                        phoneLogin ? '密码登录' : '短信验证码登录'
                    }}</el-link>
                    <el-link type="danger" v-if="!phoneLogin" @click="passwordFn">忘记密码？</el-link>
                </div>
                <div class="sub-btn">
                    <div v-if="!phoneLogin" @click="Login" class="btn1">登录</div>
                    <div class="btn1" v-else @click="loginPhone">登录</div>
                    <el-button
                        style="background: #f2f3f5; color: #292c33"
                        type="info"
                        class="btn-block"
                        @click="Register"
                        >还没有账号？立即注册</el-button
                    >
                </div>
            </div>
        </el-dialog>
        <register-info
            :isShow="isShow"
            :isShow2="isShow2"
            @init="init"
            @close1="isShow2 = false"
            @refreash="refreash"
        ></register-info>
    </div>
</template>
<script>
import phone from '../mixins/phone';
import RegisterInfo from './RegisterInfo.vue';
export default {
    mixins: [phone],
    components: { RegisterInfo },
    props: {
        // Show: {
        //     type: Boolean,
        //     default: false
        // }
    },
    data() {
        return {
            show: false,
            isShow: false,
            isShow2: false,
            phoneLogin: false,
            rules: {},
            Show: false,
            form: {},
            time: 0
        };
    },
    computed: {
        Title() {
            if (!this.phoneLogin) {
                return '登录';
            } else {
                return '短信验证码登录';
            }
        }
    },
    methods: {
        Register() {
            this.Show = false;
            this.isShow = true;
            this.isShow2 = true;
        },
        flag() {
            this.Show = true;
        },
        refreash() {
            this.Show = false;
            this.isShow = false;
        },
        init() {
            this.isShow = false;
            this.Show = true;
            this.phoneLogin = false;
        },
        handleClose() {
            this.Show = false;
            this.phoneLogin = false;
        },
        // 忘记密码
        passwordFn() {
            this.Show = false;
            this.isShow = true;
            this.isShow2 = false;
        },
        // 验证码
        send() {
            if (this.time === 0) {
                this.sendMsg(this.form.phone);
            }
        },
        // 密码登录
        check() {
            return this.$http
                .post('/auth/phonePwdLogin', {
                    phone: this.form.phone,
                    password: this.form.password
                })
                .catch(e => {
                    if (e) {
                        this.$message.error(e.error);
                    }
                    return Promise.reject();
                })
                .then(res => {
                    localStorage.setItem('webToken', res);
                    return this.$store.dispatch('getUserInfo');
                });
        },
        Login() {
            this.check().then(() => {
                this.$message.success('登录成功');
                this.Show = false;
                this.$EventBus.$emit('loginSuccess');
            });
        },
        // 验证码登录
        loginPhone() {
            return this.$http
                .post('/auth/phoneLogin', {
                    phone: this.form.phone,
                    code: this.form.code
                })
                .catch(e => {
                    if (e) {
                        this.$message.error(e.error);
                    }
                    return Promise.reject();
                })
                .then(res => {
                    localStorage.setItem('webToken', res);
                    return this.$store.dispatch('getUserInfo');
                })
                .then(() => {
                    this.$message.success('登录成功');
                    this.Show = false;
                });
        },
        closeDialog() {
            this.$emit('close');
        }
    }
};
</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-link {
    font-size: 12px;
}
/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, @prim 0%, @warn 100%);
            border-radius: 4px;
            font-size: 13px;
            color: #fff;
            text-align: center;
            cursor: pointer;
        }
        .el-button {
            font-size: 12px;
            line-height: 16px;
            width: 330px;
            border-width: 0px;
            margin-top: 20px;
        }
    }
}
</style>
