<template>
  <div class="main">
    <el-card class="login_box">
      <h2>登录</h2>
      <br>
      <el-form
          ref="formRef"
          :model="loginForm"
          :rules="loginFormRules">
        <el-form-item prop="username">
          <el-input
              type="text"
              class="apple-input"
              v-model="loginForm.username"
              placeholder="用户名"
              style="width: 100%"
              @keyup.enter="handleLogin()">
              <template #prefix>
                <el-icon><User /></el-icon>
              </template>
          </el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input
              type="password"
              class="apple-input"
              v-model="loginForm.password"
              placeholder="密码"
              style="width: 100%"
              @keyup.enter="handleLogin()">
              <template #prefix>
                <el-icon><Lock /></el-icon>
              </template>
            </el-input>
        </el-form-item>
        <br>

        <el-form-item>
          <el-button
            class="login_btn"
            type="success"
            style="width: 100%"
            @click="handleLogin()" >
            登录
          </el-button>

      </el-form-item>

      <el-form-item>
        <el-button
          class="login_btn"
          style="width: 100%"
          link
          @click="jumpRegister()" >
          注册
        </el-button>
      </el-form-item>

      <el-form-item>
        <el-button
          class="login_btn"
          style="width: 100%"
          link
          @click="jumpHome()" >
          返回首页
        </el-button>
      </el-form-item>
    </el-form>
  </el-card>
</div>
</template>

<script setup>
import { ref, reactive, getCurrentInstance } from 'vue'
import { useRouter } from 'vue-router'

import { login } from '@/api/login';
import { setToken } from '@/utils/token';
import Cookies from 'js-cookie';

const { proxy } = getCurrentInstance();

const formRef = ref();

// 使用 useRouter 创建一个 router 实例
const router = useRouter()

const loginForm = reactive({
  username: "",
  password: ""
})

const loginFormRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
}

const handleLogin = () => {
  formRef.value.validate((valid) => {
    if (valid) {
      // console.log(loginForm)
      // console.log('登录')
      // console.log(loginForm)
      login(loginForm.username, loginForm.password).then((res) => {
        console.log(res)
        if (res.code === 200) {
          proxy.$message.success(res.msg)
          // 保存token
          setToken(res.data.token)
          Cookies.set('username', res.data.user.username)
          Cookies.set('avatar', res.data.user.avatar)
          Cookies.set('nickname', res.data.user.nickname)
          Cookies.set('role', res.data.user.role)
          // 页面跳转
          // 管理端跳转
          if (res.data.user.role === '1') {
            router.push('/admin')
          } else {
            router.push('/tourist/home')
          } 
          
        } else {
          proxy.$message.error(res.msg)
        }
      })
    } else {
      console.log('验证失败')
      return false
    }
  })

}

const jumpRegister = () => {
  proxy.$router.push('/register')
}

const jumpHome = () => {
  proxy.$router.push('/tourist/home')
}

</script>

<style scoped>
.main {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  width: 100%;
  height: 100%;
  background-image: url("@/assets/1.jpg");
  background-repeat: no-repeat;
  background-size: 100% 100%;
  padding-right: 10%;
}

.login_box{
  width: 340px;
  text-align: center;
  /* 毛玻璃 */
  /*
	background: rgba(255, 255, 255, 0.6);
	backdrop-filter: blur(20px);
  border-radius: 15px;*/
  /* 液态玻璃*/
  /*
  background: rgba(255, 255, 255, 0.25);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.18);
  box-shadow: 0 8px 32px 0 rgba(31, 38, 135, 0.15);
  border-radius: 15px;
  */
  background: rgba(255, 255, 255, 0.2);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-radius: 10px;
  border: 1px solid rgba(255, 255, 255, 0.4);
  box-shadow: 2px 4px 6px rgba(0, 0, 0, 0.2);
}
.login_box:hover {
  transform: translateY(-2px);
  box-shadow: 0 12px 36px 0 rgba(31, 38, 135, 0.2);
  transition: all 0.3s ease;
}

.login_box h2 {
  font-size: 25px;
}
</style>
