<template>
  <a-modal
    v-model:open="visible"
    :title="title"
    :width="500"
    class="payment-modal"
    :mask-closable="false"
    :keyboard="true"
    @cancel="handleClose"
    :footer="null"
    wrap-class-name="payment-modal-dialog"
  >
    <div class="payment-content">
      <!-- 支付方式选择 -->
      <div class="payment-methods">
        <div 
          class="payment-method" 
          :class="{ active: selectedMethod === 'alipay' }"
          @click="selectMethod('alipay')"
        >
          <div class="method-icon alipay-icon">
            <CreditCardOutlined />
          </div>
          <span>{{ $t('orders.alipay') }}</span>
        </div>
        <div 
          class="payment-method" 
          :class="{ active: selectedMethod === 'paypal' }"
          @click="selectMethod('paypal')"
        >
          <div class="method-icon paypal-icon">
            <CreditCardOutlined />
          </div>
          <span>{{ $t('orders.paypal') }}</span>
        </div>
      </div>

      <!-- 金额显示 -->
      <div class="amount-section">
        <div class="amount-label">{{ $t('orders.amount') }}</div>
        <div class="amount-value">${{ formatAmount(amount) }}</div>
      </div>

      <!-- 支付宝支付区域 -->
      <div v-if="selectedMethod === 'alipay'" class="alipay-section">
        <div class="alipay-info">
          <div class="alipay-logo">
            <svg width="100" height="32" viewBox="0 0 100 32" fill="none">
              <text x="0" y="24" font-family="Arial" font-size="20" font-weight="bold" fill="#1677FF">{{ $t('orders.alipay') }}</text>
            </svg>
          </div>
          <p class="alipay-desc">{{ $t('paymentModal.alipayDesc') }}</p>
          <p class="alipay-desc-small">{{ $t('paymentModal.alipayHint') }}</p>
        </div>
        <button 
          class="alipay-pay-button" 
          @click="handlePay"
          :disabled="loading"
        >
          <span v-if="!loading">{{ $t('paymentModal.goToAlipay') }}</span>
          <span v-else>{{ $t('paymentModal.redirecting') }}</span>
        </button>
      </div>

      <!-- PayPal支付按钮区域 -->
      <div v-if="selectedMethod === 'paypal'" class="paypal-section">
        <div class="paypal-info">
          <div class="paypal-logo">
            <svg width="100" height="32" viewBox="0 0 100 32" fill="none">
              <text x="0" y="24" font-family="Arial" font-size="20" font-weight="bold" fill="#0070BA">PayPal</text>
            </svg>
          </div>
          <p class="paypal-desc">{{ $t('paymentModal.paypalDesc') }}</p>
          <p class="paypal-desc-small">{{ $t('paymentModal.paypalHint') }}</p>
        </div>
        <button 
          class="paypal-pay-button" 
          @click="handlePay"
          :disabled="loading"
        >
          <span v-if="!loading">{{ $t('paymentModal.goToPaypal') }}</span>
          <span v-else>{{ $t('paymentModal.redirecting') }}</span>
        </button>
      </div>

      <!-- 底部链接 -->
      <div class="footer-link">
        <a href="#" @click.prevent="showAgreement">{{ $t('paymentModal.serviceTerms') }}</a>
      </div>
    </div>
  </a-modal>
</template>

<script setup>
import { ref, watch, onUnmounted } from 'vue'
import { useRouter } from 'vue-router'
import { message } from 'ant-design-vue'
import { CreditCardOutlined } from '@ant-design/icons-vue'
import { createPayment, createAlipayPayment, createPayPalPayment, getPaymentById, getPayPalPaymentStatus } from '@/api/payments'
import { useI18n } from 'vue-i18n'

const { t } = useI18n()

const props = defineProps({
  modelValue: {
    type: Boolean,
    default: false
  },
  title: {
    type: String,
    default: '标准版会员'
  },
  amount: {
    type: [String, Number],
    default: '32.00'
  },
  orderId: {
    type: String,
    default: ''
  }
})

const emit = defineEmits(['update:modelValue', 'pay-success', 'pay-error'])

const router = useRouter()
const visible = ref(false)
const selectedMethod = ref('alipay')
const loading = ref(false)
const currentPaymentId = ref(null)
let paymentPollingTimer = null
let isPaymentStarted = false // 防止重复调用

// 格式化金额，保留两位小数
const formatAmount = (amount) => {
  if (amount === null || amount === undefined) return '0.00'
  return Number(amount).toFixed(2)
}
let lastPlanType = '' // 记录上一次的套餐类型

// 从orderId中提取套餐类型（如 SUB_standard_xxx -> standard）
const getPlanTypeFromOrderId = (orderId) => {
  if (!orderId) return ''
  const parts = orderId.split('_')
  return parts.length >= 2 ? parts[1] : ''
}

// 监听 modelValue 变化
watch(() => props.modelValue, (newVal) => {
  visible.value = newVal
  // 当模态框打开时，检查是否需要创建新订单
  if (newVal) {
    const currentPlanType = getPlanTypeFromOrderId(props.orderId)
    
    // 如果套餐类型变化了，重置paymentId
    if (currentPlanType !== lastPlanType) {
      console.log('套餐变化，重置 paymentId，旧套餐:', lastPlanType, '新套餐:', currentPlanType)
      currentPaymentId.value = null
      lastPlanType = currentPlanType
    } else {
      console.log('同一套餐，复用 paymentId:', currentPaymentId.value)
    }
    
    // 重置状态
    isPaymentStarted = false
  }
  // 关闭时重置标志
  if (!newVal) {
    isPaymentStarted = false
  }
})

// 监听 visible 变化
watch(visible, (newVal) => {
  emit('update:modelValue', newVal)
  // 如果模态框关闭，停止轮询
  if (!newVal) {
    stopPaymentPolling()
  }
})

// 选择支付方式
const selectMethod = async (method) => {
  if (selectedMethod.value !== method) {
    console.log('切换支付方式:', selectedMethod.value, '->', method, '复用 paymentId:', currentPaymentId.value)
    selectedMethod.value = method
    
    // 如果已有支付记录，更新支付方式和描述（复用paymentId）
    if (currentPaymentId.value) {
      try {
        const newDescription = `${props.title} - ${method === 'paypal' ? 'PayPal' : '支付宝'}支付`
        const newMethod = method === 'paypal' ? 'PAYPAL' : 'ALIPAY'
        
        console.log('=== 开始更新支付方式 ===')
        console.log('paymentId:', currentPaymentId.value)
        console.log('newMethod:', newMethod)
        console.log('newDescription:', newDescription)
        
        const response = await fetch(`/api/payments/${currentPaymentId.value}/method`, {
          method: 'PUT',
          headers: {
            'Content-Type': 'application/json',
            'Authorization': `Bearer ${localStorage.getItem('token')}`
          },
          body: JSON.stringify({
            method: newMethod,
            description: newDescription
          })
        })
        
        console.log('API响应状态:', response.status)
        const responseData = await response.json()
        console.log('API响应数据:', responseData)
        
        if (response.ok && responseData.success) {
          console.log('✅ 支付方式更新成功，复用paymentId:', currentPaymentId.value)
        } else {
          console.error('❌ 支付方式更新失败:', responseData.message || response.statusText)
        }
      } catch (error) {
        console.error('❌ 更新支付方式异常:', error)
      }
    }
  }
}

// 处理支付
const handlePay = async () => {
  try {
    loading.value = true
    
    // 如果还没有创建支付记录，先创建
    if (!currentPaymentId.value) {
      // 直接使用传入的orderId，不再添加时间戳（Subscription.vue已经处理了）
      const paymentData = {
        orderId: props.orderId,
        amount: props.amount.toString(),
        method: selectedMethod.value === 'paypal' ? 'PAYPAL' : 'ALIPAY',
        description: `${props.title} - ${selectedMethod.value === 'paypal' ? 'PayPal' : '支付宝'}支付`
      }
      
      console.log('=== 创建支付记录 ===')
      console.log('支付数据：', paymentData)
      
      const createResponse = await createPayment(paymentData)
      console.log('创建支付订单响应：', createResponse)
      
      if (createResponse.data && createResponse.data.success) {
        currentPaymentId.value = createResponse.data.data.id
        console.log('支付记录创建成功，ID：', currentPaymentId.value)
      } else {
        throw new Error(createResponse.data?.message || t('common.createPaymentRecordFailed'))
      }
    } else {
      // 已有支付记录，确保支付方式和描述与当前选择一致
      try {
        const newDescription = `${props.title} - ${selectedMethod.value === 'paypal' ? 'PayPal' : '支付宝'}支付`
        const newMethod = selectedMethod.value === 'paypal' ? 'PAYPAL' : 'ALIPAY'
        
        await fetch(`/api/payments/${currentPaymentId.value}/method`, {
          method: 'PUT',
          headers: {
            'Content-Type': 'application/json',
            'Authorization': `Bearer ${localStorage.getItem('token')}`
          },
          body: JSON.stringify({
            method: newMethod,
            description: newDescription
          })
        })
        console.log('支付方式已同步:', newMethod)
      } catch (e) {
        console.warn('同步支付方式失败，继续支付:', e)
      }
    }
    
    // 根据选择的支付方式处理
    if (selectedMethod.value === 'paypal') {
      await handlePayPalPayment()
    } else {
      await handleAlipayPayment()
    }
    
  } catch (error) {
    console.error('=== 支付流程出错 ===')
    console.error('错误详情：', error)
    console.error('错误响应：', error.response)
    console.error('错误状态：', error.response?.status)
    console.error('错误数据：', error.response?.data)
    
    message.error(t('common.paymentFailed', { message: error.message || t('common.pleaseTryAgain') }))
    emit('pay-error', error)
  } finally {
    loading.value = false
  }
}

// 处理支付宝支付
const handleAlipayPayment = async () => {
  message.info(t('common.creatingPaypalPayment'))
  
  const paymentId = currentPaymentId.value
  console.log('=== 开始支付宝支付流程 ===')
  console.log('使用已创建的支付ID：', paymentId)
  
  // 创建支付宝支付
  const alipayResponse = await createAlipayPayment({ paymentId })
  console.log('支付宝支付响应：', alipayResponse)
  
  if (alipayResponse.data && alipayResponse.data.success) {
    const responseData = alipayResponse.data.data
    
    // 检查是否是电脑网页支付（返回HTML表单）
    if (responseData.payType === 'PAGE_PAY' && responseData.payForm) {
      console.log('使用电脑网页支付，跳转到支付宝页面')
      message.success(t('common.redirectingToAlipay'))
      
      // 创建一个临时的div来渲染表单并自动提交
      const div = document.createElement('div')
      div.innerHTML = responseData.payForm
      document.body.appendChild(div)
      
      // 找到表单并提交
      const form = div.querySelector('form')
      if (form) {
        form.submit()
      } else {
        console.error('未找到支付表单')
        message.error(t('common.paymentPageGenerateFailed'))
        document.body.removeChild(div)
      }
      return
    }
    
    console.error('支付宝响应格式未知：', responseData)
    message.error(t('common.paymentCreateFailedRetry'))
  } else {
    console.error('支付宝响应失败：', alipayResponse)
    message.error(alipayResponse.data?.message || t('common.paymentCreateFailedRetry'))
    emit('pay-error', new Error(alipayResponse.data?.message || t('common.paymentCreateFailedRetry')))
  }
}

// 处理PayPal支付
const handlePayPalPayment = async () => {
  message.info(t('common.creatingPaypalPayment'))
  
  const paymentId = currentPaymentId.value
  console.log('=== 开始PayPal支付流程 ===')
  console.log('使用已创建的支付ID：', paymentId)
  
  const response = await createPayPalPayment({ paymentId })
  console.log('PayPal支付响应：', response)
  
  if (response.data && response.data.success) {
    const paymentUrl = response.data.paymentUrl
    
    console.log('PayPal支付URL:', paymentUrl)
    message.success(t('common.redirectingToPaypal'))
    
    // 跳转到PayPal支付页面
    window.location.href = paymentUrl
  } else {
    console.error('PayPal支付创建失败：', response)
    message.error(response.data?.message || t('common.paypalPaymentCreateFailed'))
    emit('pay-error', new Error(response.data?.message || t('common.paypalPaymentCreateFailed')))
  }
}

// 轮询支付状态
const startPaymentPolling = (paymentId) => {
  // 清除之前的轮询
  stopPaymentPolling()
  
  let pollCount = 0
  const maxPolls = 200 // 最多轮询200次（10分钟，每3秒一次）
  const pollInterval = 3000 // 3秒轮询一次，更快响应支付成功
  
  const poll = async () => {
    if (pollCount >= maxPolls) {
      console.log('轮询达到最大次数，停止轮询')
      stopPaymentPolling()
      return
    }
    
    try {
      console.log(`轮询支付状态 (${pollCount + 1}/${maxPolls})，支付ID:`, paymentId)
      const response = await getPaymentById(paymentId)
      console.log('轮询响应:', response.data)
      
      if (response.data && response.data.success) {
        const payment = response.data.data
        // 兼容枚举可能是字符串或对象的情况
        const rawStatus = payment.status
        // 更全面地解析状态：可能是字符串、对象、或者对象的属性
        let status = rawStatus
        if (typeof rawStatus === 'object' && rawStatus !== null) {
          status = rawStatus.name || rawStatus.value || rawStatus.toString()
        }
        // 转为大写以便比较
        const statusUpper = String(status).toUpperCase()
        console.log('支付状态原始值:', rawStatus, '类型:', typeof rawStatus, '解析后:', status, '大写:', statusUpper)
        
        if (statusUpper === 'SUCCESS' || statusUpper === 'COMPLETED') {
          console.log('✅ 支付成功！支付数据:', payment)
          stopPaymentPolling()
          message.success(t('common.paymentSuccessful'))
          emit('pay-success', payment)
          // 重置状态，下次购买生成新订单
          currentPaymentId.value = null
          lastPlanType = ''
          // 延迟关闭模态框
          setTimeout(() => {
            visible.value = false
          }, 2000)
          return
        } else if (statusUpper === 'FAILED' || statusUpper === 'CANCELLED') {
          console.log('支付失败或取消')
          stopPaymentPolling()
          message.warning(t('common.paymentCancelledOrFailed'))
          emit('pay-error', new Error(t('common.paymentCancelledOrFailed')))
          return
        } else if (statusUpper === 'PROCESSING') {
          console.log('支付处理中...')
          // PROCESSING 状态继续轮询
        } else if (statusUpper === 'PENDING') {
          console.log('支付待处理中（等待支付宝回调）...')
          // PENDING 状态继续轮询
        } else {
          console.log('未知状态:', statusUpper, '继续轮询...')
        }
      } else {
        console.warn('轮询响应失败:', response.data)
      }
      
      // 继续轮询
      pollCount++
      paymentPollingTimer = setTimeout(poll, pollInterval)
    } catch (error) {
      console.error('轮询支付状态失败:', error)
      // 错误时也继续轮询，直到达到最大次数
      pollCount++
      if (pollCount < maxPolls) {
        paymentPollingTimer = setTimeout(poll, pollInterval)
      }
    }
  }
  
  // 开始轮询（等待2秒后开始第一次轮询，更快响应）
  setTimeout(() => {
    poll()
  }, 2000)
}

// 停止轮询支付状态
const stopPaymentPolling = () => {
  if (paymentPollingTimer) {
    clearTimeout(paymentPollingTimer)
    paymentPollingTimer = null
    console.log('已停止轮询支付状态')
  }
}

// 手动检查支付状态
const manualCheckPayment = async () => {
  if (!currentPaymentId.value) {
    message.warning(t('common.pleaseGenerateQrFirst'))
    return
  }
  
  message.info(t('common.queryingPaymentStatus'))
  
  try {
    const response = await getPaymentById(currentPaymentId.value)
    
    if (response.data && response.data.success) {
      const payment = response.data.data
      // 兼容枚举可能是字符串或对象的情况
      const rawStatus = payment.status
      // 更全面地解析状态
      let status = rawStatus
      if (typeof rawStatus === 'object' && rawStatus !== null) {
        status = rawStatus.name || rawStatus.value || rawStatus.toString()
      }
      const statusUpper = String(status).toUpperCase()
      console.log('手动查询支付状态原始值:', rawStatus, '类型:', typeof rawStatus, '解析后:', status, '大写:', statusUpper)
      
      if (statusUpper === 'SUCCESS' || statusUpper === 'COMPLETED') {
        stopPaymentPolling()
        message.success(t('common.paymentSuccessful'))
        emit('pay-success', payment)
        // 重置状态，下次购买生成新订单
        currentPaymentId.value = null
        lastPlanType = ''
        setTimeout(() => {
          visible.value = false
        }, 1500)
      } else if (statusUpper === 'FAILED' || statusUpper === 'CANCELLED') {
        message.warning(t('common.paymentCancelledOrFailed'))
      } else {
        message.info(t('common.paymentNotCompleted', { status: statusUpper }))
      }
    }
  } catch (error) {
    console.error('查询支付状态失败:', error)
    message.error(t('common.queryFailed'))
  }
}

// 关闭模态框
const handleClose = () => {
  stopPaymentPolling()
  visible.value = false
}

// 组件卸载时清理轮询
onUnmounted(() => {
  stopPaymentPolling()
})

// 获取支付状态描述
const getStatusDescription = (status) => {
  const statusMap = {
    'PENDING': t('paymentStatus.pending'),
    'PROCESSING': t('paymentStatus.processing'),
    'SUCCESS': t('paymentStatus.success'),
    'COMPLETED': t('paymentStatus.completed'),
    'FAILED': t('paymentStatus.failed'),
    'CANCELLED': t('paymentStatus.cancelled'),
    'REFUNDED': t('paymentStatus.refunded')
  }
  return statusMap[status] || t('common.unknownStatus')
}

// 显示协议
const showAgreement = () => {
  router.push('/terms-of-service')
}
</script>

<style scoped>
.payment-modal {
  background: #000000 !important;
}

/* 移除所有可能的白边和边框 */
.payment-modal :deep(.ant-modal),
.payment-modal-dialog,
.payment-modal :deep(.ant-modal.ant-modal--center.payment-modal),
.payment-modal :deep(.ant-modal.ant-modal--center),
.payment-modal.ant-modal.ant-modal--center.payment-modal,
.payment-modal :deep(.ant-modal.ant-modal--center.payment-modal-dialog),
.payment-modal :deep(.payment-modal-dialog.ant-modal.ant-modal--center.payment-modal) {
  background: #000000 !important;
  background-color: #000000 !important;
  background-image: none !important;
  border-radius: 12px !important;
  border: none !important;
  border-width: 0 !important;
  border-style: none !important;
  border-color: transparent !important;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.6), inset 0 0 0 0 transparent !important;
  padding: 0 !important;
  margin: 0 !important;
  outline: none !important;
  outline-width: 0 !important;
  outline-style: none !important;
  outline-color: transparent !important;
  box-sizing: border-box !important;
  overflow: hidden !important;
  /* 移除所有可能的白色边框 */
  -webkit-box-shadow: 0 8px 32px rgba(0, 0, 0, 0.6), inset 0 0 0 0 transparent !important;
  -moz-box-shadow: 0 8px 32px rgba(0, 0, 0, 0.6), inset 0 0 0 0 transparent !important;
}

/* 移除所有伪元素可能产生的边框 */
.payment-modal :deep(.ant-modal::before),
.payment-modal :deep(.ant-modal::after),
.payment-modal :deep(.ant-modal-wrap::before),
.payment-modal :deep(.ant-modal-wrap::after),
.payment-modal :deep(.ant-modal-body::before),
.payment-modal :deep(.ant-modal-body::after) {
  display: none !important;
  content: none !important;
  border: none !important;
  outline: none !important;
  box-shadow: none !important;
}

/* 移除所有可能的白色边框 - 使用更具体的选择器 */
.payment-modal :deep(.ant-modal),
.payment-modal :deep(.ant-modal *) {
  border-left: none !important;
  border-right: none !important;
  border-top: none !important;
  border-bottom: none !important;
}

.payment-modal :deep(.ant-modal-body) {
  padding: 0 !important;
  margin: 0 !important;
  background: #000000 !important;
  background-color: #000000 !important;
  border: none !important;
  border-width: 0 !important;
  border-style: none !important;
  border-color: transparent !important;
  outline: none !important;
  outline-width: 0 !important;
  outline-style: none !important;
  outline-color: transparent !important;
  overflow: hidden !important;
  box-sizing: border-box !important;
}

/* 移除对话框内部的所有边框和间隙 */
.payment-modal :deep(.ant-modal) * {
  border-color: transparent !important;
}

/* 确保对话框本身没有任何白色背景或边框 */
.payment-modal :deep(.ant-modal),
.payment-modal :deep(.ant-modal.ant-modal--center.payment-modal),
.payment-modal :deep(.ant-modal.ant-modal--center),
.payment-modal :deep(.payment-modal-dialog),
.payment-modal :deep(.payment-modal-dialog.ant-modal),
.payment-modal :deep(.payment-modal-dialog.ant-modal--center),
.payment-modal :deep(.payment-modal-dialog.ant-modal--center.payment-modal) {
  background-color: #000000 !important;
  background: #000000 !important;
  background-image: none !important;
}

/* 移除所有可能的白色边框 */
.payment-modal :deep(.ant-modal),
.payment-modal :deep(.ant-modal-body) {
  border: 0 !important;
  border-top: none !important;
  border-bottom: none !important;
  border-left: none !important;
  border-right: none !important;
  border-width: 0 !important;
  border-style: none !important;
  border-color: transparent !important;
}

.payment-modal :deep(.ant-modal-header),
.payment-modal :deep(.ant-modal-header.show-close) {
  background: #000000 !important;
  background-color: #000000 !important;
  border: none !important;
  border-bottom: 1px solid #1a1a1a !important;
  border-top: none !important;
  border-left: none !important;
  border-right: none !important;
  border-width: 0 !important;
  border-bottom-width: 1px !important;
  border-bottom-style: solid !important;
  border-bottom-color: #1a1a1a !important;
  padding: 20px 24px !important;
  margin: 0 !important;
  color: white !important;
  text-align: left !important;
}

/* 确保 header.show-close 内的所有文字都是白色 */
.payment-modal :deep(.ant-modal-header.show-close),
.payment-modal :deep(.ant-modal-header.show-close *),
.payment-modal :deep(.ant-modal-header.show-close .ant-modal-title),
.payment-modal :deep(.ant-modal-header.show-close .ant-modal-close) {
  color: white !important;
}

/* 确保关闭按钮区域背景也与模态框一致 */
.payment-modal :deep(.ant-modal-close),
.payment-modal :deep(.ant-modal-close.is-close),
.payment-modal :deep(.ant-modal-header .ant-modal-close) {
  background: transparent !important;
  background-color: transparent !important;
}

.payment-modal :deep(.ant-modal-close:hover) {
  background: rgba(255, 255, 255, 0.1) !important;
  background-color: rgba(255, 255, 255, 0.1) !important;
}

.payment-modal :deep(.ant-modal-wrap) {
  padding: 0 !important;
  margin: 0 !important;
  border: none !important;
  border-width: 0 !important;
  border-style: none !important;
  border-color: transparent !important;
  outline: none !important;
  outline-width: 0 !important;
  outline-style: none !important;
  outline-color: transparent !important;
  background: transparent !important;
}

/* 确保 wrapper 内的所有对话框元素背景为黑色 */
.payment-modal :deep(.ant-modal-wrap .ant-modal),
.payment-modal :deep(.ant-modal-wrap .ant-modal.ant-modal--center),
.payment-modal :deep(.ant-modal-wrap .ant-modal.ant-modal--center.payment-modal),
.payment-modal :deep(.ant-modal-wrap .payment-modal-dialog) {
  background: #000000 !important;
  background-color: #000000 !important;
}

.payment-modal :deep(.ant-modal-mask),
.payment-modal :deep(.payment-modal-overlay),
.payment-modal :deep(.ant-modal-mask.payment-modal-overlay),
.payment-modal :deep(.ant-modal-mask.payment-modal-overlay) {
  background: rgba(0, 0, 0, 0.5) !important;
  border: none !important;
  outline: none !important;
}

/* 确保遮罩层内的对话框背景为黑色 */
.payment-modal :deep(.ant-modal-wrap .ant-modal),
.payment-modal :deep(.ant-modal-wrap .ant-modal-body),
.payment-modal :deep(.ant-modal-wrap .ant-modal-header) {
  background: #000000 !important;
  background-color: #000000 !important;
}

.payment-modal :deep(.ant-modal-wrap) {
  background: rgba(0, 0, 0, 0.5) !important;
  border: none !important;
  outline: none !important;
}

/* 确保 el-overlay-dialog 内的对话框背景为黑色 */
.payment-modal :deep(.ant-modal-wrap .ant-modal),
.payment-modal :deep(.ant-modal-wrap .ant-modal.ant-modal--center),
.payment-modal :deep(.ant-modal-wrap .ant-modal.ant-modal--center.payment-modal),
.payment-modal :deep(.ant-modal-wrap .ant-modal-body),
.payment-modal :deep(.ant-modal-wrap .ant-modal-header) {
  background: #000000 !important;
  background-color: #000000 !important;
}

/* 确保遮罩层没有白边 */
.payment-modal :deep(.ant-modal-mask.is-message-box) {
  background: rgba(0, 0, 0, 0.5) !important;
}

.payment-modal :deep(.ant-modal-title) {
  color: #ffffff !important;
  font-size: 18px !important;
  font-weight: bold !important;
  font-family: "SimSun", "宋体", serif !important;
  text-align: left !important;
  line-height: 1.5 !important;
  letter-spacing: 0.5px !important;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.8), 0 0 10px rgba(255, 255, 255, 0.3) !important;
  filter: brightness(1.1) contrast(1.2) !important;
}

.payment-modal :deep(.ant-modal-close) {
  color: white !important;
}

.payment-modal :deep(.ant-modal-close:hover) {
  color: white !important;
}

.payment-modal :deep(.ant-modal-close svg) {
  color: white !important;
  fill: white !important;
}

.payment-content {
  padding: 24px;
  background: #000000 !important;
  background-color: #000000 !important;
  color: white !important;
  margin: 0 !important;
  border: none !important;
  border-width: 0 !important;
  box-sizing: border-box !important;
}

/* 确保 payment-content 内的所有文字都是白色（链接除外） */
.payment-content,
.payment-content *:not(a),
.payment-content p,
.payment-content span,
.payment-content div {
  color: white !important;
}

/* 支付方式选择 */
.payment-methods {
  display: flex;
  gap: 12px;
  margin-bottom: 24px;
}

.payment-method {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 16px;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s ease;
  background: #1a1a1a;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
  color: white;
}

.payment-method:hover {
  background: #2a2a2a;
  box-shadow: 0 4px 16px rgba(74, 158, 255, 0.2);
}

.payment-method.active {
  background: linear-gradient(135deg, #4a9eff 0%, #3a8bdf 100%);
  color: white;
  box-shadow: 0 4px 16px rgba(74, 158, 255, 0.3);
}

.method-icon {
  width: 24px;
  height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #1677FF;
}

.paypal-icon {
  color: #0070BA;
}

.payment-method.active .method-icon {
  color: white;
}

.payment-method span {
  font-size: 14px !important;
  font-weight: 500 !important;
  color: white !important;
  line-height: 1.5 !important;
  letter-spacing: 0.3px !important;
}

/* 金额显示 */
.amount-section {
  text-align: center;
  margin-bottom: 24px;
}

.amount-label {
  font-size: 14px !important;
  color: white !important;
  margin-bottom: 8px !important;
  font-weight: 400 !important;
  line-height: 1.5 !important;
  letter-spacing: 0.3px !important;
}

.amount-value {
  font-size: 32px !important;
  font-weight: 700 !important;
  color: white !important;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3) !important;
  line-height: 1.2 !important;
  letter-spacing: 0.5px !important;
}

/* PayPal支付区域 */
.paypal-section {
  text-align: center;
  margin-bottom: 24px;
  padding: 32px 24px;
  background: linear-gradient(135deg, #1a1a1a 0%, #2a2a2a 100%);
  border-radius: 12px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.3);
}

.paypal-info {
  margin-bottom: 24px;
}

.paypal-logo {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-bottom: 16px;
}

.paypal-desc {
  color: white !important;
  font-size: 16px !important;
  font-weight: 500 !important;
  margin: 8px 0 !important;
  line-height: 1.5 !important;
}

.paypal-desc-small {
  color: rgba(255, 255, 255, 0.7) !important;
  font-size: 13px !important;
  font-weight: 400 !important;
  margin: 4px 0 !important;
  line-height: 1.5 !important;
}

.paypal-pay-button {
  width: 100%;
  padding: 16px 24px;
  background: linear-gradient(135deg, #0070BA 0%, #005EA6 100%);
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 4px 12px rgba(0, 112, 186, 0.3);
}

.paypal-pay-button:hover:not(:disabled) {
  background: linear-gradient(135deg, #005EA6 0%, #004A85 100%);
  transform: translateY(-2px);
  box-shadow: 0 6px 16px rgba(0, 112, 186, 0.4);
}

.paypal-pay-button:active:not(:disabled) {
  transform: translateY(0);
}

.paypal-pay-button:disabled {
  background: #666;
  cursor: not-allowed;
  opacity: 0.6;
  transform: none;
}

/* 支付宝支付区域 */
.alipay-section {
  text-align: center;
  margin-bottom: 24px;
  padding: 32px 24px;
  background: linear-gradient(135deg, #1a1a1a 0%, #2a2a2a 100%);
  border-radius: 12px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.3);
}

.alipay-info {
  margin-bottom: 24px;
}

.alipay-logo {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-bottom: 16px;
}

.alipay-desc {
  color: white !important;
  font-size: 16px !important;
  font-weight: 500 !important;
  margin: 8px 0 !important;
  line-height: 1.5 !important;
}

.alipay-desc-small {
  color: rgba(255, 255, 255, 0.7) !important;
  font-size: 13px !important;
  font-weight: 400 !important;
  margin: 4px 0 !important;
  line-height: 1.5 !important;
}

.alipay-pay-button {
  width: 100%;
  padding: 16px 24px;
  background: linear-gradient(135deg, #1677FF 0%, #0958d9 100%);
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 4px 12px rgba(22, 119, 255, 0.3);
}

.alipay-pay-button:hover:not(:disabled) {
  background: linear-gradient(135deg, #0958d9 0%, #003eb3 100%);
  transform: translateY(-2px);
  box-shadow: 0 6px 16px rgba(22, 119, 255, 0.4);
}

.alipay-pay-button:active:not(:disabled) {
  transform: translateY(0);
}

.alipay-pay-button:disabled {
  background: #666;
  cursor: not-allowed;
  opacity: 0.6;
  transform: none;
}

/* 二维码区域 */
.qr-section {
  text-align: center;
  margin-bottom: 24px;
}

.qr-code {
  width: 200px;
  height: 200px;
  margin: 0 auto 16px;
  padding: 0;
  background: #1a1a1a;
  border: none;
  border-radius: 8px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.3);
  overflow: hidden;
  box-sizing: border-box;
  position: relative;
  line-height: 0;
}

.qr-code img {
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
  border: none;
  display: block;
  object-fit: contain;
  object-position: center;
  background: #1a1a1a;
  box-sizing: border-box;
  image-rendering: -webkit-optimize-contrast;
  image-rendering: crisp-edges;
}

.qr-placeholder {
  width: 200px;
  height: 200px;
  background: transparent;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: hidden;
}

.qr-placeholder svg {
  width: 100%;
  height: 100%;
  border-radius: 10px;
}

.qr-tip {
  font-size: 12px !important;
  color: white !important;
  line-height: 1.5 !important;
  letter-spacing: 0.2px !important;
  font-weight: 400 !important;
}

/* 操作按钮 */
.action-section {
  margin-bottom: 16px;
}

.pay-button {
  width: 100%;
  padding: 14px;
  background: #4a9eff;
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
}

.pay-button:hover {
  background: #3a8bdf;
  transform: translateY(-1px);
}

.pay-button:active {
  transform: translateY(0);
}

.pay-button:disabled {
  background: #666;
  cursor: not-allowed;
  transform: none;
}

.pay-button:disabled:hover {
  background: #666;
  transform: none;
}

/* 支付提示 */
.pay-tip {
  text-align: center;
  padding: 20px;
  background: #1a1a1a;
  border-radius: 8px;
  border: none;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
}

.pay-tip p {
  margin: 8px 0 !important;
  color: white !important;
  font-size: 14px !important;
  line-height: 1.6 !important;
  letter-spacing: 0.3px !important;
  font-weight: 400 !important;
}

.tip-small {
  color: white !important;
  font-size: 12px !important;
  line-height: 1.5 !important;
  letter-spacing: 0.2px !important;
  font-weight: 400 !important;
  opacity: 0.8 !important;
}

/* 底部链接 */
.footer-link {
  text-align: center;
}

.footer-link a {
  color: #4a9eff !important;
  text-decoration: none !important;
  font-size: 12px !important;
  transition: color 0.3s ease !important;
  line-height: 1.5 !important;
  letter-spacing: 0.2px !important;
  font-weight: 400 !important;
}

.footer-link a:hover {
  color: #3a8bdf !important;
}

/* 我已完成支付按钮 */
.check-payment-btn {
  width: 100%;
  padding: 12px 24px;
  margin-top: 16px;
  background: linear-gradient(135deg, #10b981 0%, #059669 100%);
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 4px 12px rgba(16, 185, 129, 0.3);
}

.check-payment-btn:hover:not(:disabled) {
  background: linear-gradient(135deg, #059669 0%, #047857 100%);
  transform: translateY(-2px);
  box-shadow: 0 6px 16px rgba(16, 185, 129, 0.4);
}

.check-payment-btn:disabled {
  background: #4a4a4a;
  cursor: not-allowed;
  opacity: 0.6;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .payment-modal :deep(.ant-modal) {
    width: 90%;
    margin: 0 auto;
  }
  
  .payment-methods {
    flex-direction: column;
  }
  
  .qr-code {
    width: 160px;
    height: 160px;
  }
  
  .qr-placeholder {
    width: 140px;
    height: 140px;
  }
  
  .amount-value {
    font-size: 28px;
  }
}
</style>
