<template>
    <div class="wrap">
        <div class="bg">
            <div class="left">
                <div class="text">
                    <p>欢迎使用</p>
                    <p>Occupation Lab</p>
                </div>
            </div>
            <div class="right"></div>
        </div>

        <div class="right-form">
            <div class="form">
                <el-tabs v-model="activeName" @tab-click="handleClick">
                    <el-tab-pane label="账号登录" name="1"></el-tab-pane>
                    <el-tab-pane label="手机号/邮箱登录" name="2"></el-tab-pane>
                    <el-form :model="loginForm" :rules="loginRules" ref="loginForm" style="margin-top: 20px">
                        <el-form-item v-if="activeName === '1'" label="用户名" prop="account">
                            <el-input v-model.trim="loginForm.account" placeholder="请输入账号"></el-input>
                        </el-form-item>
                        <el-form-item v-if="activeName === '2'" label="手机号/邮箱" prop="account">
                            <el-input v-model.trim="loginForm.account" placeholder="请输入手机号/邮箱"></el-input>
                        </el-form-item>
                        <el-form-item label="密码" prop="password">
                            <el-input
                                type="password"
                                placeholder="请输入密码"
                                v-model.trim="loginForm.password"
                            >
                            </el-input>
                        </el-form-item>
                        <el-form-item label="验证码" prop="code">
                            <el-input
                                placeholder="请输入验证码"
                                v-model.trim="loginForm.code"
                                @keyup.enter.native="submitFormLogin"
                            >
                            </el-input>
                            <img @click="getVerImg" :src="verificationIMG" class="verification" alt="">
                        </el-form-item>
                        <el-button class="submit" type="primary" @click="submitFormLogin">登录</el-button>
                    </el-form>
                </el-tabs>
            </div>
        </div>
        <el-dialog title="绑定手机号" :visible.sync="phoneVisible" :close-on-click-modal="false" width="30%">
            <el-form ref="form" label-width="60px">
                <el-form-item label="手机号">
                    <el-input style="width: 100%;" placeholder="请输入手机号" v-model="phone" maxlength="11"></el-input>
                </el-form-item>
                <el-form-item label="验证码">
                    <div style="display: flex;">
                        <el-input v-model="phoneCode" placeholder="请输入验证码" maxlength="6"></el-input>
                        <el-button style="margin-left: 10px" type="text" @click="sendPhoneCode"
                                   :disabled="phoneDisabled">{{ phoneBtnText }}
                        </el-button>
                    </div>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button size="small" @click="phoneVisible = false">取 消</el-button>
                <el-button size="small" type="primary" @click="phoneSubmit">确 定</el-button>
            </span>
        </el-dialog>
        <v-footer ref="footer"></v-footer>
    </div>
</template>

<script>
import vFooter from "@/layouts/footer";
import { mapActions } from "vuex";
import util from "@/libs/util";
import Setting from "@/setting";

export default {
    data: function() {
        return {
            verificationIMG: "",
            activeName: "1",
            loginForm: {
                account: "",
                password: "",
                code: "", // 验证码
                random: "", // 随机数
                distinguish: 1, // 区分手机号账号登录,1为账号登录,2为手机号或邮箱登录
                type: 1, // 平台端区分：0->教师端 1->学生端 2->无端
                platform: Setting.platformId
            },
            loginRules: {
                account: [{ required: true, message: "请输入账号", trigger: "blur" }],
                password: [{ required: true, message: "请输入密码", trigger: "blur" }],
                code: [{ required: true, message: "请输入验证码", trigger: "blur" }]
            },
            phoneVisible: false,
            phone: "",
            phoneCode: "",
            phoneDisabled: false,
            phoneTimer: null,
            phoneBtnText: "发送验证码"
        };
    },
    components: {
        vFooter
    },
    created() {
        this.getVerImg();
    },
    mounted() {
        // 页面离开的时候销毁手机和邮箱验证码定时器
        this.$once("hook:beforeDestroy", function() {
            clearInterval(this.phoneTimer);
            this.phoneTimer = null;
        });
    },
    methods: {
        ...mapActions("user", [
            "login"
        ]),
        getVerImg() { // 获取验证码图片
            this.loginForm.random = Math.floor(Math.random() * 999999999);
            this.verificationIMG = this.api.verification + "?random=" + `${this.loginForm.random}`;
        },
        handleClick(tab, event) { // 切换标签
            this.loginForm.account = "";
            this.$refs.loginForm.clearValidate();
            this.loginRules.account[0].message = tab.index === "1" ? "请输入账号" : "请输入手机号/邮箱";
        },
        submitFormLogin() { // 提交登录
            this.$refs.loginForm.validate(valid => {
                if (valid) {
                    this.loginForm.distinguish = Number(this.activeName);
                    this.login(this.loginForm).then(() => {
                        let redirect = this.$route.query.redirect ? decodeURIComponent(this.$route.query.redirect) : "/index";
                        this.$router.replace(redirect);
                    }).catch(res => {
                        if (res && res.status == 30001) {
                            this.phoneVisible = true;
                        }
                        this.getVerImg();
                        this.loginForm.code = "";
                    });
                } else {
                    return util.errorMsg("请检查表单数据");
                }
            });
        },
        phoneCountdown() {
            let count = 60;
            if (!this.phoneTimer) {
                this.phoneDisabled = true;
                this.phoneTimer = setInterval(() => {
                    console.log("倒计时中");
                    if (count > 0) {
                        count--;
                        this.phoneBtnText = `${count}秒后重试`;
                    } else {
                        this.phoneDisabled = false;
                        clearInterval(this.phoneTimer);
                        this.phoneTimer = null;
                        this.phoneBtnText = `发送验证码`;
                    }
                }, 1000);
            }
        },
        sendPhoneCode() {
            if (!this.phone) return util.warningMsg("请输入手机号");
            if (!/^1[3456789]\d{9}$/.test(this.phone)) return util.warningMsg("请输入正确的手机号");
            let data = {
                platform: Setting.platformId,
                phone: this.phone,
                types: 2
            };
            this.$post(this.api.sendPhoneOrEmailCode, data).then(res => {
                if (res.message.opener) {
                    this.phoneCountdown();
                    this.phoneOpener = res.message.opener;
                } else {
                    util.errorMsg(res.message);
                }

            }).catch(res => {
            });
        },
        phoneSubmit() {
            if (!this.phone) return util.warningMsg("请输入手机号");
            if (!/^1[3456789]\d{9}$/.test(this.phone)) return util.warningMsg("请输入正确的手机号");
            if (!this.phoneCode) return util.warningMsg("请输入验证码");
            let data = {
                phone: this.phone,
                types: 2,
                code: this.phoneCode,
                opener: this.phoneOpener,
                platform: Setting.platformId,
                account: this.loginForm.account
            };
            this.$post(this.api.bindPhoneOrEmail, data).then(res => {
                util.successMsg("绑定成功");
                this.loginForm.phone = this.phone;
                this.phoneVisible = false;
            }).catch(res => {
            });
        }
    }
};
</script>

<style scoped lang="scss">
.wrap {
    position: relative;
    width: 100%;
    height: 100%;
    overflow: hidden;

    .bg {
        display: flex;
        justify-content: space-between;
        align-items: center;
        height: calc(100% - 56px);

        .left {
            position: relative;
            width: 40%;
            height: 100%;
            background: url(../../../assets/img/bg_2.png) 0 0/100% 100% no-repeat;

            .text {
                position: absolute;
                top: 35%;
                left: 15%;
                color: #fff;
                font-size: 46px;
                font-weight: bold;

                p:first-child {
                    margin-bottom: 20px;
                }
            }
        }

        .right {
            width: 50%;
            height: 100%;
            background: url(../../../assets/img/bg_1.png) center center/80% auto no-repeat;
        }
    }


    /deep/ .right-form {
        position: absolute;
        top: 47%;
        right: 12%;
        transform: translateY(-50%);
        width: 30%;

        .logo {
            width: 100%;
            margin-bottom: 40px;
        }

        .form {
            padding: 50px 20px 20px;
            background-color: #fff;
            border-radius: 16px;
            box-sizing: border-box;
            box-shadow: 0 1px 20px rgba(146, 120, 255, 0.3);
        }

        .back {
            position: absolute;
            top: 20px;
            left: 20px;
            font-size: 24px;
            color: #9278ff;
            cursor: pointer;

            &:hover {
                opacity: .8;
            }
        }

        .el-tabs__nav-scroll {
            display: flex;
            justify-content: center;
        }

        h2 {
            padding-bottom: 10px;
            font-size: 20px;
            font-weight: 400;
            color: #8F73FF;
            text-align: center;
            border-bottom: 1px solid #f3f3f3;
        }

        .el-form {
            width: 70%;
            margin: 30px auto 0;

            .label {
                line-height: 1.8;
                color: #929292;
            }

            /deep/ .el-input__inner {
                height: 46px;
                padding: 0 23px;
                line-height: 46px;
                border: 1px solid #E5E5E5;
                border-radius: 8px !important;
            }

            /deep/ .el-form-item__error {
                top: 105%;
                left: auto;
                right: 0;
                color: #FFA94E;
            }

            .verification {
                position: absolute;
                right: 0px;
                width: 100px;
                height: 32px;
                border: 1px solid #DCDFE6;
                cursor: pointer;
            }

            .submit {
                width: 100%;
                height: 48px;
                margin-top: 20px;
                line-height: 48px;
                padding: 0;
                font-size: 18px;
                background-color: #9278ff;
                border-radius: 6px;
                border: 0;
            }
        }
    }
}

.switch {
    span {
        cursor: pointer;
        color: #9076FF;
    }
}

.links {
    width: 70%;
    margin: 20px auto 0;
    text-align: right;
}

.ques {
    color: #9278ff;
    font-size: 14px;
}

.forget {
    color: #ffa94e;
    font-size: 14px;
}
</style>