<script setup lang="ts">
import Motion from "./utils/motion";
import { useRouter } from "vue-router";
import { message } from "@/utils/message";
import { loginRules } from "./utils/rule";
// import { useNav } from "@/layout/hooks/useNav";
import type { FormInstance } from "element-plus";
import { useLayout } from "@/layout/hooks/useLayout";
import { useUserStoreHook } from "@/store/modules/user";
import { initRouter, getTopMenu } from "@/router/utils";
import { bg, leftLogo, sst } from "./utils/static";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
// import update from "./components/update.vue";
import { ref, reactive, onMounted, onBeforeUnmount } from "vue";
// import { useDataThemeChange } from "@/layout/hooks/useDataThemeChange";

// import dayIcon from "@/assets/svg/day.svg?component";
// import darkIcon from "@/assets/svg/dark.svg?component";
import Lock from "@iconify-icons/ri/lock-fill";
import User from "@iconify-icons/ri/user-3-fill";

defineOptions({
  name: "Login"
});
const router = useRouter();
const loading = ref(false);
const ruleFormRef = ref<FormInstance>();
const checked = ref(false);
// const currentPage = computed(() => {
//   return useUserStoreHook().currentPage;
// });

const { initStorage } = useLayout();
initStorage();

// const { dataTheme, dataThemeChange } = useDataThemeChange();
// dataThemeChange();
// const { title } = useNav();

const ruleForm = reactive({
  username: "admin",
  password: "admin123"
});

const onLogin = async (formEl: FormInstance | undefined) => {
  loading.value = true;
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      useUserStoreHook()
        .loginByUsername({ username: ruleForm.username, password: "admin123" })
        .then(res => {
          if (res.success) {
            // 获取后端路由
            initRouter().then(() => {
              router.push(getTopMenu(true).path);
              message("登录成功", { type: "success" });
            });
          }
        });
    } else {
      loading.value = false;
      return fields;
    }
  });
};

/** 使用公共函数，避免`removeEventListener`失效 */
function onkeypress({ code }: KeyboardEvent) {
  if (code === "Enter") {
    onLogin(ruleFormRef.value);
  }
}

onMounted(() => {
  window.document.addEventListener("keypress", onkeypress);
});

onBeforeUnmount(() => {
  window.document.removeEventListener("keypress", onkeypress);
});
</script>

<template>
  <div class="select-none">
    <img :src="bg" class="wave" />
    <img :src="sst" class="logo-small" />
    <!-- <div class="flex-c absolute right-5 top-3"> -->
    <!-- 主题 -->
    <!-- <el-switch
        v-model="dataTheme"
        inline-prompt
        :active-icon="dayIcon"
        :inactive-icon="darkIcon"
        @change="dataThemeChange"
      /> -->
    <!-- </div> -->
    <div class="login-container">
      <div class="img">
        <!-- <div class="platform-logo">
          <img :src="sst" />
        </div> -->
        <p class="describe">智能制造公共服务平台</p>
        <img :src="leftLogo" />
        <!-- <component :is="toRaw(illustration)" /> -->
      </div>
      <div class="login-box">
        <div class="login-form">
          <!-- <avatar class="avatar" /> -->
          <Motion>
            <h2 class="outline-none">{{ "欢迎登录" }}</h2>
          </Motion>

          <el-form
            ref="ruleFormRef"
            :model="ruleForm"
            :rules="loginRules"
            size="large"
          >
            <Motion :delay="100">
              <el-form-item
                :rules="[
                  {
                    required: true,
                    message: '请输入账号',
                    trigger: 'blur'
                  }
                ]"
                prop="username"
              >
                <el-input
                  clearable
                  v-model="ruleForm.username"
                  placeholder="账号"
                  :prefix-icon="useRenderIcon(User)"
                />
              </el-form-item>
            </Motion>

            <Motion :delay="150">
              <el-form-item prop="password">
                <el-input
                  clearable
                  show-password
                  v-model="ruleForm.password"
                  placeholder="密码"
                  :prefix-icon="useRenderIcon(Lock)"
                />
              </el-form-item>
            </Motion>
            <Motion :delay="250">
              <div class="w-full h-[20px] flex justify-between items-center">
                <el-checkbox v-model="checked">
                  {{ "记住密码" }}
                </el-checkbox>
                <el-button
                  link
                  class="btn-color"
                  type="primary"
                  @click="useUserStoreHook().SET_CURRENTPAGE(4)"
                >
                  {{ "忘记密码？" }}
                </el-button>
              </div>
              <el-button
                class="w-full mt-7 login-btn"
                size="default"
                type="primary"
                color="#1C0D82"
                :loading="loading"
                @click="onLogin(ruleFormRef)"
              >
                登录
              </el-button>
              <div class="w-full h-[60px] flex justify-center items-center">
                <el-button link type="primary" text class="btn-color">
                  {{ "企业用户注册" }}
                </el-button>
              </div>
            </Motion>
          </el-form>
          <!-- 忘记密码 -->
          <!-- <update v-if="currentPage === 4" /> -->
        </div>
        <div class="login-form-footer">
          {{ "Copyright2021-2022 xxxxxx All Rights Reserved" }}
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
@import url("@/style/login.css");
</style>

<style lang="scss" scoped>
:deep(.el-input-group__append, .el-input-group__prepend) {
  padding: 0;
}

.login-btn {
  font-weight: 600;
  // font-family: "PingFang SC";
  color: #fff;
}

.btn-color {
  color: #1c0d82;
}

.btn-color:hover {
  color: #1c0d82;
  opacity: 0.5;
}

// :deep(.el-button) {
//   font-weight: bold;
//   color: #fff;
// }
</style>
