<template>
  <view class="page">
    <!-- 导航栏 -->
    <NavBar title="设备详情" :showBack="true" />
    
    <view class="detail-card" v-if="device">
      <!-- 设备状态 -->
      <view class="status-section">
        <view class="status-header">
          <text class="device-name">{{ device.name }}</text>
          <view class="device-status" :class="getStatusClass(device.status)">
            <view class="status-dot"></view>
            <text>{{ getStatusText(device.status) }}</text>
          </view>
        </view>
        <text class="device-no">{{ device.deviceNo }}</text>
      </view>
      
      <!-- 基本信息 -->
      <view class="info-section">
        <text class="section-title">基本信息</text>
        <view class="info-item">
          <text class="label">所属门店</text>
          <text class="value">{{ device.shopName }}</text>
        </view>
        <view class="info-item">
          <text class="label">门店地址</text>
          <text class="value">{{ device.shopAddress }}</text>
        </view>
      </view>
      
      <!-- 运行状态 -->
      <view class="info-section" v-if="device.status === 1">
        <text class="section-title">运行状态</text>
        <view class="status-grid">
          <view class="status-item">
            <view class="status-icon temp">
              <view class="icon-inner"></view>
            </view>
            <text class="status-value">{{ device.temperature }}°C</text>
            <text class="status-label">柜内温度</text>
          </view>
          <view class="status-item">
            <view class="status-icon volume">
              <view class="icon-inner"></view>
            </view>
            <text class="status-value">{{ device.volume }}%</text>
            <text class="status-label">音量</text>
          </view>
        </view>
      </view>
      
      <!-- 销售数据 -->
      <view class="info-section">
        <text class="section-title">销售数据</text>
        <view class="sales-grid">
          <view class="sales-item">
            <text class="sales-value accent">¥{{ formatMoney(device.todaySales) }}</text>
            <text class="sales-label">今日销售额</text>
          </view>
          <view class="sales-item">
            <text class="sales-value">{{ device.todayOrders }}</text>
            <text class="sales-label">今日订单</text>
          </view>
          <view class="sales-item">
            <text class="sales-value primary">¥{{ formatMoney(device.totalSales) }}</text>
            <text class="sales-label">累计销售</text>
          </view>
        </view>
      </view>
      
      <!-- 库存状态 -->
      <view class="info-section">
        <text class="section-title">库存状态</text>
        <view class="inventory-info">
          <view class="inventory-bar">
            <view 
              class="inventory-progress" 
              :style="{ width: (device.inventory / device.capacity * 100) + '%' }"
            ></view>
          </view>
          <text class="inventory-text">{{ device.inventory }}/{{ device.capacity }}</text>
        </view>
        
        <!-- 库存预警 -->
        <view class="low-stock-list" v-if="device.lowStockItems && device.lowStockItems.length > 0">
          <view class="low-stock-title">
            <view class="warning-icon"></view>
            <text>库存预警</text>
          </view>
          <view class="low-stock-item" v-for="item in device.lowStockItems" :key="item.productName">
            <text class="product-name">{{ item.productName }}</text>
            <text class="stock-warning">
              库存不足: {{ item.currentStock }}/{{ item.minStock }}
            </text>
          </view>
        </view>
      </view>
      
      <!-- 最近订单 -->
      <view class="info-section" v-if="device.recentOrders && device.recentOrders.length > 0">
        <text class="section-title">最近订单</text>
        <view class="order-list">
          <view class="order-item" v-for="order in device.recentOrders" :key="order.orderNo">
            <text class="order-no">{{ order.orderNo }}</text>
            <text class="order-amount">¥{{ formatMoney(order.amount) }}</text>
            <text class="order-time">{{ order.time }}</text>
          </view>
        </view>
      </view>
    </view>
    
    <!-- 底部操作 -->
    <view class="bottom-actions" v-if="device && device.status === 1">
      <button class="action-btn" @click="handleReboot">重启设备</button>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import NavBar from '@/components/NavBar.vue';
import { getDeviceDetail, rebootDevice } from '@/api/device';
import { DeviceStatusText, DeviceStatusColor } from '@/mock/device';
import { formatMoney as formatMoneyUtil, showToast, showConfirm, navigateBack } from '@/utils/common';

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

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

const getStatusClass = (status: number) => {
  if (status === 1) return 'online';
  if (status === 0) return 'offline';
  return 'maintenance';
};

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 {
    device.value = await getDeviceDetail(Number(id));
  } catch (error) {
    showToast('加载设备详情失败');
    navigateBack();
  }
};

const handleReboot = async () => {
  const confirmed = await showConfirm('确认重启该设备？');
  if (confirmed && device.value) {
    try {
      await rebootDevice(device.value.id);
      showToast('重启指令已发送', 'success');
    } catch (error) {
      showToast('重启失败');
    }
  }
};

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

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

.status-section {
  background: #ffffff;
  padding: 30rpx 32rpx;
  margin-bottom: 16rpx;
  
  .status-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 12rpx;
    
    .device-name {
      font-size: 36rpx;
      font-weight: 700;
      color: #1e293b;
    }
    
    .device-status {
      display: flex;
      align-items: center;
      justify-content: center;
      gap: 8rpx;
      padding: 10rpx 20rpx;
      font-size: 24rpx;
      font-weight: 500;
      border-radius: 20rpx;
      
      .status-dot {
        width: 8rpx;
        height: 8rpx;
        border-radius: 50%;
      }
      
      &.online {
        background: #ecfdf5;
        color: #10b981;
        .status-dot { background: #10b981; }
      }
      
      &.offline {
        background: #fff7ed;
        color: #f97316;
        .status-dot { background: #f97316; }
      }
      
      &.maintenance {
        background: #faf5ff;
        color: #a855f7;
        .status-dot { background: #a855f7; }
      }
    }
  }
  
  .device-no {
    font-size: 26rpx;
    color: #94a3b8;
    font-family: monospace;
  }
}

.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;
    }
  }
  
  .status-grid {
    display: flex;
    
    .status-item {
      flex: 1;
      text-align: center;
      padding: 20rpx;
      background: #f8fafc;
      border-radius: 12rpx;
      margin-right: 16rpx;
      
      &:last-child {
        margin-right: 0;
      }
      
      .status-icon {
        width: 56rpx;
        height: 56rpx;
        border-radius: 16rpx;
        margin: 0 auto 16rpx;
        display: flex;
        align-items: center;
        justify-content: center;
        
        &.temp {
          background: #ecfdf5;
          
          .icon-inner {
            width: 20rpx;
            height: 20rpx;
            border: 4rpx solid #10b981;
            border-radius: 50%;
          }
        }
        
        &.volume {
          background: #f0f9ff;
          
          .icon-inner {
            width: 0;
            height: 0;
            border-left: 10rpx solid #0ea5e9;
            border-top: 8rpx solid transparent;
            border-bottom: 8rpx solid transparent;
          }
        }
      }
      
      .status-value {
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 32rpx;
        font-weight: 700;
        color: #1e293b;
        margin-bottom: 8rpx;
      }
      
      .status-label {
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 24rpx;
        color: #64748b;
      }
    }
  }
  
  .sales-grid {
    display: flex;
    background: #f8fafc;
    border-radius: 12rpx;
    padding: 20rpx 0;
    
    .sales-item {
      flex: 1;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      
      .sales-value {
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 30rpx;
        font-weight: 700;
        color: #1e293b;
        margin-bottom: 8rpx;
        
        &.accent {
          color: #f97316;
        }
        
        &.primary {
          color: #10b981;
        }
      }
      
      .sales-label {
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 22rpx;
        color: #64748b;
      }
    }
  }
  
  .inventory-info {
    display: flex;
    align-items: center;
    
    .inventory-bar {
      flex: 1;
      height: 20rpx;
      background: #e2e8f0;
      border-radius: 10rpx;
      overflow: hidden;
      
      .inventory-progress {
        height: 100%;
        background: #10b981;
        border-radius: 10rpx;
      }
    }
    
    .inventory-text {
      width: 120rpx;
      text-align: right;
      font-size: 26rpx;
      color: #475569;
      font-weight: 500;
    }
  }
  
  .low-stock-list {
    margin-top: 20rpx;
    padding: 20rpx;
    background: #fef2f2;
    border-radius: 12rpx;
    
    .low-stock-title {
      display: flex;
      align-items: center;
      gap: 8rpx;
      font-size: 26rpx;
      font-weight: 600;
      color: #ef4444;
      margin-bottom: 16rpx;
      
      .warning-icon {
        width: 20rpx;
        height: 20rpx;
        border: 3rpx solid #ef4444;
        border-radius: 50%;
        position: relative;
        
        &::before {
          content: '';
          position: absolute;
          top: 4rpx;
          left: 50%;
          transform: translateX(-50%);
          width: 3rpx;
          height: 3rpx;
          background: #ef4444;
          border-radius: 50%;
        }
        
        &::after {
          content: '';
          position: absolute;
          bottom: 3rpx;
          left: 50%;
          transform: translateX(-50%);
          width: 3rpx;
          height: 6rpx;
          background: #ef4444;
          border-radius: 2rpx;
        }
      }
    }
    
    .low-stock-item {
      display: flex;
      justify-content: space-between;
      padding: 12rpx 0;
      
      .product-name {
        font-size: 26rpx;
        color: #1e293b;
      }
      
      .stock-warning {
        font-size: 24rpx;
        color: #ef4444;
      }
    }
  }
  
  .order-list {
    .order-item {
      display: flex;
      align-items: center;
      padding: 16rpx 0;
      border-bottom: 1rpx solid #f1f5f9;
      
      &:last-child {
        border-bottom: none;
      }
      
      .order-no {
        flex: 1;
        font-size: 26rpx;
        color: #1e293b;
        font-family: monospace;
      }
      
      .order-amount {
        width: 120rpx;
        text-align: right;
        font-size: 26rpx;
        color: #1e293b;
        font-weight: 600;
      }
      
      .order-time {
        width: 160rpx;
        text-align: right;
        font-size: 24rpx;
        color: #94a3b8;
      }
    }
  }
}

.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);
  
  .action-btn {
    width: 100%;
    height: 96rpx;
    background: #10b981;
    color: #fff;
    font-size: 32rpx;
    font-weight: 600;
    border-radius: 16rpx;
    border: none;
    
    &::after {
      border: none;
    }
    
    &:active {
      background: #059669;
    }
  }
}
</style>
