<template>
  <div class="login-form-wrapper">
    <div class="item">
      <div class="login-form-title">FlexiStream</div>
      <div class="login-form-sub-title">Login to FlexiStream</div>

      <a-form ref="loginForm" :model="formData" class="login-form" @submit-success="handleSubmit">
        <a-form-item field="username" :rules="[{ required: true, message: '用户名不能为空' }]"
          :validate-trigger="['change', 'blur']" hide-label>
          <a-input v-model="formData.username" :placeholder="$t('login.userName')">
            <template #prefix>
              <icon-user />
            </template>
          </a-input>
        </a-form-item>
        <a-form-item field="password" :rules="[{ required: true, message: '密码不能为空' }]"
          :validate-trigger="['change', 'blur']" hide-label>
          <a-input-password v-model="formData.password" :placeholder="$t('login.password')" allow-clear>
            <template #prefix>
              <icon-lock />
            </template>
          </a-input-password>
        </a-form-item>


        <a-space :size="16" direction="vertical">
          <div class="login-form-password-actions">
            <a-checkbox v-model="rememberPassword">
              {{ $t('login.RememberAccountNumber') }}
            </a-checkbox>
            <!-- <a-link>忘记密码</a-link> -->
          </div>
          <a-button type="primary" html-type="submit" long :loading="formLoading">{{ $t('login.disembark') }}</a-button>
        </a-space>
      </a-form>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, toRefs, onMounted } from 'vue'
import { encryptByDES } from "@/utils"
import { loginApi } from "@/api/path/login.api"
import { useSystemStore } from "@/store/modules/systemStore"
import { useRouter } from 'vue-router'
import { updateRouteByMenu } from '@/router/router.update'
import { FormLoginUser } from '@/store/modules/Login'
import { useI18n } from 'vue-i18n'
const { t } = useI18n();
const router = useRouter()
const systemStore = useSystemStore()
const LoginSet = FormLoginUser()
const state = ref({
  formData: {
    username: '',
    password: ''
  },
  formLoading: false,
  rememberPassword: false,
})

const { formData, formLoading, rememberPassword } = toRefs(state.value)

const handleSubmit = async () => {
  const { data } = await loginApi({
    username: formData.value.username,
    password: encryptByDES(formData.value.password)
  })

  if (rememberPassword.value) {
    LoginSet.loginYester(formData.value)
  } else {
    const nste = localStorage.getItem("USER_KEY_ACCOUT") || false;
    if (nste) {
      localStorage.removeItem('USER_KEY_ACCOUT')
    }
  }

  systemStore.setStateValue({
    key: 'token',
    value: data.token,
    localStorage: true,
  })
  systemStore.setStateValue({
    key: 'user_login_information',
    value: JSON.stringify(data.userInfo ? data.userInfo : data),
    localStorage: true,
  })
  systemStore.setStateValue({
    key: 'role',
    value: data.userType,
    localStorage: true,
  })
  await updateRouteByMenu(router, systemStore)


  router.push({
    path: "/",
  })
}

onMounted(() => {
  if (LoginSet.loadCredentials()) {
    formData.value.username = LoginSet.loadCredentials()?.username
    formData.value.password = LoginSet.loadCredentials()?.password
  }
})


</script>

<style lang="less" scoped>
.login-form-wrapper {
  position: absolute;
  top: 50%;
  left: 80%;
  transform: translate(-50%, -50%);
  background-color: #fff;
  width: 550px;
  height: 500px;
  border-radius: 16px;
}

.item {
  width: 80%;
}

.login-form {
  margin-top: 32px;

  &-wrapper {
    display: flex;
    justify-content: center;
    align-items: center;
  }

  &-title {
    color: @text_color_1;
    font-weight: 500;
    font-size: 24px;
    line-height: 32px;
  }

  &-sub-title {
    color: @text_color_2;
    font-size: 16px;
    line-height: 24px;
  }

  &-password-actions {
    display: flex;
    justify-content: space-between;
    margin-bottom: 15px;
  }

  &-register-btn {
    color: @text_color_3 !important;
  }
}
</style>