<style lang="scss">
@import '@/styles/index.scss'; //导入样式

$bg: #1f2e3d;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
    .login-container .el-input input {
        color: $cursor;
    }
}

/* reset element-ui css */
.login-container {
    .el-input {
        display: inline-block;
        height: 47px;
        width: 85%;

        input {
            background: transparent;
            border: 0px;
            -webkit-appearance: none;
            border-radius: 0px;
            padding: 12px 5px 12px 15px;
            height: 47px;
            caret-color: $cursor;

            &:-webkit-autofill {
                box-shadow: 0 0 0px 1000px $bg inset !important;
                -webkit-text-fill-color: $cursor !important;
            }
        }
    }

    .el-form-item {
        border: 1px solid rgba(255, 255, 255, 0.1);
        //background: $color-form-item-bg;
        border-radius: 5px;
        color: #454545;
    }

    .loginBtn {
        background: $color-button-bg;
        height: 60px;
        line-height: 30px;
        font-size: $font-size-large;
    }
}
</style>

<style lang="scss" scoped>
$bg: #1f2e3d;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
    min-height: 100%;
    width: 100%;
    background-color: $bg;
    overflow: hidden;

    .login-form {
        position: relative;
        width: 520px;
        max-width: 100%;
        padding: 160px 35px 0;
        margin: 0 auto;
        overflow: hidden;
    }

    .svg-container {
        padding: 6px 5px 6px 15px;
        color: $dark_gray;
        vertical-align: middle;
        width: 30px;
        display: inline-block;
    }

    .title-container {
        position: relative;

        .title {
            font-size: 30px;
            color: $light_gray;
            margin: 0px auto 50px auto;
            text-align: center;
            font-weight: bold;
            letter-spacing: 5px;
        }
    }

    .show-pwd {
        position: absolute;
        right: 10px;
        top: 7px;
        font-size: 16px;
        color: $dark_gray;
        cursor: pointer;
        user-select: none;
    }

    .register {
        text-align: right;
        box-sizing: border-box;
        padding: 75px 50px;

        &-regbtn {
            &-span1 {
                color: #ffffff;
            }

            &-span2 {
                color: rgb(55, 140, 196);
                cursor: pointer;
            }
        }
    }

    .beian {
        position: absolute;
        bottom: 50px;
        width: 100%;
        text-align: center;
        box-sizing: border-box;
        font-size: 13px;
        color: #fff;
    }
}
</style>

<template>
    <div class="login-container">
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on"
            label-position="left">
            <div class="title-container">
                <h3 class="title">声学定位系统管理中心</h3>
            </div>
            <el-form-item prop="user_name">
                <span class="svg-container">
                    <svg-icon icon-class="user" />
                </span>
                <el-input v-model="loginForm.user_name" placeholder="请输入登录账号" name="user_name" type="text"
                    auto-complete="on" />
            </el-form-item>
            <el-form-item prop="password">
                <span class="svg-container">
                    <svg-icon icon-class="password" />
                </span>
                <el-input :key="passwordType" ref="password" v-model="loginForm.password" :type="passwordType"
                    placeholder="请输入密码" name="password" tabindex="2" auto-complete="on"
                    @keyup.enter.native="operatorLogin" />
                <span class="show-pwd" @click="showPwd">
                    <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
                </span>
            </el-form-item>
            <el-button class="loginBtn" :loading="loading" type="primary" style="width:100%;margin-bottom:30px;"
                @click.native.prevent="operatorLogin">登 录</el-button>
            <!-- <span class="register-regbtn-span1">还没有账号？</span><span class="register-regbtn-span2"
                @click="onRegister">立即注册</span> -->
        </el-form>
        <div class="beian">
            浙ICP备2025145156号
        </div>
    </div>
</template>

<script>

export default {
    name: 'Login',
    data() {
        return {
            loginForm: {
                user_name: '',
                password: ''
            },
            loginRules: {
                user_name: [{ required: true, message: "请输入登录账号", trigger: 'blur' }],
                password: [{ required: true, message: "请输入登录密码", trigger: 'blur' }]
            },
            loading: false,
            passwordType: 'password',
            redirect: undefined
        }
    },
    watch: {
        $route: {
            handler: function (route) {
                this.redirect = route.query && route.query.redirect
            },
            immediate: true
        }
    },
    methods: {
        showPwd() {
            if (this.passwordType === 'password') {
                this.passwordType = ''
            } else {
                this.passwordType = 'password'
            }
            this.$nextTick(() => {
                this.$refs.password.focus()
            })
        },
        async operatorLogin() {//操作员登录
            //校验数据是否正确
            this.$refs.loginForm.validate(valid => {
                if (!valid) return
            })

            try {
                //登录
                await this.$store.dispatch("user/userLogin", this.loginForm)

                let userId = this.$store.state.user.userInfo.id //获得当前登录用户的ID
                let oldUserId = this.$route.query.id //上一次登出的用户ID

                //跳转到上一次页面或者首页
                if (userId == oldUserId) {
                    this.$router.push(this.$route.query.return_url || '/')
                } else {
                    this.$router.push('/')
                }

            } catch (e) {
                this.$message({ message: e, type: 'error' })
            }
        },
        onRegister() {
            this.$router.push("register")
        }
    }
}
</script>
