<template>
  <div class="login-content">
    <div class="login-form" v-if="status">
      <div class="login-title">登录</div>
      <div class="form">
        <el-form :model="form" label-width="80">
          <el-form-item label="账号">
            <el-input v-model="form.account" style="height: 0.2344rem" />
          </el-form-item>
          <el-form-item label="密码">
            <el-input
              v-model="form.password"
              style="height: 0.2344rem"
              type="password"
            />
          </el-form-item>
          <el-form-item label="验证码">
            <div class="captcha">
              <el-input
                v-model="form.captcha"
                style="height: 0.2344rem"
                maxlength="4"
              />
              <div class="code" @click="getcodeinfo">
                <img :src="codeUrl" alt="" srcset="" />
              </div>
            </div>
          </el-form-item>
        </el-form>
        <div class="submit gradient" @click="submit">登录</div>
      </div>
      <div class="registered">
        还没账号？
        <span @click="registered">立即注册</span>
      </div>
    </div>
    <RegisTered v-else @backLogin="backLoginEvent" />
  </div>
</template>

<script lang="ts" setup>
import { onMounted, reactive, ref, toRefs, watch } from 'vue'
import { getCode } from '@/api/user'
import userStore from '@/store/module/user'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import RegisTered from './registered.vue'
const useUserStore = userStore()
const form = ref({
  account: '',
  password: '',
  captcha: '',
})
const codeUrl = ref('')
const getcodeinfo = async () => {
  const res: any = await getCode(1629428467008)
  codeUrl.value = res.result
  console.log(codeUrl.value)
}
getcodeinfo()
const Router = useRouter()
const submit = async () => {
  console.log(111, useUserStore)
  if (!form.value.account) {
    return ElMessage('账号不能为空')
  } else if (!form.value.password) {
    return ElMessage('密码不能为空')
  } else if (!form.value.captcha) {
    return ElMessage('验证码不能为空')
  }
  let data = {
    captcha: form.value.captcha,
    checkKey: 1629428467008,
    password: form.value.password,
    username: form.value.account,
  }
  const res = await useUserStore.login(data)
  console.log(res)
  if (res === 0) {
    ElMessage('验证码错误')
    getcodeinfo()
  } else if (res === 1) {
    ElMessage('账号或密码错误')
    getcodeinfo()
  } else {
    Router.push('/')
  }
}
onMounted(() => {
  window.addEventListener('keydown', keyDown)
  // setTimeout(() => {
  //   getcodeinfo()
  // },6000)
})
const keyDown = (e: any) => {
  //如果是回车则执行登录方法
  if (e.keyCode == 13) {
    //需要执行的登录方法
    submit()
  }
}

const status = ref(true)
const registered = () => {
  console.log(111)
  status.value = false
}
const backLoginEvent = () => {
  status.value = true
}
</script>

<style lang="scss" scoped>
.login-content {
  width: 100%;
  height: 100vh;
  background: url('../../assets/images/bg.png') no-repeat;
  background-size: cover;
  display: flex;
  align-items: center;
  justify-content: center;
  .login-form {
    width: 640px;
    height: 820px;
    border-radius: 15px;
    background-color: #ffffff1a !important;
    padding: 70px 60px;
    backdrop-filter: blur(10px);
    box-shadow: 0 4px 8px 1px rgba(0, 0, 0, 0.2);
    .login-title {
      font-size: 32px;
      font-weight: 700;
    }
    .form {
      margin-top: 35px;
      padding: 0 40px;
      .captcha {
        width: 100%;
        height: 100%;
        position: relative;
        .code {
          width: 100%;
          height: 100%;
          position: absolute;
          top: 0;
          right: 0;
          width: 105px;
          height: 100%;
          // background-color: pink;
          display: flex;
          align-items: center;
          img {
            width: 100%;
            height: 100%;
          }
        }
      }
      .submit {
        width: 399px;
        height: 50px;
        text-align: center;
        line-height: 50px;
        font-size: 20px;
        font-weight: 600;
        cursor: pointer;
        color: #fff;
        border-radius: 10px;
        margin-top: 35px;
      }
    }
  }
  .registered {
    margin-top: 25px;
    text-align: center;
    color: #666666;
    font-size: 12px;
    span {
      color: #0bd7c6;
      cursor: pointer;
    }
  }
}
:deep(.el-form-item) {
  display: flex;
  flex-direction: column;
}
:deep(.el-input__wrapper) {
  box-shadow: none;
}
:deep(.el-form-item__label) {
  justify-content: start;
}
</style>
