<template>
  <div class="min-h-screen bg-gray-100 dark:bg-gray-900">
    <!-- 顶部导航 -->
    <div class="bg-white dark:bg-gray-800 shadow">
      <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-4">
        <div class="flex justify-between items-center">
          <h1 class="text-2xl font-bold text-gray-900 dark:text-white">文档管理</h1>
          <div class="flex space-x-4">
            <button @click="showUploadModal = true" class="bg-blue-600 text-white px-4 py-2 rounded-md hover:bg-blue-700">
              上传文档
            </button>
            <button @click="showShareModal = true" class="bg-green-600 text-white px-4 py-2 rounded-md hover:bg-green-700">
              共享文档
            </button>
            <button @click="exportDocumentData" class="bg-purple-600 text-white px-4 py-2 rounded-md hover:bg-purple-700">
              导出数据
            </button>
          </div>
        </div>
      </div>
    </div>

    <!-- 主要内容区域 -->
    <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
      <!-- 文档统计卡片 -->
      <div class="grid grid-cols-1 md:grid-cols-4 gap-6 mb-6">
        <div class="bg-white dark:bg-gray-800 rounded-lg shadow p-6">
          <h3 class="text-lg font-medium text-gray-900 dark:text-white">总文档数</h3>
          <p class="mt-2 text-3xl font-bold text-blue-600 dark:text-blue-400">{{ totalDocuments }}</p>
        </div>
        <div class="bg-white dark:bg-gray-800 rounded-lg shadow p-6">
          <h3 class="text-lg font-medium text-gray-900 dark:text-white">共享文档</h3>
          <p class="mt-2 text-3xl font-bold text-green-600 dark:text-green-400">{{ sharedDocuments }}</p>
        </div>
        <div class="bg-white dark:bg-gray-800 rounded-lg shadow p-6">
          <h3 class="text-lg font-medium text-gray-900 dark:text-white">最近更新</h3>
          <p class="mt-2 text-3xl font-bold text-yellow-600 dark:text-yellow-400">{{ recentUpdates }}</p>
        </div>
        <div class="bg-white dark:bg-gray-800 rounded-lg shadow p-6">
          <h3 class="text-lg font-medium text-gray-900 dark:text-white">存储空间</h3>
          <p class="mt-2 text-3xl font-bold text-red-600 dark:text-red-400">{{ storageUsed }}GB</p>
        </div>
      </div>

      <!-- 搜索和筛选 -->
      <div class="bg-white dark:bg-gray-800 rounded-lg shadow p-6 mb-6">
        <div class="grid grid-cols-1 md:grid-cols-4 gap-4">
          <div>
            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">文档类型</label>
            <select v-model="filters.type" class="w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:text-white">
              <option value="">全部</option>
              <option v-for="type in documentTypes" :key="type.id" :value="type.id">{{ type.name }}</option>
            </select>
          </div>
          <div>
            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">共享状态</label>
            <select v-model="filters.shareStatus" class="w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:text-white">
              <option value="">全部</option>
              <option v-for="status in shareStatuses" :key="status.id" :value="status.id">{{ status.name }}</option>
            </select>
          </div>
          <div>
            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">创建时间</label>
            <div class="flex space-x-2">
              <input type="date" v-model="filters.startDate" class="w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:text-white">
              <input type="date" v-model="filters.endDate" class="w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:text-white">
            </div>
          </div>
          <div>
            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">搜索</label>
            <input type="text" v-model="filters.search" placeholder="文档名称/描述" class="w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:text-white">
          </div>
        </div>
      </div>

      <!-- 文档列表 -->
      <div class="bg-white dark:bg-gray-800 rounded-lg shadow overflow-hidden">
        <table class="min-w-full divide-y divide-gray-200 dark:divide-gray-700">
          <thead class="bg-gray-50 dark:bg-gray-700">
            <tr>
              <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-300 uppercase tracking-wider">文档信息</th>
              <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-300 uppercase tracking-wider">版本信息</th>
              <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-300 uppercase tracking-wider">共享状态</th>
              <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-300 uppercase tracking-wider">操作</th>
            </tr>
          </thead>
          <tbody class="bg-white dark:bg-gray-800 divide-y divide-gray-200 dark:divide-gray-700">
            <tr v-for="document in filteredDocuments" :key="document.id">
              <td class="px-6 py-4">
                <div class="flex items-center">
                  <div class="flex-shrink-0 h-10 w-10">
                    <img :src="getFileIcon(document.type)" :alt="document.name" class="h-10 w-10 rounded-full">
                  </div>
                  <div class="ml-4">
                    <div class="text-sm font-medium text-gray-900 dark:text-white">{{ document.name }}</div>
                    <div class="text-sm text-gray-500 dark:text-gray-400">{{ document.description }}</div>
                    <div class="text-sm text-gray-500 dark:text-gray-400">{{ formatDate(document.createdAt) }}</div>
                  </div>
                </div>
              </td>
              <td class="px-6 py-4">
                <div class="text-sm text-gray-900 dark:text-white">版本: {{ document.version }}</div>
                <div class="text-sm text-gray-500 dark:text-gray-400">大小: {{ formatFileSize(document.size) }}</div>
                <div class="text-sm text-gray-500 dark:text-gray-400">更新: {{ formatDate(document.updatedAt) }}</div>
              </td>
              <td class="px-6 py-4 whitespace-nowrap">
                <span :class="getShareStatusClass(document.shareStatus)" class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full">
                  {{ getShareStatusName(document.shareStatus) }}
                </span>
                <div v-if="document.sharedWith" class="text-sm text-gray-500 dark:text-gray-400 mt-1">
                  共享给: {{ document.sharedWith }}
                </div>
              </td>
              <td class="px-6 py-4 whitespace-nowrap text-sm font-medium">
                <button @click="downloadDocument(document)" class="text-blue-600 hover:text-blue-900 dark:text-blue-400 dark:hover:text-blue-300 mr-3">下载</button>
                <button @click="viewDocument(document)" class="text-green-600 hover:text-green-900 dark:text-green-400 dark:hover:text-green-300 mr-3">查看</button>
                <button @click="editDocument(document)" class="text-yellow-600 hover:text-yellow-900 dark:text-yellow-400 dark:hover:text-yellow-300 mr-3">编辑</button>
                <button @click="deleteDocument(document)" class="text-red-600 hover:text-red-900 dark:text-red-400 dark:hover:text-red-300">删除</button>
              </td>
            </tr>
          </tbody>
        </table>
      </div>
    </div>

    <!-- 上传文档模态框 -->
    <div v-if="showUploadModal" class="fixed inset-0 bg-gray-500 bg-opacity-75 flex items-center justify-center">
      <div class="bg-white dark:bg-gray-800 rounded-lg shadow-xl max-w-md w-full p-6">
        <h2 class="text-xl font-bold mb-4 text-gray-900 dark:text-white">上传文档</h2>
        <form @submit.prevent="uploadDocument">
          <div class="mb-4">
            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">选择文件</label>
            <input type="file" @change="handleFileChange" class="w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:text-white">
          </div>
          <div class="mb-4">
            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">文档名称</label>
            <input type="text" v-model="newDocument.name" required class="w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:text-white">
          </div>
          <div class="mb-4">
            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">文档类型</label>
            <select v-model="newDocument.type" required class="w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:text-white">
              <option v-for="type in documentTypes" :key="type.id" :value="type.id">{{ type.name }}</option>
            </select>
          </div>
          <div class="mb-4">
            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">文档描述</label>
            <textarea v-model="newDocument.description" class="w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:text-white" rows="4"></textarea>
          </div>
          <div class="flex justify-end space-x-3">
            <button type="button" @click="showUploadModal = false" class="px-4 py-2 border border-gray-300 rounded-md text-gray-700 dark:text-gray-300 hover:bg-gray-50 dark:hover:bg-gray-700">
              取消
            </button>
            <button type="submit" class="px-4 py-2 bg-blue-600 text-white rounded-md hover:bg-blue-700">
              上传
            </button>
          </div>
        </form>
      </div>
    </div>

    <!-- 共享文档模态框 -->
    <div v-if="showShareModal" class="fixed inset-0 bg-gray-500 bg-opacity-75 flex items-center justify-center">
      <div class="bg-white dark:bg-gray-800 rounded-lg shadow-xl max-w-md w-full p-6">
        <h2 class="text-xl font-bold mb-4 text-gray-900 dark:text-white">共享文档</h2>
        <form @submit.prevent="shareDocument">
          <div class="mb-4">
            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">选择文档</label>
            <select v-model="shareForm.documentId" required class="w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:text-white">
              <option v-for="doc in myDocuments" :key="doc.id" :value="doc.id">{{ doc.name }}</option>
            </select>
          </div>
          <div class="mb-4">
            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">共享对象</label>
            <select v-model="shareForm.sharedWith" required class="w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:text-white">
              <option v-for="user in users" :key="user.id" :value="user.id">{{ user.name }}</option>
            </select>
          </div>
          <div class="mb-4">
            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">权限设置</label>
            <select v-model="shareForm.permission" required class="w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:text-white">
              <option v-for="permission in permissions" :key="permission.id" :value="permission.id">{{ permission.name }}</option>
            </select>
          </div>
          <div class="flex justify-end space-x-3">
            <button type="button" @click="showShareModal = false" class="px-4 py-2 border border-gray-300 rounded-md text-gray-700 dark:text-gray-300 hover:bg-gray-50 dark:hover:bg-gray-700">
              取消
            </button>
            <button type="submit" class="px-4 py-2 bg-green-600 text-white rounded-md hover:bg-green-700">
              共享
            </button>
          </div>
        </form>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'

// 文档数据
const documents = ref([
  {
    id: 1,
    name: '项目需求文档',
    description: '详细描述项目需求和功能规格',
    type: 1,
    version: '1.0.0',
    size: 1024 * 1024 * 2, // 2MB
    createdAt: '2024-03-01',
    updatedAt: '2024-03-15',
    shareStatus: 1,
    sharedWith: '李四, 王五'
  },
  {
    id: 2,
    name: '技术方案设计',
    description: '系统架构和技术选型说明',
    type: 2,
    version: '1.1.0',
    size: 1024 * 1024 * 3, // 3MB
    createdAt: '2024-03-05',
    updatedAt: '2024-03-18',
    shareStatus: 2,
    sharedWith: '张三, 赵六'
  }
])

// 文档类型
const documentTypes = [
  { id: 1, name: '需求文档' },
  { id: 2, name: '设计文档' },
  { id: 3, name: '测试文档' },
  { id: 4, name: '用户手册' },
  { id: 5, name: '其他文档' }
]

// 共享状态
const shareStatuses = [
  { id: 1, name: '已共享' },
  { id: 2, name: '未共享' }
]

// 用户列表
const users = [
  { id: 1, name: '张三' },
  { id: 2, name: '李四' },
  { id: 3, name: '王五' },
  { id: 4, name: '赵六' }
]

// 权限设置
const permissions = [
  { id: 1, name: '只读' },
  { id: 2, name: '可编辑' },
  { id: 3, name: '完全控制' }
]

// 筛选条件
const filters = ref({
  type: '',
  shareStatus: '',
  startDate: '',
  endDate: '',
  search: ''
})

// 模态框状态
const showUploadModal = ref(false)
const showShareModal = ref(false)

// 新文档表单
const newDocument = ref({
  name: '',
  type: '',
  description: ''
})

// 共享表单
const shareForm = ref({
  documentId: '',
  sharedWith: '',
  permission: ''
})

// 计算属性
const filteredDocuments = computed(() => {
  return documents.value.filter(doc => {
    const matchesType = !filters.value.type || doc.type === filters.value.type
    const matchesShareStatus = !filters.value.shareStatus || doc.shareStatus === filters.value.shareStatus
    const matchesDate = (!filters.value.startDate || doc.createdAt >= filters.value.startDate) &&
                       (!filters.value.endDate || doc.createdAt <= filters.value.endDate)
    const matchesSearch = !filters.value.search ||
      doc.name.toLowerCase().includes(filters.value.search.toLowerCase()) ||
      doc.description.toLowerCase().includes(filters.value.search.toLowerCase())
    return matchesType && matchesShareStatus && matchesDate && matchesSearch
  })
})

const totalDocuments = computed(() => {
  return documents.value.length
})

const sharedDocuments = computed(() => {
  return documents.value.filter(doc => doc.shareStatus === 1).length
})

const recentUpdates = computed(() => {
  const today = new Date()
  const sevenDaysAgo = new Date(today.getTime() - 7 * 24 * 60 * 60 * 1000)
  return documents.value.filter(doc => new Date(doc.updatedAt) >= sevenDaysAgo).length
})

const storageUsed = computed(() => {
  const totalSize = documents.value.reduce((sum, doc) => sum + doc.size, 0)
  return (totalSize / (1024 * 1024 * 1024)).toFixed(2) // 转换为GB
})

const myDocuments = computed(() => {
  return documents.value.filter(doc => doc.shareStatus === 2)
})

// 获取文件图标
const getFileIcon = (typeId) => {
  const icons = {
    1: 'https://cdn-icons-png.flaticon.com/512/281/281760.png', // 文档图标
    2: 'https://cdn-icons-png.flaticon.com/512/2097/2097743.png', // 设计图标
    3: 'https://cdn-icons-png.flaticon.com/512/2097/2097743.png', // 测试图标
    4: 'https://cdn-icons-png.flaticon.com/512/2097/2097743.png', // 手册图标
    5: 'https://cdn-icons-png.flaticon.com/512/2097/2097743.png'  // 其他图标
  }
  return icons[typeId] || icons[5]
}

// 获取共享状态名称
const getShareStatusName = (statusId) => {
  const status = shareStatuses.find(s => s.id === statusId)
  return status ? status.name : '未知'
}

// 获取共享状态样式
const getShareStatusClass = (statusId) => {
  const classes = {
    1: 'bg-green-100 text-green-800 dark:bg-green-700 dark:text-green-300',
    2: 'bg-gray-100 text-gray-800 dark:bg-gray-700 dark:text-gray-300'
  }
  return classes[statusId] || classes[2]
}

// 格式化日期
const formatDate = (date) => {
  return new Date(date).toLocaleDateString('zh-CN')
}

// 格式化文件大小
const formatFileSize = (bytes) => {
  if (bytes === 0) return '0 B'
  const k = 1024
  const sizes = ['B', 'KB', 'MB', 'GB', 'TB']
  const i = Math.floor(Math.log(bytes) / Math.log(k))
  return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]
}

// 处理文件选择
const handleFileChange = (event) => {
  const file = event.target.files[0]
  if (file) {
    newDocument.value.name = file.name
    newDocument.value.size = file.size
  }
}

// 上传文档
const uploadDocument = () => {
  const document = {
    id: documents.value.length + 1,
    version: '1.0.0',
    createdAt: new Date().toISOString().split('T')[0],
    updatedAt: new Date().toISOString().split('T')[0],
    shareStatus: 2, // 默认未共享
    sharedWith: '',
    ...newDocument.value
  }
  
  documents.value.push(document)
  showUploadModal.value = false
  resetNewDocument()
}

// 共享文档
const shareDocument = () => {
  const document = documents.value.find(doc => doc.id === shareForm.value.documentId)
  if (document) {
    document.shareStatus = 1
    document.sharedWith = users.find(user => user.id === shareForm.value.sharedWith).name
  }
  
  showShareModal.value = false
  resetShareForm()
}

// 重置新文档表单
const resetNewDocument = () => {
  newDocument.value = {
    name: '',
    type: '',
    description: ''
  }
}

// 重置共享表单
const resetShareForm = () => {
  shareForm.value = {
    documentId: '',
    sharedWith: '',
    permission: ''
  }
}

// 下载文档
const downloadDocument = (document) => {
  // 实现下载文档的逻辑
  console.log('下载文档:', document)
}

// 查看文档
const viewDocument = (document) => {
  // 实现查看文档的逻辑
  console.log('查看文档:', document)
}

// 编辑文档
const editDocument = (document) => {
  // 实现编辑文档的逻辑
  console.log('编辑文档:', document)
}

// 删除文档
const deleteDocument = (document) => {
  if (confirm(`确定要删除文档 ${document.name} 吗？`)) {
    const index = documents.value.findIndex(doc => doc.id === document.id)
    if (index !== -1) {
      documents.value.splice(index, 1)
    }
  }
}

// 导出文档数据
const exportDocumentData = () => {
  // 实现导出文档数据的逻辑
  console.log('导出文档数据')
}
</script> 