<template>
  <view class="page">
    <view class="header-hero" :style="{ paddingTop: statusBarHeight + 'px' }">
      <view class="top-bar">
        <view class="top-left" @click="goBack">
          <AppIcon name="chevronLeft" size="sm" color="white" />
        </view>
        <view class="top-center">
          <text class="page-title">{{ t('password.changeTitle') }}</text>
          <LocationPicker />
        </view>
        <view class="top-right" @click="isMenuOpen = true">
          <AppIcon name="menu" size="sm" color="white" />
        </view>
      </view>
    </view>

    <view class="content">
      <view class="lock-icon-wrap">
        <view class="lock-circle">
          <AppIcon name="lock" size="lg" color="white" />
        </view>
      </view>

      <view class="form-card">
        <view class="form-group">
          <text class="label">{{ t('password.current') }}</text>
          <view class="input-wrap">
            <input
              v-model="currentPassword"
              class="input"
              :type="showCurrent ? 'text' : 'password'"
              :placeholder="t('password.currentPlaceholder')"
              placeholder-class="placeholder-cls"
            />
            <view class="eye-btn" @click="showCurrent = !showCurrent">
              <AppIcon name="eye" size="sm" :color="showCurrent ? 'primary' : 'gray'" />
            </view>
          </view>
        </view>

        <view class="form-group">
          <text class="label">{{ t('password.new') }}</text>
          <view class="input-wrap">
            <input
              v-model="newPassword"
              class="input"
              :type="showNew ? 'text' : 'password'"
              :placeholder="t('password.newPlaceholder')"
              placeholder-class="placeholder-cls"
            />
            <view class="eye-btn" @click="showNew = !showNew">
              <AppIcon name="eye" size="sm" :color="showNew ? 'primary' : 'gray'" />
            </view>
          </view>
        </view>

        <view class="form-group form-group-last">
          <text class="label">{{ t('password.confirm') }}</text>
          <view class="input-wrap">
            <input
              v-model="confirmPassword"
              class="input"
              :type="showConfirm ? 'text' : 'password'"
              :placeholder="t('password.confirmPlaceholder')"
              placeholder-class="placeholder-cls"
            />
            <view class="eye-btn" @click="showConfirm = !showConfirm">
              <AppIcon name="eye" size="sm" :color="showConfirm ? 'primary' : 'gray'" />
            </view>
          </view>
        </view>
      </view>

      <view class="tips-card">
        <text class="tips-title">{{ t('password.requirements') }}</text>
        <text class="tips-item">{{ t('password.reqLength') }}</text>
        <text class="tips-item">{{ t('password.reqCase') }}</text>
        <text class="tips-item">{{ t('password.reqNumber') }}</text>
        <text class="tips-item tips-last">{{ t('password.reqSpecial') }}</text>
      </view>

      <view
        class="submit-btn"
        :class="{ disabled: !canSubmit }"
        @click="handleSubmit"
      >
        <text class="submit-btn-text">{{ t('password.update') }}</text>
      </view>
    </view>

    <SideMenu v-model="isMenuOpen" />
  </view>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'
import { useI18n } from 'vue-i18n'
import AppIcon from '../../components/AppIcon.vue'
import SideMenu from '../../components/SideMenu.vue'
import LocationPicker from '../../components/LocationPicker.vue'
import { getStatusBarHeight } from '../../utils/statusBar'
import { usAppChangePassword } from '../../services/usAppAuth'

const statusBarHeight = getStatusBarHeight()
const { t } = useI18n()
const isMenuOpen = ref(false)

const currentPassword = ref('')
const newPassword = ref('')
const confirmPassword = ref('')
const showCurrent = ref(false)
const showNew = ref(false)
const showConfirm = ref(false)

/** 与文档「Password Requirements」一致：8+、大小写、数字、特殊字符 */
function newPasswordMeetsComplexity(p: string): boolean {
  if (p.length < 8) return false
  if (!/[A-Z]/.test(p)) return false
  if (!/[a-z]/.test(p)) return false
  if (!/[0-9]/.test(p)) return false
  if (!/[^A-Za-z0-9]/.test(p)) return false
  return true
}

const canSubmit = computed(() => {
  return currentPassword.value.length > 0
    && newPasswordMeetsComplexity(newPassword.value)
    && confirmPassword.value === newPassword.value
})

const goBack = () => {
  const pages = getCurrentPages()
  if (pages.length > 1) {
    uni.navigateBack()
  } else {
    uni.redirectTo({ url: '/pages/more/profile' })
  }
}

const handleSubmit = async () => {
  if (!canSubmit.value) return

  if (newPassword.value !== confirmPassword.value) {
    uni.showToast({ title: t('password.mismatch'), icon: 'none' })
    return
  }
  if (newPassword.value === currentPassword.value) {
    uni.showToast({ title: t('password.sameAsCurrent'), icon: 'none' })
    return
  }
  if (!newPasswordMeetsComplexity(newPassword.value)) {
    uni.showToast({
      title: t('password.notMeetRequirements'),
      icon: 'none',
    })
    return
  }

  uni.showLoading({ title: t('password.updating'), mask: true })
  try {
    await usAppChangePassword({
      currentPassword: currentPassword.value,
      newPassword: newPassword.value,
      confirmNewPassword: confirmPassword.value,
    })
    uni.hideLoading()
    uni.showToast({ title: t('password.updated'), icon: 'success' })
    setTimeout(() => {
      uni.navigateBack()
    }, 1200)
  } catch (e: unknown) {
    uni.hideLoading()
    const msg = e instanceof Error ? e.message : String(e)
    uni.showToast({ title: msg || t('password.updateFailed'), icon: 'none', duration: 2500 })
  }
}
</script>

<style scoped>
.page {
  min-height: 100vh;
  background: #f9fafb;
}

.header-hero {
  background: linear-gradient(135deg, var(--theme-primary), var(--theme-primary-dark));
  padding: 16rpx 32rpx 24rpx;
}

.top-bar {
  height: 96rpx;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.top-left,
.top-right {
  width: 64rpx;
  height: 64rpx;
  border-radius: 999rpx;
  background: rgba(255, 255, 255, 0.15);
  display: flex;
  align-items: center;
  justify-content: center;
}

.top-center {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.page-title {
  font-size: 34rpx;
  font-weight: 600;
  color: #fff;
}

.content {
  padding: 48rpx;
}

.lock-icon-wrap {
  display: flex;
  justify-content: center;
  margin-bottom: 40rpx;
}

.lock-circle {
  width: 120rpx;
  height: 120rpx;
  background: var(--theme-primary);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.form-card {
  background: #fff;
  padding: 32rpx;
  border-radius: 20rpx;
  margin-bottom: 32rpx;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.04);
}

.form-group {
  margin-bottom: 32rpx;
}

.form-group-last {
  margin-bottom: 0;
}

.label {
  font-size: 28rpx;
  font-weight: 500;
  color: #374151;
  display: block;
  margin-bottom: 12rpx;
}

.input-wrap {
  display: flex;
  align-items: center;
  background: #f3f4f6;
  border-radius: 16rpx;
  padding-right: 16rpx;
}

.input {
  flex: 1;
  height: 96rpx;
  padding: 0 24rpx;
  font-size: 30rpx;
  background: transparent;
}

.placeholder-cls {
  color: #9ca3af;
}

.eye-btn {
  width: 64rpx;
  height: 64rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.tips-card {
  background: #eff6ff;
  padding: 28rpx 32rpx;
  border-radius: 16rpx;
  margin-bottom: 40rpx;
}

.tips-title {
  font-size: 26rpx;
  font-weight: 600;
  color: var(--theme-primary);
  display: block;
  margin-bottom: 16rpx;
}

.tips-item {
  font-size: 24rpx;
  color: #4b5563;
  display: block;
  margin-bottom: 8rpx;
  padding-left: 20rpx;
}

.tips-last {
  margin-bottom: 0;
}

.submit-btn {
  width: 100%;
  height: 96rpx;
  background: var(--theme-primary);
  border-radius: 16rpx;
  display: flex;
  align-items: center;
  justify-content: center;
}

.submit-btn.disabled {
  opacity: 0.5;
}

.submit-btn-text {
  font-size: 32rpx;
  font-weight: 600;
  color: #ffffff;
  line-height: 1;
}
</style>
