<script setup lang="ts">
import {
  showImagePreview,
  Icon as VanIcon,
  Image as VanImage,
} from 'vant'
import { ref } from 'vue'

// props
const props = defineProps({
  files: {
    type: Array<any>,
    required: true,
  },
  downloadable: {
    type: Boolean,
    default: true,
  },
})

// data
const previewVisible = ref(false)
const previewPath = ref('')

// methods
function handlePreview(path) {
  previewPath.value = path
  showImagePreview([path])
  previewVisible.value = true
}

function formatFileSize(size) {
  if (!size)
    return ''
  if (size < 1)
    return `${(size * 1024).toFixed(1)} KB`
  return `${size.toFixed(1)} MB`
}

function getFileType(filename) {
  const ext = filename.split('.').pop().toLowerCase()
  const typeMap = {
    pdf: 'PDF文档',
    doc: 'Word文档',
    docx: 'Word文档',
    xls: 'Excel表格',
    xlsx: 'Excel表格',
    ppt: 'PPT演示',
    pptx: 'PPT演示',
    jpg: '图片',
    jpeg: '图片',
    png: '图片',
    gif: '图片',
    txt: '文本文件',
    zip: '压缩文件',
    rar: '压缩文件',
  }
  return typeMap[ext] || '文件'
}

function getFileIcon(filename) {
  const ext = filename.split('.').pop().toLowerCase()
  const iconMap = {
    pdf: 'info-o',
    doc: 'info-o',
    docx: 'info-o',
    xls: 'info-o',
    xlsx: 'info-o',
    ppt: 'info-o',
    pptx: 'info-o',
    jpg: 'photo-o',
    jpeg: 'photo-o',
    png: 'photo-o',
    gif: 'photo-o',
    txt: 'sort-list',
    zip: 'send-gift-o',
    rar: 'send-gift-o',
  }
  return iconMap[ext] || 'file'
}

function getFileTypeClass(filename) {
  const ext = filename.split('.').pop().toLowerCase()
  return `file-type-${ext}`
}
function isImage(file: any) {
  return /\.(?:png|jpe?g|gif|webp|bmp|svg)$/i.test(file.url || file.name || '')
}
</script>

<template>
  <div class="file-list">
    <div class="file-grid">
      <div v-for="file in props.files" :key="file.id" class="file-item" @click="handlePreview(file.url)">
        <div class="file-info">
          <VanImage v-if="isImage(file)" :src="file.url" class="file-thumb" />
          <div v-else class="file-icon-wrapper" :class="getFileTypeClass(file.name)">
            <VanIcon :name="getFileIcon(file.name)" class="file-icon" />
          </div>
          <div class="file-content">
            <div class="file-header">
              <span class="file-name">{{ file.real_name || file.name }}</span>
              <span class="file-type">{{ getFileType(file.name) }}</span>
            </div>
            <div class="file-footer">
              <span v-if="file.size" class="file-size">{{ formatFileSize(file.size) }}</span>
              <span v-if="file.upload_time" class="file-time">{{ file.upload_time }}</span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style lang="less" scoped>
.file-list {
  .file-grid {
    display: grid;
  }

  .file-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px;
    background: #fff;
    border: 1px solid rgba(0, 0, 0, 0.06);

    .file-info {
      display: flex;
      align-items: center;
      flex: 1;
      min-width: 0;
      .file-thumb {
        width: 48px;
        height: 48px;
        object-fit: cover;
        margin-right: 16px;
        border-radius: 12px;
        box-shadow: 0 1px 4px rgba(0, 0, 0, 0.08);
      }
      .file-icon-wrapper {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 48px;
        height: 48px;
        margin-right: 16px;
        border-radius: 12px;
        transition: all 0.3s;

        &.file-type-pdf {
          background: linear-gradient(135deg, #ff4d4f, #ff7875);
        }
        &.file-type-doc,
        &.file-type-docx {
          background: linear-gradient(135deg, #1890ff, #69c0ff);
        }
        &.file-type-xls,
        &.file-type-xlsx {
          background: linear-gradient(135deg, #52c41a, #95de64);
        }
        &.file-type-ppt,
        &.file-type-pptx {
          background: linear-gradient(135deg, #fa8c16, #ffc069);
        }
        &.file-type-jpg,
        &.file-type-jpeg,
        &.file-type-png,
        &.file-type-gif {
          background: linear-gradient(135deg, #722ed1, #b37feb);
        }
        &.file-type-txt {
          background: linear-gradient(135deg, #13c2c2, #5cdbd3);
        }
        &.file-type-zip,
        &.file-type-rar {
          background: linear-gradient(135deg, #eb2f96, #ff85c0);
        }

        .file-icon {
          font-size: 24px;
          color: #fff;
        }
      }

      .file-content {
        display: flex;
        flex-direction: column;
        min-width: 0;
        flex: 1;

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

          .file-name {
            color: rgba(0, 0, 0, 0.85);
            font-size: 15px;
            font-weight: 500;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
            margin-right: 8px;
            width: 0;
            flex: 1;
          }

          .file-type {
            color: rgba(0, 0, 0, 0.45);
            font-size: 12px;
            padding: 2px 6px;
            background: rgba(0, 0, 0, 0.04);
            border-radius: 4px;
          }
        }

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

          .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%;
            }
          }
        }
      }
    }

    .file-actions {
      display: flex;
      align-items: center;
      margin-left: 16px;

      .action-btn {
        width: 36px;
        height: 36px;
        padding: 0;
        margin-left: 8px;
        border-radius: 8px;
        color: rgba(0, 0, 0, 0.45);
        background: transparent;
        transition: all 0.3s;

        &:hover {
          color: #fff;
          background: #1890ff;
          transform: scale(1.05);
        }

        .anticon {
          font-size: 16px;
        }
      }
    }
  }
}
</style>
