<template>
  <div class="login-from">
    <div class="item">
      <div class="title">用户名</div>
      <div>
        <input
          v-model="useInfo.username"
          class="ipt"
          @focus="stopKeyborad"
          @blur="stopKeyborad"
          @click="stopInput"
          placeholder="请输入" />
      </div>
    </div>
    <div class="item">
      <div class="title">密码</div>
      <div>
        <vxe-input
          class="ipt"
          v-model="useInfo.password"
          placeholder="请输入"
          :type="showPwd ? 'string' : 'password'"
          @suffix-click="changePwdType"
          @focus="stopKeyborad"
          @blur="stopKeyborad"
          @click="stopInput"></vxe-input>
      </div>
    </div>
    <div class="login-btn" @click="userLogin">登录</div>
    <div class="face-link-btn" @click="showFaceDialog">人脸登录</div>
  </div>

  <div class="fooder-mark">
    <div class="content">COPYRIGHT © 2025 杭州研一智控科技有限公司 版权所有</div>
  </div>

  <FaceDialog
    v-if="faceDialogVisible"
    oprType="FACE_LOGIN"
    @cancel="faceDialogVisible = false"
    @submit="faceLogin"></FaceDialog>
</template>

<script setup>
import { reactive, ref, watch, watchEffect, onBeforeUnmount, nextTick, onMounted } from 'vue';
import { useLogin, useIdLogin } from '@/api/login.js';
import { useRouter } from 'vue-router';
import { message, notify } from '@/utils/message.js';
import { doubleUserLogin } from '@/utils/login.js';
import { getDeviceInfo } from '@/api/user';
import FaceDialog from '../../components/FaceDialog/index.vue';

const router = useRouter();
const useInfo = ref({
  username: '',
  password: ''
});
let faceDialogVisible = ref(false);

let verify_num = ref(1);
const getDeviceInfoMethods = async () => {
  let { data } = await getDeviceInfo();
  if (data.code == 200) {
    verify_num.value = data.data.need_two_face;
  }
};
getDeviceInfoMethods();

const isFocused = ref(false);
let showPwd = ref(false);
const changePwdType = () => {
  showPwd.value = !showPwd.value;
};
const stopInput = (e) => {
  if (isFocused.value) {
    e.target.setAttribute('readonly', true);
    setTimeout(() => {
      e.target.removeAttribute('readonly');
    }, 100);
  }
};

const stopKeyborad = (e) => {
  isFocused.value = true;
  e.target.setAttribute('readonly', true);
  setTimeout(() => {
    e.target.removeAttribute('readonly');
  }, 100);
};

const userLogin = async () => {
  if (useInfo.value.username == '' || useInfo.value.password == '') {
    notify('请输入用户名或密码');
    return;
  }
  const res = await useLogin(useInfo.value);
  console.log('login---res-----', res);
  if (res?.data?.code === 200) {
    let { user_id, user_name } = res.data.data || {};
    if (verify_num.value != 2) {
      notify('验证成功！', 'success');
      sessionStorage.setItem('userInfo', JSON.stringify(res.data.data));
      router.push('/home');
      return;
    }
    let isDouble = doubleUserLogin(res.data.data);
    if (isDouble) {
      sessionStorage.setItem('userInfo', JSON.stringify(res.data.data));
      router.push('/home');
    } else {
      useInfo.value = {
        userId: '',
        username: ''
      };
    }
  } else {
    notify(res.data.desc, 'danger');
  }
};

const faceLogin = async (userInfo) => {
  sessionStorage.setItem('userInfo', JSON.stringify(userInfo));
  router.push('/home');
};

const showFaceDialog = () => {
  faceDialogVisible.value = true;
};

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

<style lang="less" scoped>
@commonSize: 40px;
.login-from {
  position: absolute;
  top: 52%;
  left: 50%;
  transform: translate(-50%, -50%);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  width: 510px;
  height: 530px;
  background: #ffffff;
  border-radius: 12px 12px 12px 12px;
  border: 1px solid #dcdcdc;
  .item {
    margin-bottom: 40px;
    .title {
      margin-bottom: 15px;
      font-size: 23px;
      color: #666666;
    }

    .ipt,
    ::v-deep .vxe-input--inner {
      width: 376px;
      height: 67px;
      padding-left: 0;
      background: #ffffff;
      box-shadow: inset 0px 3px 6px 1px rgba(0, 0, 0, 0.16);
      border-radius: 4px 4px 4px 4px;
      border: 1px solid #dcdcdc;
      font-size: 24px;
    }
    ::v-deep .vxe-input--inner::placeholder {
      color: rgb(117, 117, 117);
    }
  }

  .login-btn,
  .face-link-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 375px;
    height: 65px;
    background: linear-gradient(180deg, #cf4a4a 0%, #a21313 100%);
    box-shadow: 0px 3px 6px 1px rgba(0, 0, 0, 0.2);
    border-radius: 6px;
    font-size: 24px;
    color: #fff;
    cursor: pointer;
  }
  .face-link-btn {
    margin-top: 24px;
    background: linear-gradient(180deg, #1f89e7 0%, #1472c5 100%);
    box-shadow: 0px 3px 6px 1px rgba(0, 0, 0, 0.2);
  }
}

.fooder-mark {
  position: absolute;
  bottom: 30px;
  left: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100vw;
  .content {
    width: 730px;
    height: 31px;
    font-family: Microsoft YaHei, Microsoft YaHei;
    font-size: 20px;
    color: #c3c3c3;
    text-align: center;
    font-style: normal;
    text-transform: none;
  }
}
</style>
