<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="showAddAssetModal = true" class="bg-blue-600 text-white px-4 py-2 rounded-md hover:bg-blue-700">
              资产登记
            </button>
            <button @click="showBorrowModal = true" class="bg-green-600 text-white px-4 py-2 rounded-md hover:bg-green-700">
              资产领用
            </button>
            <button @click="showMaintenanceModal = true" class="bg-yellow-600 text-white px-4 py-2 rounded-md hover:bg-yellow-700">
              维修申请
            </button>
            <button @click="exportAssetData" 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">{{ totalAssets }}</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">{{ inUseAssets }}</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">{{ maintenanceAssets }}</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">{{ scrappedAssets }}</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 assetTypes" :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>
            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">状态</label>
            <select v-model="filters.status" 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 assetStatus" :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>
            <select v-model="filters.department" 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="dept in departments" :key="dept.id" :value="dept.id">{{ dept.name }}</option>
            </select>
          </div>
        </div>
      </div>

      <!-- 资产列表 -->
      <div class="bg-white dark:bg-gray-800 rounded-lg shadow overflow-hidden mb-6">
        <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>
              <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="asset in filteredAssets" :key="asset.id">
              <td class="px-6 py-4 whitespace-nowrap">
                <div class="text-sm font-medium text-gray-900 dark:text-white">{{ asset.assetNumber }}</div>
                <div class="text-sm text-gray-500 dark:text-gray-400">{{ asset.name }}</div>
              </td>
              <td class="px-6 py-4 whitespace-nowrap">
                <div class="text-sm text-gray-900 dark:text-white">{{ getAssetTypeName(asset.type) }}</div>
              </td>
              <td class="px-6 py-4 whitespace-nowrap">
                <div class="text-sm text-gray-900 dark:text-white">¥{{ asset.value }}</div>
                <div class="text-xs text-gray-500 dark:text-gray-400">已折旧: ¥{{ asset.depreciatedValue }}</div>
              </td>
              <td class="px-6 py-4 whitespace-nowrap">
                <span :class="getStatusClass(asset.status)" class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full">
                  {{ getStatusName(asset.status) }}
                </span>
              </td>
              <td class="px-6 py-4 whitespace-nowrap text-sm font-medium">
                <button @click="viewAsset(asset)" class="text-blue-600 hover:text-blue-900 dark:text-blue-400 dark:hover:text-blue-300 mr-3">查看</button>
                <button @click="editAsset(asset)" class="text-yellow-600 hover:text-yellow-900 dark:text-yellow-400 dark:hover:text-yellow-300 mr-3">编辑</button>
                <button @click="deleteAsset(asset)" class="text-red-600 hover:text-red-900 dark:text-red-400 dark:hover:text-red-300">删除</button>
              </td>
            </tr>
          </tbody>
        </table>
      </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>
              <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="transfer in filteredTransfers" :key="transfer.id">
              <td class="px-6 py-4 whitespace-nowrap">
                <div class="text-sm font-medium text-gray-900 dark:text-white">{{ transfer.transferNumber }}</div>
                <div class="text-sm text-gray-500 dark:text-gray-400">
                  {{ formatDate(transfer.date) }}
                </div>
              </td>
              <td class="px-6 py-4 whitespace-nowrap">
                <div class="text-sm text-gray-900 dark:text-white">{{ getAssetName(transfer.assetId) }}</div>
              </td>
              <td class="px-6 py-4 whitespace-nowrap">
                <div class="text-sm text-gray-900 dark:text-white">{{ getDepartmentName(transfer.fromDepartment) }}</div>
              </td>
              <td class="px-6 py-4 whitespace-nowrap">
                <div class="text-sm text-gray-900 dark:text-white">{{ getDepartmentName(transfer.toDepartment) }}</div>
              </td>
              <td class="px-6 py-4 whitespace-nowrap text-sm font-medium">
                <button @click="viewTransfer(transfer)" class="text-blue-600 hover:text-blue-900 dark:text-blue-400 dark:hover:text-blue-300 mr-3">查看</button>
                <button @click="editTransfer(transfer)" class="text-yellow-600 hover:text-yellow-900 dark:text-yellow-400 dark:hover:text-yellow-300 mr-3">编辑</button>
                <button @click="deleteTransfer(transfer)" 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="showAddAssetModal" 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="addAsset">
          <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="newAsset.assetNumber" 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>
            <input type="text" v-model="newAsset.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="newAsset.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 assetTypes" :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>
            <input type="number" v-model="newAsset.value" 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="newAsset.department" 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="dept in departments" :key="dept.id" :value="dept.id">{{ dept.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="newAsset.status" 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="status in assetStatus" :key="status.id" :value="status.id">{{ status.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="newAsset.remarks" 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"></textarea>
          </div>
          <div class="flex justify-end space-x-3">
            <button type="button" @click="showAddAssetModal = 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="showTransferModal" 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="addTransfer">
          <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="newTransfer.transferNumber" 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="newTransfer.assetId" 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="asset in assetList" :key="asset.id" :value="asset.id">{{ asset.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="newTransfer.fromDepartment" 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="dept in departments" :key="dept.id" :value="dept.id">{{ dept.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="newTransfer.toDepartment" 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="dept in departments" :key="dept.id" :value="dept.id">{{ dept.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="newTransfer.reason" 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"></textarea>
          </div>
          <div class="flex justify-end space-x-3">
            <button type="button" @click="showTransferModal = 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-yellow-600 text-white rounded-md hover:bg-yellow-700">
              提交
            </button>
          </div>
        </form>
      </div>
    </div>
  </div>
</template>

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

// 资产数据
const assetList = ref([
  {
    id: 1,
    assetNumber: 'AS202403150001',
    name: '办公电脑',
    type: 1,
    value: 5000,
    depreciatedValue: 1000,
    department: 1,
    status: 1,
    remarks: '正常使用中'
  },
  {
    id: 2,
    assetNumber: 'AS202403150002',
    name: '打印机',
    type: 2,
    value: 3000,
    depreciatedValue: 600,
    department: 2,
    status: 2,
    remarks: '需要维修'
  }
])

// 调拨记录数据
const transferRecords = ref([
  {
    id: 1,
    transferNumber: 'TR202403150001',
    assetId: 1,
    fromDepartment: 1,
    toDepartment: 2,
    date: '2024-03-15',
    reason: '部门调整'
  },
  {
    id: 2,
    transferNumber: 'TR202403150002',
    assetId: 2,
    fromDepartment: 2,
    toDepartment: 3,
    date: '2024-03-15',
    reason: '设备更新'
  }
])

// 资产类型
const assetTypes = ref([
  { id: 1, name: '办公设备' },
  { id: 2, name: '生产设备' },
  { id: 3, name: '运输设备' }
])

// 资产状态
const assetStatus = ref([
  { id: 1, name: '使用中' },
  { id: 2, name: '维修中' },
  { id: 3, name: '已报废' }
])

// 部门
const departments = ref([
  { id: 1, name: '行政部' },
  { id: 2, name: '生产部' },
  { id: 3, name: '销售部' }
])

// 筛选条件
const searchText = ref('')
const filters = ref({
  type: '',
  status: '',
  department: ''
})

// 模态框状态
const showAddAssetModal = ref(false)
const showTransferModal = ref(false)

// 新资产表单
const newAsset = ref({
  assetNumber: '',
  name: '',
  type: '',
  value: '',
  department: '',
  status: '',
  remarks: ''
})

// 新调拨记录表单
const newTransfer = ref({
  transferNumber: '',
  assetId: '',
  fromDepartment: '',
  toDepartment: '',
  reason: ''
})

// 计算属性
const filteredAssets = computed(() => {
  return assetList.value.filter(item => {
    const matchesSearch = !searchText.value || 
      item.assetNumber.toLowerCase().includes(searchText.value.toLowerCase()) ||
      item.name.toLowerCase().includes(searchText.value.toLowerCase())
    const matchesType = !filters.value.type || item.type === filters.value.type
    const matchesStatus = !filters.value.status || item.status === filters.value.status
    const matchesDepartment = !filters.value.department || item.department === filters.value.department
    return matchesSearch && matchesType && matchesStatus && matchesDepartment
  })
})

const filteredTransfers = computed(() => {
  return transferRecords.value.filter(item => {
    const asset = assetList.value.find(a => a.id === item.assetId)
    const matchesSearch = !searchText.value || 
      item.transferNumber.toLowerCase().includes(searchText.value.toLowerCase()) ||
      (asset && asset.name.toLowerCase().includes(searchText.value.toLowerCase()))
    return matchesSearch
  })
})

const totalAssets = computed(() => assetList.value.length)
const scrappedAssets = computed(() => assetList.value.filter(item => item.status === 3).length)

const inUseAssets = computed(() => {
  return assetList.value.filter(asset => asset.status === 1).length
})

const maintenanceAssets = computed(() => {
  return assetList.value.filter(asset => asset.status === 2).length
})

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

// 获取资产类型名称
const getAssetTypeName = (typeId) => {
  const type = assetTypes.value.find(t => t.id === typeId)
  return type ? type.name : '未知类型'
}

// 获取资产状态名称
const getStatusName = (statusId) => {
  const status = assetStatus.value.find(s => s.id === statusId)
  return status ? status.name : '未知状态'
}

// 获取部门名称
const getDepartmentName = (departmentId) => {
  const department = departments.value.find(d => d.id === departmentId)
  return department ? department.name : '未知部门'
}

// 获取资产名称
const getAssetName = (assetId) => {
  const asset = assetList.value.find(a => a.id === assetId)
  return asset ? asset.name : '未知资产'
}

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

// 获取头像
const getAvatar = (id) => {
  return `https://ui-avatars.com/api/?name=${id}&background=random&color=fff&size=150`
}

// 添加资产
const addAsset = () => {
  const item = {
    id: assetList.value.length + 1,
    ...newAsset.value,
    depreciatedValue: 0
  }
  assetList.value.push(item)
  showAddAssetModal.value = false
  resetNewAsset()
}

// 重置新资产表单
const resetNewAsset = () => {
  newAsset.value = {
    assetNumber: '',
    name: '',
    type: '',
    value: '',
    department: '',
    status: '',
    remarks: ''
  }
}

// 添加调拨记录
const addTransfer = () => {
  const item = {
    id: transferRecords.value.length + 1,
    ...newTransfer.value,
    date: new Date().toISOString().split('T')[0]
  }
  transferRecords.value.push(item)
  showTransferModal.value = false
  resetNewTransfer()
}

// 重置新调拨记录表单
const resetNewTransfer = () => {
  newTransfer.value = {
    transferNumber: '',
    assetId: '',
    fromDepartment: '',
    toDepartment: '',
    reason: ''
  }
}

// 查看资产
const viewAsset = (item) => {
  // 实现查看资产逻辑
  console.log('查看资产:', item)
}

// 编辑资产
const editAsset = (item) => {
  // 实现编辑资产逻辑
  console.log('编辑资产:', item)
}

// 删除资产
const deleteAsset = (item) => {
  if (confirm(`确定要删除资产 ${item.assetNumber} 吗？`)) {
    const index = assetList.value.findIndex(a => a.id === item.id)
    if (index !== -1) {
      assetList.value.splice(index, 1)
    }
  }
}

// 查看调拨记录
const viewTransfer = (item) => {
  // 实现查看调拨记录逻辑
  console.log('查看调拨记录:', item)
}

// 编辑调拨记录
const editTransfer = (item) => {
  // 实现编辑调拨记录逻辑
  console.log('编辑调拨记录:', item)
}

// 删除调拨记录
const deleteTransfer = (item) => {
  if (confirm(`确定要删除调拨记录 ${item.transferNumber} 吗？`)) {
    const index = transferRecords.value.findIndex(t => t.id === item.id)
    if (index !== -1) {
      transferRecords.value.splice(index, 1)
    }
  }
}
</script> 