<template>
  <view class="page">
    <!-- 顶部导航栏 -->
    <view class="nav-bar">
      <view class="nav-content">
        <view class="nav-left" @click="goBack">
          <text class="back-icon">←</text>
        </view>
        <text class="page-title">支付结果</text>
      </view>
    </view>

    <!-- 支付结果内容 -->
    <view class="result-container">
      <!-- 成功状态 -->
      <view v-if="status === 'success'" class="result-card success">
        <text class="result-icon">✓</text>
        <text class="result-title">支付成功</text>
        <text class="result-desc">感谢您的支持，您的会员权益已生效</text>
        <view class="order-info">
          <text class="info-label">订单号</text>
          <text class="info-value">{{ orderNo || '-' }}</text>
        </view>
      </view>

      <!-- 失败状态 -->
      <view v-else-if="status === 'failed'" class="result-card failed">
        <text class="result-icon">✕</text>
        <text class="result-title">支付失败</text>
        <text class="result-desc">{{ errorMsg || '支付未能完成，请稍后重试' }}</text>
      </view>

      <!-- 待支付状态 -->
      <view v-else-if="status === 'pending'" class="result-card pending">
        <text class="result-icon">⏳</text>
        <text class="result-title">等待支付</text>
        <text class="result-desc">请在支付宝中完成支付</text>
      </view>

      <!-- 未知状态 -->
      <view v-else class="result-card unknown">
        <text class="result-icon">?</text>
        <text class="result-title">状态未知</text>
        <text class="result-desc">未能获取支付状态，请联系客服</text>
      </view>

      <!-- 操作按钮 -->
      <view class="action-buttons">
        <button class="btn-primary" @click="goToMember">
          <text>返回会员中心</text>
        </button>
        <button class="btn-secondary" @click="goBack">
          <text>返回上页</text>
        </button>
      </view>

      <!-- 提示信息 -->
      <view class="tips">
        <text class="tips-title">温馨提示</text>
        <text class="tips-text">
          • 支付结果通常会在1-3分钟内到账
        </text>
        <text class="tips-text">
          • 如支付成功但权益未生效，请稍后刷新页面
        </text>
        <text class="tips-text">
          • 如有问题请联系客服
        </text>
      </view>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';

const status = ref<'success' | 'failed' | 'pending' | 'unknown'>('unknown');
const orderNo = ref('');
const errorMsg = ref('');

onMounted(() => {
  // 从 URL 参数获取支付结果
  const pages = getCurrentPages();
  const currentPage = pages[pages.length - 1] as any;
  // #ifdef APP-PLUS
  const options = currentPage?.$page?.options || {};
  // #endif
  // #ifndef APP-PLUS
  const options = currentPage?.options || {};
  // #endif

  // 支付宝同步返回参数
  if (options.trade_status) {
    // 支付宝同步返回
    if (options.trade_status === 'TRADE_SUCCESS' || options.trade_status === 'TRADE_FINISHED') {
      status.value = 'success';
    } else if (options.trade_status === 'WAIT_BUYER_PAY') {
      status.value = 'pending';
    } else {
      status.value = 'failed';
      errorMsg.value = options.memo || options.errorMsg || '支付未完成';
    }
  } else if (options.status) {
    // 自定义状态参数
    status.value = options.status;
  }

  // 订单号
  if (options.out_trade_no) {
    orderNo.value = options.out_trade_no;
  } else if (options.orderNo) {
    orderNo.value = options.orderNo;
  }

  // 错误信息
  if (options.error_msg) {
    errorMsg.value = decodeURIComponent(options.error_msg);
  }
});

function goBack() {
  const pages = getCurrentPages();
  if (pages.length > 1) {
    uni.navigateBack();
  } else {
    uni.switchTab({ url: '/pages/mine/index' });
  }
}

function goToMember() {
  uni.switchTab({ url: '/pages/mine/index' });
}
</script>

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

.nav-bar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 88rpx;
  padding-top: env(safe-area-inset-top);
  background: #ffffff;
  border-bottom: 1px solid #e5e7eb;
  z-index: 100;
}

.nav-content {
  height: 100%;
  display: flex;
  align-items: center;
  padding: 0 24rpx;
}

.nav-left {
  width: 80rpx;
  display: flex;
  align-items: center;
}

.back-icon {
  font-size: 36rpx;
  color: #333;
}

.page-title {
  flex: 1;
  text-align: center;
  font-size: 32rpx;
  font-weight: 500;
  color: #333;
  margin-right: 80rpx;
}

.result-container {
  padding-top: calc(120rpx + env(safe-area-inset-top));
  padding-left: 32rpx;
  padding-right: 32rpx;
}

.result-card {
  background: #ffffff;
  border-radius: 24rpx;
  padding: 60rpx 40rpx;
  text-align: center;
  margin-bottom: 32rpx;
}

.result-card.success {
  border: 2rpx solid #10b981;
}

.result-card.failed {
  border: 2rpx solid #ef4444;
}

.result-card.pending {
  border: 2rpx solid #f59e0b;
}

.result-card.unknown {
  border: 2rpx solid #9ca3af;
}

.result-icon {
  font-size: 100rpx;
  display: block;
  margin-bottom: 24rpx;
}

.success .result-icon {
  color: #10b981;
}

.failed .result-icon {
  color: #ef4444;
}

.pending .result-icon {
  color: #f59e0b;
}

.unknown .result-icon {
  color: #9ca3af;
}

.result-title {
  font-size: 36rpx;
  font-weight: 600;
  color: #1f2937;
  display: block;
  margin-bottom: 16rpx;
}

.result-desc {
  font-size: 28rpx;
  color: #6b7280;
  display: block;
  margin-bottom: 24rpx;
}

.order-info {
  background: #f9fafb;
  border-radius: 12rpx;
  padding: 24rpx;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.info-label {
  font-size: 26rpx;
  color: #9ca3af;
}

.info-value {
  font-size: 26rpx;
  color: #1f2937;
  font-family: monospace;
}

.action-buttons {
  display: flex;
  flex-direction: column;
  gap: 24rpx;
  margin-bottom: 48rpx;
}

.btn-primary,
.btn-secondary {
  height: 88rpx;
  border-radius: 44rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  border: none;
  font-size: 30rpx;
}

.btn-primary {
  background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%);
  color: #ffffff;
}

.btn-secondary {
  background: #ffffff;
  color: #4f46e5;
  border: 2rpx solid #4f46e5;
}

.tips {
  background: #fef3c7;
  border-radius: 16rpx;
  padding: 32rpx;
  margin-bottom: 48rpx;
}

.tips-title {
  font-size: 28rpx;
  font-weight: 600;
  color: #92400e;
  display: block;
  margin-bottom: 16rpx;
}

.tips-text {
  font-size: 24rpx;
  color: #78350f;
  display: block;
  margin-bottom: 8rpx;
}

.tips-text:last-child {
  margin-bottom: 0;
}
</style>
