<template>
  <view class="success-container">
    <!-- 支付成功状态 -->
    <view class="status-card">
      <view class="status-icon success"></view>
      <view class="status-text">支付成功</view>
      <view class="status-desc">您的订单已支付成功</view>
    </view>
    
    <!-- 订单信息 -->
    <view class="order-card">
      <view class="card-title">订单信息</view>
      <view class="info-item">
        <text class="label">订单号</text>
        <text class="value">{{ orderInfo.orderNo || '-' }}</text>
      </view>
      <view class="info-item">
        <text class="label">设备号</text>
        <text class="value">{{ orderInfo.deviceNo || '-' }}</text>
      </view>
      <view class="info-item">
        <text class="label">支付金额</text>
        <text class="value">￥{{ orderInfo.amount || '0.00' }}</text>
      </view>
      <view class="info-item">
        <text class="label">支付时间</text>
        <text class="value">{{ orderInfo.payTime || '-' }}</text>
      </view>
    </view>

    <!-- 操作按钮 -->
    <view class="button-group">
      <button class="primary-btn" @click="goToHome">返回首页</button>
      <button class="secondary-btn" @click="goToOrderList">查看订单</button>
    </view>
  </view>
</template>

<script>
import { queryById } from '@/config/user.js'

export default {
  data() {
    return {
      orderId: '',
      orderInfo: {}
    }
  },
  onLoad(options) {
    if (options && options.orderId) {
      this.orderId = options.orderId
      this.loadOrderInfo()
    } else {
      uni.showToast({
        title: '订单信息不存在',
        icon: 'none'
      })
      setTimeout(() => {
        this.goToHome()
      }, 1500)
    }
  },
  methods: {
    async loadOrderInfo() {
      try {
        uni.showLoading({
          title: '加载中'
        })
        
        const res = await queryById(this.orderId)
        if (res.code === 200 && res.data) {
          const orderData = res.data
          this.orderInfo = {
            orderNo: orderData.orderNo || orderData.orderId,
            deviceNo: orderData.deviceNo,
            amount: orderData.amount,
            payTime: this.formatTime(new Date())
          }
        } else {
          throw new Error('获取订单信息失败')
        }
        
        uni.hideLoading()
      } catch (error) {
        uni.hideLoading()
        uni.showToast({
          title: error.message || '获取订单信息失败',
          icon: 'none'
        })
      }
    },
    formatTime(date) {
      const year = date.getFullYear()
      const month = (date.getMonth() + 1).toString().padStart(2, '0')
      const day = date.getDate().toString().padStart(2, '0')
      const hour = date.getHours().toString().padStart(2, '0')
      const minute = date.getMinutes().toString().padStart(2, '0')
      const second = date.getSeconds().toString().padStart(2, '0')
      return `${year}-${month}-${day} ${hour}:${minute}:${second}`
    },
    goToHome() {
      uni.switchTab({
        url: '/pages/index/index'
      })
    },
    goToOrderList() {
      uni.redirectTo({
        url: '/pages/order/index'
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.success-container {
  padding: 20px;
  background-color: #f5f5f5;
  min-height: 100vh;
}

.status-card {
  background-color: #fff;
  border-radius: 12px;
  padding: 30px;
  text-align: center;
  margin-bottom: 20px;
  
  .status-icon {
    width: 60px;
    height: 60px;
    margin: 0 auto 16px;
    background-color: #07c160;
    border-radius: 50%;
    position: relative;
    
    &::after {
      content: '';
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
      width: 30px;
      height: 20px;
      border: 3px solid #fff;
      border-top: none;
      border-right: none;
      transform-origin: center;
      transform: translate(-50%, -70%) rotate(-45deg);
    }
  }
  
  .status-text {
    font-size: 24px;
    font-weight: bold;
    color: #07c160;
    margin-bottom: 8px;
  }
  
  .status-desc {
    font-size: 14px;
    color: #666;
  }
}

.order-card {
  background-color: #fff;
  border-radius: 12px;
  padding: 20px;
  margin-bottom: 20px;
  
  .card-title {
    font-size: 16px;
    font-weight: bold;
    margin-bottom: 16px;
    color: #333;
  }
  
  .info-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 12px;
    
    .label {
      color: #666;
      font-size: 14px;
    }
    
    .value {
      color: #333;
      font-size: 14px;
    }
  }
}

.button-group {
  margin-top: 30px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  
  .primary-btn {
    background-color: #07c160;
    color: #fff;
    border: none;
    border-radius: 24px;
    padding: 12px;
    font-size: 16px;
    
    &:active {
      opacity: 0.8;
    }
  }
  
  .secondary-btn {
    background-color: #fff;
    color: #07c160;
    border: 1px solid #07c160;
    border-radius: 24px;
    padding: 12px;
    font-size: 16px;
    
    &:active {
      background-color: #f5f5f5;
    }
  }
}
</style>