<template>
  <div class="login-page">
    <!-- 主要内容区域 -->
    <div class="content">
      <!-- 返回按钮 -->
      <div class="back-button" @click="goBack">
        <!-- <van-icon name="arrow-left" size="3rem" /> -->
        <img src="@/assets/images/arrow_left.png" class="back-icon" />
      </div>

      <!-- 标题区域 -->
      <div class="header">
        <div class="title">Sign up.</div>
        <p class="subtitle">Redefine the styling experience with AI.</p>
        <p class="subtitle">Use Styling Angel to speed up your fashion journey.</p>
      </div>

      <div class="login-container">
        <div class="login-form">
          <div class="input-group">
            <input type="text" v-model="formData.name" placeholder="Name" class="input-field" />
          </div>
          <div class="input-group">
            <input type="email" v-model="formData.email" placeholder="Email" class="input-field" />
          </div>
          <div class="input-group pwd">
            <input
              type="password"
              v-model="formData.password"
              placeholder="Your Password"
              class="input-field"
            />
          </div>

          <!-- 登录按钮 -->
          <button type="button" class="login-button" @click="handleConfirm">Sign Up</button>

          <!-- Google登录按钮 -->
          <div type="button" class="google-button" @click="handleSignupByGoogle">
            <img :src="google" class="google-icon" />
            Sign up with Google
          </div>
        </div>
      </div>
    </div>

    <div class="footer">
      <p>Powered by AiDLab for Lane Crawford</p>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { showToast } from 'vant'
import { google } from '@/assets/base64'
import { fetchRegisterOrLogin } from '@/api/login'

const router = useRouter()

// 表单数据
const formData = reactive({
  name: '',
  email: '',
  password: ''
})

// 表单验证状态
const formErrors = reactive<Record<string, string>>({
  name: '',
  email: '',
  password: ''
})

// 加载状态
const isLoading = ref(false)

// 表单验证规则
const validateEmail = (email: string) => {
  const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
  return emailRegex.test(email)
}

const validatePassword = (password: string) => {
  return password.length >= 6
}

// 验证表单
const validateForm = () => {
  let isValid = true

  // 重置错误信息
  formErrors.name = ''
  formErrors.email = ''
  formErrors.password = ''

  // 验证邮箱
  if (!formData.name) {
    formErrors.name = '请输入姓名'
    isValid = false
  }

  // 验证邮箱
  if (!formData.email) {
    formErrors.email = '请输入邮箱地址'
    isValid = false
  } else if (!validateEmail(formData.email)) {
    formErrors.email = '请输入有效的邮箱地址'
    isValid = false
  }

  // 验证密码
  if (!formData.password) {
    formErrors.password = '请输入密码'
    isValid = false
  } else if (!validatePassword(formData.password)) {
    formErrors.password = '密码至少需要6位字符'
    isValid = false
  }

  return isValid
}

// 返回上一页
const goBack = () => {
  router.go(-1)
}

// 处理注册
const handleConfirm = async () => {
  if (!validateForm()) {
    showToast('请检查输入信息')
    return
  }

  isLoading.value = true

  fetchRegisterOrLogin({ ...formData, operationType: 'REGISTER' })
    .then((res) => {
      if (res.success) {
        showToast('注册成功')
        router.push('/login')
      } else {
        showToast(res.message)
      }
    })
    .catch((err) => {
      showToast(err.message)
    })
    .finally(() => {
      isLoading.value = false
    })
}

// 处理Google登录
const handleSignupByGoogle = async () => {
  try {
    isLoading.value = true
    showToast('Google登录功能开发中...')

    // 这里添加Google OAuth登录逻辑
    // const response = await googleLoginAPI()
  } catch (error) {
    console.error('Google登录失败:', error)
    showToast('Google登录失败，请重试')
  } finally {
    isLoading.value = false
  }
}
</script>

<style scoped lang="less">
.login-page {
  position: relative;
  color: #fff;
  width: 100%;
  height: 100vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  background-image: url('@/assets/images/sginup_bg.png');
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  padding-top: 12rem;
  font-family: 'satoshiRegular';
}

.back-button {
  // position: absolute;
  // top: 2rem;
  // left: 2rem;
  margin-top: 2.4rem;
  margin-left: 6.1rem;
  width: 2rem;
  height: 3.4rem;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  z-index: 3;
  font-size: 3.4rem;
  .back-icon {
    width: 2.83rem;
    height: 3.47rem;
  }
}

.header {
  margin-top: 1.42rem;
  padding-left: 15.5rem;
  color: white;
  font-family: 'satoshiRegular';
  .title {
    font-size: 11rem;
    font-weight: bold;
    margin-bottom: 0.8rem;
    color: white;
    font-family: 'satoshiBold';
  }

  .subtitle {
    font-size: 3rem;
    color: white;
    font-weight: 400;
    line-height: 141%;
    letter-spacing: 0.08rem;
  }
}

.content {
  position: relative;
  // z-index: 2;
  flex: 1;
}

.login-container {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-top: 7.2rem;
  .login-form {
    position: relative;
    width: calc(100% - 28.4rem);
    height: 118.4rem;
    background: radial-gradient(
      100% 100% at 0% 0%,
      rgba(115, 115, 115, 0.4) 0%,
      rgba(0, 0, 0, 0) 100%
    );
    backdrop-filter: blur(35px);
    border: 2px solid rgba(255, 255, 255, 0.15);
    border-radius: 4.79rem;
    padding: 11.2rem 8.62rem 14.28rem 7.18rem;
    box-shadow: 0 0.8rem 3.2rem rgba(0, 0, 0, 0.1);
    overflow: hidden;
    border: 2px solid #fff;
    font-size: 3.83rem;
  }

  .input-field {
    width: 100%;
    height: 10rem;
    padding: 16px 20px;
    border: 2px solid #fff;
    background: transparent;
    border-radius: 7.1rem;
    color: white;
    outline: none;
    font-size: 3.83rem;
    padding: 0 5.5rem;
  }

  .input-field::placeholder {
    color: rgba(255, 255, 255, 0.6);
    font-size: 3.83rem;
  }

  .input-group {
    margin-bottom: 4rem;
    &.pwd {
      margin-bottom: 6.7rem;
    }
  }
  .login-button {
    width: 100%;
    height: 10rem;
    background: #000;
    color: white;
    border: none;
    border-radius: 7rem;
    font-size: 4rem;
    margin-bottom: 26.3rem;
  }
}

.google-button {
  width: 100%;
  padding: 16px;
  border: 2px solid #fff;
  border-radius: 7rem;
  font-size: 3.83rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  column-gap: 3.1rem;
}

.google-icon {
  width: 4.8rem;
  height: 4.8rem;
}

.footer {
  position: relative;
  text-align: center;
  color: white;
  font-size: 3rem;
  margin-bottom: 9rem;
}
</style>
