<template>
  <div style="background-color: rgba(251, 251, 251, 1)">
    <a-row>
      <a-col :span="24">
        <div :class="prefixCls">
          <div style="margin:50px">
            <div>
              <img width="233px" :src="logoImg" alt="jeecg" />
            </div>
          </div>
        </div>
      </a-col>
    </a-row>
    <a-row>
      <a-col :span="12">
        <div class="login-bg">
          <img :src="loginBg" alt="">
        </div>
      </a-col>
      <a-col :span="12" class="login-box">
        <div class="login-content">
          <div class="login-title">
            登录 AdWeb V3
          </div>
          <div class="login-form-box">
            <a-form ref="loginRef" :model="formData" @keyup.enter.native="loginHandleClick">
              <div class="login-account">
                <div class="login-inputClear">
                  <div class="login-label">
                    <span>账 号</span>
                  </div>
                  <a-form-item>
                    <a-input class="auto-fill" style="padding: 0 15px;font-size: 14px;" :placeholder="t('sys.login.userName')" v-model:value="formData.username" />
                  </a-form-item>
                </div>
                <div class="login-inputClear">
                  <div class="login-label">
                    <span>密 码</span>
                  </div>
                  <a-form-item>
                    <a-input class="auto-fill" style="padding: 0 15px;font-size: 14px;" type="password" :placeholder="t('sys.login.password')" v-model:value="formData.password" />
                  </a-form-item>
                </div>
                <div class="login-inputClear">
                  <div class="login-label">
                    <span>验证码</span>
                  </div>
                  <div class="login-code">
                    <a-form-item>
                      <a-input class="auto-fill" style="padding: 0 15px;font-size: 14px; width: 100%;" type="text" :placeholder="t('sys.login.inputCode')" v-model:value="formData.inputCode" />
                    </a-form-item>
                    <div style="margin-left: 20px">
                      <img v-if="randCodeData.requestCodeSuccess" :src="randCodeData.randCodeImage" @click="handleChangeCheckCode" />
                      <img v-else style="margin-top: 2px; max-width: initial" :src="codeImg" @click="handleChangeCheckCode" />
                    </div>
                  </div>
                </div>
                <div class="aui-flex" style="padding-top: 30px">
                  <div class="aui-flex-box">
                    <div class="aui-choice">
                      <a-input class="auto-fill" type="checkbox" v-model:value="rememberMe" />
                      <span style="margin-left: 5px">{{ t('sys.login.rememberMe') }}</span>
                    </div>
                  </div>
                </div>
              </div>
              <div class="aui-formButton">
                <div class="aui-flex">
                  <a-button :loading="loginLoading" class="aui-link-login" type="primary" @click="loginHandleClick">
                    {{ t('sys.login.loginButton') }}</a-button>
                </div>
              </div>
            </a-form>
          </div>
        </div>
      </a-col>
    </a-row>
  </div>
</template>
<script lang="ts" setup name="login-mini">
import { getCaptcha, getCodeInfo } from '/@/api/sys/user';
import { computed, onMounted, reactive, ref, toRaw, unref } from 'vue';
import codeImg from '/@/assets/images/checkcode.png';
import { useUserStore } from '/@/store/modules/user';
import { useMessage } from '/@/hooks/web/useMessage';
import { useI18n } from '/@/hooks/web/useI18n';
import logoImg from '/@/assets/loginmini/icon/soho_logo.webp';
import loginBg from '/@/assets/loginmini/icon/login_bg.webp';
import { useLocaleStore } from '/@/store/modules/locale';
import { useDesign } from "/@/hooks/web/useDesign";
import { useAppInject } from "/@/hooks/web/useAppInject";
import { useModal } from "@/components/Modal";

const { prefixCls } = useDesign('mini-login');
const { notification, createMessage } = useMessage();
const userStore = useUserStore();
const { t } = useI18n();
const localeStore = useLocaleStore();
const randCodeData = reactive<any>({
  randCodeImage: '',
  requestCodeSuccess: false,
  checkKey: null,
});
const rememberMe = ref<string>('0');
//手机号登录还是账号登录
const activeIndex = ref<string>('accountLogin');
const type = ref<string>('login');
//账号登录表单字段
const formData = reactive<any>({
  inputCode: '',
  username: 'admin',
  password: '123456',
});
//手机登录表单字段
const phoneFormData = reactive<any>({
  mobile: '',
  smscode: '',
});
const loginRef = ref();
//第三方登录弹窗
const thirdModalRef = ref();
//扫码登录
const codeRef = ref();
//是否显示获取验证码
const showInterval = ref<boolean>(true);
//60s
const timeRuning = ref<number>(60);
//定时器
const timer = ref<any>(null);
//忘记密码
const forgotRef = ref();
//注册
const registerRef = ref();
const loginLoading = ref<boolean>(false);
const { getIsMobile } = useAppInject();
const [captchaRegisterModal, { openModal: openCaptchaModal }] = useModal();
defineProps({
  sessionTimeout: {
    type: Boolean,
  },
});

/**
 * 获取验证码
 */
function handleChangeCheckCode() {
  formData.inputCode = '';

  randCodeData.checkKey = 1629428467008;
  getCodeInfo(randCodeData.checkKey).then((res) => {
    randCodeData.randCodeImage = res;
    randCodeData.requestCodeSuccess = true;
  });
}

/**
 * 账号或者手机登录
 */
async function loginHandleClick() {
  // if (unref(activeIndex) === 'accountLogin') {
  //   accountLogin();
  // } else {
  //   //手机号登录
  // }
  accountLogin();
}

async function accountLogin() {
  if (!formData.username) {
    createMessage.warn(t('sys.login.accountPlaceholder'));
    return;
  }
  if (!formData.password) {
    createMessage.warn(t('sys.login.passwordPlaceholder'));
    return;
  }
  try {
    loginLoading.value = true;
    const { userInfo } = await userStore.login(
      toRaw({
        password: formData.password,
        username: formData.username,
        captcha: formData.inputCode,
        checkKey: randCodeData.checkKey,
        mode: 'none', //不要默认的错误提示
      })
    );
    if (userInfo) {
      notification.success({
        message: t('sys.login.loginSuccessTitle'),
        description: `${t('sys.login.loginSuccessDesc')}: ${userInfo.realname}`,
        duration: 3,
      });
    }
  } catch (error) {
    notification.error({
      message: t('sys.api.errorTip'),
      description: error.message || t('sys.login.networkExceptionMsg'),
      duration: 3,
    });
    handleChangeCheckCode();
  } finally {
    loginLoading.value = false;
  }
}

onMounted(() => {
  //加载验证码
  handleChangeCheckCode();
});
</script>

<style lang="less" scoped>
  @import '/@/assets/loginmini/style/home.less';
  @import '/@/assets/loginmini/style/base.less';

  :deep(.ant-input:focus) {
    box-shadow: none;
  }
  .aui-get-code {
    float: right;
    position: relative;
    z-index: 3;
    background: #ffffff;
    color: #1573e9;
    border-radius: 100px;
    padding: 5px 16px;
    margin: 7px;
    border: 1px solid #1573e9;
    top: 12px;
  }

  .aui-get-code:hover {
    color: #1573e9;
  }

  .code-shape {
    border-color: #dadada !important;
    color: #aaa !important;
  }

  :deep(.jeecg-dark-switch){
    position:absolute;
    margin-right: 10px;
  }
  .aui-link-login{
    height: 42px;
    padding: 10px 15px;
    font-size: 14px;
    border-radius: 8px;
    margin-top: 35px;
    margin-bottom: 8px;
    flex: 1;
    color: #fff;
  }
  .aui-phone-logo{
    position: absolute;
    margin-left: 10px;
    width: 60px;
    top:2px;
    z-index: 4;
  }
  .top-3{
    top: 0.45rem;
  }
  .login-bg{
    width: 100%;
    z-index: -1;
  }
</style>

<style lang="less">
#app{
  background-color: rgba(251, 251, 251, 1);
}
@prefix-cls: ~'@{namespace}-mini-login';
@dark-bg: #293146;

html[data-theme='dark'] {
  .@{prefix-cls} {
    background-color: @dark-bg !important;
    background-image: none;

    &::before {
      background-image: url(/@/assets/svg/login-bg-dark.svg);
    }
    .aui-inputClear{
      background-color: #232a3b !important;
    }
    .ant-input,
    .ant-input-password {
      background-color: #232a3b !important;
    }

    .ant-btn:not(.ant-btn-link):not(.ant-btn-primary) {
      border: 1px solid #4a5569 !important;
    }

    &-form {
      background: @dark-bg !important;
    }

    .app-iconify {
      color: #fff !important;
    }
    .aui-inputClear input,.aui-input-line input,.aui-choice{
      color: #c9d1d9 !important;
    }

    .aui-formBox{
      background-color: @dark-bg !important;
    }
    .aui-third-text span{
      background-color: @dark-bg !important;
    }
    .aui-form-nav .aui-flex-box{
      color: #c9d1d9 !important;
    }

    .aui-formButton .aui-linek-code{
      background:  @dark-bg !important;
      color: white !important;
    }
    .aui-code-line{
      border-left: none !important;
    }
    .ant-checkbox-inner,.aui-success h3{
      border-color: #c9d1d9;
    }
    //update-begin---author:wangshuai ---date:20230828  for：【QQYUN-6363】这个样式代码有问题，不在里面，导致表达式有问题------------
    &-sign-in-way {
      .anticon {
        font-size: 22px !important;
        color: #888 !important;
        cursor: pointer !important;

        &:hover {
          color: @primary-color !important;
        }
      }
    }
    //update-end---author:wangshuai ---date:20230828  for：【QQYUN-6363】这个样式代码有问题，不在里面，导致表达式有问题------------
  }
  
  .ant-divider-inner-text {
    font-size: 12px !important;
    color: @text-color-secondary !important;
  }
  .aui-third-login a{
    background: transparent;
  }
}
.auto-fill {
  width: 100%;
  height: 36px;
  opacity: 1;
  border-radius: 8px;
  background: rgba(227, 236, 250, 1);
  border: 1px solid rgba(216, 224, 240, 1);
  box-shadow: 0px 1px 2px  rgba(184, 200, 224, 0.22);

}
</style>
