<template>
  <div class="admin-login">
    <div class="title">管理员登录</div>
    <div class="tip">输入账号和密码</div>
    <qm-low-form
      :fields="fields"
      :params="params"
      :labelWidth="20"
      :rules="rules"
      ref="loginForm"
      :search="loginHandle"
    ></qm-low-form>
    <div class="text-center">
      <qm-button type="link" @click="emit('toIndex', 2)"
        >返回选择登录身份</qm-button
      >
    </div>
    <qm-button
      :icon="h(GlobalOutlined)"
      class="reset-btn"
      @click="emit('toIndex', 0)"
    >
      连接设置</qm-button
    >
  </div>
</template>
<script name="AdminLogin" lang="ts" setup>
import { reactive, ref, h } from "vue";
import { useRouter } from "vue-router";
import { GlobalOutlined } from "@ant-design/icons-vue";
import { adminLogin } from "@/ap/system";
import { useUserStore } from "@/store";

const userStore = useUserStore();

const loginForm = ref();
const emit = defineEmits(["toIndex"]);
const params = reactive({ loginName: "", password: "" });
const router = useRouter();
const rules = {
  loginName: [{ required: true, message: "请输入用户名", trigger: "change" }],
  password: [{ required: true, message: "请输入密码", trigger: "change" }],
};
const loginHandle = () => {
  loginForm.value.formRef
    .validate()
    .then(() => {
      adminLogin(params).then((res: any) => {
        userStore.setUserInfo(res);
        let routeName =
          res.role === "SCHOOL_ADMIN"
            ? "CurExam"
            : res.role === "AUDITOR"
            ? "Audit"
            : "";
        if (routeName) {
          router.push({ name: routeName });
          window.electronApi.changeWinSize("big");
        }
      });
    })
    .catch((error: any) => {
      console.log("验证不通过", error);
    });
};
const fields = ref([
  {
    prop: "loginName",
    colSpan: 24,
    attrs: {
      placeholder: "输入你的账号",
      allowClear: true,
      size: "large",
    },
  },
  {
    prop: "password",
    type: "password",
    colSpan: 24,
    attrs: {
      placeholder: "输入你的密码",
      size: "large",
    },
  },
  {
    type: "button",
    colSpan: 24,
    text: "登录",
    attrs: {
      block: true,
      size: "large",
      onClick: () => {
        loginHandle();
      },
    },
  },
]);
</script>
<style lang="less" scoped>
.admin-login {
  padding: 88px 80px 0 80px;
  position: relative;
  :deep(.reset-btn) {
    position: absolute;
    top: 25px;
    right: 65px;
    z-index: 1;
  }
  .title {
    color: @text-color1;
    font-weight: bold;
    line-height: 32px;
    font-size: 24px;
  }
  .tip {
    color: @text-color3;
    line-height: 26px;
    font-size: 18px;
    margin-top: 4px;
    margin-bottom: 28px;
  }
}
</style>
