<template>
  <div class="login login-main" @keydown.enter="handleSubmit">
    <div class="login-box">
      <div class="left-box">
        <h1>机考数据中心</h1>
        <div class="form-list">
          <Form ref="loginForm" :model="form" :rules="rules">
            <FormItem prop="userName">
              <Input v-model="form.userName" placeholder="请输入用户名">
              </Input>
              <label
                class="label-name"
                :class="{ 'label-fill': form.userName }"
                for="userName"
              ></label>
            </FormItem>

            <FormItem label="" prop="password">
              <Input
                @keyup.13="loginnow($event)"
                type="password"
                v-model="form.password"
                placeholder="请输入密码"
              >
              </Input>
              <label
                class="label-pswd"
                :class="{ 'label-fill': form.password }"
                for="password"
              ></label>
            </FormItem>
          </Form>
        </div>
        <div class="login-submit">
          <div class="editor-btn" @click="handleSubmit">登录</div>
        </div>
        <p class="login-tips">
          本产品仅支持谷歌浏览器，若您尚未安装，请<a
            href="http://cdn.qmth.com.cn/soft/ChromeStandalone.exe"
            >点击下载</a
          >
        </p>
      </div>
    </div>
  </div>
</template>

<script>
import { axios } from "../libs/http";

export default {
  data() {
    return {
      form: {
        userName: "",

        password: "",
      },

      rules: {
        userName: [
          {
            required: true,

            message: "账号不能为空",

            trigger: "blur",
          },
        ],

        password: [
          {
            required: true,

            message: "密码不能为空",

            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    localStorage.clear();
  },
  methods: {
    loginnow(evt) {
      if (evt.keyCode == 13) {
        this.handleSubmit();
      }
    },

    handleSubmit() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          axios.post("/system/user/login", this.form).then((response) => {
            let result = response.data;

            let user = result.data.user;

            let accessToken = result.data.accessToken;

            let schoolId = user.schoolId;

            let school = user.basicSchool;

            this.$store.commit("clearAllTags");

            this.$store.commit("resolveName", user.userName);

            this.$store.commit("resolveAccessToken", accessToken);

            this.$store.commit("resolveRole", user.role);

            this.$store.commit("resolveEncryptToken");

            this.$store.commit("addOpenSubmenu", "examplan");

            this.$store.commit("resolveSchoolId", schoolId);

            this.$store.commit("resolveSchool", school);

            let resolveToken = this.$store.state.user.accessToken;

            if (!resolveToken || resolveToken.length == 0) {
              this.$Notice.error({
                title: "系统消息",

                desc: "<p>获取登录凭据失败,请重新登录</p>",
              });

              return;
            }

            this.$router.push({
              name: "home",
            });
          });
        }
      });
    },
  },
};
</script>
