<template>
  <div class="login-container">
    <div class="login-container-center">
      <div class="login-container-left">
        <el-image
          :src="loginImg"
          fit="cover"
        />
      </div>
      <div class="login-container-right">
        <el-form
          ref="loginFormRef"
          :model="loginData"
          :rules="loginRules"
          class="login-form"
          auto-complete="on"
          label-position="left"
        >
          <div class="title-container">
            <h3 class="title">{{$t('login.title')}}</h3>
          </div>

          <el-form-item prop="username">
            <span class="svg-container">
              <svg-icon icon-class="user" />
            </span>
            <el-input
              ref="username"
              clearable
              v-model="loginData.username"
              :placeholder="$t('login.username')"
              name="username"
              type="text"
              tabindex="1"
              auto-complete="on"
            />
          </el-form-item>

          <el-tooltip
            :disabled="capslockTooltipDisabled"
            content="Caps lock is On"
            placement="right"
          >
            <el-form-item prop="password">
              <span class="svg-container">
                <svg-icon icon-class="password" />
              </span>
              <el-input
                ref="passwordRef"
                clearable
                :key="passwordType"
                v-model="loginData.password"
                :type="passwordType"
                :placeholder="$t('login.password')"
                name="password"
                tabindex="2"
                auto-complete="on"
                @keyup="checkCapslock"
                @blur="capslockTooltipDisabled = true"
                @keyup.enter="handleLogin"
              />
              <span
                class="show-pwd"
                @click="showPwd"
              >
                <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
              </span>
            </el-form-item>
          </el-tooltip>

          <el-form-item
            prop="code"
            class="login-code"
          >
            <span class="svg-container">
              <svg-icon icon-class="valid_code" />
            </span>
            <el-input
              style="width: 60%;"
              clearable
              v-model="loginData.code"
              :placeholder="$t('login.code')"
              name="username"
              type="text"
              tabindex="3"
              auto-complete="on"
              @keyup.enter="handleLogin"
            />
            <span class="svg-container svg-container-refresh flex items-center" @click="getCode">
              <img :src="codeUrl" class="mr-[10px]">
              <svg-icon icon-class="refresh" />
            </span>
          </el-form-item>
          <div>
            <el-checkbox
              v-model="remember"
              :label="$t('login.remember')"
              size="large"
            />
          </div>
          <el-button
            size="default"
            :loading="loading"
            type="primary"
            style="width: 100%;height: 50px; margin-bottom: 10px"
            @click.prevent="handleLogin"
          >{{ $t('login.login') }}
          </el-button>
        </el-form>

        <div
          v-if="showCopyright == true"
          class="copyright"
        >
          <p>{{ $t('login.copyright') }}</p>
          <p>{{ $t('login.icp') }}</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { onMounted, reactive, ref, toRefs, watch, nextTick } from 'vue';
import loginImg from '@/assets/images/u287.gif';
import { getCaptcha } from '@/api/auth';

// 组件依赖
import { ElForm, ElInput } from 'element-plus';
import router from '@/router';
import SvgIcon from '@/components/SvgIcon/index.vue';
import Cookies from 'js-cookie';
// API依赖
import { useRoute } from 'vue-router';
import { LoginData } from '@/api/auth/types';

//密码加密
import { encrypt,decrypt } from '@/utils/rsaEncrypt';

// 状态管理依赖
import { useUserStore } from '@/store/modules/user';

// 国际化
import { useI18n } from 'vue-i18n';
const { t } = useI18n();

const userStore = useUserStore();
const route = useRoute();
// 图片验证码
const codeUrl = ref('');

const loginFormRef = ref(ElForm);
const passwordRef = ref(ElInput);
// 记住密码
let remember = ref(false);

const state = reactive({
  redirect: '',
  loginData: {
    uuid: '',
    username: 'admin',
    password: '123456',
    code: ''
  } as LoginData,
  loginRules: {
    username: [{ required: true, trigger: 'blur', message: t('login.rulesUsername') }],
    password: [{ required: true, trigger: 'blur',  message: t('login.rulesPassword') }],
    code: [{ required: true, trigger: 'blur', message: '请输入验证码' }],
  },
  loginImg: loginImg[0],
  loading: false,
  passwordType: 'password',
  // 大写提示禁用
  capslockTooltipDisabled: true,
  otherQuery: {},
  clientHeight: document.documentElement.clientHeight,
  showCopyright: true,
  showDialog: false,

  cookiePass: ''
});

// function validatePassword(rule: any, value: any, callback: any) {
//   if (value.length == 0) {
//     callback(new Error(t('login.rulesPassword')));
//   } else if (value.length < 6) {
//     callback(new Error(t('login.rulesPasswordPlace')));
//   } else {
//     callback();
//   }
// }

const {
  loginData,
  loginRules,
  loading,
  passwordType,
  capslockTooltipDisabled,
  showCopyright
} = toRefs(state);

function checkCapslock(e: any) {
  const { key } = e;
  state.capslockTooltipDisabled =
    key && key.length === 1 && key >= 'A' && key <= 'Z';
}

function showPwd() {
  if (passwordType.value === 'password') {
    passwordType.value = '';
  } else {
    passwordType.value = 'password';
  }
  nextTick(() => {
    passwordRef.value.focus();
  });
}

/**
 *  登录
 */
function handleLogin() {
  loginFormRef.value.validate((valid: boolean) => {
    if (valid) {
      const user = {
        username: state.loginData.username,
        password: state.loginData.password,
        // rememberMe: state.loginData.rememberMe,
        code: state.loginData.code,
        uuid: state.loginData.uuid
      };
      if (user.password !== state.cookiePass) {
        user.password = encrypt(user.password);
      }
      state.loading = true;
      userStore
        .login(user)
        .then(() => {
          Cookies.set('username', user.username);
          if (remember.value == true) {
            Cookies.set('password', user.password);
            Cookies.set('rememberMe', String(remember.value));
          }
          router.push({ path: state.redirect || '/', query: state.otherQuery });
          state.loading = false;
        })
        .catch(() => {
          getCode();
          state.loading = false;
        });
    } else {
      return false;
    }
  });
}

watch(
  route,
  () => {
    const query = route.query;
    if (query) {
      state.redirect = query.redirect as string;
      state.otherQuery = getOtherQuery(query);
    }
  },
  {
    immediate: true
  }
);

function getOtherQuery(query: any) {
  return Object.keys(query).reduce((acc: any, cur: any) => {
    if (cur !== 'redirect') {
      acc[cur] = query[cur];
    }
    return acc;
  }, {});
}
function getCookie() {
  const username = Cookies.get('username');
  let password = Cookies.get('password');
  const rememberMe = Cookies.get('rememberMe');
  rememberMe == 'true' ? (remember.value = Boolean(rememberMe)) : false;
  // 保存cookie里面的加密后的密码
  state.cookiePass = password === undefined ? '' : password;
  password = password === undefined ? state.loginData.password : password;
  state.loginData = {
    username: username === undefined ? state.loginData.username : username,
    password: decrypt(password),
    code: '',
    uuid: ''
  };
  remember.value = rememberMe === undefined ? false : Boolean(rememberMe)
}
function getCode() {
  getCaptcha().then((result:any) => {
    codeUrl.value = result.data.img
    state.loginData.uuid = result.data.uuid;
  })
}

onMounted(() => {
  getCookie();
  getCode();
  window.onresize = () => {
    if (state.clientHeight > document.documentElement.clientHeight) {
      state.showCopyright = false;
    } else {
      state.showCopyright = true;
    }
  };
});
</script>

<style lang="scss">
$bg: #283443;
$light_gray: #000000;
/* reset element-ui css */
.login-container {
  .title-container {
    position: relative;

    .title {
      font-size: 36px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }

    .set-language {
      color: #000;
      position: absolute;
      top: 3px;
      font-size: 18px;
      right: 0px;
      cursor: pointer;
    }
  }
  .el-form-item__content {
    width: 100%;
    background: #ffffff;
    border-radius: 5px;
    flex-wrap: nowrap;
  }
  .el-input {
    display: inline-block;
    width: calc(100% - 80px);
    height: 50px;
    background: #ffffff;
    .el-input__wrapper {
      width: 100%;
      padding: 0;
      background: #ffffff;
      box-shadow: none;
      .el-input__inner {
        background: #ffffff;
        border: 0px;
        -webkit-appearance: none;
        border-radius: 0px;
        color: $light_gray;
        height: 50px;
      }
    }
  }

  .el-input__inner {
    &:hover {
      border-color: var(--el-input-hover-border, var(--el-border-color-hover));
      box-shadow: none;
    }

    box-shadow: none;
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }

  .copyright {
    width: 100%;
    position: absolute;
    bottom: 0;
    font-size: 12px;
    text-align: center;
    color: #cccccc;
  }
  .login-code {
    width: 100%;
    .el-form-item__content {
      width: 100%;
      .el-input {
        width: calc(100% - 100px);
      }
    }
  }
}
</style>

<style lang="scss" scoped>
$bg: #fff;
$dark_gray: #889aa4;
$light_gray: #eee;
.login-container-center {
  width: 75%;
  height: calc(100vh - 200px);
  // min-height: 400px;
  background-color: $bg;
  display: flex;
  display: -webkit-flex;
  align-items: center;
  -webkit-align-items: center;
  justify-content: center;
  .login-container-left {
    .el-image {
      min-height: 420px;
      height: calc(100vh - 200px) !important;
      min-width: 310px;
    }
  }
  .login-container-right {
    width: 625px;
    height: calc(100vh - 200px);
    min-height: 420px;
    min-width: 350px;
    border-radius: 5px 5px 0;
    box-shadow: 0px 0px 10px rgb(0 0 0 / 10%);
    display: flex;
    display: -webkit-flex;
    justify-content: center;
    -webkit-justify-content: center;
    align-items: center;
    -webkit-align-items: center;
  }
}
.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;
  display: flex;
  display: -webkit-flex;
  justify-content: center;
  -webkit-justify-content: center;
  align-items: center;
  -webkit-align-items: center;
  .login-form {
    width: 85%;
    margin: 0 auto;
    overflow: hidden;
  }

  .svg-container {
    padding: 0 10px;
    color: $dark_gray;
    vertical-align: text-bottom;
    font-size: 24px;
    color: #cccccc;
  }
  .svg-container-refresh {
    padding-right: 5px;
    padding-left: 0;
    font-size: 26px;
    color: rgb(64, 158, 255);
    cursor: pointer;
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .captcha {
    position: absolute;
    right: 0;
    top: 0;

    img {
      height: 42px;
      cursor: pointer;
      vertical-align: middle;
    }
  }
}

.thirdparty-button {
  position: absolute;
  right: 40px;
  bottom: 6px;
}

@media only screen and (max-width: 470px) {
  .thirdparty-button {
    display: none;
  }
}
@media only screen and (max-width: 1366px) {
  .title {
    margin: 0px auto 20px auto !important;
  }
  .login-code {
    margin-bottom: 0;
  }
}
</style>
