<script setup>
import { ref, watch } from 'vue'
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 loadRecords = async (pkg) => {
  if (!pkg) {
    records.value = []
    return
  }
  loading.value = true
  try {
    const result = await getPackageRecords(pkg.id, { limit: 500 })
    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-table
        v-else-if="contentMode === 'table'"
        :data="records"
        border
        stripe
        height="calc(100vh - 250px)"
      >
        <el-table-column prop="time" label="时间" min-width="140" />
        <el-table-column prop="current" label="电流" min-width="90" />
        <el-table-column prop="voltage" label="电压" min-width="90" />
        <el-table-column prop="set_temperature" label="设定温度" min-width="100" />
        <el-table-column prop="actual_temperature" label="实际温度" min-width="100" />
      </el-table>

      <DataCurve v-else :records="records" />
    </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>
