<template>
  <!-- Single scrollable container for tablet optimization -->
  <scroll-view class="forgot-password-container" scroll-y="true" show-scrollbar="false">
    <!-- Language switch positioned absolutely -->
    <view class="language-switch">
      <button class="language-btn" @click="toggleLanguage">
        {{ currentLocale === 'zh-CN' ? 'EN' : '中' }}
      </button>
    </view>
    
    <!-- Main content wrapper -->
    <view class="content-wrapper">
      <!-- Logo and title section -->
      <view class="logo-section">
        <text class="app-title">Akmon</text>
        <text class="page-title">{{ $t('user.forgot_password.title') }}</text>
        <text class="page-subtitle">{{ $t('user.forgot_password.subtitle') }}</text>
      </view>
      
      <!-- Form container with content inside -->
      <view class="form-container">
        <view v-if="!resetEmailSent">
          <form @submit="onSubmit">
            <!-- Email input -->
            <view class="input-group" :class="{ 'input-error': emailError }">
              <text class="input-label">{{ $t('user.forgot_password.email') }}</text>
              <input 
                class="input-field" 
                name="email"
                type="text" 
                v-model="email" 
                :placeholder="$t('user.forgot_password.email_placeholder')" 
                @blur="validateEmail"
              />
              <text v-if="emailError" class="error-text">{{ emailError }}</text>
            </view>
            
            <!-- Submit button -->
            <button form-type="submit" class="submit-button" :disabled="isLoading" :loading="isLoading">
              {{ $t('user.forgot_password.submit_button') }}
            </button>
            
            <!-- General error message -->
            <text v-if="generalError" class="general-error">{{ generalError }}</text>
          </form>
          
          <!-- Login option -->
          <view class="login-option">
            <text class="login-text">{{ $t('user.forgot_password.remember_password') }}</text>
            <text class="login-link" @click="navigateToLogin">{{ $t('user.forgot_password.login') }}</text>
          </view>
        </view>
        
        <!-- Success message -->
        <view v-else class="success-container">
          <view class="success-icon">✓</view>
          <text class="success-title">{{ $t('user.forgot_password.email_sent_title') }}</text>
          <text class="success-message">{{ $t('user.forgot_password.email_sent_message') }}</text>
          <button class="back-button" @click="navigateToLogin">
            {{ $t('user.forgot_password.back_to_login') }}
          </button>
        </view>
      </view>
    </view>
  </scroll-view>
</template>

<script lang="uts">
import supa from '@/components/supadb/aksupainstance.uts';
import { switchLocale, getCurrentLocale } from '@/utils/utils.uts';

export default {
  data() {
    return {
      email: "",
      emailError: "",
      generalError: "",
      isLoading: false,
      resetEmailSent: false,
      currentLocale: getCurrentLocale()
    };
  },  methods: {
    toggleLanguage() {
      const newLocale = this.currentLocale === 'zh-CN' ? 'en-US' : 'zh-CN';
      switchLocale(newLocale);
      this.currentLocale = newLocale;
      
      uni.showToast({
        title: this.$t('user.forgot_password.language_switched'),
        icon: 'success'
      });
    },
    onSubmit(e: UniFormSubmitEvent) {
      this.handleResetRequest();
    },
    validateEmail() {
      const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
      if (this.email == null || this.email === "") {
        this.emailError = this.$t('user.forgot_password.email_required');
        return false;
      } else if (!emailRegex.test(this.email)) {
        this.emailError = this.$t('user.forgot_password.email_invalid');
        return false;
      } else {
        this.emailError = '';
        return true;
      }
    },
    async handleResetRequest() {
      this.generalError = '';
      if (!this.validateEmail()) {
        return;
      }
      
      this.isLoading = true;
      try {
        // Call Supabase password reset method
        const result = await supa.resetPassword(this.email);
        
        // Show success view
        this.resetEmailSent = true;
        
      } catch (err) {
        console.error("Password reset error:", err);

        // Format error message
        if (typeof err === 'object' && err !== null) {
          const errObj = err as UniError;
          if (typeof errObj.message === 'string') {
            // If we have a specific error message from Supabase
            this.generalError = errObj.message;
          } else {
            this.generalError = this.$t('user.forgot_password.unknown_error');
          }
        } else {
          this.generalError = this.$t('user.forgot_password.unknown_error');
        }
      } finally {
        this.isLoading = false;
      }
    },
    navigateToLogin() {
      uni.navigateTo({
        url: '/pages/user/login'
      });
    }
  }
};
</script>

<style>
/* Single scrollable container for tablet optimization */
.forgot-password-container {
  height: 100%;
  /* #ifdef APP-PLUS */
  padding: 20rpx;
  /* #endif */
  /* #ifndef APP-PLUS */
  padding: 40rpx;
  /* #endif */
  background-color: #f8f9fa;
  box-sizing: border-box;
}

/* Content wrapper for centered layout */
.content-wrapper {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  padding-bottom: 40rpx;
  /* 确保内容足够高以触发滚动 */
  /* #ifdef APP-PLUS */
  min-height: 800rpx;
  /* #endif */
  /* #ifndef APP-PLUS */
  min-height: 1000rpx;
  /* #endif */
}

/* Language switch button - positioned absolutely */
.language-switch {
  position: absolute;
  /* #ifdef APP-PLUS */
  top: 30rpx;
  right: 30rpx;
  /* #endif */
  /* #ifndef APP-PLUS */
  top: 40rpx;
  right: 40rpx;
  /* #endif */
  z-index: 10;
}

.language-btn {
  /* #ifdef APP-PLUS */
  width: 50rpx;
  height: 50rpx;
  border-radius: 25rpx;
  font-size: 18rpx;
  /* #endif */
  /* #ifndef APP-PLUS */
  width: 80rpx;
  height: 80rpx;
  border-radius: 40rpx;
  font-size: 28rpx;
  /* #endif */  background-color: rgba(33, 150, 243, 0.8);  color: #fff;
  font-weight: normal;
  border: 2rpx solid rgba(255, 255, 255, 0.3);
  text-align: center;
  box-shadow: 0 4rpx 12rpx rgba(33, 150, 243, 0.3);
}

/* Logo and title section */
.logo-section {
  display: flex;
  flex-direction: column;
  align-items: center;
  /* #ifdef APP-PLUS */
  margin-top: 60rpx;
  margin-bottom: 30rpx;
  /* #endif */
  /* #ifndef APP-PLUS */
  margin-top: 80rpx;
  margin-bottom: 40rpx;
  /* #endif */
}

.app-title {
  /* #ifdef APP-PLUS */
  font-size: 28rpx;
  /* #endif */
  /* #ifndef APP-PLUS */
  font-size: 36rpx;
  /* #endif */
  font-weight: bold;
  color: #2196f3;
}

.page-title {
  /* #ifdef APP-PLUS */
  font-size: 32rpx;
  margin-top: 12rpx;
  /* #endif */
  /* #ifndef APP-PLUS */
  font-size: 48rpx;
  margin-top: 20rpx;
  /* #endif */
  font-weight: bold;
  color: #333;
}

.page-subtitle {
  /* #ifdef APP-PLUS */
  font-size: 18rpx;
  margin-top: 6rpx;
  /* #endif */
  /* #ifndef APP-PLUS */
  font-size: 28rpx;
  margin-top: 10rpx;
  /* #endif */
  color: #666;
}

/* Form container */
.form-container {
  width: 100%;
  /* #ifdef APP-PLUS */
  max-width: 500rpx;
  padding: 25rpx;
  /* #endif */
  /* #ifndef APP-PLUS */
  max-width: 680rpx;
  padding: 40rpx;
  /* #endif */
  background-color: #ffffff;
  border-radius: 20rpx;
  box-shadow: 0 10rpx 30rpx rgba(0, 0, 0, 0.1);
  box-sizing: border-box;
}

/* Input groups */
.input-group {
  /* #ifdef APP-PLUS */
  margin-bottom: 18rpx;
  /* #endif */
  /* #ifndef APP-PLUS */
  margin-bottom: 30rpx;
  /* #endif */
}

.input-label {
  /* #ifdef APP-PLUS */  font-size: 20rpx;
  margin-bottom: 6rpx;
  /* #endif */
  /* #ifndef APP-PLUS */
  font-size: 28rpx;
  margin-bottom: 10rpx;
  /* #endif */
  font-weight: normal;
  color: #333;
  display: flex;
}

.input-field {
  width: 100%;
  /* #ifdef APP-PLUS */
  height: 60rpx;
  padding: 0 20rpx;
  font-size: 22rpx;
  /* #endif */
  /* #ifndef APP-PLUS */
  height: 90rpx;
  padding: 0 30rpx;
  font-size: 28rpx;
  /* #endif */
  border-radius: 10rpx;
  border: 2rpx solid #ddd;
  background-color: #f9f9f9;
  box-sizing: border-box;
}

.input-error .input-field {
  border-color: #f44336;
}

.error-text {
  /* #ifdef APP-PLUS */
  font-size: 18rpx;
  margin-top: 4rpx;
  /* #endif */
  /* #ifndef APP-PLUS */
  font-size: 24rpx;
  margin-top: 6rpx;
  /* #endif */
  color: #f44336;
}

/* Submit button */
.submit-button {
  width: 100%;
  /* #ifdef APP-PLUS */
  height: 60rpx;
  font-size: 24rpx;
  margin: 12rpx 0;
  border-radius: 30rpx;
  /* #endif */
  /* #ifndef APP-PLUS */
  height: 90rpx;
  font-size: 32rpx;
  margin: 20rpx 0;  border-radius: 45rpx;
  /* #endif */
  background-image: linear-gradient(to right, #2196f3, #03a9f4);  color: #fff;
  font-weight: normal;
  text-align: center;
  box-shadow: 0 10rpx 20rpx rgba(3, 169, 244, 0.2);
}

.submit-button:disabled {
  background: #ccc;
  box-shadow: none;
}

/* General error */
.general-error {
  width: 100%;
  text-align: center;
  color: #f44336;
  /* #ifdef APP-PLUS */
  font-size: 20rpx;
  margin-top: 12rpx;
  /* #endif */
  /* #ifndef APP-PLUS */
  font-size: 28rpx;
  margin-top: 20rpx;
  /* #endif */
}

/* Login option */
.login-option {
  display: flex;
  justify-content: center;
  /* #ifdef APP-PLUS */
  margin-top: 25rpx;
  /* #endif */
  /* #ifndef APP-PLUS */
  margin-top: 40rpx;
  /* #endif */
}

.login-text {
  /* #ifdef APP-PLUS */
  font-size: 20rpx;
  margin-right: 5rpx;
  /* #endif */
  /* #ifndef APP-PLUS */
  font-size: 28rpx;
  margin-right: 8rpx;
  /* #endif */
  color: #666;
}

.login-link {
  /* #ifdef APP-PLUS */
  font-size: 20rpx;
  /* #endif */
  /* #ifndef APP-PLUS */
  font-size: 28rpx;  /* #endif */
  color: #2196f3;
  font-weight: normal;
}

/* Success container */
.success-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  /* #ifdef APP-PLUS */
  padding: 12rpx 0;
  /* #endif */
  /* #ifndef APP-PLUS */
  padding: 20rpx 0;
  /* #endif */
}

.success-icon {
  /* #ifdef APP-PLUS */
  width: 75rpx;
  height: 75rpx;
  font-size: 38rpx;
  margin-bottom: 20rpx;
  /* #endif */
  /* #ifndef APP-PLUS */
  width: 120rpx;
  height: 120rpx;
  font-size: 60rpx;
  margin-bottom: 30rpx;
  /* #endif */
    background-color: #4caf50;
    color: white;
    /* #ifdef APP-PLUS */
    border-radius: 75rpx;
    /* #endif */
    /* #ifndef APP-PLUS */
    border-radius: 120rpx;
    /* #endif */
    display: flex;
    align-items: center;
    justify-content: center;
}

.success-title {
  /* #ifdef APP-PLUS */
  font-size: 24rpx;
  margin-bottom: 12rpx;
  /* #endif */
  /* #ifndef APP-PLUS */
  font-size: 36rpx;
  margin-bottom: 20rpx;
  /* #endif */
  font-weight: bold;
  color: #333;
}

.success-message {
  /* #ifdef APP-PLUS */
  font-size: 20rpx;
  margin-bottom: 25rpx;
  /* #endif */
  /* #ifndef APP-PLUS */
  font-size: 28rpx;
  margin-bottom: 40rpx;
  /* #endif */
  color: #666;
  text-align: center;
}

.back-button {
  width: 100%;
  /* #ifdef APP-PLUS */
  height: 60rpx;
  font-size: 24rpx;
  border-radius: 30rpx;
  /* #endif */
  /* #ifndef APP-PLUS */
  height: 90rpx;
  font-size: 32rpx;
  border-radius: 45rpx;
  /* #endif */
  background-color: #f0f0f0;  color: #333;
  font-weight: normal;
  text-align: center;
}
</style>