<template>
  <div class="login-home login">
    <div class="login login-box">
      <div class="login-title">
        <h1>美术阅卷系统登录</h1>
      </div>
      <div class="login-form">
        <Form ref="loginForm" :model="loginModel" :rules="loginRules">
          <FormItem prop="loginName">
            <Input
              v-model.trim="loginModel.loginName"
              prefix="md-person"
              placeholder="请输入用户名"
              clearable
            ></Input>
          </FormItem>
          <FormItem prop="password">
            <Input
              type="password"
              v-model.trim="loginModel.password"
              prefix="md-lock"
              placeholder="请输入密码"
              clearable
            ></Input>
          </FormItem>
          <FormItem>
            <Button
              long
              type="primary"
              :disabled="isSubmit"
              @click="submit('loginForm')"
              >登录</Button
            >
            <div class="login-link">
              <!-- <router-link :to="{ name: 'ForgetPwd' }">忘记密码</router-link> -->
            </div>
          </FormItem>
        </Form>
      </div>
    </div>

    <reset-pwd @confirm="resetSuccess" ref="ResetPwd"></reset-pwd>
  </div>
</template>

<script>
import { username, password } from "@/plugins/formRules";
import { login, subjectDetail } from "@/api";
import ResetPwd from "./ResetPwd";

export default {
  name: "login",
  components: { ResetPwd },
  data() {
    return {
      loginModel: {
        loginName: "admin-cy",
        password: "123456"
      },
      loginRules: {
        loginName: username,
        password
      },
      rightRoutes: {
        ADMIN: {
          router: "WorkManage"
        },
        MARKER_LEVEL: {
          router: "GradingOperation"
        },
        MARKER_SCORE: {
          router: "MarkOperation"
        },
        MARK_LEADER_LEVEL: {
          router: "Grading"
        },
        MARK_LEADER_SCORE: {
          router: "Mark"
        },
        INSPECTOR: {
          router: "Inspection"
        }
      },
      isSubmit: false
    };
  },
  mounted() {
    this.$ls.clear();
  },
  methods: {
    async submit(name) {
      const valid = await this.$refs[name].validate();
      if (!valid) return;

      if (this.isSubmit) return;
      this.isSubmit = true;
      const data = await login(this.loginModel).catch(() => {});
      this.isSubmit = false;
      if (!data) return;

      this.$ls.set("user", data, this.GLOBAL.authTimeout);
      this.$store.commit("setUser", data);
      // 初次登陆强制修改密码
      // if (
      //   data.role !== "ADMIN" &&
      //   data.role != "INSPECTION" &&
      //   !data.pwChangedCount
      // ) {
      //   this.$refs.ResetPwd.open();
      //   return;
      // }

      const params = {
        workId: data.workId,
        subjectId: `${data.workId}-${data.subject}`
      };
      const curSubject = await subjectDetail(params.subjectId);
      const stage = curSubject.stage;

      // 根据角色跳转不同的路由
      const { role, markRight } = data;
      if (role === "MARKER") {
        if (stage === "INIT") {
          this.$Message.error("当前工作未开启分档！");
          return;
        }
        if (
          (stage === "LEVEL" && markRight === "ALLOW_SCORING") ||
          (stage === "SCORE" && markRight === "ALLOW_LEVELING")
        ) {
          this.$Message.error("您的权限不够！");
          return;
        }
      }

      const type = role.includes("MARK") ? `${role}_${stage}` : role;
      const routerName = this.rightRoutes[type].router;
      this.$router.push({
        name: routerName,
        params
      });
    },
    resetSuccess(newpassword) {
      this.loginModel.password = newpassword;
    }
  }
};
</script>
