<template>
  <div class="login-container">
    <div class="login-header">葛洲坝集团交通投资有限公司投资决策分析系统</div>
    <div class="login-form-wrapper">
      <div class="company-logo"></div>
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginRules"
        label-width="70px"
        class="login-form"
        labelPosition="top"
        @keydown.enter="handleLogin"
      >
        <el-form-item label="用户名" prop="mobile">
          <el-input
            :prefix-icon="User"
            v-model="loginForm.mobile"
            placeholder="请输入用户名"
            prefix-icon="el-icon-user"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            prefix-icon="Lock"
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            class="login-btn"
            :loading="loading"
            @click="handleLogin"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref, getCurrentInstance } from "vue";
import { useRouter } from "vue-router";
import { User, Lock } from "@element-plus/icons-vue";
import { useUserStore } from "@/stores/user.js";
import CryptoJS from "crypto-js";
const userStore = useUserStore();

const { proxy } = getCurrentInstance();
const router = useRouter();
const loginFormRef = ref();
const loading = ref(false);

// 登录表单数据
const loginForm = ref({
  mobile: "",
  password: ""
});

// 表单验证规则
const loginRules = ref({
  mobile: [
    { required: true, message: "请输入用户名", trigger: "blur" }
  ],
  password: [
    { required: true, message: "请输入密码", trigger: "blur" }
  ],
});

// 处理登录
const handleLogin = async () => {
  loginFormRef.value?.validate(valid => {
    if (valid) {
      proxy.$post({
          url: "/api/user/login",
          data: {
              encryptLogStr: CryptoJS.AES.encrypt(`${loginForm.value.mobile},${loginForm.value.password}`, "GFG5w5AP0Ja2rNaa").toString()
          },
          callback: (data) => {
            userStore.setUseInfo(data);
            router.replace("/");
          }
      })
    }
  })
};
</script>

<style scoped lang="less">
.login-container {
  width: 100%;
  height: 100vh;
  background-image: url("@/assets/images/login.png");
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  padding-top: 15px;
}

.login-header {
  color: #fff;
  font-size: 42px;
  font-weight: bold;
  padding-left: 20px;
  margin: 0 20px;
  border-radius: 5px;
  display: flex;
  justify-content: left;
  align-items: center;
  height: 60px;
  line-height: 60px;
  background: linear-gradient(
    to right,
    rgba(0, 0, 0, 0.3),
    rgba(0, 0, 0, 0.6),
    rgba(0, 0, 0, 0.3)
  );
}

.login-form-wrapper {
  background-color: rgba(0, 0, 0, 0.8);
  position: absolute;
  top: 50%;
  right: 1%;
  transform: translateY(-50%);
  padding: 15px;
  box-sizing: border-box;
  width: 375px;
  height: 350px;
  box-shadow: 0 0 20px rgba(0, 0, 0, 0.5);
  border-radius: 8px;
  :deep(.el-form-item) {
    margin-bottom: 10px;
    .el-form-item__content {
      .el-input {
        height: 48px;
        border-radius: 4px;
        .el-input__wrapper {
          background: rgba(255, 255, 255, 0.9);
        }
        .el-input__inner {
          color: #333;
        }
      }
      .el-form-item__label {
        color: #fff;
      }
    }
  }
  :deep(.el-form-item__label) {
    color: #fff;
  }
}

.company-logo {
  width: 100%;
  height: 45px;
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-bottom: 30px;
  background-image: url(/src/assets/images/logo.png);
  background-size: 100% 80%;
  background-position: center;
  background-repeat: no-repeat;
}

.login-form {
  .login-btn {
    width: 100%;
    height: 30px;
    border-radius: 4px;
    background-color: #1890ff;
    margin-top: 18px;
  }
  .forgot-password {
    color: #fff;
    float: right;
    margin-top: 10px;
  }
}

.captcha-image {
  position: absolute;
  right: 0;
  top: 0;
  cursor: pointer;
  img {
    width: 120px;
    height: 40px;
    border-radius: 4px;
  }
}

:deep(.el-form-item__content) {
  position: relative;
  &.el-form-item__content--with-captcha {
    padding-right: 130px;
  }
}

:deep(.el-input__inner) {
  color: #333;
}

:deep(.el-input__prefix-inner) {
  color: #909399;
}
.login-form {
  :deep(.el-form-item) {
    .el-form-item__content {
      .el-input {
        height: 30px;
      }
    }
  }
  :deep(.el-form-item--label-top) {
    .el-form-item__label {
      line-height: 15px;
    }
  }
}
</style>
