<!--登录界面-->
<template>
    <div class="login">
        <header class="login-header" :style="{background:loginJson[0].BrackgroupStr}">
            <h1>分布式视频处理系统</h1>
        </header>

        <section class="login-content">
            <h2>用户登录</h2>
            <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="55px" class="login-form">
                <el-form-item label="用户名" prop="username">
                    <el-input v-model="ruleForm.username" prefix-icon="el-icon-user"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input type="password" v-model="ruleForm.password" autocomplete="off" prefix-icon="el-icon-lock"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
                    <el-button @click="resetForm('ruleForm')">重置</el-button>
                </el-form-item>
            </el-form>
        </section>
    </div>
</template>

<script>
    import '../assets/less/login.less'
    import loginJson from '../../../static/EnityLogin'

    export default {
        data() {
            const checkUsername = (rule, value, callback) => {
                if (!value) {
                    return callback(new Error('用户名不能为空'))
                }
            }
            const validatePasswrod = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'))
                }
            }
            return {
                ruleForm: {
                    username: '',
                    password: '',
                },
                rules: {
                    username: [
                        { validator: checkUsername, trigger: 'blur' }
                    ],
                    password: [
                        { validator: validatePasswrod, trigger: 'blur' }
                    ]
                },
                loginJson
            }
        },

        methods: {
            submitForm() {
                const {username,password} = this.ruleForm
                if(username === '' || password === ''){
                    this.$message.warning('请输入用户名和密码！')
                    return
                }
                if(username === 'admin' && password === '123'){
                    const user = {username,password}
                    // 将user保存到vuex的state
                    this.$store.dispatch('saveUser', user)
                    // 去管理界面
                    this.$router.replace('/admin')
                }else {
                    this.$message.error('用户名或密码错误！')
                }
            },

            resetForm(formName) {
                this.$refs[formName].resetFields()
            }
        }
    }
</script>

<style lang="less">

    /*.login {
        width: 100%;
        height: 980px;
        background-image: url('../assets/images/bg.jpg');
        background-size: 100% 100%;

        .login-header {
            display: flex;
            align-items: center;
            height: 80px;
            background-color: rgba(21, 20, 13, 0.5);
            img {
                width: 40px;
                height: 40px;
                margin: 0 15px 0 50px;
            }
            h1 {
                font-size: 30px;
                color: white;
                margin-left:20px;
            }
        }

        .login-content {
            width: 400px;
            height: 300px;
            background-color: #fff;
            margin: 50px auto;
            padding: 20px 40px;
            border-radius: 2px;
            h2 {
                text-align: center;
                font-size: 30px;
                font-weight:bold;
                margin-bottom: 20px;
            }
            .login-form {
                .login-form-button {
                    width: 100%;
                }
            }
        }
    }*/
</style>
