<template>
    <div class="backdrop" :style="{backgroundImage: 'url('+(this.BackgroundImg  ? this.BackgroundImg : this.baseImg)+')'}">
        <el-container>
            <el-header style="display:block;position:relative;margin:auto;">
                <!-- logo -->
                <img :src="this.logo ? this.logo  : this.baseLogo" alt="" class="rightulliimg" />
            </el-header> 
             <el-main>
                <!-- 中间部分 -->
                <div class="login">
                    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
                        <el-tabs v-model="activeName" @tab-click="handleClick" style="margin-left: -61px;margin-top:-45px">
                            <el-tab-pane label="账号密码登录" name="first">
                                <el-form-item prop="username">
                                    <el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="账号">
                                        <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
                                    </el-input>
                                </el-form-item>
                                <el-form-item prop="password">
                                    <el-input v-model="loginForm.password" type="password" auto-complete="off" placeholder="密码" @keyup.enter.native="handleLogin" style="  width: 260px; ">
                                        <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
                                    </el-input>
                                </el-form-item>
                                <el-form-item prop="code">
                                    <el-input v-model="loginForm.code" auto-complete="off" placeholder="验证码" style="width: 53%;" @keyup.enter.native="handleLogin">
                                        <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
                                    </el-input>
                                    <div class="login-code">
                                        <img :src="codeUrl" @click="getCode" class="login-code-img" />
                                    </div>
                                </el-form-item>
                                <el-checkbox v-model="loginForm.rememberMe">记住密码</el-checkbox>
                                <el-form-item style="width:100%;">
                                    <el-button :loading="loading" size="medium" type="primary" @click.native.prevent="handleLogin" style="width: 190px;border-radius: 50px;margin-left:10%">
                                        <span v-if="!loading">登 录</span>
                                        <span v-else>登 录 中...</span>
                                    </el-button>
                                </el-form-item>
                                <el-divider class="divider_left"><span style="font-size:10px;word-break: keep-all;">第三方快捷登录</span></el-divider>
                                <el-form-item style="width:100%;">
                                    <img :src="wxSrc" @mouseover="changeImg" @mouseleave="returnImg" @click="getWeChatImg" style="width:30px;margin-left: 77px;">
                                </el-form-item>
                            </el-tab-pane>
                            <el-tab-pane label="手机验证码" name="second">
                                
                                <el-form-item prop="phone">
                                    <el-input v-model="loginForm.phone" type="tel" maxlength="11" auto-complete="off" placeholder="手机号" style="  width: 260px; ">
                                        <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
                                    </el-input>
                                </el-form-item>
                                <el-form-item prop="cade">
                                    <el-input v-model="loginForm.cade" type="text"  maxlength="6" auto-complete="off" placeholder="短信验证码" id="" onkeydown="enterHandler(event)" style="  width: 120px; ">
                                        <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
                                    </el-input>
                                    <el-button :loading="loading" size="min"  type="primary"  style="width: 120px; margin-left:12px">
                                        <span >获取动态密码 </span>
                                </el-button>
                                </el-form-item>

                                <el-button :loading="loading" size="medium" type="primary" @click.native.prevent="handleLogin" style="width: 190px;border-radius: 50px;margin-left:10%;margin-top:10%">
                                    <span v-if="!loading">登 录</span>
                                    <span v-else>登 录 中...</span>
                                </el-button>
                            </el-tab-pane>
                        </el-tabs>

                    </el-form>
                    <!--  底部  -->
                </div>
            </el-main>
        </el-container>
        <el-footer></el-footer>
    </div>
</template>
<script>
import { getCodeImg, listSscDomain } from "@/api/login";
import { wxImg } from "@/api/system/weChat";
import Cookies from "js-cookie";
import { encrypt, decrypt } from "@/utils/jsencrypt";
export default {
    name: "Login",
    data() {
        return {

            //默认背景图
            activeName: "first",
            baseImg: require('../assets/images/logi_bg1.jpg'),
            BackgroundImg : require('../assets/images/logi_bg1.jpg'),
            logo: '',
            baseLogo: require('../assets/images/lgo.png'),
            codeUrl: "",
            cookiePassword: "",
            loginForm: {
              username: "",
                password: "",
                phone: "",
                rememberMe: false,
                code: "",
                cade: "",
                uuid: "",
            },
            loginRules: {
                username: [
                    {
                        required: true,
                        trigger: "blur",
                        message: "用户名不能为空",
                    },
                ],
                //   phone: [
                //     {
                //         required: true,
                //         trigger: "blur",
                //         message: "手机号不能为空",
                //     },
                // ],
                code: [
                    {
                        required: true,
                        trigger: "change",
                        message: "验证码不能为空",
                    },
                ],
                // cade: [
                //     {
                //         required: true,
                //         trigger: "change",
                //         message: "验证码不能为空",
                //     },
                // ],
            },
            loading: false,
            redirect: undefined,
            wxSrc: require("../assets/images/wx1.png"),
        };
    },
    watch: {
        $route: {
            handler: function (route) {
                this.redirect = route.query && route.query.redirect;
            },
            immediate: true,
        },
    },
    created() {
        this.getCode();
        this.getCookie();
        this.getlistDomain();
    },
    methods: {
        changeImg(){
            this.wxSrc = require("../assets/images/wx.png")
        },
        returnImg(){
            this.wxSrc = require("../assets/images/wx1.png")
        },
        getCode() {
            getCodeImg().then((res) => {
                this.codeUrl = "data:image/gif;base64," + res.img;
                this.loginForm.uuid = res.uuid;
            });
        },
        getCookie() {
            const username = Cookies.get("username");
            const password = Cookies.get("password");
            const rememberMe = Cookies.get("rememberMe");
            this.loginForm = {
                username:
                    username === undefined ? this.loginForm.username : username,
                password:
                    password === undefined
                        ? this.loginForm.password
                        : decrypt(password),
                rememberMe:
                    rememberMe === undefined ? false : Boolean(rememberMe),
            };
        },
        // 查询域名
        getlistDomain() {
            var sscDomain = document.location.hostname;
            this.loginForm.sscDomain = document.location.hostname;
            listSscDomain(sscDomain).then(response => {
            if(response && response.data.lenth > 0){
            const sscConfig = response.data[0].sscConfig;
            const companyId = response.data[0].sscCompanyId;
            console.log(companyId)
            const imgForm = JSON.parse(sscConfig);
            if(imgForm){
            this.BackgroundImg = imgForm.BackgroundImg;
            this.logo = imgForm.logo;
            }
            console.log(this.logo)
            }
      });
        },

        handleLogin() {
            this.$refs.loginForm.validate((valid) => {
                if (valid) {
                    this.loading = true;
                    if (this.loginForm.rememberMe) {
                        Cookies.set("username", this.loginForm.username, {
                            expires: 30,
                        });
                        Cookies.set(
                            "password",
                            encrypt(this.loginForm.password),
                            { expires: 30 }
                        );
                        Cookies.set("rememberMe", this.loginForm.rememberMe, {
                            expires: 30,
                        });
                    } else {
                        Cookies.remove("username");
                        Cookies.remove("password");
                        Cookies.remove("rememberMe");
                    }
                    this.$store
                        .dispatch("Login", this.loginForm)
                        .then(() => {
                            if(this.redirect == '/index'){
                                this.$router.push({ path: "/homePage"+this.redirect || "/" });
                            }else{
                                this.$router.push({ path: this.redirect || "/" });
                            }
                            
                            
                        })
                        .catch(() => {
                            this.loading = false;
                            this.getCode();
                        });
                }
            });
        },
        getUrlKey(name) {
            return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [, ""])[1].replace(/\+/g, '%20')) || null
        },
        getWeChatImg(){
            const origin = document.location.origin.toString();
            let params = {}
            params.state = origin
            wxImg(params).then((res) => {
                // console.log(res)
                location.href = res
            })
        },
    },
};
</script>
<style rel="stylesheet/scss" lang="scss">
.login {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100%;
    background-size: cover;
}
// 中间背景图
.login-form {
    border-radius: 6px;
    width: 935px;
    height: 434px;
    padding: 73px 138px 103px 597px;
    background-image: url("../assets/images/login_bg2.png");
    background-size: 935px 434px;
    background-repeat: no-repeat;
    margin-top: 40px;
    // 输入框大小
    .el-input {
        height: 40px;
        width: 100%;
        input {
            height: 40px;
            width: 100%;
        }
    }
    .input-icon {
        height: 39px;
        width: 14px;
        margin-left: 2px;
    }
}
.login-tip {
    font-size: 13px;
    text-align: center;
}
// 验证码
.login-code {
    width: 33%;
    height: 38px;
    float: right;
        margin-right: 6%;

    img {
        cursor: pointer;
        vertical-align: middle;
    }
}
.login-code-img {
    height: 38px;
}
// 大背景图
.backdrop {
    
    background-repeat: no-repeat;
    background-size: 1536px 752px;
    background-size: cover;
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    background-position: right top;
    background-attachment: fixed;
}
.rightulliimg{
    width: 860px;
   
}
.divider_left{
    margin-left: -40px;
}
</style>
