<template>
  <div class="view-account">
    <div class="view-account-header"></div>
    <div class="view-account-container">
      <div class="view-account-top">
        <div class="view-account-top-logo">
          <img :src="websiteConfig.loginImage" alt="" />
        </div>
        <div class="view-account-top-desc">{{ websiteConfig.loginDesc }}</div>
      </div>
      <div class="view-account-form">
        <n-form
          ref="formRef"
          label-placement="left"
          size="large"
          :model="formInline"
          :rules="rules"
        >
          <n-form-item path="username">
            <n-input
              v-model:value="formInline.username"
              placeholder="请输入用户名"
            >
              <template #prefix>
                <n-icon size="18" color="#808695">
                  <PersonOutline />
                </n-icon>
              </template>
            </n-input>
          </n-form-item>
          <n-form-item path="password">
            <n-input
              v-model:value="formInline.password"
              type="password"
              showPasswordOn="click"
              placeholder="请输入密码"
            >
              <template #prefix>
                <n-icon size="18" color="#808695">
                  <LockClosedOutline />
                </n-icon>
              </template>
            </n-input>
          </n-form-item>

          <n-form-item>
            <n-button
              type="info"
              @click="handleSubmit"
              size="large"
              :loading="loading"
              block
            >
              登录
            </n-button>
          </n-form-item>
        </n-form>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import { PersonOutline, LockClosedOutline } from "@vicons/ionicons5";
import logoImage from "@/assets/images/logo.png";
import { routerTurnByName, cryptoEncode, setLocalStorage } from "@/utils";
import { StorageEnum } from "@/enums/storageEnum";
const { ZS_LOGIN_INFO_STORE } = StorageEnum;
interface FormState {
  username: string;
  password: string;
}

const formRef = ref();
const loading = ref(false);

const formInline = reactive({
  username: "admin",
  password: "zsqy123",
  isCaptcha: true,
});

const rules = {
  username: { required: true, message: "请输入用户名", trigger: "blur" },
  password: { required: true, message: "请输入密码", trigger: "blur" },
};

const websiteConfig = {
  title: "中盛起元基础框架",
  logo: logoImage,
  loginImage: logoImage,
  loginDesc: "中盛起元基础框架",
};

const handleSubmit = (e: any) => {
  e.preventDefault();
  formRef.value.validate(async (errors: any) => {
    if (!errors) {
      const { username, password } = formInline;
      window["$message"].loading("登录中...");
      loading.value = true;

      const params: FormState = {
        username,
        password,
      };

      if (params.username === "admin" && params.password === "zsqy123") {
        window["$message"].success("登录成功，即将进入系统");
        setLocalStorage(
          ZS_LOGIN_INFO_STORE,
          cryptoEncode(
            JSON.stringify({
              username,
              password,
            })
          )
        );
        setTimeout(() => {
          routerTurnByName("Root", true);
        }, 3000);
      } else {
        loading.value = false;
        window["$message"].error("用户名或密码错误");
      }
    } else {
      loading.value = false;
      window["$message"].error("用户名或密码错误");
    }
  });
};
</script>

<style lang="scss" scoped>
.view-account {
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: auto;

  &-container {
    flex: 1;
    min-width: 320px;
    max-width: 384px;
    padding: 32px 12px;
    margin: 0 auto;
  }

  &-top {
    padding: 32px 0;
    text-align: center;

    &-desc {
      font-size: 14px;
      color: #808695;
    }
  }

  &-other {
    width: 100%;
  }

  .default-color {
    color: #515a6e;

    .ant-checkbox-wrapper {
      color: #515a6e;
    }
  }
}

@media (width >= 768px) {
  .view-account {
    background-image: url("../../assets/images/login.svg");
    background-repeat: no-repeat;
    background-position: 50%;
    background-size: 100%;
  }

  .page-account-container {
    padding: 32px 0 24px;
  }
}
</style>
