<!--登录界面-->
<template>
    <div class="login" :style="loginStyle">
        <section class="login-content" :style="loginContentStyle">
            <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 :style="buttonStyle" type="primary" @click="submitForm('ruleForm')">提交</el-button>
                    <el-button :style="buttonStyle" type="primary" @click="resetForm('ruleForm')">重置</el-button>
                </el-form-item>
            </el-form>
        </section>
    </div>
</template>

<script>
    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,
                loginStyle:{
                    "background-color": `#${loginJson[0].BrackgroupStr.slice(3)}`,
                },
                loginContentStyle: {
                    "width": loginJson[0].Width + 'px',
                    "height": loginJson[0].Height + 'px',
                    "left":loginJson[0].Left + 'px',
                    "top":loginJson[0].Top + 'px',
                    "z-index": loginJson[0].ZIndex,
                    "border": `1px solid #${loginJson[0].BorderStr.slice(3)}`,
                    "background-color": `#${loginJson[0].ForegroundStr.slice(3)}`,
                },
                buttonStyle: {
                  'background-color': `#${loginJson[0].BTNForegroundStr.slice(3)}`,
                }
            }
        },

        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-size: 100% 100%;
        position: relative;

        .login-content {
            position: absolute;
            h2 {
                text-align: center;
                font-weight:bold;
                margin-bottom: 20px;
            }
            .el-input {
                width:90%;
            }
            .login-form {
                .login-form-button {
                    width: 100%;
                }
            }
        }
    }
</style>
