<template>
  <div class="chart-block" :class="{ 'edit-mode': editMode }">
    <WorkspaceChartWidget
      :chart-name="chartName"
      :label="label"
      :chart-settings="chartSettings"
      :edit-mode="editMode"
      :height="height"
    />
    <div v-if="editMode" class="card-actions">
      <button class="action-btn" @click="$emit('edit')" :title="t('Edit')">
        <Icon icon="tabler:edit" />
      </button>
      <button class="action-btn drag-handle" :title="t('Drag')">
        <Icon icon="tabler:grip-vertical" />
      </button>
      <n-dropdown trigger="click" :options="menuOptions" @select="handleMenuSelect">
        <button class="action-btn" :title="t('More')">
          <Icon icon="tabler:dots" />
        </button>
      </n-dropdown>
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed, h } from 'vue'
import { Icon } from '@iconify/vue'
import { NDropdown } from 'naive-ui'
import { t } from '@/shared/i18n'
import WorkspaceChartWidget from './WorkspaceChartWidget.vue'

const props = defineProps<{
  chartName: string
  label: string
  chartSettings: Record<string, any>
  editMode: boolean
  height?: number
}>()

const emit = defineEmits<{
  'edit': []
  'delete': []
}>()

const menuOptions = computed(() => [
  { label: t('Delete'), key: 'delete', icon: () => h(Icon, { icon: 'tabler:trash', size: 16 }) }
])

function handleMenuSelect(key: string) {
  if (key === 'delete') {
    emit('delete')
  }
}
</script>

<style scoped>
.chart-block {
  background: #fff;
  border: 1px solid #eef2f7;
  border-radius: 12px;
  padding: 16px;
  position: relative;
  transition: all 0.2s;
  box-shadow: 0 2px 8px rgba(15,23,42,0.04);
}

.edit-mode .chart-block {
  border: 2px dashed transparent;
}

.card-actions {
  display: flex;
  gap: 4px;
  align-items: center;
  opacity: 0;
  transition: opacity 0.2s;
  position: absolute;
  top: 8px;
  right: 8px;
}

.edit-mode:hover .card-actions {
  opacity: 1;
}

.action-btn {
  background: transparent;
  border: none;
  color: #64748b;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 6px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: all 0.15s;
  font-size: 16px;
}

.action-btn:hover {
  background: #f1f5f9;
  color: #0f172a;
}

.drag-handle {
  cursor: grab;
}

.drag-handle:active {
  cursor: grabbing;
}
</style>
