<!-- MetricCategory.vue - 一级指标组件 -->
<template>
  <div class="metric-category" @click="activeCategory(category.id)" :class="{ activeCategory: activeId === category.id }">
    <el-card class="category-card">
        <div class="card-container">
          <div class="category-info">
            <div><el-tag type="primary">一级指标</el-tag></div>
            <span class="category-name">{{ category.name }}</span>
          </div>
          <div class="category-actions">
            <el-button size="small" @click="handleEdit" :icon="Edit" />
            <el-button size="small" type="danger" @click="handleRemove" :icon="Delete" />
          </div>
        </div>
    </el-card>

    <!-- 编辑一级指标对话框 -->
    <el-dialog v-model="showEditDialog" title="编辑一级指标" width="500px">
      <MetricForm
        ref="metricFormRef"
        :metric="editingCategory"
        :isNew="false"
        @refresh="handleRefresh"
      />
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="showEditDialog = false">取消</el-button>
          <el-button type="primary" @click="submitMetricForm">确认111</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import MetricForm from './MetricForm.vue'
import { Delete, Edit } from '@element-plus/icons-vue'

const props = defineProps({
  category: {
    type: Object,
    required: true
  },
  // 新增props：接收父组件的全局激活ID（支持字符串/数字/空值）
  activeId: {
    type: [String, Number],
    default: ''
  }
})

const emit = defineEmits(['update-category', 'remove-category', 'active'])

const showEditDialog = ref(false)
const metricFormRef = ref(null)
const editingCategory = reactive({ ...props.category })


const handleEdit = () => {
  Object.assign(editingCategory, props.category)
  showEditDialog.value = true
}

const handleRemove = () => {
  emit('remove-category', props.category.id)
}

const submitMetricForm = () => {
  if (metricFormRef.value) {
    metricFormRef.value.submitForm()
  }
}

const handleRefresh = () => {
  emit('refresh')
  showEditDialog.value = false
}

const activeCategory = (categoryId) => {
  // 可以在这里处理点击一级指标卡片时的逻辑
  console.log("点击了一级指标:", categoryId);
  emit('active', categoryId) // 通知父组件更新全局激活ID
}
</script>

<style lang="scss" scoped>
.metric-category {
  cursor: pointer;

  .category-card {
    border-radius: 8px;

    .card-container {
      display: flex;
      justify-content: space-between;
      align-items: center;

      .category-info {
        display: flex;
        align-items: center;
        gap: 12px;

        .category-name {
          font-size: 14px;
          color: #303133;
        }
      }
    }
  }
}

.activeCategory {
  border: 2px solid #409EFF;
  border-radius: 8px;
}

</style>