<!-- File详情页工具栏组件 -->
<template>
  <div class="file-toolbar">
    <n-space>
      <n-button type="default" @click="viewFile" v-if="context.record.file_url">
        <template #icon>
          <n-icon><Icon icon="tabler:eye" /></n-icon>
        </template>
        {{ context.t('View File') }}
      </n-button>
      
      <n-button type="default" @click="downloadFile" v-if="context.record.file_url && !context.record.is_folder">
        <template #icon>
          <n-icon><Icon icon="tabler:download" /></n-icon>
        </template>
        {{ context.t('Download') }}
      </n-button>
      
      <n-button type="default" @click="optimizeFile" v-if="isOptimizable">
        <template #icon>
          <n-icon><Icon icon="tabler:compress" /></n-icon>
        </template>
        {{ context.t('Optimize') }}
      </n-button>
      
      <n-button type="default" @click="unzipFile" v-if="isZipFile">
        <template #icon>
          <n-icon><Icon icon="tabler:archive" /></n-icon>
        </template>
        {{ context.t('Unzip') }}
      </n-button>
    </n-space>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { NButton, NSpace, NIcon } from 'naive-ui'
import { Icon } from '@iconify/vue'

const props = defineProps<{
  context: {
    entity: string
    id: string
    record: any
    canEdit: boolean
    loading: boolean
    save: () => Promise<void>
    router: any
    t: (key: string) => string
  }
}>()

// 检查是否可优化（图片文件且大小大于0）
const isOptimizable = computed(() => {
  const record = props.context.record
  if (record.is_folder || !record.file_size || record.file_size <= 0) return false
  
  const isRasterImage = /\.(gif|jpg|jpeg|tiff|png)$/i.test(record.file_url)
  return isRasterImage
})

// 检查是否是ZIP文件
const isZipFile = computed(() => {
  const record = props.context.record
  return record.file_name && record.file_name.split('.').pop() === 'zip'
})

// 查看文件
const viewFile = () => {
  const record = props.context.record
  if (!record.file_url) return
  
  if (record.file_url.startsWith('http')) {
    window.open(record.file_url)
  } else {
    window.open(window.location.origin + record.file_url)
  }
}

// 下载文件
const downloadFile = () => {
  const record = props.context.record
  if (!record.file_url) return
  
  let fileUrl = record.file_url
  if (record.file_name) {
    fileUrl = fileUrl.replace(/#/g, '%23')
  }
  window.open(fileUrl)
}

// 优化文件
const optimizeFile = () => {
  // TODO: 实现文件优化功能
  console.log('Optimize file:', props.context.record.name)
}

// 解压文件
const unzipFile = () => {
  // TODO: 实现文件解压功能
  console.log('Unzip file:', props.context.record.name)
}
</script>

<style scoped>
.file-toolbar {
  display: flex;
  align-items: center;
}
</style>