<template>
  <el-dialog
    v-model="visible"
    :title="title"
    width="500px"
    class="payment-modal"
    :modal="true"
    :close-on-click-modal="false"
    :close-on-press-escape="true"
    @close="handleClose"
    center
  >
    <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">
            <el-icon><CreditCard /></el-icon>
          </div>
          <span>Alipay扫码支付</span>
        </div>
      </div>

      <!-- 金额显示 -->
      <div class="amount-section">
        <div class="amount-label">金额</div>
        <div class="amount-value">${{ amount }}</div>
      </div>

      <!-- 二维码区域 -->
      <div class="qr-section">
        <div class="qr-code">
          <img id="qr-code-img" style="display: none; width: 200px; height: 200px;" alt="支付二维码" />
          <div class="qr-placeholder">
            <div class="qr-grid">
              <div class="qr-dot" v-for="i in 64" :key="i"></div>
            </div>
          </div>
        </div>
        <div class="qr-tip">支付前请阅读《XX 付费服务协议》</div>
      </div>

      <!-- 支付提示 -->
      <div class="action-section">
        <div class="pay-tip">
          <p>请使用支付宝扫描上方二维码完成支付</p>
          <p class="tip-small">支付完成后页面将自动更新</p>
        </div>
      </div>

      <!-- 底部链接 -->
      <div class="footer-link">
        <a href="#" @click.prevent="showAgreement">《XX 付费服务协议》</a>
      </div>
    </div>
  </el-dialog>
</template>

<script setup>
import { ref, watch } from 'vue'
import { ElMessage } from 'element-plus'
import { CreditCard } from '@element-plus/icons-vue'
import { createPayment, createAlipayPayment } from '@/api/payments'

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 visible = ref(false)
const selectedMethod = ref('alipay')
const loading = ref(false)

// 监听 modelValue 变化
watch(() => props.modelValue, (newVal) => {
  visible.value = newVal
})

// 监听 visible 变化
watch(visible, (newVal) => {
  emit('update:modelValue', newVal)
})

// 选择支付方式
const selectMethod = (method) => {
  selectedMethod.value = method
}

// 处理支付
const handlePay = async () => {
  try {
    loading.value = true
    ElMessage.info('正在创建支付订单...')
    
    // 创建支付订单数据
    const paymentData = {
      orderId: props.orderId,
      amount: props.amount.toString(),
      method: 'ALIPAY',
      description: `${props.title} - 支付宝支付`
    }

    console.log('=== 开始支付流程 ===')
    console.log('支付数据：', paymentData)
    
    // 先创建支付记录
    console.log('1. 创建支付订单...')
    const createResponse = await createPayment(paymentData)
    console.log('创建支付订单响应：', createResponse)
    
        if (createResponse.data && createResponse.data.success) {
      const paymentId = createResponse.data.data.id
      console.log('2. 支付订单创建成功，ID：', paymentId)
      
      ElMessage.info('正在生成支付宝二维码...')
      
      console.log('3. 创建支付宝支付...')
      // 创建支付宝支付
      const alipayResponse = await createAlipayPayment({ paymentId })
      console.log('支付宝支付响应：', alipayResponse)
      console.log('支付宝支付响应数据：', alipayResponse.data)
      console.log('支付宝支付响应数据详情：', JSON.stringify(alipayResponse.data, null, 2))
      
      if (alipayResponse.data && alipayResponse.data.success) {
        // 显示二维码
        const qrCode = alipayResponse.data.data.qrCode
        console.log('4. 支付宝二维码：', qrCode)
        
        // 更新二维码显示
        const qrCodeElement = document.querySelector('#qr-code-img')
        if (qrCodeElement) {
          qrCodeElement.src = `https://api.qrserver.com/v1/create-qr-code/?size=200x200&data=${encodeURIComponent(qrCode)}`
          qrCodeElement.style.display = 'block'
          console.log('5. 二维码图片已设置')
        }
        
        // 隐藏模拟二维码
        const qrPlaceholder = document.querySelector('.qr-placeholder')
        if (qrPlaceholder) {
          qrPlaceholder.style.display = 'none'
          console.log('6. 模拟二维码已隐藏')
        }
        
        ElMessage.success('二维码已生成，请使用支付宝扫码支付')
        console.log('=== 支付流程完成 ===')
        
      } else {
        console.error('支付宝响应失败：', alipayResponse)
        ElMessage.error(alipayResponse.data?.message || '生成二维码失败')
        emit('pay-error', new Error(alipayResponse.data?.message || '生成二维码失败'))
      }
      
    } else {
      console.error('创建支付订单失败：', createResponse)
      ElMessage.error(createResponse.data?.message || '创建支付订单失败')
      emit('pay-error', new Error(createResponse.data?.message || '创建支付订单失败'))
    }
    
  } catch (error) {
    console.error('=== 支付流程出错 ===')
    console.error('错误详情：', error)
    console.error('错误响应：', error.response)
    console.error('错误状态：', error.response?.status)
    console.error('错误数据：', error.response?.data)
    
    ElMessage.error(`支付失败：${error.message || '请重试'}`)
    emit('pay-error', error)
  } finally {
    loading.value = false
  }
}

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

// 显示协议
const showAgreement = () => {
  ElMessage.info('服务协议页面')
}
</script>

<style scoped>
.payment-modal {
  background: #0a0a0a;
}

.payment-modal :deep(.el-dialog) {
  background: #0a0a0a;
  border-radius: 12px;
  border: none;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.6);
}

.payment-modal :deep(.el-dialog__header) {
  background: #0a0a0a;
  border-bottom: 1px solid #1a1a1a;
  padding: 20px 24px;
}

.payment-modal :deep(.el-dialog__title) {
  color: white;
  font-size: 18px;
  font-weight: 600;
}

.payment-modal :deep(.el-dialog__headerbtn) {
  color: #999;
}

.payment-modal :deep(.el-dialog__headerbtn:hover) {
  color: white;
}

.payment-content {
  padding: 24px;
  background: #0a0a0a;
  color: white;
}

/* 支付方式选择 */
.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);
}

.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;
}

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

.payment-method span {
  font-size: 14px;
  font-weight: 500;
}

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

.amount-label {
  font-size: 14px;
  color: #999;
  margin-bottom: 8px;
}

.amount-value {
  font-size: 32px;
  font-weight: 700;
  color: #e0e0e0;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}

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

.qr-code {
  width: 200px;
  height: 200px;
  margin: 0 auto 16px;
  background: #1a1a1a;
  border: none;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.3);
}

.qr-placeholder {
  width: 180px;
  height: 180px;
  background: linear-gradient(135deg, #1a1a1a 0%, #2a2a2a 100%);
  border-radius: 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: hidden;
}

.qr-placeholder::before {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  width: 60px;
  height: 60px;
  border: 3px solid #4a9eff;
  border-radius: 8px;
  transform: translate(-50%, -50%);
  opacity: 0.6;
  animation: pulse 2s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% {
    opacity: 0.6;
    transform: translate(-50%, -50%) scale(1);
  }
  50% {
    opacity: 0.8;
    transform: translate(-50%, -50%) scale(1.05);
  }
}

.qr-placeholder::after {
  content: '扫码支付';
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, calc(-50% + 40px));
  color: #4a9eff;
  font-size: 12px;
  font-weight: 500;
  opacity: 0.8;
}

.qr-tip {
  font-size: 12px;
  color: #999;
}

/* 操作按钮 */
.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;
  color: #e0e0e0;
  font-size: 14px;
}

.tip-small {
  color: #999 !important;
  font-size: 12px !important;
}

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

.footer-link a {
  color: #4a9eff;
  text-decoration: none;
  font-size: 12px;
  transition: color 0.3s ease;
}

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

/* 响应式设计 */
@media (max-width: 768px) {
  .payment-modal :deep(.el-dialog) {
    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>
