<!-- 登录页面 -->

<template>
  <div class="page page-login flex flex-v">
    <div class="form-wrap flex flex-1">
      <el-form
        @submit.prevent="onSubmit"
        ref="form"
        :model="form"
        label-width="80px"
        size="small"
      >
        <h1>样表管理平台</h1>
        <el-form-item label="用户名" prop="loginName">
          <el-input v-model="form.loginName"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" type="password"></el-input>
        </el-form-item>
        <el-form-item size="large">
          <el-button
            type="primary"
            native-type="submit"
            :loading="loading"
            @click="onSubmit"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <div class="footer">
      登陆 &copy; <a href="">信宏翔网络科技有限公司</a> 出品
    </div>
  </div>
</template>

<script>
import { login } from "@/api/login";
import local from "@/utils/local";
import { calcMenu } from "@/router";
export default {
  name: "login",
  data() {
    return {
      originData: [],
      loading: false,
      redirect: this.$route.query.redirect || "/",
      form: {
        loginName: "",
        password: "",
      },
    };
  },
  created() {},
  methods: {
    async login() {
      let res = await login(this.form);
      let { data, code } = res.data;
      if (code === 1) {
        if (data.token) {
          let token = data.token;
          let siteIds = data.user.siteIds;
          let siteid = siteIds.split(",")[0];
          if (token) {
            local.setLocal("sampleToken", token);
            local.setLocal("sampleSiteId", siteid);
            // 渲染菜单
            calcMenu();
            this.$router.push("/basicsset");
          }
        }
      }
    },
    loginSuccess({ data }) {
      console.log("userData", data);
      this.$store.commit("setUserData", data);
      this.$router.replace({
        path: this.redirect,
      });
    },
    loginFail(error) {
      this.loading = false;
      //this.refreshCode();
      this.$message.error(error.message);
    },

    onSubmit(e) {
      e.preventDefault();
      if (!this.form.loginName.length) {
        return this.$message.warning("请输入用户名");
      }
      if (!this.form.password.length) {
        return this.$message.warning("请输入密码");
      }

      this.login();
    },
  },
};
</script>

<style lang="less">
.el-divider--horizontal {
  margin: 5px 0;
  background: 0 0;
  border-top: 2px solid #e8eaec;
}

.page-login {
  width: 100%;
  background-image: linear-gradient(45deg, #333, #111);
  background-size: 40px 40px;
  margin: 0;
  height: auto;

  .el-form {
    margin: auto;
    width: 400px;
    padding: 25px 25px 0 0;
    background: #fff;

    h1 {
      text-align: center;
      font-size: 20px;
      font-weight: 500;
      margin-bottom: 20px;
    }

    button {
      margin-top: 10px;
      width: 240px;
    }

    .el-input-group__append {
      padding: 0;
      font-size: 0;
    }
  }

  .footer {
    height: 80px;
    font-size: 12px;
    color: #999;
    text-align: center;
    line-height: 80px;

    a {
      color: #999;
    }
  }
}
</style>

