<template>
    <div class="wrap">
        <div class="shapes">
            <img class="shape1" src="@/assets/img/shapes/shape1.png" alt="">
            <img class="shape2" src="@/assets/img/shapes/shape2.png" alt="">
            <img class="shape3" src="@/assets/img/shapes/shape3.png" alt="">
            <img class="shape4" src="@/assets/img/shapes/shape4.png" alt="">
            <img class="shape5" src="@/assets/img/shapes/shape5.png" alt="">
            <img class="shape6" src="@/assets/img/shapes/shape6.png" alt="">
        </div>
        <div class="login">
            <div class="form">
                <h6 class="title">欢迎使用请登录</h6>
                <ul class="tab">
                    <li v-for="(item,index) in tabList" :key="index" :class="{active: form.distinguish == item.id}" @click="typeClick(item)">{{item.label}}</li>
                </ul>
                <el-form :model="form" :rules="loginRules" ref="form" style="margin-top: 20px">
                    <el-form-item v-if="form.distinguish === '1'" prop="account">
                        <label class="account"></label>
                        <el-input v-model.trim="form.account" placeholder="请输入账号"></el-input>
                    </el-form-item>
                    <el-form-item v-if="form.distinguish === '2'" prop="account">
                        <label class="account"></label>
                        <el-input v-model.trim="form.account" placeholder="请输入手机号/邮箱"></el-input>
                    </el-form-item>
                    <el-form-item prop="password">
                        <label class="password"></label>
                        <el-input
                            type="password"
                            placeholder="请输入密码"
                            v-model.trim="form.password"
                        >
                        </el-input>
                    </el-form-item>
                    <el-form-item prop="code">
                        <label class="code"></label>
                        <el-input
                            placeholder="请输入验证码"
                            v-model.trim="form.code"
                            @keyup.enter.native="submitFormLogin"
                        >
                        </el-input>
                        <img @click="getVerImg" :src="verificationIMG" class="ver-img" alt="">
                    </el-form-item>
                    <el-button class="submit" type="primary" @click="submitFormLogin">登录</el-button>
                </el-form>
            </div>
        </div>
        <el-dialog title="绑定手机号" :visible.sync="phoneVisible" :close-on-click-modal="false" width="30%">
            <div style='padding: 0 13px 20px 13px;'>
                依据国家政策法规，需绑定手机号进行实网络实名才可登录使用本平台
            </div>
            <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 {
            tabList: [
                {
                    id: '1',
                    label: '账号'
                },{
                    id: '2',
                    label: '手机号/邮箱'
                }
            ],
            verificationIMG: "",
            form: {
                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.form.random = Math.floor(Math.random() * 999999999);
            this.verificationIMG = this.api.verification + "?random=" + `${this.form.random}`;
        },
        typeClick(tab) { // 切换标签
        console.log(tab)
            this.form.distinguish = tab.id
            this.form.account = "";
            this.$refs.form.clearValidate();
            this.loginRules.account[0].message = tab.id === "1" ? "请输入账号" : "请输入手机号/邮箱";
        },
        submitFormLogin() { // 提交登录
            this.$refs.form.validate(valid => {
                if (valid) {
                    this.form.distinguish = Number(this.form.distinguish);
                    this.login(this.form).then(() => {
                        let redirect = this.$route.query.redirect ? decodeURIComponent(this.$route.query.redirect) : "/station";
                        this.$router.replace(redirect);
                    }).catch(res => {
                        if (res && res.status == 30001) {
                            this.phoneVisible = true;
                        }
                        this.getVerImg();
                        this.form.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.form.account
            };
            this.$post(this.api.bindPhoneOrEmail, data).then(res => {
                util.successMsg("绑定成功");
                this.form.phone = this.phone;
                this.phoneVisible = false;

                util.local.set(Setting.tokenKey, res.token, Setting.tokenExpires);
                let redirect = this.$route.query.redirect ? decodeURIComponent(this.$route.query.redirect) : "/index";
                this.$router.replace(redirect);
                util.successMsg("登录成功");
            }).catch(res => {
            });
        }
    }
};
</script>

<style scoped lang="scss">
.wrap {
    min-height: 100%;
    background-color:  #F3F6FA;
    overflow: hidden;
    .shapes{
        img{
            position: absolute;
        }
    }
    .shape1{
        top: 123px;
    }
    .shape2{
        top: 238px;
        left: 35px;
    }
    .shape3{
        top: 485px;
    }
    .shape4{
        bottom: 285px;
        right: 0;
    }
    .shape5{
        bottom: 145px;
        right: 0;
    }
    .shape6{
        bottom: 0;
        right: 0;
    }
    .login{
        width: 436px;
        margin: 188px auto 150px;
        .form{
            padding: 38px 38px 60px;
            border-radius: 6px;
            background-color: #fff;
            .title{
                margin-bottom: 25px;
                font-size: 26px;
                color: #0B1D30;
                letter-spacing: 4px;
            }
            .tab{
                display: flex;
                align-items: center;
                margin-bottom: 24px;
                border-bottom: 2px solid #E1E6F2;
                li{
                    padding: 18px 0;
                    margin-bottom: -1px;
                    font-size: 18px;
                    color: #555;
                    cursor: pointer;
                    border-bottom: 4px solid transparent;
                    &:first-child{
                        margin-right: 50px;
                    }
                    &.active{
                        color: $main-color;;
                        border-bottom-color: $main-color;
                    }
                }
            }
            .label{
                margin-bottom: 10px;
                color: #105CB2;
            }
            /deep/.el-form-item{
                margin-bottom: 20px;
            }
            /deep/.el-input__inner{
                position: relative;
                height: 52px;
                padding: 0 20px 0 34px;
                line-height: 50px;
                background-color: #FBFBFB;
                border: 1px solid #E1E6F2;
                border-radius: 4px !important;
            }
            .account,.password,.code{
                z-index: 1;
                position: absolute;
                top: 17px;
                left: 11px;
                width: 18px;
                height: 18px;
                background: url(../../../assets/img/login/account.png) 0 0/100% 100% no-repeat;
            }
            .password{
                top: 18px;
                background-image: url(../../../assets/img/login/password.png);
            }
            .code{
                top: 18px;
                background-image: url(../../../assets/img/login/code.png);
            }
            .ver-img{
                position: absolute;
                top: 1px;
                right: 1px;
            }
            /deep/.el-form-item__error{
                top: 105%;
                left: auto;
                right: 0;
                color: #FFA94E;
            }
            .submit{
                width: 100%;
                height: 48px;
                margin-top: 30px;
                line-height: 48px;
                padding: 0;
                font-size: 20px;
                background-color: $main-color;
                border-radius: 4px;
                border: 0;
            }
        }
    }
}
.footer{
    z-index: 3;
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
}
.copyright{
    padding: 18px 0;
    font-size: 12px;
    text-align: center;
    background-color: #F3F6FA;
    a{
        color:#B1B4B8;
        font-size: 12px;
        &:hover{
            opacity: .8;
        }
    }
}
.tips {
    margin: 0 0 10px 5px;
    font-size: 14px;
    text-align: center;
    color: #666;
}
</style>