<template>
  <div class="payments">
    <!-- 页面标题 -->
    <div class="page-header">
      <h2>
        <CreditCardOutlined />
        {{ $t('payments.title') }}
      </h2>
    </div>

    <!-- 筛选和搜索 -->
    <a-card class="filter-card">
      <a-row :gutter="20">
        <a-col :xs="24" :sm="12" :md="8">
          <a-select
            v-model="filters.status"
            :placeholder="$t('payments.statusPlaceholder')"
            clearable
            @change="handleFilterChange"
          >
            <a-select-option value="">{{ $t('payments.allStatus') }}</a-select-option>
            <a-select-option value="PENDING">{{ $t('payments.pending') }}</a-select-option>
            <a-select-option value="SUCCESS">{{ $t('payments.paid') }}</a-select-option>
            <a-select-option value="FAILED">{{ $t('payments.failed') }}</a-select-option>
            <a-select-option value="CANCELLED">{{ $t('payments.cancelled') }}</a-select-option>
          </a-select>
        </a-col>
        <a-col :xs="24" :sm="12" :md="8">
          <a-input
            v-model="filters.search"
            :placeholder="$t('payments.searchPlaceholder')"
            clearable
            @input="handleSearch"
          >
            <template #prefix>
              <SearchOutlined />
            </template>
          </a-input>
        </a-col>
        <a-col :xs="24" :sm="12" :md="8">
          <a-button @click="resetFilters">{{ $t('common.reset') }}</a-button>
          <a-button type="success" @click="showSubscriptionDialog('standard')">{{ $t('subscription.standard') }}</a-button>
          <a-button type="warning" @click="showSubscriptionDialog('professional')">{{ $t('subscription.professional') }}</a-button>
        </a-col>
      </a-row>
    </a-card>

    <!-- 支付记录列表 -->
    <a-card class="payments-card">
      <a-table
        :data="payments"
        :spinning="loading"
        :empty-text="$t('subscription.noPointsHistory')"
      >
        <a-table-column prop="orderId" :label="$t('orders.orderNumber')" width="150">
          <template #default="{ row }">
            <router-link :to="`/orders/${row.orderId}`" class="order-link">
              {{ row.orderId }}
            </router-link>
          </template>
        </a-table-column>
        
        <a-table-column prop="amount" :label="$t('orders.amount')" width="120">
          <template #default="{ row }">
            <span class="amount">{{ row.currency }} {{ row.amount }}</span>
          </template>
        </a-table-column>
        
        <a-table-column prop="paymentMethod" :label="$t('orders.paymentMethod')" width="120">
          <template #default="{ row }">
            <a-tag :type="getPaymentMethodType(row.paymentMethod)">
              {{ getPaymentMethodText(row.paymentMethod) }}
            </a-tag>
          </template>
        </a-table-column>
        
        <a-table-column prop="status" :label="$t('orders.status')" width="120">
          <template #default="{ row }">
            <a-tag :type="getStatusType(row.status)">
              {{ getStatusText(row.status) }}
            </a-tag>
          </template>
        </a-table-column>
        
        <a-table-column prop="description" :label="$t('orders.description')" min-width="200">
          <template #default="{ row }">
            <span class="description">{{ row.description }}</span>
          </template>
        </a-table-column>
        
        <a-table-column prop="createdAt" :label="$t('orders.createTime')" width="160">
          <template #default="{ row }">
            {{ formatDate(row.createdAt) }}
          </template>
        </a-table-column>
        
        <a-table-column prop="paidAt" :label="$t('orders.paidTime')" width="160">
          <template #default="{ row }">
            {{ row.paidAt ? formatDate(row.paidAt) : '-' }}
          </template>
        </a-table-column>
        
        <a-table-column :label="$t('orders.operation')" width="280" fixed="right">
          <template #default="{ row }">
            <a-button
              size="small"
              @click="viewPaymentDetail(row)"
            >
              {{ $t('common.view') }}
            </a-button>
            <a-button
              v-if="row.status === 'PENDING'"
              size="small"
              type="success"
              @click="testPaymentComplete(row)"
            >
              {{ $t('common.confirmTest') }}
            </a-button>
            <a-button
              size="small"
              type="danger"
              @click="handleDeletePayment(row)"
            >
              {{ $t('common.delete') }}
            </a-button>
          </template>
        </a-table-column>
      </a-table>

      <!-- 分页 -->
      <div class="pagination-container">
        <a-pagination
          v-model:current-page="pagination.page"
          v-model:page-size="pagination.size"
          :page-sizes="[10, 20, 50, 100]"
          :total="pagination.total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </a-card>

    <!-- 支付详情对话框 -->
    <a-modal
      v-model:open="detailDialogVisible"
      :title="$t('payments.paymentDetail')"
      width="600px"
    >
      <div v-if="currentPayment">
        <a-descriptions :column="2" border>
          <a-descriptions-item :label="$t('orders.orderNumber')">{{ currentPayment.orderId }}</a-descriptions-item>
          <a-descriptions-item :label="$t('orders.paymentMethod')">
            <a-tag :type="getPaymentMethodType(currentPayment.paymentMethod)">
              {{ getPaymentMethodText(currentPayment.paymentMethod) }}
            </a-tag>
          </a-descriptions-item>
          <a-descriptions-item :label="$t('orders.amount')">
            <span class="amount">{{ currentPayment.currency }} {{ currentPayment.amount }}</span>
          </a-descriptions-item>
          <a-descriptions-item :label="$t('orders.status')">
            <a-tag :type="getStatusType(currentPayment.status)">
              {{ getStatusText(currentPayment.status) }}
            </a-tag>
          </a-descriptions-item>
          <a-descriptions-item :label="$t('payments.externalTransactionId')" v-if="currentPayment.externalTransactionId">
            {{ currentPayment.externalTransactionId }}
          </a-descriptions-item>
          <a-descriptions-item :label="$t('orders.createTime')">{{ formatDate(currentPayment.createdAt) }}</a-descriptions-item>
          <a-descriptions-item :label="$t('orders.paidTime')" v-if="currentPayment.paidAt">
            {{ formatDate(currentPayment.paidAt) }}
          </a-descriptions-item>
          <a-descriptions-item :label="$t('payments.updateTime')">{{ formatDate(currentPayment.updatedAt) }}</a-descriptions-item>
        </a-descriptions>
        
        <div v-if="currentPayment.description" class="payment-description">
          <h4>{{ $t('orders.description') }}</h4>
          <p>{{ currentPayment.description }}</p>
        </div>
      </div>
    </a-modal>

    <!-- 订阅对话框 -->
    <a-modal
      v-model:open="subscriptionDialogVisible"
      :title="subscriptionDialogTitle"
      width="500px"
    >
      <div class="subscription-info">
        <h3>{{ subscriptionInfo.title }}</h3>
        <p class="price">${{ subscriptionInfo.price }}</p>
        <p class="description">{{ subscriptionInfo.description }}</p>
        <div class="benefits">
          <h4>{{ $t('subscription.features') }}：</h4>
          <ul>
            <li v-for="benefit in subscriptionInfo.benefits" :key="benefit">
              {{ benefit }}
            </li>
          </ul>
        </div>
        <div class="points-info">
          <a-tag type="success">支付完成后可获得 {{ subscriptionInfo.points }} 积分</a-tag>
        </div>
        <div class="payment-method">
          <h4>选择支付方式：</h4>
          <a-radio-group v-model:value="selectedPaymentMethod" @change="updatePrice">
            <a-radio label="ALIPAY">支付宝</a-radio>
            <a-radio label="PAYPAL">PayPal</a-radio>
          </a-radio-group>
          <div class="converted-price" v-if="convertedPrice">
            <p>支付金额：<span class="price-display">{{ convertedPrice }}</span></p>
          </div>
        </div>
      </div>
      <template #footer>
        <a-button @click="subscriptionDialogVisible = false">取消</a-button>
        <a-button type="primary" @click="createSubscription" :loading="subscriptionLoading">
          立即订阅
        </a-button>
      </template>
    </a-modal>

  </div>
</template>

<script setup>
import { ref, reactive, onMounted, computed } from 'vue'
import { message, Modal } from 'ant-design-vue'
import { useI18n } from 'vue-i18n'
import {
  DollarOutlined,
  CreditCardOutlined,
  WalletOutlined,
  SearchOutlined,
  FilterOutlined,
  PlusOutlined,
  EyeOutlined,
  ReloadOutlined,
  DownloadOutlined,
  UploadOutlined,
  SettingOutlined,
  CheckOutlined,
  CloseOutlined,
  WarningOutlined
} from '@ant-design/icons-vue'
import { getPayments, testPaymentComplete as testPaymentCompleteApi, createTestPayment, deletePayment } from '@/api/payments'
import { useUserStore } from '@/stores/user'

const { t } = useI18n()
const userStore = useUserStore()
const loading = ref(false)
const payments = ref([])

// 筛选条件
const filters = reactive({
  status: '',
  search: ''
})

// 分页信息
const pagination = reactive({
  page: 1,
  size: 10,
  total: 0
})

// 支付详情对话框
const detailDialogVisible = ref(false)
const currentPayment = ref(null)


// 订阅对话框
const subscriptionDialogVisible = ref(false)
const subscriptionLoading = ref(false)
const subscriptionType = ref('')
const selectedPaymentMethod = ref('ALIPAY')
const convertedPrice = ref('')
const exchangeRate = ref(7.2) // 美元对人民币汇率，可以根据实际情况调整
const subscriptionInfo = reactive({
  title: '',
  price: 0,
  description: '',
  benefits: [],
  points: 0
})

// 计算属性
const subscriptionDialogTitle = computed(() => {
  return subscriptionType.value === 'standard' ? '标准版订阅' : '专业版订阅'
})

// 获取支付方式类型
const getPaymentMethodType = (method) => {
  const methodMap = {
    'ALIPAY': 'primary',
    'PAYPAL': 'success',
    'WECHAT': 'success',
    'UNIONPAY': 'warning'
  }
  return methodMap[method] || ''
}

// 获取支付方式文本
const getPaymentMethodText = (method) => {
  const methodMap = {
    'ALIPAY': '支付宝',
    'PAYPAL': 'PayPal',
    'WECHAT': '微信支付',
    'UNIONPAY': '银联支付'
  }
  return methodMap[method] || method
}

// 获取状态类型
const getStatusType = (status) => {
  const statusMap = {
    'PENDING': 'warning',
    'SUCCESS': 'success',
    'FAILED': 'danger',
    'CANCELLED': 'info'
  }
  return statusMap[status] || ''
}

// 获取状态文本
const getStatusText = (status) => {
  const statusMap = {
    'PENDING': '待支付',
    'SUCCESS': '支付成功',
    'FAILED': '支付失败',
    'CANCELLED': '已取消'
  }
  return statusMap[status] || status
}

// 格式化日期
const formatDate = (dateString) => {
  const date = new Date(dateString)
  return date.toLocaleDateString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit'
  })
}

// 获取支付记录列表
const fetchPayments = async () => {
  try {
    loading.value = true
    
    const response = await getPayments({
      page: pagination.page - 1,
      size: pagination.size,
      status: filters.status,
      search: filters.search
    })
    
    if (response.success) {
      payments.value = response.data
      pagination.total = response.total || response.data.length
    } else {
      message.error(response.message || t('common.fetchPaymentsFailed'))
    }
    
  } catch (error) {
    console.error('Fetch payments error:', error)
    message.error(t('common.fetchPaymentsFailed'))
  } finally {
    loading.value = false
  }
}

// 筛选变化
const handleFilterChange = () => {
  pagination.page = 1
  fetchPayments()
}

// 搜索
const handleSearch = () => {
  pagination.page = 1
  fetchPayments()
}

// 重置筛选
const resetFilters = () => {
  filters.status = ''
  filters.search = ''
  pagination.page = 1
  fetchPayments()
}

// 分页大小变化
const handleSizeChange = (size) => {
  pagination.size = size
  pagination.page = 1
  fetchPayments()
}

// 当前页变化
const handleCurrentChange = (page) => {
  pagination.page = page
  fetchPayments()
}

// 查看支付详情
const viewPaymentDetail = (payment) => {
  currentPayment.value = payment
  detailDialogVisible.value = true
}

// 更新价格显示
const updatePrice = () => {
  if (selectedPaymentMethod.value === 'ALIPAY') {
    // 支付宝使用人民币
    const cnyPrice = (subscriptionInfo.price * exchangeRate.value).toFixed(2)
    convertedPrice.value = `¥${cnyPrice}`
  } else if (selectedPaymentMethod.value === 'PAYPAL') {
    // PayPal使用美元
    convertedPrice.value = `$${subscriptionInfo.price}`
  }
}

// 显示订阅对话框
const showSubscriptionDialog = (type) => {
  if (!userStore.isAuthenticated) {
    message.warning(t('common.pleaseLoginFirst'))
    return
  }
  
  subscriptionType.value = type
  
  if (type === 'standard') {
    subscriptionInfo.title = '标准版订阅'
    subscriptionInfo.price = 59
    subscriptionInfo.description = '适合个人用户的基础功能订阅'
    subscriptionInfo.benefits = [
      '基础AI功能使用',
      '每月100次API调用',
      '邮件技术支持',
      '基础模板库访问'
    ]
    subscriptionInfo.points = 200
  } else if (type === 'professional') {
    subscriptionInfo.title = '专业版订阅'
    subscriptionInfo.price = 259
    subscriptionInfo.description = '适合企业用户的高级功能订阅'
    subscriptionInfo.benefits = [
      '高级AI功能使用',
      '每月1000次API调用',
      '优先技术支持',
      '完整模板库访问',
      'API接口集成',
      '数据分析报告'
    ]
    subscriptionInfo.points = 1000
  }
  
  subscriptionDialogVisible.value = true
  // 初始化价格显示
  updatePrice()
}

// 创建订阅支付
const createSubscription = async () => {
  try {
    subscriptionLoading.value = true
    
    // 根据支付方式确定实际支付金额
    let actualAmount
    if (selectedPaymentMethod.value === 'ALIPAY') {
      // 支付宝使用人民币
      actualAmount = (subscriptionInfo.price * exchangeRate.value).toFixed(2)
    } else {
      // PayPal使用美元
      actualAmount = subscriptionInfo.price.toString()
    }
    
    const response = await createTestPayment({
      amount: actualAmount,
      method: selectedPaymentMethod.value
    })
    
    if (response.success) {
      message.success(t('common.paymentRecordCreated', { title: subscriptionInfo.title }))
      
      // 根据支付方式调用相应的支付接口
      if (selectedPaymentMethod.value === 'ALIPAY') {
        try {
          const alipayResponse = await createAlipayPayment({
            paymentId: response.data.id
          })
          
          if (alipayResponse.success) {
            // 跳转到支付宝支付页面
            window.open(alipayResponse.data.paymentUrl, '_blank')
            message.success(t('common.redirectingToAlipay'))
          } else {
            message.error(alipayResponse.message || t('common.createAlipayFailed'))
          }
        } catch (error) {
          console.error('创建支付宝支付失败:', error)
          message.error(t('common.createAlipayFailed'))
        }
      } else if (selectedPaymentMethod.value === 'PAYPAL') {
        try {
          const paypalResponse = await createPayPalPayment({
            paymentId: response.data.id
          })
          
          if (paypalResponse.success) {
            // 跳转到PayPal支付页面
            window.open(paypalResponse.data.paymentUrl, '_blank')
            message.success(t('common.redirectingToPaypal'))
          } else {
            message.error(paypalResponse.message || t('common.createPaypalFailed'))
          }
        } catch (error) {
          console.error('创建PayPal支付失败:', error)
          message.error(t('common.createPaypalFailed'))
        }
      }
      
      subscriptionDialogVisible.value = false
      // 刷新支付记录列表
      fetchPayments()
    } else {
      message.error(response.message || t('common.createSubscriptionFailed'))
    }
    
  } catch (error) {
    console.error('Create subscription error:', error)
    message.error(t('common.createSubscriptionFailed'))
  } finally {
    subscriptionLoading.value = false
  }
}


// 测试支付完成
const testPaymentComplete = async (payment) => {
  try {
    await new Promise((resolve, reject) => {
      Modal.confirm({
        title: t('common.confirmTest'),
        content: t('common.confirmTestPayment', { orderId: payment.orderId }),
        okText: t('common.confirm'),
        cancelText: t('common.cancel'),
        onOk() { resolve() },
        onCancel() { reject('cancel') }
      })
    })
    
    const response = await testPaymentCompleteApi(payment.id)
    
    if (response.success) {
      message.success(t('common.testPaymentSuccess'))
      // 刷新支付记录列表
      fetchPayments()
    } else {
      message.error(response.message || t('common.testPaymentFailed'))
    }
    
  } catch (error) {
    if (error !== 'cancel') {
      console.error('Test payment complete error:', error)
      message.error(t('common.testPaymentFailed'))
    }
  }
}

// 删除支付记录
const handleDeletePayment = async (payment) => {
  try {
    await new Promise((resolve, reject) => {
      Modal.confirm({
        title: t('common.confirm'),
        content: `确定要删除支付记录 ${payment.orderId} 吗？`,
        okText: t('common.confirm'),
        cancelText: t('common.cancel'),
        onOk() { resolve() },
        onCancel() { reject('cancel') }
      })
    })
    
    const response = await deletePayment(payment.id)
    
    if (response.data?.success) {
      message.success(t('common.deleteSuccess'))
      // 刷新支付记录列表
      fetchPayments()
    } else {
      message.error(response.data?.message || t('common.deleteFailed'))
    }
    
  } catch (error) {
    if (error !== 'cancel') {
      console.error('Delete payment error:', error)
      message.error(t('common.deleteFailed'))
    }
  }
}

onMounted(() => {
  fetchPayments()
})
</script>

<style scoped>
.payments {
  max-width: 1200px;
  margin: 0 auto;
}

.page-header {
  margin-bottom: var(--space-5);
}

.page-header h2 {
  margin: 0;
  color: var(--text-primary);
  display: flex;
  align-items: center;
  gap: var(--space-2);
}

.filter-card {
  margin-bottom: var(--space-5);
}

.payments-card {
  margin-bottom: var(--space-5);
}

.order-link {
  color: var(--primary-500);
  text-decoration: none;
  font-weight: var(--font-medium);
}

.order-link:hover {
  text-decoration: underline;
}

.amount {
  font-weight: var(--font-semibold);
  color: var(--warning-500);
}

.description {
  color: var(--text-secondary);
}

.pagination-container {
  display: flex;
  justify-content: center;
  margin-top: var(--space-5);
}

.payment-description {
  margin-top: var(--space-5);
}

.payment-description h4 {
  margin-bottom: var(--space-3);
  color: var(--text-primary);
}

.payment-description p {
  color: var(--text-secondary);
  line-height: var(--leading-relaxed);
}

.subscription-info {
  text-align: center;
}

.subscription-info h3 {
  color: var(--primary-500);
  margin-bottom: var(--space-2);
}

.subscription-info .price {
  font-size: var(--text-4xl);
  font-weight: var(--font-bold);
  color: var(--error-400);
  margin: var(--space-4) 0;
}

.subscription-info .description {
  color: var(--text-tertiary);
  margin-bottom: var(--space-4);
}

.subscription-info .benefits {
  text-align: left;
  margin: var(--space-4) 0;
}

.subscription-info .benefits h4 {
  color: var(--text-primary);
  margin-bottom: var(--space-2);
}

.subscription-info .benefits ul {
  list-style: none;
  padding: 0;
}

.subscription-info .benefits li {
  padding: var(--space-1) 0;
  color: var(--text-tertiary);
}

.subscription-info .benefits li:before {
  content: "✓ ";
  color: var(--success-500);
  font-weight: var(--font-bold);
}

.subscription-info .points-info {
  margin-top: var(--space-4);
}

.subscription-info .payment-method {
  margin-top: var(--space-6);
  padding-top: var(--space-4);
  border-top: 1px solid var(--border-default);
}

.subscription-info .payment-method h4 {
  color: var(--text-primary);
  margin-bottom: var(--space-2);
}

.subscription-info .converted-price {
  margin-top: var(--space-2);
  padding: var(--space-2);
  background-color: var(--primary-glow-subtle);
  border-radius: var(--radius-sm);
  border: 1px solid var(--primary-glow-medium);
}

.subscription-info .price-display {
  font-size: var(--text-xl);
  font-weight: var(--font-bold);
  color: var(--primary-500);
}
</style>
