<template>
  <view class="page">
    <!-- 导航栏 -->
    <NavBar title="订单详情" :showBack="true" />
    
    <view class="detail-card" v-if="order">
      <!-- 订单状态 -->
      <view class="status-section">
        <view class="status-icon" :class="getStatusClass(order.status)">
          <view class="icon-check" v-if="order.status === 2"></view>
          <view class="icon-clock" v-else-if="order.status === 4"></view>
          <view class="icon-box" v-else></view>
        </view>
        <text class="status-text">{{ getStatusText(order.status) }}</text>
      </view>
      
      <!-- 门店信息 -->
      <view class="info-section">
        <text class="section-title">门店信息</text>
        <view class="info-item">
          <text class="label">门店名称</text>
          <text class="value">{{ order.shopName }}</text>
        </view>
        <view class="info-item">
          <text class="label">设备名称</text>
          <text class="value">{{ order.deviceName }}</text>
        </view>
      </view>
      
      <!-- 商品列表 -->
      <view class="info-section">
        <text class="section-title">商品信息</text>
        <view class="product-list">
          <view class="product-item" v-for="item in order.items" :key="item.productId">
            <view class="product-info">
              <text class="product-name">{{ item.productName }}</text>
              <text class="product-price">¥{{ formatMoney(item.price) }}</text>
            </view>
            <view class="product-count">
              <text>x{{ item.quantity }}</text>
            </view>
            <view class="product-amount">
              <text>¥{{ formatMoney(item.amount) }}</text>
            </view>
          </view>
        </view>
      </view>
      
      <!-- 订单信息 -->
      <view class="info-section">
        <text class="section-title">订单信息</text>
        <view class="info-item">
          <text class="label">订单编号</text>
          <text class="value mono">{{ order.orderNo }}</text>
        </view>
        <view class="info-item">
          <text class="label">创建时间</text>
          <text class="value">{{ order.createdAt }}</text>
        </view>
        <view class="info-item" v-if="order.paidAt">
          <text class="label">支付时间</text>
          <text class="value">{{ order.paidAt }}</text>
        </view>
        <view class="info-item">
          <text class="label">支付方式</text>
          <text class="value">{{ order.paymentMethod }}</text>
        </view>
      </view>
      
      <!-- 金额信息 -->
      <view class="info-section amount-section">
        <text class="section-title">金额信息</text>
        <view class="info-item">
          <text class="label">商品总额</text>
          <text class="value">¥{{ formatMoney(order.totalAmount) }}</text>
        </view>
        <view class="info-item highlight">
          <text class="label">实付金额</text>
          <text class="value">¥{{ formatMoney(order.payAmount) }}</text>
        </view>
      </view>
    </view>
    
    <!-- 底部操作 -->
    <view class="bottom-actions" v-if="order && order.status === 4">
      <button class="refund-btn" @click="handleRefund">同意退款</button>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import NavBar from '@/components/NavBar.vue';
import { getOrderDetail, handleRefund as refundOrder } from '@/api/order';
import { OrderStatusText, OrderStatusColor } from '@/mock/order';
import { formatMoney as formatMoneyUtil, showToast, navigateBack } from '@/utils/common';

const order = ref<any>(null);
const formatMoney = formatMoneyUtil;

const getStatusText = (status: number) => OrderStatusText[status] || '未知';
const getStatusColor = (status: number) => OrderStatusColor[status] || '#94a3b8';

const getStatusClass = (status: number) => {
  if (status === 2) return 'success';
  if (status === 4) return 'warning';
  if (status === 1) return 'processing';
  return 'pending';
};

const loadDetail = async () => {
  const pages = getCurrentPages();
  const currentPage = pages[pages.length - 1];
  const id = (currentPage as any).options?.id;
  
  if (!id) {
    showToast('订单ID不存在');
    navigateBack();
    return;
  }
  
  try {
    order.value = await getOrderDetail(Number(id));
  } catch (error) {
    showToast('加载订单详情失败');
    navigateBack();
  }
};

const handleRefund = async () => {
  if (!order.value) return;
  
  try {
    await refundOrder(order.value.id, true);
    showToast('退款处理成功', 'success');
    setTimeout(() => {
      navigateBack();
    }, 1000);
  } catch (error) {
    showToast('退款处理失败');
  }
};

onMounted(() => {
  loadDetail();
});
</script>

<style lang="scss" scoped>
.page {
  min-height: 100vh;
  background: #f8fafc;
  padding-bottom: 120rpx;
}

.status-section {
  background: #ffffff;
  padding: 48rpx 32rpx;
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-bottom: 16rpx;
  
  .status-icon {
    width: 88rpx;
    height: 88rpx;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 20rpx;
    
    &.success {
      background: #ecfdf5;
      
      .icon-check {
        width: 40rpx;
        height: 24rpx;
        border-left: 4rpx solid #10b981;
        border-bottom: 4rpx solid #10b981;
        transform: rotate(-45deg);
        margin-top: -8rpx;
      }
    }
    
    &.warning {
      background: #fff7ed;
      
      .icon-clock {
        width: 36rpx;
        height: 36rpx;
        border: 4rpx solid #f97316;
        border-radius: 50%;
        position: relative;
        
        &::before {
          content: '';
          position: absolute;
          top: 50%;
          left: 50%;
          transform: translate(-50%, -50%);
          width: 14rpx;
          height: 4rpx;
          background: #f97316;
          transform-origin: left center;
        }
        
        &::after {
          content: '';
          position: absolute;
          top: 50%;
          left: 50%;
          transform: translate(-50%, -100%);
          width: 4rpx;
          height: 10rpx;
          background: #f97316;
        }
      }
    }
    
    &.processing {
      background: #f0f9ff;
      
      .icon-box {
        width: 32rpx;
        height: 32rpx;
        border: 4rpx solid #0ea5e9;
        border-radius: 6rpx;
      }
    }
    
    &.pending {
      background: #faf5ff;
      
      .icon-box {
        width: 32rpx;
        height: 32rpx;
        border: 4rpx solid #a855f7;
        border-radius: 6rpx;
      }
    }
  }
  
  .status-text {
    font-size: 36rpx;
    font-weight: 600;
    color: #1e293b;
    display: flex;
    align-items: center;
    justify-content: center;
  }
}

.detail-card {
  .info-section {
    background: #ffffff;
    margin-bottom: 16rpx;
    padding: 24rpx 32rpx;
    
    .section-title {
      display: block;
      font-size: 28rpx;
      font-weight: 600;
      color: #1e293b;
      margin-bottom: 20rpx;
      padding-bottom: 16rpx;
      border-bottom: 1rpx solid #f1f5f9;
    }
    
    .info-item {
      display: flex;
      justify-content: space-between;
      padding: 14rpx 0;
      
      .label {
        font-size: 28rpx;
        color: #64748b;
      }
      
      .value {
        font-size: 28rpx;
        color: #1e293b;
        
        &.mono {
          font-family: monospace;
        }
      }
      
      &.highlight {
        .value {
          color: #f97316;
          font-weight: 600;
        }
      }
    }
    
    .product-list {
      .product-item {
        display: flex;
        align-items: center;
        padding: 20rpx 0;
        border-bottom: 1rpx solid #f1f5f9;
        
        &:last-child {
          border-bottom: none;
        }
        
        .product-info {
          flex: 1;
          
          .product-name {
            display: block;
            font-size: 28rpx;
            color: #1e293b;
            margin-bottom: 8rpx;
          }
          
          .product-price {
            font-size: 24rpx;
            color: #94a3b8;
          }
        }
        
        .product-count {
          width: 100rpx;
          text-align: center;
          font-size: 28rpx;
          color: #64748b;
        }
        
        .product-amount {
          width: 120rpx;
          text-align: right;
          font-size: 28rpx;
          color: #1e293b;
          font-weight: 500;
        }
      }
    }
  }
  
  .amount-section {
    .info-item:last-child {
      background: #fff7ed;
      margin: 16rpx -32rpx -24rpx;
      padding: 20rpx 32rpx;
      border-radius: 0 0 0 0;
    }
  }
}

.bottom-actions {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: #ffffff;
  padding: 20rpx 32rpx;
  padding-bottom: calc(20rpx + env(safe-area-inset-bottom));
  box-shadow: 0 -2rpx 10rpx rgba(0, 0, 0, 0.05);
  
  .refund-btn {
    width: 100%;
    height: 96rpx;
    background: #ef4444;
    color: #fff;
    font-size: 32rpx;
    font-weight: 600;
    border-radius: 16rpx;
    border: none;
    
    &::after {
      border: none;
    }
    
    &:active {
      background: #dc2626;
    }
  }
}
</style>
