<template>
    <div class="login-bg">
        <div class="login-container">
            <div class="login-header">
                <img class="logo mr10" src="../../assets/img/logo.svg" alt="" />
                <div class="login-title">{{ $t('kingCasino') }}</div>
            </div>
            <el-form :model="param" :rules="rules" ref="login">
                <el-form-item prop="account">
                    <el-input v-model="param.account" :placeholder="$t('name')">
                        <template #prepend>
                            <el-icon>
                                <User />
                            </el-icon>
                        </template>
                    </el-input>
                </el-form-item>
                <el-form-item prop="loginPwd">
                    <el-input
                        type="password"
                        :placeholder="$t('cipher')"
                        v-model="param.loginPwd"
                        @keyup.enter="submitForm()"
                    >
                        <template #prepend>
                            <el-icon>
                                <Lock />
                            </el-icon>
                        </template>
                    </el-input>
                </el-form-item>
                <el-form-item >
                  <el-checkbox class="pwd-checkbox" v-model="checked" :label="$t('remember')" />
                  <!-- <el-link type="primary" @click="$router.push('/reset-pwd')">忘记密码</el-link> -->
                </el-form-item>
                <el-button class="login-btn" type="primary" size="default" @click="submitForm()">{{ $t('login') }}</el-button>
                <!-- <el-form-item > -->
                  <div style="width: 100%;padding: 0 31%;display: flex;justify-content: space-around;margin-top: 10px; box-sizing: border-box;">
                    <el-button text type='primary' size="small" @click="setLanguage('en')">English</el-button>
                    <el-button text type='primary' size="small" @click="setLanguage('es')">Spanish</el-button>
                  </div>
                <!-- </el-form-item> -->
            </el-form>
        </div>
    </div>
</template>

<script setup>
import { ref, reactive } from 'vue';
// import { usePermissStore } from '@/store/permiss';
import { useRouter } from 'vue-router';
import { debounce } from 'lodash';
import {md5Data} from '@/utils/index';
import req from '@/utils/request' 
import { userInfoStore } from '../../store/userInfo';
import i18n from '@/utils/i18n'
import { useI18n } from 'vue-i18n';
const { locale } = useI18n()
const userInfo = userInfoStore();
const lgStr = localStorage.getItem('login-param');
const defParam = lgStr ? JSON.parse(lgStr) : null;
const checked = ref(lgStr ? true : false);
const router = useRouter();
const param = reactive({
  account: defParam ? defParam.account : '',
  loginPwd: defParam ? defParam.loginPwd : ''
});

const rules = {
  account: [
        {
            required: true,
            message: i18n.global.t('enterUsername'),
            trigger: 'blur',
        },
    ],
    loginPwd: [{ required: true, message: i18n.global.t('enterPassword'), trigger: 'blur' }],
};
// const permiss = usePermissStore();
const login = ref(null);
const setLanguage = (newLang) => {
  console.log(newLang, 'newLang');
  
  locale.value = newLang; 
};
const submitForm = debounce(() => {
    login.value.validate(async (valid) => {
      if (valid) {
          const encryptedPassword = md5Data(param.loginPwd);
        if (typeof encryptedPassword === 'string') {
          const res = await req('post', '/admin/user/passwordLogin', {...param, loginPwd: encryptedPassword})
          if (res.code===200) {
            userInfo.setUserInfo(res.data);
            if (checked.value) {
                localStorage.setItem('login-param', JSON.stringify(param));
            } else {
                localStorage.removeItem('login-param');
            }
            router.push('/');
          }
        }
      }
    });
}, 500);
</script>

<style lang="scss" scoped>
.login-bg {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100vh;
    background: url(../../assets/img/login-bg.png) center/cover no-repeat;
}

.login-header {
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 40px;
}

.logo {
    width: 35px;
}

.login-title {
    font-size: 18px;
    color: #333;
    font-weight: bold;
}

.login-container {
    width: 450px;
    border-radius: 5px;
    background: #ffffffcd;
    padding: 40px 50px 50px;
    box-sizing: border-box;
}

.pwd-tips {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 14px;
    margin: -10px 0 10px;
    color: #787878;
}

.pwd-checkbox {
    height: auto;
}

.login-btn {
    display: block;
    width: 100%;
}

.login-tips {
    font-size: 12px;
    color: #999;
}

.login-text {
    display: flex;
    align-items: center;
    margin-top: 20px;
    font-size: 14px;
    color: #787878;
}
</style>
