<script setup>
import { ref, onMounted } from "vue";
import { ElMessage } from "element-plus";
import { captchaApi, login, info } from "@/api/user.js";
import { useUserStore } from "@/store/user.js";
import { useRouter } from "vue-router";

const userStore = useUserStore();
const router = useRouter();

const accRef = ref(null);
const pwdRef = ref(null);

const formLogin = ref({
  account: "xiao123",
  password: "123456",
  terminal: 3,
  scene: 1,
});

const onLogin = () => {
  if (!formLogin.value.account)
    return ElMessage({
      message: "请填写账号",
      type: "error",
    });
  if (!formLogin.value.password)
    return ElMessage({
      message: "请填写密码",
      type: "error",
    });
  login(formLogin.value)
    .then((res) => {
      console.log(res);
      if (res.data?.token) {
        userStore.setToken(res.data.token);
        userStore.setUserInfo(res.data);
        router.push("/");
      }
    })
    .catch((err) => {});
};

onMounted(() => {
});
</script>

<template>
  <div class="body">
    <div class="login">
      <div class="title">里海收银系统 - 登录</div>
      <el-form>
        <el-form-item>
          <el-input
            ref="accRef"
            v-model="formLogin.account"
            placeholder="请输入账号"
            :autofocus="true"
            @keydown.enter="pwdRef.focus()"
          >
            <template #prefix>
              <el-icon><User /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-input
            ref="pwdRef"
            v-model="formLogin.password"
            type="password"
            placeholder="请输入密码"
            show-password
            @keydown.enter="onLogin"
          >
            <template #prefix>
              <el-icon><Lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button style="width: 100%" type="primary" @click="onLogin"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<style scoped lang="scss">
.body {
  width: 100vw;
  height: 100vh;
  box-sizing: border-box;
  background-image: url(https://lihai001.oss-cn-chengdu.aliyuncs.com/def/b84ef202404101425077785.png);
  background-size: 100% 100%;
  background-repeat: no-repeat;
  display: flex;
  justify-content: center;
  align-items: center;
  .login {
    width: 18rem;
    /* height: 12rem; */
    background-color: #fff;
    border-radius: 2rem;
    padding: 2rem 3rem;
    .title {
      text-align: center;
      font-size: 1.3rem;
      padding-bottom: 2rem;
    }
  }
}
</style>
