<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="showAddOrderModal = true" class="bg-blue-600 text-white px-4 py-2 rounded-md hover:bg-blue-700">
              创建物流订单
            </button>
            <button @click="exportOrders" class="bg-green-600 text-white px-4 py-2 rounded-md hover:bg-green-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="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>
            <input type="text" v-model="searchText" 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>
            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">运输方式</label>
            <select v-model="filters.transportType" 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 transportTypes" :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.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 orderStatuses" :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.createTime" 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 value="today">今天</option>
              <option value="week">本周</option>
              <option value="month">本月</option>
            </select>
          </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>
              <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="order in filteredOrders" :key="order.id">
              <td class="px-6 py-4 whitespace-nowrap">
                <div class="text-sm font-medium text-gray-900 dark:text-white">{{ order.orderNumber }}</div>
                <div class="text-sm text-gray-500 dark:text-gray-400">{{ order.recipient }}</div>
                <div class="text-sm text-gray-500 dark:text-gray-400">{{ order.phone }}</div>
              </td>
              <td class="px-6 py-4 whitespace-nowrap">
                <span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-blue-100 text-blue-800 dark:bg-blue-700 dark:text-blue-300">
                  {{ order.transportType }}
                </span>
              </td>
              <td class="px-6 py-4 whitespace-nowrap">
                <span :class="getStatusClass(order.status)" class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full">
                  {{ order.status }}
                </span>
              </td>
              <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500 dark:text-gray-400">
                {{ formatDate(order.createTime) }}
              </td>
              <td class="px-6 py-4 whitespace-nowrap text-sm font-medium">
                <button @click="viewOrder(order)" class="text-blue-600 hover:text-blue-900 dark:text-blue-400 dark:hover:text-blue-300 mr-3">查看</button>
                <button @click="trackOrder(order)" class="text-green-600 hover:text-green-900 dark:text-green-400 dark:hover:text-green-300 mr-3">跟踪</button>
                <button @click="updateStatus(order)" class="text-yellow-600 hover:text-yellow-900 dark:text-yellow-400 dark:hover:text-yellow-300 mr-3">更新状态</button>
                <button @click="deleteOrder(order)" 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="showAddOrderModal" 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="addOrder">
          <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="newOrder.orderNumber" 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="newOrder.recipient" 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="tel" v-model="newOrder.phone" 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="newOrder.transportType" 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 transportTypes" :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="newOrder.address" 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="showAddOrderModal = 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>
</template>

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

// 物流订单数据
const orders = ref([
  {
    id: 1,
    orderNumber: 'LOG202403150001',
    recipient: '张三',
    phone: '13800138000',
    transportType: '快递',
    status: '待发货',
    createTime: new Date().toISOString(),
    address: '北京市朝阳区xxx街道xxx号'
  },
  {
    id: 2,
    orderNumber: 'LOG202403150002',
    recipient: '李四',
    phone: '13800138001',
    transportType: '物流',
    status: '运输中',
    createTime: new Date(Date.now() - 86400000).toISOString(),
    address: '上海市浦东新区xxx街道xxx号'
  }
])

// 运输方式
const transportTypes = ref([
  { id: 1, name: '快递' },
  { id: 2, name: '物流' },
  { id: 3, name: '空运' },
  { id: 4, name: '海运' }
])

// 订单状态
const orderStatuses = ref([
  { id: 1, name: '待发货' },
  { id: 2, name: '运输中' },
  { id: 3, name: '已签收' },
  { id: 4, name: '已取消' }
])

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

// 模态框状态
const showAddOrderModal = ref(false)

// 新订单表单
const newOrder = ref({
  orderNumber: '',
  recipient: '',
  phone: '',
  transportType: '',
  address: ''
})

// 计算属性
const filteredOrders = computed(() => {
  return orders.value.filter(order => {
    const matchesSearch = !searchText.value || 
      order.orderNumber.toLowerCase().includes(searchText.value.toLowerCase()) ||
      order.recipient.toLowerCase().includes(searchText.value.toLowerCase()) ||
      order.phone.includes(searchText.value)
    const matchesTransportType = !filters.value.transportType || order.transportType === filters.value.transportType
    const matchesStatus = !filters.value.status || order.status === filters.value.status
    const matchesTime = !filters.value.createTime || checkCreateTime(order.createTime)
    return matchesSearch && matchesTransportType && matchesStatus && matchesTime
  })
})

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

// 获取状态样式
const getStatusClass = (status) => {
  const classes = {
    '待发货': 'bg-yellow-100 text-yellow-800 dark:bg-yellow-700 dark:text-yellow-300',
    '运输中': 'bg-blue-100 text-blue-800 dark:bg-blue-700 dark:text-blue-300',
    '已签收': 'bg-green-100 text-green-800 dark:bg-green-700 dark:text-green-300',
    '已取消': 'bg-red-100 text-red-800 dark:bg-red-700 dark:text-red-300'
  }
  return classes[status] || 'bg-gray-100 text-gray-800 dark:bg-gray-700 dark:text-gray-300'
}

// 检查创建时间
const checkCreateTime = (date) => {
  const now = new Date()
  const createDate = new Date(date)
  const diff = now - createDate

  switch (filters.value.createTime) {
    case 'today':
      return diff < 86400000
    case 'week':
      return diff < 604800000
    case 'month':
      return diff < 2592000000
    default:
      return true
  }
}

// 添加订单
const addOrder = () => {
  const order = {
    id: orders.value.length + 1,
    ...newOrder.value,
    status: '待发货',
    createTime: new Date().toISOString()
  }
  orders.value.push(order)
  showAddOrderModal.value = false
  resetNewOrder()
}

// 重置新订单表单
const resetNewOrder = () => {
  newOrder.value = {
    orderNumber: '',
    recipient: '',
    phone: '',
    transportType: '',
    address: ''
  }
}

// 查看订单
const viewOrder = (order) => {
  // 实现查看订单逻辑
  console.log('查看订单:', order)
}

// 跟踪订单
const trackOrder = (order) => {
  // 实现订单跟踪逻辑
  console.log('跟踪订单:', order)
}

// 更新状态
const updateStatus = (order) => {
  // 实现更新状态逻辑
  console.log('更新状态:', order)
}

// 删除订单
const deleteOrder = (order) => {
  if (confirm(`确定要删除订单 ${order.orderNumber} 吗？`)) {
    const index = orders.value.findIndex(o => o.id === order.id)
    if (index !== -1) {
      orders.value.splice(index, 1)
    }
  }
}

// 导出订单
const exportOrders = () => {
  // 实现导出订单逻辑
  console.log('导出订单')
}
</script> 