<template>
  <div class="login" @click="$refs.verify.show = false">
    <Row class="login-form" @keydown.enter.native="submitLogin">
      <Header></Header>
      <Card class="form-card">
        <div class="form-title">欢迎登录集客</div>
        <div class="form-line"></div>
        <Form
          ref="usernameLoginForm"
          :model="form"
          :rules="rules"
          class="form-con"
        >
          <FormItem prop="username">
            <Input
              class="login-input"
              v-model="form.username"
              size="large"
              clearable
              placeholder="请输入账号"
              autocomplete="off"
            >
              <template #prefix>
                <iconpark-icon
                  class="login-icon"
                  size="20"
                  name="index-icon-yonghuming"
                ></iconpark-icon>
              </template>
            </Input>
          </FormItem>
          <FormItem prop="password">
            <Input
              class="login-input"
              type="password"
              v-model="form.password"
              size="large"
              password
              placeholder="请输入密码"
              autocomplete="off"
            >
              <template #prefix>
                <iconpark-icon
                  class="login-icon"
                  size="20"
                  name="index-icon-mima"
                ></iconpark-icon>
              </template>
            </Input>
          </FormItem>
        </Form>
        <Button
          class="login-btn"
          type="primary"
          size="large"
          :loading="loading"
          @click="submitLogin"
          long
        >
          <span v-if="!loading">{{ $t("login") }}</span>
          <span v-else>{{ $t("logining") }}</span>
        </Button>
      </Card>
      <!-- 拼图验证码 -->
      <verify
        ref="verify"
        class="verify-con"
        verifyType="LOGIN"
        @change="verifyChange"
      ></verify>
    </Row>
    <Footer></Footer>
  </div>
</template>

<script>
import { login, userInfo } from "@/api/index";
import Cookies from "js-cookie";
import Header from "@/views/main-parts/header";
import Footer from "@/views/main-parts/footer";
import util from "@/libs/util.js";
import verify from "@/components/verify";

export default {
  components: {
    Header,
    Footer,
    verify,
  },
  data() {
    return {
      loading: false, // 加载状态
      form: {
        // 表单数据
        username: "",
        password: "",
        mobile: "",
        code: "",
      },
      rules: {
        // 验证规则
        username: [
          {
            required: true,
            message: "账号不能为空",
            trigger: "blur",
          },
        ],
        password: [
          {
            required: true,
            message: "密码不能为空",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {},
  methods: {
    afterLogin(res) {
      // 登录成功后处理
      let accessToken = res.result.accessToken;
      let refreshToken = res.result.refreshToken;
      this.setStore("accessToken", accessToken);
      this.setStore("refreshToken", refreshToken);
      // 获取用户信息
      userInfo().then((res) => {
        if (res.success) {
          // 加载菜单
          Cookies.set("userInfoManager", JSON.stringify(res.result));
          this.$store.commit("setAvatarPath", res.result.avatar);
          util.initRouter(this);
          this.$router.push({
            name: "home_index",
          });
        } else {
          this.loading = false;
        }
      });
    },
    submitLogin() {
      // 登录操作
      this.$refs.usernameLoginForm.validate((valid) => {
        if (valid) {
          this.$refs.verify.init();
        }
      });
    },
    verifyChange(con) {
      // 拼图验证码回显
      if (!con.status) return;

      this.loading = true;

      let fd = new FormData();
      fd.append("username", this.form.username);
      fd.append("password", this.md5(this.form.password));
      login(fd)
        .then((res) => {
          if (res && res.success) {
            this.afterLogin(res);
          } else {
            this.loading = false;
          }
        })
        .catch(() => {
          this.loading = false;
        });
      this.$refs.verify.show = false;
    },
  },
};
</script>

<style lang="scss" scoped>
.login {
  height: 100%;
  background: url("../assets/background.png") no-repeat;
  background-size: cover;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  padding: 120px 220px 30px;
  .login-form {
    width: 100%;
    display: flex;
    justify-content: space-between;
  }
  .form-card {
    min-width: 450px;
    height: 470px;
    background: #ffffff;
    border-radius: 4px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    padding: 60px;
    text-align: center;

    /deep/.ivu-card-body {
      width: 100% !important;
      height: 470px !important;
      padding: 0;
    }
    .form-title {
      font-size: 20px;
      font-weight: bold;
      color: #333333;
      line-height: 28px;
    }
    .form-line {
      width: 32px;
      height: 3px;
      background: #1890ff;
      border-radius: 2px;
      margin: 0 auto;
      margin-bottom: 60px;
      margin-top: 10px;
    }
    .form-con {
      .login-input {
        /deep/.ivu-input {
          height: 56px;
          background: #f2f4f8;
          border-radius: 4px;
        }
        /deep/.ivu-input-prefix {
          top: 50%;
          margin-top: -10px;
        }
        /deep/.ivu-input-suffix i {
          line-height: 56px;
          color: #1890ff;
        }
      }
    }
  }
  .verify-con {
    position: absolute;
    top: 50%;
    z-index: 10;
    left: 50%;
    transform: translate(-50%, -50%);
  }

  .login-btn {
    margin-top: 60px;
    background: #1890ff;
    height: 56px;
    border-radius: 4px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    font-weight: bold;
    color: #ffffff;
    line-height: 22px;
  }
}
@media screen and (max-width: 1280px) {
  .login {
    padding: 110px 140px 30px;
  }
}
</style>
