<template>
  <div class="login-flex">
    <div class="login">
      <div class="logo"><img src="img/logo_blue.png" /></div>
      <form @submit="loginAction">
        <div>
          <a-select
            style="width: 100%"
            v-model:value="server"
            :options="servers"
            placeholder="请选择服务地址"
          >
          </a-select>
        </div>
        <div>
          <a-input
            v-model:value="loginName"
            id="loginName-input"
            type="text"
            placeholder="请输入账号"
          />
        </div>
        <div>
          <a-input
            v-model:value="password"
            id="password-input"
            type="password"
            placeholder="请输入密码"
          />
        </div>
        <div><a href="##" id="login-button" @click="loginAction">登录</a></div>
      </form>
    </div>
    <div class="ft">
      Copyright © 2011-2021 www.qmth.com.cn, All Rights Reserved
    </div>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref } from "vue";
import { store } from "@/store";
import { login } from "@/api/api";
import router from "@/router";

Object.assign(
  window.electron.env,
  JSON.parse(window.localStorage.getItem("env") || "{}")
);

let server = ref(null);
const servers = store.config.servers.map((v) => {
  return { label: v.name, value: v.host };
});

let loginName = ref("");
let password = ref("");

onMounted(() => {
  window.localStorage.clear();
});

const loginAction = () => {
  // window.electron.env.server = server.value;

  // window.electron.env.loginName = loginName.value;
  // window.electron.env.password = password.value;
  Object.assign(store.env, {
    server: server.value,
    loginName: loginName.value,
    password: password.value,
  });
  // if (window.electron.env.server == undefined) {
  //   alert("请选择服务地址");
  //   return;
  // }
  // if (window.electron.env.loginName == "") {
  //   alert("请输入账号");
  //   return;
  // }
  // if (window.electron.env.password == "") {
  //   alert("请输入密码");
  //   return;
  // }
  login()
    .then((user) => {
      console.log(user);
      Object.assign(store.env.user, user);
      window.localStorage.setItem("env", JSON.stringify(store.env));
      // window.location.href = 'exam-list.html'
      router.push("/home");
    })
    .catch((err) => {
      alert("登陆失败，用户名或密码错误");
    });
};
</script>
