<template>
  <div class="page-container">
    <div class="header">
      <div class="title">设备预警</div>
    </div>
    
    <!-- 预警统计卡片 -->
    <div class="stats-container">
      <div class="stat-card">
        <div class="stat-value">{{ warningStats.earTag }}</div>
        <div class="stat-label">耳标预警</div>
      </div>
      <div class="stat-card">
        <div class="stat-value">{{ warningStats.neckband }}</div>
        <div class="stat-label">项圈预警</div>
      </div>
      <div class="stat-card">
        <div class="stat-value">{{ warningStats.host }}</div>
        <div class="stat-label">主机预警</div>
      </div>
    </div>

    <!-- 设备类型标签页和搜索 -->
    <a-card :bordered="false" class="main-card">
      <div class="table-header">
        <a-tabs v-model:activeKey="activeTab" @change="handleTabChange">
          <a-tab-pane key="earTag" tab="智能耳标"></a-tab-pane>
          <a-tab-pane key="neckband" tab="智能项圈"></a-tab-pane>
          <a-tab-pane key="host" tab="智能主机"></a-tab-pane>
        </a-tabs>
        <div class="search-container">
          <a-input 
            v-model:value="searchKeyword" 
            placeholder="请输入养殖户" 
            style="width: 200px;"
            @pressEnter="handleSearch"
          >
            <template #suffix>
              <span class="iconfont icon-sousuo" @click="handleSearch"></span>
            </template>
          </a-input>
          <a-button type="primary" @click="handleSearch" style="margin-left: 8px;">Q 搜索</a-button>
        </div>
      </div>

      <!-- 数据表格 -->
      <a-table
        :columns="columns"
        :data-source="filteredTableData"
        :pagination="pagination"
        :loading="loading"
        row-key="id"
        @change="handleTableChange"
      >
        <template #bodyCell="{ column, record }">
          <template v-if="column.key === 'alertType'">
            <a-tag :color="getAlertTypeColor(record.alertType)">
              {{ record.alertType }}
            </a-tag>
          </template>
        </template>
      </a-table>
    </a-card>
  </div>
</template>

<script setup>
import { ref, reactive, computed, onMounted } from 'vue';
import { message } from 'ant-design-vue';
import deviceWarningApi from '@/utils/deviceWarningApi';

const activeTab = ref('neckband');
const searchKeyword = ref('');
const loading = ref(false);
const tableData = ref([]);
const warningStats = ref({
  earTag: 0,
  neckband: 0,
  host: 0
});

const columns = [
  { title: '养殖场', dataIndex: 'farmName', key: 'farmName', width: 150 },
  { title: '养殖户', dataIndex: 'farmerName', key: 'farmerName', width: 150 },
  { title: '联系电话', dataIndex: 'phone', key: 'phone', width: 120 },
  { title: '设备类型', dataIndex: 'deviceType', key: 'deviceType', width: 100 },
  { title: '设备编号', dataIndex: 'deviceNumber', key: 'deviceNumber', width: 120 },
  { title: '预警类型', dataIndex: 'alertType', key: 'alertType', width: 100 },
];

const pagination = reactive({
  current: 1,
  pageSize: 20,
  total: 0,
  showSizeChanger: true,
  showQuickJumper: true,
  showTotal: (total, range) => `共${total}条`
});

// 根据当前标签页过滤数据
const filteredTableData = computed(() => {
  let filtered = tableData.value;
  
  // 根据设备类型过滤
  if (activeTab.value === 'earTag') {
    filtered = filtered.filter(item => item.deviceType === '智能耳标');
  } else if (activeTab.value === 'neckband') {
    filtered = filtered.filter(item => item.deviceType === '智能项圈');
  } else if (activeTab.value === 'host') {
    filtered = filtered.filter(item => item.deviceType === '智能主机');
  }
  
  // 根据搜索关键词过滤
  if (searchKeyword.value) {
    filtered = filtered.filter(item => 
      item.farmerName.includes(searchKeyword.value) || 
      item.farmName.includes(searchKeyword.value)
    );
  }
  
  pagination.total = filtered.length;
  return filtered;
});

// 获取预警类型颜色
const getAlertTypeColor = (alertType) => {
  const colorMap = {
    '设备离线': 'red',
    '电量不足': 'orange',
    '信号异常': 'yellow',
    '温度异常': 'purple',
    '其他': 'default'
  };
  return colorMap[alertType] || 'default';
};

// 获取设备预警数据
const fetchDeviceWarnings = async () => {
  try {
    loading.value = true;
    const params = {
      page: pagination.current,
      pageSize: pagination.pageSize,
      deviceType: activeTab.value === 'earTag' ? '智能耳标' : 
                  activeTab.value === 'neckband' ? '智能项圈' : 
                  activeTab.value === 'host' ? '智能主机' : undefined,
      farmerName: searchKeyword.value || undefined
    };
    
    const response = await deviceWarningApi.getDeviceWarnings(params);
    if (response.code === 200) {
      tableData.value = response.data.list;
      pagination.total = response.data.total;
    }
  } catch (error) {
    console.error('获取设备预警数据失败:', error);
    message.error('获取设备预警数据失败');
  } finally {
    loading.value = false;
  }
};

// 获取预警统计
const fetchWarningStats = async () => {
  try {
    const response = await deviceWarningApi.getWarningStats();
    if (response.code === 200) {
      warningStats.value = response.data;
    }
  } catch (error) {
    console.error('获取预警统计失败:', error);
  }
};

// 标签页切换
const handleTabChange = (key) => {
  activeTab.value = key;
  pagination.current = 1;
  fetchDeviceWarnings();
};

// 搜索
const handleSearch = () => {
  pagination.current = 1;
  fetchDeviceWarnings();
};

// 表格变化
const handleTableChange = (pag) => {
  pagination.current = pag.current;
  pagination.pageSize = pag.pageSize;
  fetchDeviceWarnings();
};

// 组件挂载
onMounted(() => {
  fetchDeviceWarnings();
  fetchWarningStats();
});

</script>

<style scoped>
.page-container {
  background-color: #f0f2f5;
  padding: 24px;
  min-height: 100vh;
}

.header {
  margin-bottom: 24px;
}

.title {
  font-size: 24px;
  font-weight: bold;
  color: #262626;
}

.stats-container {
  display: flex;
  gap: 24px;
  margin-bottom: 24px;
}

.stat-card {
  background-color: #fff;
  padding: 24px;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.09);
  text-align: center;
  flex: 1;
  border: 1px solid #f0f0f0;
  transition: all 0.3s;
}

.stat-card:hover {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  transform: translateY(-2px);
}

.stat-value {
  font-size: 32px;
  font-weight: bold;
  color: #1890ff;
  margin-bottom: 8px;
}

.stat-label {
  font-size: 16px;
  color: #595959;
  font-weight: 500;
}

.main-card {
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.09);
  border-radius: 8px;
}

.table-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
  padding-bottom: 16px;
  border-bottom: 1px solid #f0f0f0;
}

.search-container {
  display: flex;
  align-items: center;
  gap: 8px;
}

:deep(.ant-tabs-tab) {
  font-size: 16px;
  font-weight: 500;
  padding: 12px 24px;
}

:deep(.ant-tabs-tab-active) {
  color: #1890ff;
}

:deep(.ant-tabs-ink-bar) {
  background-color: #1890ff;
}

:deep(.ant-table-thead > tr > th) {
  background-color: #fafafa;
  font-weight: 600;
  color: #262626;
  border-bottom: 2px solid #f0f0f0;
}

:deep(.ant-table-tbody > tr > td) {
  border-bottom: 1px solid #f0f0f0;
  padding: 12px 16px;
}

:deep(.ant-table-tbody > tr:hover > td) {
  background-color: #f5f5f5;
}

:deep(.ant-pagination) {
  margin-top: 16px;
  text-align: right;
}

:deep(.ant-pagination-total-text) {
  color: #666;
  font-size: 14px;
}

:deep(.ant-input-affix-wrapper) {
  border-radius: 6px;
}

:deep(.ant-input-affix-wrapper:focus) {
  border-color: #1890ff;
  box-shadow: 0 0 0 2px rgba(24, 144, 255, 0.2);
}

:deep(.ant-btn-primary) {
  border-radius: 6px;
  font-weight: 500;
}

:deep(.ant-tag) {
  border-radius: 4px;
  font-weight: 500;
  padding: 2px 8px;
}

.icon-sousuo {
  cursor: pointer;
  color: #999;
  font-size: 16px;
}

.icon-sousuo:hover {
  color: #1890ff;
}

.important-messages {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.important-message-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 16px;
  border: 1px solid #ffccc7;
  border-radius: 6px;
  background-color: #fff2f0;
  cursor: pointer;
  transition: all 0.3s;
}

.important-message-item:hover {
  background-color: #ff4d4f10;
}

.important-icon {
  font-size: 24px;
  color: #ff4d4f;
}

.important-content {
  flex: 1;
}

.important-title {
  font-weight: bold;
  color: #ff4d4f;
  margin-bottom: 4px;
}

.important-time {
  font-size: 12px;
  color: #999;
}

.view-btn {
  margin-left: auto;
}

.no-data {
  text-align: center;
  color: #999;
  padding: 24px;
}

.unread-dot {
  width: 8px;
  height: 8px;
  background-color: #ff4d4f;
  border-radius: 50%;
  margin-right: 8px;
}

.message-title {
  cursor: pointer;
  transition: color 0.3s;
}

.message-title:hover {
  color: #1890ff;
}

.unread-title {
  font-weight: bold;
}

.important-title {
  color: #ff4d4f;
  font-weight: bold;
}

.top-icon {
  font-size: 14px;
  color: #faad14;
  margin-left: 8px;
}

.message-content {
  cursor: pointer;
  color: #666;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.message-content:hover {
  color: #1890ff;
}

.message-time {
  color: #999;
  font-size: 12px;
}

.detail-header {
  margin-bottom: 24px;
  padding-bottom: 16px;
  border-bottom: 1px solid #f0f0f0;
}

.detail-title {
  margin: 0 0 12px 0;
  color: #333;
}

.detail-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  font-size: 14px;
  color: #666;
}

.detail-content {
  line-height: 1.8;
  color: #333;
  margin-bottom: 24px;
}

.attachments h4 {
  margin: 0 0 12px 0;
  color: #333;
}

.attachment-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.attachment-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border: 1px solid #e8e8e8;
  border-radius: 4px;
  background-color: #f9f9f9;
}

.attachment-name {
  flex: 1;
}

.attachment-size {
  font-size: 12px;
  color: #999;
  margin-right: 8px;
}
</style>