<template>
  <view class="page">
    <view class="header-hero" :style="{ paddingTop: statusBarHeight + 'px' }">
      <view class="top-bar">
        <view class="top-left" @click="goBack">
          <AppIcon name="chevronLeft" size="sm" color="white" />
        </view>
        <view class="top-center">
          <text class="page-title">Print Detail</text>
          <LocationPicker />
        </view>
        <view class="top-right" @click="isMenuOpen = true">
          <AppIcon name="menu" size="sm" color="white" />
        </view>
      </view>
    </view>

    <scroll-view v-if="record" class="content" scroll-y>
      <view class="detail-label-section">
        <text class="detail-section-title">Label Content</text>
        <view class="detail-label-wrap">
          <image :src="labelImage" class="detail-label-img" mode="widthFix" />
        </view>
      </view>

      <view class="detail-info-section">
        <text class="detail-section-title">Print Info</text>
        <view class="info-card">
          <view class="info-row">
            <text class="info-label">Label ID</text>
            <text class="info-value">{{ record.labelId }}</text>
          </view>
          <view class="info-row">
            <text class="info-label">Print Time</text>
            <text class="info-value">{{ record.dateFull }} {{ record.time }}</text>
          </view>
          <view class="info-row">
            <text class="info-label">Product</text>
            <text class="info-value">{{ displayProductName }}</text>
          </view>
          <view class="info-row">
            <text class="info-label">Quantity</text>
            <text class="info-value">{{ record.qty }} label{{ record.qty > 1 ? 's' : '' }}</text>
          </view>
          <view v-if="record.labelType" class="info-row">
            <text class="info-label">Label Type</text>
            <text class="info-value">{{ record.labelType }}</text>
          </view>
          <view class="info-row">
            <text class="info-label">Template</text>
            <text class="info-value">{{ record.templateSize }} {{ record.templateName }}</text>
          </view>
          <view class="info-row">
            <text class="info-label">Printed By</text>
            <text class="info-value">{{ record.userName }}</text>
          </view>
          <view class="info-row">
            <text class="info-label">Printer</text>
            <text class="info-value">{{ record.printer }}</text>
          </view>
        </view>
      </view>
    </scroll-view>

    <view v-else class="empty-wrap">
      <AppIcon name="alert" size="lg" color="gray" />
      <text class="empty-text">Record not found</text>
    </view>

    <SideMenu v-model="isMenuOpen" />
  </view>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'
import { onLoad } from '@dcloudio/uni-app'
import AppIcon from '../../components/AppIcon.vue'
import SideMenu from '../../components/SideMenu.vue'
import LocationPicker from '../../components/LocationPicker.vue'
import { getStatusBarHeight } from '../../utils/statusBar'
import { getRecordByLabelId } from '../../utils/printLog'
import chickenLabelImg from '../../static/chicken-lable.png'

const statusBarHeight = getStatusBarHeight()
const isMenuOpen = ref(false)
const record = ref<any>(null)

const labelImage = computed(() => {
  const r = record.value
  if (!r) return '/static/lable1.png'
  if (r.productName === 'Chicken') {
    return chickenLabelImg
  }
  const size = r.templateSize || ''
  if (size.indexOf('2"x6"') >= 0 || size.indexOf('2"x4"') >= 0) {
    return '/static/lable2.png'
  }
  return '/static/lable1.png'
})

// 与产品列表、详情一致：按标签图 Chicken/Syrup/Cheese Burger Deluxe
const displayProductName = computed(() => {
  const r = record.value
  if (!r) return ''
  if (r.productName === 'Chicken') return 'Chicken'
  const size = r.templateSize || ''
  if (size.indexOf('2"x6"') >= 0 || size.indexOf('2"x4"') >= 0) return 'Cheese Burger Deluxe'
  return 'Syrup'
})

onLoad((opts: any) => {
  const labelId = opts && opts.labelId
  if (labelId) {
    record.value = getRecordByLabelId(labelId)
  }
})

const goBack = () => uni.navigateBack()
</script>

<style scoped>
.page {
  min-height: 100vh;
  background: #f9fafb;
  display: flex;
  flex-direction: column;
  overflow-x: hidden;
}

.header-hero {
  background: linear-gradient(135deg, var(--theme-primary), var(--theme-primary-dark));
  padding: 16rpx 32rpx 24rpx;
}

.top-bar {
  height: 96rpx;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.top-left,
.top-right {
  width: 64rpx;
  height: 64rpx;
  border-radius: 999rpx;
  background: rgba(255, 255, 255, 0.15);
  display: flex;
  align-items: center;
  justify-content: center;
}

.top-center {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.page-title {
  font-size: 34rpx;
  font-weight: 600;
  color: #fff;
}

.content {
  flex: 1;
  padding: 32rpx;
  box-sizing: border-box;
  overflow-x: hidden;
}

.detail-label-section {
  margin-bottom: 32rpx;
}

.detail-section-title {
  font-size: 28rpx;
  font-weight: 600;
  color: #111827;
  display: block;
  margin-bottom: 20rpx;
}

.detail-label-wrap {
  width: 100%;
  padding: 24rpx 0;
  background: #fff;
  border-radius: 16rpx;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.04);
  overflow: hidden;
  box-sizing: border-box;
}

.detail-label-img {
  width: 100%;
  max-width: 100%;
  display: block;
  border-radius: 12rpx;
  box-sizing: border-box;
}

.detail-info-section {
  padding-top: 0;
}

.info-card {
  background: #fff;
  border-radius: 16rpx;
  padding: 28rpx 32rpx;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.04);
}

.info-row {
  padding: 20rpx 0;
  border-bottom: 1rpx solid #f3f4f6;
}

.info-row:last-child {
  border-bottom: none;
}

.info-label {
  font-size: 24rpx;
  color: #6b7280;
  display: block;
  margin-bottom: 8rpx;
}

.info-value {
  font-size: 30rpx;
  font-weight: 500;
  color: #111827;
  display: block;
}

.empty-wrap {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 48rpx;
}

.empty-text {
  font-size: 28rpx;
  color: #6b7280;
  margin-top: 24rpx;
}
</style>
