<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="showAddTicketModal = true" class="bg-blue-600 text-white px-4 py-2 rounded-md hover:bg-blue-700">
              创建工单
            </button>
            <button @click="exportServiceData" 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="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-red-600 dark:text-red-400">{{ pendingTickets }}</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">{{ todayProcessed }}</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-blue-600 dark:text-blue-400">{{ averageResponseTime }}分钟</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">{{ satisfactionRate }}%</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 ticketTypes" :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 ticketStatuses" :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.priority" 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="priority in priorities" :key="priority.id" :value="priority.id">{{ priority.name }}</option>
            </select>
          </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>
              <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="ticket in filteredTickets" :key="ticket.id">
              <td class="px-6 py-4 whitespace-nowrap">
                <div class="text-sm font-medium text-gray-900 dark:text-white">{{ ticket.number }}</div>
                <div class="text-sm text-gray-500 dark:text-gray-400">{{ ticket.type }}</div>
                <div class="text-sm text-gray-500 dark:text-gray-400">{{ formatDate(ticket.createdAt) }}</div>
              </td>
              <td class="px-6 py-4 whitespace-nowrap">
                <div class="text-sm font-medium text-gray-900 dark:text-white">{{ ticket.customerName }}</div>
                <div class="text-sm text-gray-500 dark:text-gray-400">{{ ticket.customerContact }}</div>
              </td>
              <td class="px-6 py-4 whitespace-nowrap">
                <span :class="getPriorityClass(ticket.priority)" class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full">
                  {{ getPriorityName(ticket.priority) }}
                </span>
              </td>
              <td class="px-6 py-4 whitespace-nowrap">
                <span :class="getStatusClass(ticket.status)" class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full">
                  {{ getStatusName(ticket.status) }}
                </span>
              </td>
              <td class="px-6 py-4 whitespace-nowrap text-sm font-medium">
                <button @click="viewTicket(ticket)" class="text-blue-600 hover:text-blue-900 dark:text-blue-400 dark:hover:text-blue-300 mr-3">查看</button>
                <button @click="processTicket(ticket)" class="text-green-600 hover:text-green-900 dark:text-green-400 dark:hover:text-green-300 mr-3">处理</button>
                <button @click="closeTicket(ticket)" class="text-gray-600 hover:text-gray-900 dark:text-gray-400 dark:hover:text-gray-300">关闭</button>
              </td>
            </tr>
          </tbody>
        </table>
      </div>
    </div>

    <!-- 创建工单模态框 -->
    <div v-if="showAddTicketModal" 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="addTicket">
          <div class="mb-4">
            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">工单类型</label>
            <select v-model="newTicket.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 ticketTypes" :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="text" v-model="newTicket.customerName" 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="newTicket.customerContact" 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="newTicket.priority" 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="priority in priorities" :key="priority.id" :value="priority.id">{{ priority.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="newTicket.description" 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" rows="4"></textarea>
          </div>
          <div class="flex justify-end space-x-3">
            <button type="button" @click="showAddTicketModal = 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 tickets = ref([
  {
    id: 1,
    number: 'CS20240315001',
    type: 1,
    customerName: '张三',
    customerContact: '13800138000',
    priority: 2,
    status: 1,
    description: '产品使用问题咨询',
    createdAt: new Date().toISOString()
  },
  {
    id: 2,
    number: 'CS20240315002',
    type: 2,
    customerName: '李四',
    customerContact: '13900139000',
    priority: 1,
    status: 2,
    description: '售后服务请求',
    createdAt: new Date(Date.now() - 86400000).toISOString()
  }
])

// 工单类型
const ticketTypes = [
  { id: 1, name: '咨询' },
  { id: 2, name: '投诉' },
  { id: 3, name: '建议' },
  { id: 4, name: '售后' }
]

// 工单状态
const ticketStatuses = [
  { id: 1, name: '待处理' },
  { id: 2, name: '处理中' },
  { id: 3, name: '已完成' },
  { id: 4, name: '已关闭' }
]

// 优先级
const priorities = [
  { id: 1, name: '低' },
  { id: 2, name: '中' },
  { id: 3, name: '高' },
  { id: 4, name: '紧急' }
]

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

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

// 新工单表单
const newTicket = ref({
  type: '',
  customerName: '',
  customerContact: '',
  priority: '',
  description: ''
})

// 计算属性
const filteredTickets = computed(() => {
  return tickets.value.filter(ticket => {
    const matchesType = !filters.value.type || ticket.type === filters.value.type
    const matchesStatus = !filters.value.status || ticket.status === filters.value.status
    const matchesPriority = !filters.value.priority || ticket.priority === filters.value.priority
    const matchesSearch = !filters.value.search ||
      ticket.number.toLowerCase().includes(filters.value.search.toLowerCase()) ||
      ticket.customerName.toLowerCase().includes(filters.value.search.toLowerCase())
    return matchesType && matchesStatus && matchesPriority && matchesSearch
  })
})

const pendingTickets = computed(() => {
  return tickets.value.filter(ticket => ticket.status === 1).length
})

const todayProcessed = computed(() => {
  const today = new Date().toISOString().split('T')[0]
  return tickets.value.filter(ticket => 
    ticket.status === 3 && 
    new Date(ticket.createdAt).toISOString().split('T')[0] === today
  ).length
})

const averageResponseTime = computed(() => {
  // 这里使用一个简单的计算方式，实际应用中应该根据实际的响应时间计算
  return 30
})

const satisfactionRate = computed(() => {
  // 这里使用一个简单的计算方式，实际应用中应该根据实际的满意度评价计算
  return 95
})

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

// 获取优先级名称
const getPriorityName = (priorityId) => {
  const priority = priorities.find(p => p.id === priorityId)
  return priority ? priority.name : '未知'
}

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

// 获取优先级样式
const getPriorityClass = (priorityId) => {
  const classes = {
    1: 'bg-gray-100 text-gray-800 dark:bg-gray-700 dark:text-gray-300',
    2: 'bg-blue-100 text-blue-800 dark:bg-blue-700 dark:text-blue-300',
    3: 'bg-yellow-100 text-yellow-800 dark:bg-yellow-700 dark:text-yellow-300',
    4: 'bg-red-100 text-red-800 dark:bg-red-700 dark:text-red-300'
  }
  return classes[priorityId] || classes[1]
}

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

// 添加工单
const addTicket = () => {
  const ticket = {
    id: tickets.value.length + 1,
    number: `CS${new Date().getTime()}`,
    ...newTicket.value,
    status: 1,
    createdAt: new Date().toISOString()
  }
  tickets.value.push(ticket)
  showAddTicketModal.value = false
  resetNewTicket()
}

// 重置新工单表单
const resetNewTicket = () => {
  newTicket.value = {
    type: '',
    customerName: '',
    customerContact: '',
    priority: '',
    description: ''
  }
}

// 查看工单
const viewTicket = (ticket) => {
  // 实现查看工单逻辑
  console.log('查看工单:', ticket)
}

// 处理工单
const processTicket = (ticket) => {
  if (confirm(`确定要处理工单 ${ticket.number} 吗？`)) {
    ticket.status = 2
  }
}

// 关闭工单
const closeTicket = (ticket) => {
  if (confirm(`确定要关闭工单 ${ticket.number} 吗？`)) {
    ticket.status = 4
  }
}

// 导出服务数据
const exportServiceData = () => {
  // 实现导出服务数据的逻辑
  console.log('导出服务数据')
}
</script> 