<template>
    <div class="login-par">
        <div class="changeLanguage">
            <span @click="changeLanguage" :class="$i18n.locale=='zh-CN'?'choseColor':'noColor'">中文</span>
                /
            <span @click="changeLanguage" :class="$i18n.locale=='zh-CN'?'noColor':'choseColor'">Engilsh</span>
        </div>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-position="left" label-width="0px" class="demo-ruleForm login-container">
            <div class="login-logo">
                <img src="../assets/image/login_logo.png" style="width:80px;height:80px;"/>
            </div>
            <h3 class="title">{{$t('base.title')}}</h3>
            <el-form-item class="login-input" prop="account">
                <el-input type="text" v-model="ruleForm.account" autocomplete="off" :placeholder="$t('base.account')" clearable prefix-icon="el-icon-user-solid"></el-input>
            </el-form-item>
            <el-form-item class="login-input" prop="password">
                <el-input type="password" v-model="ruleForm.password" @keyup.enter.native="handleSubmit" autocomplete="off" :placeholder="$t('base.password')" show-password prefix-icon="el-icon-lock"></el-input>
            </el-form-item>
            <el-form-item class="login-button" style="width:100%;">
                <el-button type="primary" style="width:100%;" @click.native.prevent="handleSubmit" :loading="logining">{{$t('base.login')}}</el-button>
            </el-form-item>
            <div style="float:right;margin-top:7px;"><a href="http://www.yunsu.cn">返回官网</a></div>
        </el-form>
        
        <div class="login-backImg">
            <img src="../assets/image/login_center.png" />
        </div>
        
    </div>
</template>

<script>
    export default {
        data() {
            const msg = (rule, value, callback) => {
                if (!value) {
                    if(rule.field == "account") {
                        return callback(new Error(this.$t('msg.loginAcc')));
                    } else {
                        return callback(new Error(this.$t('msg.loginPass')));
                    }
                } else {
                    callback();
                }
            };
            return {
                logining: false,
                // 登录信息
                ruleForm: {
                    account: '',
                    password: ''
                },
                rules: {
                    account: [
                        { required: true, validator: msg , trigger: 'blur' },
                    ],
                    password: [
                        { required: true, validator: msg , trigger: 'blur' },
                    ]
                }
            };
        },
        methods: {
            handleReset2() {
                this.$refs.ruleForm.resetFields();
            },
            changeLanguage() {
                this.$confirm(this.$t('msg.changeLanguage'), this.$t('el.messagebox.title'), {
                    confirmButtonText: this.$t('el.messagebox.confirm'),
                    cancelButtonText: this.$t('el.messagebox.cancel'),
                    type: 'warning'
                }).then(() => {
                    this.$router.go(0);
                    if ( this.$i18n.locale === 'zh-CN' ) {
                        this.$i18n.locale = 'en-US';
                        localStorage.lang = 'en-US';
                    }else {
                        this.$i18n.locale = 'zh-CN';
                        localStorage.lang = 'zh-CN';
                    }
                }).catch(() => {});
            },
            handleSubmit() {
                this.$refs.ruleForm.validate((valid) => {
                    if (valid) {
                        var _this = this;
                        this.logining = true;
                        this.http.post(this.port.manage.login, this.ruleForm , res => {
                            this.logining = false;
                            if (res.code == "ok") {
                                sessionStorage.setItem('user', JSON.stringify(res.data));
                                this.$router.push({ path: '/map' });
                            } else {
                                this.$message({
                                    message: res.msg,
                                    type: 'error'
                                });
                            }
                        }, error => {
                            this.logining = false;
                            this.$message({
                                message: error,
                                type: 'error'
                            });
                        })
                    }
                });
            }
        }
    }
</script>

<style lang="scss" scoped>
    .login-par {
        position: relative;
    }

    .changeLanguage {
        position: absolute;
        right:30px;
        top:-160px;
        font-size: 17px;
        cursor: pointer;

        .choseColor {
            color: #409EFF;
        }

        .noColor {
            color: #cac6c6;
        }
    }

    .login-logo {
        text-align: center;
        margin: 0 0 20px 0;
    }

    .login-container {
        /*box-shadow: 0 0px 8px 0 rgba(0, 0, 0, 0.06), 0 1px 0px 0 rgba(0, 0, 0, 0.02);*/
        -webkit-border-radius: 5px;
        border-radius: 5px;
        -moz-border-radius: 5px;
        background-clip: padding-box;
        margin: 180px auto;
        // width: 350px;
        width: 315px;
        height: 365px;
        // padding: 35px 35px 15px 35px;
        padding: 25px 35px 25px 35px;
        background: #fff;
        border: 1px solid #eaeaea;
        box-shadow: 0 0 5px #cac6c6;
        border-top: 10px solid #409EFF;
        .title {
            font-size: 20px;
            margin: 0px auto 40px auto;
            text-align: center;
            color: #505458;
        }
        .remember {
            margin: 0px 0px 35px 0px;
        }
        .login-input {
            margin: 30px 0 0 0;
        }
        .login-button {
            margin: 30px 0 0 0;
        }
        .login-button .el-button {
            padding: 14px;
        }
    }

    .login-backImg {
        position: absolute;
        width: 100%;
        height: 350px;
        background-color: #409EFF;
        top: 110px;
        z-index: -1;
        img {
            width: 100%;
            height: 100%;
        }
    }
</style>