<template>
  <div class="login flex justify-center items-center h-full">
    <img class="logo" src="../../assets/imgs/login_logo.png" />
    <div class="login-bg">
      <div class="login-bg-inner-box text-center">
        <div class="title1">欢 迎 使 用</div>
        <div class="title2">项目质量控制管理平台</div>
      </div>
    </div>
    <div class="login-box">
      <div class="title1 flex justify-between items-center">
        <span>{{ forgetStatus ? '忘记密码' : '输入信息' }}</span>
        <t-button
          v-if="forgetStatus"
          variant="outline"
          @click="forgetStatus = false"
        >
          <template #icon><RollbackIcon /></template>
          返回登录
        </t-button>
        <!-- <t-button
            v-else
            variant="outline"
            @click="loginType = loginType === 'ACCOUNT' ? 'PHONE' : 'ACCOUNT'"
          >
            <template #icon><SwapRightIcon /></template>
            {{ loginType === 'ACCOUNT' ? '手机号' : '账号' }}登录
          </t-button> -->
      </div>
      <div class="title2" v-if="forgetStatus">{{ `请输入新的账号与密码` }}</div>
      <div class="title2" v-else>{{
        `请输入${loginType === 'ACCOUNT' ? '账号与密码' : '手机号与验证码'}`
      }}</div>
      <template v-if="!forgetStatus">
        <t-form
          ref="form"
          :data="formData"
          :label-width="0"
          class="login-form"
          :rules="rules"
          v-if="loginType === 'ACCOUNT'"
        >
          <t-form-item name="loginName">
            <t-input
              v-model="formData.loginName"
              clearable
              placeholder="请输入手机号码"
              size="medium"
              @enter="loginHandle"
            >
            </t-input>
          </t-form-item>

          <t-form-item name="password">
            <t-input
              v-model="formData.password"
              type="password"
              clearable
              placeholder="请输入密码"
              size="medium"
              @enter="loginHandle"
            >
            </t-input>
          </t-form-item>
        </t-form>

        <t-form
          v-else
          ref="form2"
          :data="formData2"
          :label-width="0"
          class="login-form"
          :rules="rules2"
        >
          <t-form-item name="mobileNumber">
            <t-input
              v-model="formData2.mobileNumber"
              clearable
              placeholder="请输入你的手机号"
              size="medium"
            >
            </t-input>
          </t-form-item>
          <t-form-item name="code">
            <div class="flex items-center">
              <t-input
                style="width: 170px"
                v-model="formData2.code"
                clearable
                placeholder="验证码"
                size="medium"
              >
              </t-input>
              <t-button
                class="m-l-20px"
                style="width: 110px"
                :loading="loading"
                :disabled="isActive"
                @click="getCode"
                >{{ isActive ? lastSeconds + 's' : '获取验证码' }}</t-button
              >
            </div>
          </t-form-item>
        </t-form>
        <t-link theme="primary" class="m-t-28px" @click="forgetStatus = true"
          >忘记密码</t-link
        >
        <t-button
          block
          class="m-t-28px"
          @click="loginHandle"
          size="medium"
          theme="primary"
          >登 录</t-button
        >
      </template>
      <ForgetPwd v-else @success="findSuccess" />
    </div>
  </div>
  <!-- <button @click="loginHandle" class="m-t-10px">登录！</button> -->
</template>

<script setup name="Login">
import { ref, reactive, onBeforeUnmount } from 'vue';
import { RollbackIcon } from 'tdesign-icons-vue-next';
import { useRoute, useRouter } from 'vue-router';
import { useUserStore, useAppStore } from '@/store';
import { MessagePlugin } from 'tdesign-vue-next';
import { getBase64 } from '@/utils/crypto';
import ForgetPwd from './forget-pwd.vue';
import { useRequest } from 'vue-request';
import { getVerifyCode } from '@/api/user';
import { useIntervalFn } from '@vueuse/core';
const { run, loading } = useRequest(getVerifyCode, {
  onSuccess: () => {
    MessagePlugin.success('验证码已发送');
    resume();
  },
});
const lastSeconds = ref(60);
const countdown = () => {
  if (lastSeconds.value == 0) {
    pause();
  } else {
    lastSeconds.value = lastSeconds.value - 1;
  }
};
const { pause, resume, isActive } = useIntervalFn(
  () => {
    countdown();
  },
  1000,
  { immediate: false }
);
onBeforeUnmount(() => {
  pause();
});
const getCode = () => {
  if (!formData2.mobileNumber) {
    MessagePlugin.error('请先输入手机号');
    return;
  }
  run({
    mobileNumber: formData2.mobileNumber,
  });
  // resume();
};
const loginType = ref('ACCOUNT');
const appStore = useAppStore();
const forgetStatus = ref(false);
const form = ref(null);
const form2 = ref(null);

const route = useRoute();
const router = useRouter();

const formData = reactive({
  loginName: '',
  password: '',
});
const formData2 = reactive({
  mobileNumber: '',
  code: '',
});
const rules = {
  loginName: [
    {
      required: true,
      message: '请输入账号',
      type: 'error',
      trigger: 'change',
    },
  ],
  password: [
    { required: true, message: '请输入密码', type: 'error', trigger: 'change' },
  ],
};
const rules2 = {
  mobileNumber: [
    {
      required: true,
      message: '请输入手机号',
      type: 'error',
      trigger: 'change',
    },
  ],
  code: [
    {
      required: true,
      message: '请输入验证码',
      type: 'error',
      trigger: 'change',
    },
  ],
};

const userStore = useUserStore();

const loginHandle = () => {
  (loginType.value === 'ACCOUNT' ? form.value : form2.value)
    .validate()
    .then(async (result) => {
      const redirect = route.query.redirect ? route.query.redirect : '';
      if (result === true) {
        let params =
          loginType.value === 'ACCOUNT'
            ? {
                loginName: formData.loginName,
                password: getBase64(formData.password),
              }
            : formData2;
        await userStore.login({
          ...params,
          type: loginType.value,
        });
        await userStore.requestUserMenu();
        await appStore.getFlowParams();
        if (redirect) {
          router.push(redirect);
        } else {
          const menu = userStore.moduleMenus.find((item) => !item.disabled);
          if (!menu) {
            MessagePlugin.error('权限配置错误！');
            return;
          }
          router.push({ name: menu.name });
        }
      }
    });
};
const findSuccess = () => {
  forgetStatus.value = false;
  formData.loginName = '';
  formData.password = '';
};
</script>
<style lang="less" scoped>
.login {
  background: linear-gradient(180deg, #f4f9ff 0%, #d7eaff 100%);
  position: relative;
  min-height: 550px;

  .logo {
    width: 180px;
    z-index: 100;
    top: 48px;
    left: 48px;
    position: absolute;
  }
  .login-bg {
    width: 488px;
    height: 586px;
    background: url(../../assets//imgs/login_bg.png);
    background-size: 100% 100%;
    position: relative;
    .login-bg-inner-box {
      position: absolute;
      bottom: 114px;
      width: 100%;
      .title1 {
        font-size: 32px;
        color: #262626;
        margin-bottom: 15px;
        font-weight: bold;
        line-height: 40px;
      }
      .title2 {
        font-size: 20px;
        color: #8c8c8c;
        line-height: 28px;
      }
    }
  }
  .login-box {
    width: 344px;
    background: #fff;
    box-shadow: 0px 2px 12px 0px rgba(0, 0, 0, 0.08);
    border-radius: 8px;
    border: 1px solid #e5e6eb;
    padding: 32px;
    position: relative;
    .title1 {
      span {
        font-size: 24px;
        font-weight: bold;
        color: #262626;
        line-height: 32px;
      }
    }
    .title2 {
      font-size: 16px;
      color: #8c8c8c;
      line-height: 24px;
      margin-top: 4px;
      margin-bottom: 28px;
      font-weight: 400;
    }
  }
  .login-form {
    .t-form__item:not(:last-of-type) {
      margin-bottom: 28px;
    }
  }
  :deep(.t-button--variant-outline:hover) {
    background-color: #e8f3ff;
  }
}
</style>
