<script setup lang="ts">
import {
  formatAttachmentSize,
  getAttachmentDisplayName,
  getFileTypeLabel,
  getFileTypeMeta,
  isAttachmentImage,
} from '@af-mobile-client-vue3/utils/fileType'
import { resolveResourceUrl } from '@af-mobile-client-vue3/utils/resourceUrl'
import { Image as VanImage } from 'vant'
import { computed } from 'vue'

interface AttachmentFile {
  id?: string | number
  url?: string
  f_downloadpath?: string
  real_name?: string
  name?: string
  size?: number
  upload_time?: string
}

const props = withDefaults(defineProps<{
  file: AttachmentFile
  thumbSize?: number
}>(), {
  thumbSize: 48,
})

const emit = defineEmits<{
  preview: [file: AttachmentFile]
}>()

const displayName = computed(() => getAttachmentDisplayName(props.file))
const isImage = computed(() => isAttachmentImage(props.file))
const typeMeta = computed(() => getFileTypeMeta(displayName.value))
const typeLabel = computed(() => getFileTypeLabel(displayName.value))
const thumbStyle = computed(() => ({
  width: `${props.thumbSize}px`,
  height: `${props.thumbSize}px`,
  minWidth: `${props.thumbSize}px`,
}))

function handlePreview() {
  emit('preview', props.file)
}
</script>

<template>
  <div class="attachment-list-item" @click="handlePreview">
    <VanImage
      v-if="isImage"
      :src="resolveResourceUrl(file.url || file.f_downloadpath)"
      :width="thumbSize"
      :height="thumbSize"
      fit="cover"
      class="attachment-thumb"
      :style="thumbStyle"
      :alt="displayName"
    />
    <div
      v-else
      class="attachment-type-badge"
      :style="{ ...thumbStyle, background: typeMeta.color }"
    >
      {{ typeMeta.ext }}
    </div>

    <div class="file-content">
      <div class="file-header">
        <span class="file-name">{{ displayName }}</span>
        <span class="file-type">{{ typeLabel }}</span>
      </div>
      <div v-if="file.size || file.upload_time" class="file-footer">
        <span v-if="file.size" class="file-size">{{ formatAttachmentSize(file.size) }}</span>
        <span v-if="file.upload_time" class="file-time">{{ file.upload_time }}</span>
      </div>
    </div>
  </div>
</template>

<style scoped lang="less">
.attachment-list-item {
  display: flex;
  align-items: center;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.07);
  padding: 10px 12px;
  cursor: pointer;
  transition: box-shadow 0.2s;
  min-height: 56px;
  gap: 12px;

  &:active {
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.12);
  }
}

.attachment-thumb,
.attachment-type-badge {
  border-radius: 6px;
  flex-shrink: 0;
}

.attachment-type-badge {
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: 10px;
  font-weight: 700;
}

.file-content {
  flex: 1;
  min-width: 0;
}

.file-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 4px;
}

.file-name {
  flex: 1;
  min-width: 0;
  font-size: 14px;
  font-weight: 500;
  color: #333;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.file-type {
  flex-shrink: 0;
  font-size: 11px;
  color: #1890ff;
  background: rgba(24, 144, 255, 0.08);
  padding: 2px 6px;
  border-radius: 4px;
}

.file-footer {
  display: flex;
  align-items: center;
  font-size: 12px;
  color: rgba(0, 0, 0, 0.45);
}

.file-size {
  margin-right: 12px;
}

.file-time {
  position: relative;
  padding-left: 12px;

  &::before {
    content: '';
    position: absolute;
    left: 0;
    top: 50%;
    transform: translateY(-50%);
    width: 4px;
    height: 4px;
    background: rgba(0, 0, 0, 0.25);
    border-radius: 50%;
  }
}
</style>
