<script setup>
import { computed, ref, watch } from 'vue'
import { ElAutoResizer, ElTableV2 } from 'element-plus'
import { getPackageRecords } from '@/api/packageManagement'
import DataCurve from '@/views/DataManagement/components/DataCurve.vue'

const props = defineProps({
  package: {
    type: Object,
    default: null,
  },
  style: {
    type: Object,
    default: () => ({}),
  },
})

const loading = ref(false)
const records = ref([])
const contentMode = ref('table')

const detailColumns = [
  { key: 'time', dataKey: 'time', title: '时间', width: 170 },
  { key: 'current', dataKey: 'current', title: '电流', width: 110 },
  { key: 'voltage', dataKey: 'voltage', title: '电压', width: 110 },
  { key: 'set_temperature', dataKey: 'set_temperature', title: '设定温度', width: 120 },
  { key: 'actual_temperature', dataKey: 'actual_temperature', title: '实际温度', width: 120 },
]

const curveRecords = computed(() => {
  const r = records.value
  if (r.length <= 2000) return r
  const step = Math.ceil(r.length / 2000)
  return r.filter((_, i) => i % step === 0)
})

const loadRecords = async (pkg) => {
  if (!pkg) {
    records.value = []
    return
  }
  loading.value = true
  try {
    const result = await getPackageRecords(pkg.id)
    records.value = result.records
  } finally {
    loading.value = false
  }
}

watch(() => props.package, (pkg) => {
  contentMode.value = 'table'
  loadRecords(pkg)
}, { immediate: true })
</script>

<template>
  <el-card class="pane-card" shadow="hover" :style="props.style">
    <template #header>
      <div class="pane-header">
        <span>
          数据包内容
          <span v-if="props.package" class="pkg-title">— {{ props.package.name }}</span>
        </span>
        <el-radio-group v-model="contentMode" size="small">
          <el-radio-button value="table">表格</el-radio-button>
          <el-radio-button value="curve">曲线</el-radio-button>
        </el-radio-group>
      </div>
    </template>

    <div class="content-wrap" v-loading="loading">
      <el-empty v-if="!props.package" description="请选择一个数据包查看" />

      <el-auto-resizer v-else-if="contentMode === 'table'">
        <template #default="{ height, width }">
          <el-table-v2
            :columns="detailColumns"
            :data="records"
            :width="width"
            :height="height"
            :row-height="36"
            :header-height="40"
            fixed
          />
        </template>
      </el-auto-resizer>

      <DataCurve v-else :records="curveRecords" :total-count="records.length" />
    </div>
  </el-card>
</template>

<style lang="scss" scoped>
.pane-card {
  height: 100%;
  overflow: hidden;
  border-radius: 0;
  border: none;
  box-shadow: none !important;
  display: flex;
  flex-direction: column;
  background: var(--bg-white);

  :deep(.el-card__header) {
    padding: 0 16px;
    height: 44px;
    display: flex;
    align-items: center;
    border-bottom: 1px solid var(--border-color);
    background: #F7F8FA;
  }

  :deep(.el-card__body) {
    flex: 1;
    overflow: hidden;
    padding: 0;
  }
}

.pane-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  font-size: 14px;
  font-weight: 600;
  color: var(--text-primary);
}

.pkg-title {
  font-weight: 400;
  color: var(--text-secondary);
  font-size: 13px;
  margin-left: 4px;
}

.content-wrap {
  height: 100%;
  padding: 0 16px 12px;
}

:deep(.el-radio-group) {
  .el-radio-button__inner {
    background: transparent;
    color: var(--text-secondary);
    border-color: var(--border-color);
    font-size: 13px;
    padding: 4px 12px;
  }

  .el-radio-button__original-radio:checked + .el-radio-button__inner {
    background: var(--primary) !important;
    border-color: var(--primary) !important;
    color: #fff;
    box-shadow: none !important;
  }
}
</style>
