<template>
  <div class="login-container">
    <div class="login-form-container animated zoomIn">
      <el-form
        ref="login-form"
        :model="loginForm"
        label-width="0"
        :rules="rules"
      >
        <el-form-item label>
          <span class="login-title">iCubes</span>
        </el-form-item>
        <el-form-item label prop="username">
          <el-input
            v-model="loginForm.username"
            type="text"
            placeholder="用户名："
          >
            <em slot="prefix" class="el-icon-user"></em>
          </el-input>
        </el-form-item>
        <el-form-item label prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="密码："
            show-password
            @keyup.enter.native="validationLogin"
          >
            <em slot="prefix" class="el-icon-lock"></em>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :loading="loading" @click="validationLogin"
            >{{ loading ? "登录中..." : "登录" }}
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
// import API from "@/axios/api";
import { mapMutations } from "vuex";
import Cookies from "js-cookie";
import { getCookieDomain } from "@/util/util.js";

export default {
  data() {
    return {
      loginForm: {
        username: "",
        password: "",
        grant_type: "password"
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" }
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }]
      },
      loading: false
    };
  },
  methods: {
    ...mapMutations(["setUserInfo"]),
    validationLogin() {
      this.loading = true;
      this.$refs["login-form"].validate(valid => {
        if (!valid) {
          this.loading = false;
        } else {
          this.login();
        }
      });
    },
    async login() {
      // try {
      //   const result = await API.login(this.loginForm);
      //   if (result.access_token) {
      //     this.writeTokenToCookie(result);
      //     this.setUserInfo(result);
      this.$router.push({ path: "/task-monitor" });
      //   } else {
      //     this.$notify({
      //       title: result.message,
      //       type: "warning"
      //     });
      //   }
      //   this.loading = false;
      // } catch (error) {
      //   this.loading = false;
      // }
    },
    // 写验证信息到cookie和vuex
    writeTokenToCookie(result) {
      const token = {
        dataType: "string",
        content: result.access_token
      };
      const userInfo = {
        dataType: "object",
        content: {
          username: result.username,
          userId: result.user_id,
          displayName: result.displayName
        }
      };
      Cookies.set("OO-access_token", JSON.stringify(token), {
        path: "/",
        domain: getCookieDomain()
      });
      Cookies.set("OO-refresh_userInfo", JSON.stringify(userInfo), {
        path: "/",
        domain: getCookieDomain()
      });
    }
  }
};
</script>
<style lang="scss" scoped>
.login-container {
  height: 100%;
  background-image: url("../../assets/imgs/background.jpg");
  background-position: center;
  background-size: cover;
  background-repeat: no-repeat;
  position: relative;
  overflow: hidden;

  .login-form-container {
    position: absolute;
    z-index: 2;
    width: 500px;
    background: #fff;
    border-radius: 8px;
    padding: 24px;
    left: calc(50% - 420px / 2);
    top: 30%;
    letter-spacing: 1px;
    .login-title {
      font-size: 24px;
    }
    .el-form-item {
      margin-bottom: 28px;
    }

    .el-form-item:first-child {
      text-align: center;

      img {
        height: 40px;
      }
    }

    .el-form-item:last-child {
      .el-button {
        width: 100%;
      }
    }

    ::v-deep .el-form {
      .el-input__prefix {
        left: 8px;
      }

      .el-button {
        letter-spacing: 1px;
      }
    }
  }
}
</style>
