<template>
  <div :class="prefixCls" class="login-background-img">
    <div v-show="type === 'login'">
      <div class="aui-content">
        <div class="aui-container">
          <div class="login-title">
            <img src="@/assets/loginmini/icon/title_logo.png" alt="" />
            <span>广东省建设用地污染地块信息系统</span>
          </div>
          <div class="aui-form">
            <div class="aui-formBox">
              <div class="aui-formWell">
                <a-tabs v-model:activeKey="activeKey" @change="changeTab">
                  <a-tab-pane key="1" tab="建设用地账号登录">
                    <!-- <div class="aui-flex aui-form-nav investment_title">
                      <div class="aui-flex-box">{{ t('sys.login.signInFormTitle') }} </div>
                    </div> -->
                    <div class="aui-form-box" style="height: 260px">
                      <a-form ref="loginRef" :model="formData" v-if="activeIndex === 'accountLogin'" @keyup.enter.native="loginHandleClick">
                        <div class="aui-account">
                          <div class="aui-inputClear">
                            <i class="icon icon-code"></i>
                            <a-form-item>
                              <a-input class="fix-auto-fill" :placeholder="t('sys.login.userName')" v-model:value="formData.username" />
                            </a-form-item>
                          </div>
                          <div class="aui-inputClear">
                            <i class="icon icon-password"></i>
                            <a-form-item>
                              <a-input-password class="fix-auto-fill" :placeholder="t('sys.login.password')" v-model:value="formData.password" />
                            </a-form-item>
                          </div>
                          <div class="aui-inputClear">
                            <a-form-item>
                              <a-input class="fix-auto-fill" type="text" :placeholder="t('sys.login.inputCode')" v-model:value="formData.inputCode" />
                            </a-form-item>
                            <div class="aui-code">
                              <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>
                      </a-form>
                    </div>
                  </a-tab-pane>
                  <a-tab-pane key="2" tab="智慧云平台登录" v-if="isgld">
                    <!-- <div class="aui-flex aui-form-nav investment_title">
                      <div class="aui-flex-box">{{ t('sys.login.signInFormTitle') }} </div>
                    </div> -->
                    <div class="aui-form-box" style="height: 260px" v-if="false">
                      <a-form ref="loginRef" :model="formData" v-if="activeIndex === 'accountLogin'" @keyup.enter.native="loginHandleClick">
                        <div class="aui-account">
                          <div class="aui-inputClear">
                            <i class="icon icon-code"></i>
                            <a-form-item>
                              <a-input class="fix-auto-fill" :placeholder="t('sys.login.userName')" v-model:value="formData.username" />
                            </a-form-item>
                          </div>
                          <div class="aui-inputClear">
                            <i class="icon icon-password"></i>
                            <a-form-item>
                              <a-input-password class="fix-auto-fill" :placeholder="t('sys.login.password')" v-model:value="formData.password" />
                            </a-form-item>
                          </div>
                          <div class="aui-inputClear">
                            <a-form-item>
                              <a-input class="fix-auto-fill" type="text" :placeholder="t('sys.login.inputCode')" v-model:value="formData.inputCode" />
                            </a-form-item>
                            <div class="aui-code">
                              <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>
                      </a-form>
                    </div>
                  </a-tab-pane>
                </a-tabs>

                <!-- 登录按钮 -->
                <div class="aui-formButton">
                  <div class="aui-flex">
                    <a-button :loading="loginLoading" class="aui-link-login aui-flex-box" type="primary" @click="loginHandleClick">
                      {{ t('sys.login.loginButton') }}</a-button
                    >
                  </div>
                </div>
                <div style="width: 100%; text-align: center; padding-bottom: 30px"><a style="color: #23527c; font-size: 12px">用户手册</a></div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script lang="ts" setup name="login-mini">
import { getCodeInfo } from '/@/api/sys/user';
import { 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 { useDesign } from '/@/hooks/web/useDesign';
import { RsaParams } from '/@/utils/rsa.js';
import { router } from '/@/router';

const { prefixCls } = useDesign('mini-login');
const { notification, createMessage } = useMessage();
const userStore = useUserStore();
const { t } = useI18n();
const randCodeData = reactive<any>({
  randCodeImage: '',
  requestCodeSuccess: false,
  checkKey: null,
});
//手机号登录还是账号登录
const activeIndex = ref<string>('accountLogin');
const type = ref<string>('login');
//账号登录表单字段
const formData = reactive<any>({
  inputCode: '',
  username: '',
  password: '',
  url: '',
});
//手机登录表单字段
const activeKey = ref('1');

const loginRef = ref();
const isgld = ref(false);

const loginLoading = ref<boolean>(false);

defineProps({
  sessionTimeout: {
    type: Boolean,
  },
});

function changeTab(e) {
  console.log(e,'防守打法')
  if (e == 2) {
    router.push({path:'/tokenLogin'})
    return
  }
  formData.value = {
    inputCode: '',
    username: '',
    password: '',
    url: '',
  };
}

/**
 * 获取验证码
 */
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();
  }
}

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;

    if (activeKey.value == '1') {
      const { userInfo }: any = await userStore.login(
        toRaw({
          password: RsaParams(formData.password),
          username: RsaParams(formData.username),
          url: RsaParams(formData.url),
          captcha: formData.inputCode,
          checkKey: randCodeData.checkKey,
          mode: 'none', //不要默认的错误提示
        })
      );
      if (userInfo) {
        userStore.setUserInfo(userInfo);
        notification.success({
          message: t('sys.login.loginSuccessTitle'),
          description: `${t('sys.login.loginSuccessDesc')}: ${userInfo.realname}`,
          duration: 3,
        });
      }
    } else {
      const { userInfo }: any = await userStore.yzylogin(
        toRaw({
          password: RsaParams(formData.password),
          username: RsaParams(formData.username),
          url: RsaParams(formData.url),
          captcha: formData.inputCode,
          checkKey: randCodeData.checkKey,
          mode: 'none', //不要默认的错误提示
        })
      );
      if (userInfo) {
        userStore.setUserInfo(userInfo);
        notification.success({
          message: t('sys.login.loginSuccessTitle'),
          description: `${t('sys.login.loginSuccessDesc')}: ${userInfo.realname}`,
          duration: 3,
        });
      }
    }
  } catch (error: any) {
    notification.error({
      message: t('sys.api.errorTip'),
      description: error?.message || t('sys.login.networkExceptionMsg'),
      duration: 3,
    });
    handleChangeCheckCode();
  } finally {
    loginLoading.value = false;
  }
}

onMounted(() => {
  //加载验证码
  handleChangeCheckCode();
  formData.url = window.location.href;
  if (window.location.href.indexOf("jsydgld") > -1) {
    isgld.value = true
  }
});
</script>

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

:deep(.ant-input:focus) {
  box-shadow: none;
}
.ant-input-affix-wrapper-focused{
  border: none!important;
}
.ant-input-affix-wrapper{
  border: none!important;
  box-shadow: none!important;
}
.aui-inputClear {
      background-color: #ffffff !important;
    }

.aui-get-code {
  float: right;
  position: relative;
  z-index: 3;
  background: @white;
  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: 55px;
  padding: 10px 1px;
  font-size: 14px;
  border-radius: 25px;
  margin-top: 0px;
  margin-bottom: 8px;
  font-size: 20px;
  font-weight: bold;
}

.aui-phone-logo {
  position: absolute;
  margin-left: 10px;
  width: 60px;
  top: 2px;
  z-index: 4;
}

.top-3 {
  top: 0.45rem;
}

.login-title {
  height: 77px;
  display: flex;
  justify-content: center;

  img {
    width: 75px;
    height: 75px;
    margin-right: 20px;
  }

  span {
    color: #fff;
    font-size: 45px;
  }

  margin-bottom: 50px;
}
</style>

<style lang="less">
@prefix-cls: ~'@{namespace}-mini-login';
@dark-bg: #293146;

.ant-tabs-tab {
  font-size: 22px;
  font-weight: bold;
  flex: 1;
  justify-content: center;
}
.ant-tabs-nav-wrap,
.ant-tabs-nav-list {
  width: 100%;
}

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;
    }
  }

  input.fix-auto-fill,
  .fix-auto-fill input {
    -webkit-text-fill-color: #c9d1d9 !important;
    box-shadow: inherit !important;
  }

  &-sign-in-way {
    .anticon {
      font-size: 22px !important;
      color: #888 !important;
      cursor: pointer !important;

      &:hover {
        color: @primary-color !important;
      }
    }
  }

  .ant-divider-inner-text {
    font-size: 12px !important;
    color: @text-color-secondary !important;
  }

  .aui-third-login a {
    background: transparent;
  }
}
.aui-formButton {
  .ant-btn-primary {
    border-color: #3183ff;
    background: #3183ff;
  }
}
</style>
