<script setup>
import { ElMessage } from 'element-plus'
import  {ref,reactive,onMounted} from 'vue'
import {useRouter} from "vue-router";

const ruleForm = reactive({
  user:'',
  password:''
})
const router = useRouter()
const rules = reactive({
  //用户名密码验证规则
  user: [{ required: true, message: "请输入用户名", trigger: "blur" }],
  password: [{ required: true, message: "请输入密码", trigger: "blur" }],
});

const ruleFormRef = ref()
const passwordCheck = ref(false)
function submitForm(elForm){
  if(!elForm) return
  elForm.validate((valid)=>{
    if(valid){
      if(passwordCheck.value){
        localStorage.setItem('loginInfo',JSON.stringify(ruleForm))
      }else{
        localStorage.setItem('loginInfo',JSON.stringify({}))
      }
      ElMessage({
        message:'登录成功',
        type:"success"
      })

      router.push('/home')
    }else{
      ElMessage({
        message:'登录失败',
        type:"error"
      })
    }
  })

}
// 校验是否保存密码
function checkSavePassword(){
  let loginInfo = localStorage.getItem('loginInfo')
  console.log(loginInfo,'xxxxxxxxxxxx')
  if(loginInfo!==null&&Object.keys(loginInfo).length > 2){
    console.log(loginInfo)
    ruleForm.user = JSON.parse(loginInfo).user
    ruleForm.password = JSON.parse(loginInfo).password
    passwordCheck.value = true

  }else{
    passwordCheck.value = false
  }
}
onMounted(()=>{
  // 校验缓存中是否有用户密码
  checkSavePassword()
})
</script>

<template>
<div class="login-bg">
  <div class="login-form">
    <div class="login-form-title">
      欢迎登录
    </div>
    <el-form
        ref="ruleFormRef"
        :model="ruleForm"
        status-icon
        class="demo-ruleForm"
        :rules="rules"
    >
      <el-form-item prop="user">
        <el-input v-model="ruleForm.user" type="text" placeholder="请输入用户名" >
<!--          <template #prepend>-->
<!--            <div class="user-icon">-->

<!--            </div>-->
<!--          </template>-->
          <template #prefix>
            <i class="user-icon"></i>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
            v-model="ruleForm.password"
            type="password"
            placeholder="请输入密码"
        >
<!--          <template #prepend>-->
<!--            <div class="password-icon">-->

<!--            </div>-->
<!--          </template>-->
          <template #prefix>
            <i class="password-icon"></i>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item>
        <div>
          <el-checkbox v-model="passwordCheck" label="保存密码" />
        </div>
        <div class="submit" @click="submitForm(ruleFormRef)">

        </div>


      </el-form-item>
    </el-form>
  </div>

</div>
</template>

<style scoped lang="scss">
//提交按钮
.submit{
  cursor: pointer;
  background-image: url('@/assets/images/submit.png');
  width: 100%;
  height: 100%;
  background-size: 100% 100%;
}
.login-bg{
  width: 100%;
  height: 100%;
  background-image: url('@/assets/images/login-bg.png');
  background-size: 100% 100%;
}
.user-icon,.password-icon{
  width: 100%;
  display: block;
  height: 100%;
  background-size: 100% 100%;
}
:deep(.el-input__prefix){
  width: 15%;
  height: 100%;
}
.user-icon{
  background-image: url('@/assets/images/user.png');
}
.password-icon{
  background-image: url('@/assets/images/password.png');
}
:deep(.el-input__prefix-inner){
  width: 100%;
  height: 100%;
}
:deep(.el-input__wrapper){
  background-color: transparent;
  padding-left: 1px;
  box-shadow: 0 0 0 0px var(--el-input-border-color,var(--el-border-color)) inset;
  cursor: default;
}
.el-input__inner{
  cursor: default !important;
}
:deep(.el-input){
  width: 100%;
  height: 100%;
  background-image: url('@/assets/images/el-input.png');
  background-size: 100% 100%;
}
:deep(.el-checkbox__label){
  color: rgba(233, 244, 255, 1);
}
:deep(.el-form-item) {
  height: 13%;
}
:deep(.el-form) {
  width: 100%;
  height: 100%;
}
.login-form{
  width: vw(600);
  height: vh(400);
  padding: 0 6.25rem;
  box-sizing: border-box;
  position: absolute;
  top: 0;
  bottom: 0;
  right: 0;
  left: 0;
  margin: auto;
  background-image: url('@/assets/images/login-form.png');
  background-size: 100% 100%;
  &-title{
    letter-spacing: 3px;
    text-shadow: 0px 0px 7px #129DFF;
    font-size: 1.875rem;
    font-weight: 400;
    font-family: pangmen;
    text-align: center;
    margin:3.125rem 0 2.25rem 0;
  }
}
</style>