<template>
  <div class="inquiries-container">
    <h1 class="page-title">合作咨询管理</h1>
    
    <!-- Inquiries Table -->
    <div class="table-container">
      <table class="admin-table">
        <thead>
          <tr>
            <th>ID</th>
            <th>姓名</th>
            <th>公司</th>
            <th>联系方式</th>
            <th>预算</th>
            <th>描述</th>
            <th>状态</th>
            <th>提交时间</th>
            <th>操作</th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="item in inquiries" :key="item.id">
            <td class="table-cell">{{ item.id }}</td>
            <td class="table-cell">{{ item.name }}</td>
            <td class="table-cell">{{ item.company || '-' }}</td>
            <td class="table-cell">
              <div class="flex flex-col text-xs">
                <span class="opacity-70">{{ getContactMethodLabel(item.contactMethod) }}</span>
                <span>{{ item.contactValue }}</span>
              </div>
            </td>
            <td class="table-cell">{{ item.budget || '-' }}</td>
            <td class="table-cell" :title="item.description">
              {{ item.description && item.description.length > 20 ? item.description.substring(0, 20) + '...' : (item.description || '-') }}
            </td>
            <td class="table-cell">
              <span 
                class="px-2 py-1 rounded text-xs"
                :class="getStatusClass(item.status)"
              >
                {{ getStatusLabel(item.status) }}
              </span>
            </td>
            <td class="table-cell">{{ item.createdAt ? new Date(item.createdAt).toLocaleString() : '-' }}</td>
            <td class="table-cell actions">
              <button 
                v-if="item.status === 0"
                class="btn-action btn-read" 
                @click="updateStatus(item.id!, 1)" 
                title="标记为已读"
              >
                👁️
              </button>
              <button 
                v-if="item.status !== 2"
                class="btn-action btn-contact" 
                @click="updateStatus(item.id!, 2)" 
                title="标记为已联系"
              >
                ✅
              </button>
            </td>
          </tr>
        </tbody>
      </table>
      
      <!-- Empty State -->
      <div v-if="inquiries.length === 0" class="empty-state">
        <p>暂无咨询数据</p>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { API_BASE, getAuthHeaders, type Inquiry } from '../../services/api'
import { useToast } from '../../composables/useToast'

const toast = useToast()
const inquiries = ref<Inquiry[]>([])

const fetchInquiries = async () => {
  try {
    const response = await fetch(`${API_BASE}/admin/inquiries`, {
      headers: getAuthHeaders()
    })
    if (response.ok) {
      const data = await response.json()
      inquiries.value = data.result || []
    }
  } catch (error) {
    console.error('Failed to fetch inquiries:', error)
    toast.error('获取咨询列表失败')
  }
}

const updateStatus = async (id: number, status: number) => {
  try {
    const response = await fetch(`${API_BASE}/admin/inquiries/${id}/status`, {
      method: 'PUT',
      headers: getAuthHeaders(),
      body: JSON.stringify({ status })
    })
    
    if (response.ok) {
      toast.success('状态更新成功')
      fetchInquiries()
    } else {
      const errorData = await response.json()
      toast.error(errorData.message || '更新失败')
    }
  } catch (error) {
    console.error(error)
    toast.error('更新失败')
  }
}

const getContactMethodLabel = (method: string) => {
  const map: Record<string, string> = {
    'wechat': '微信',
    'email': '邮箱',
    'phone': '电话'
  }
  return map[method] || method
}

const getStatusLabel = (status?: number) => {
  switch (status) {
    case 0: return '未读'
    case 1: return '已读'
    case 2: return '已联系'
    default: return '未知'
  }
}

const getStatusClass = (status?: number) => {
  switch (status) {
    case 0: return 'bg-red-500/20 text-red-400'
    case 1: return 'bg-blue-500/20 text-blue-400'
    case 2: return 'bg-green-500/20 text-green-400'
    default: return 'bg-gray-500/20 text-gray-400'
  }
}

onMounted(() => {
  fetchInquiries()
})
</script>

<style scoped>
.inquiries-container {
  width: 100%;
}

.page-title {
  font-size: 1.75rem;
  font-weight: 600;
  color: #d4b383;
  margin-bottom: 1.5rem;
  font-family: 'Inter', sans-serif;
}

.table-container {
  background: rgba(255, 255, 255, 0.05);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-radius: 0.5rem;
  border: 1px solid rgba(255, 255, 255, 0.1);
  overflow: hidden;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

.admin-table {
  width: 100%;
  border-collapse: collapse;
  font-family: 'Inter', sans-serif;
}

.admin-table thead {
  background: rgba(255, 255, 255, 0.08);
}

.admin-table th {
  padding: 1rem;
  text-align: left;
  font-size: 0.875rem;
  font-weight: 600;
  color: #d4b383;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.admin-table td {
  padding: 1rem;
  font-size: 0.95rem;
  color: rgba(255, 255, 255, 0.8);
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.admin-table tr:last-child td {
  border-bottom: none;
}

.admin-table tr:hover {
  background: rgba(255, 255, 255, 0.02);
}

.actions {
  display: flex;
  gap: 0.5rem;
}

.btn-action {
  padding: 0.5rem;
  border: none;
  border-radius: 0.375rem;
  cursor: pointer;
  font-size: 1rem;
  transition: all 0.3s;
}

.btn-read {
  background: rgba(59, 130, 246, 0.1);
  color: rgba(59, 130, 246, 0.8);
}
.btn-read:hover {
  background: rgba(59, 130, 246, 0.2);
}

.btn-contact {
  background: rgba(16, 185, 129, 0.1);
  color: rgba(16, 185, 129, 0.8);
}
.btn-contact:hover {
  background: rgba(16, 185, 129, 0.2);
}

.empty-state {
  padding: 3rem;
  text-align: center;
  color: rgba(255, 255, 255, 0.5);
}
</style>