<template>
  <GiPageLayout>
    <GiTable
      row-key="id"
      title="风电塔下监测系统"
      :data="dataList"
      :columns="tableColumns"
      :loading="loading"
      :scroll="{ x: '100%', y: '100%', minWidth: 1200 }"
      :pagination="pagination"
      @page-change="onPageChange"
      @page-size-change="onPageSizeChange"
      @refresh="search"
    >
      <template #top>
        <GiForm 
          v-model="searchForm" 
          search 
          :columns="queryFormColumns" 
          size="medium" 
          @search="search" 
          @reset="reset"
        />
      </template>
      
      <template #toolbar-left>
        <a-button type="primary" @click="openAddModal">
          <template #icon><icon-plus /></template>
          <template #default>添加设备</template>
        </a-button>
      </template>
      
      <!-- 设备状态 -->
      <template #status="{ record }">
        <a-tag :color="getStatusColor(record.status)">
          {{ getStatusText(record.status) }}
        </a-tag>
      </template>
      
      <!-- 操作列 -->
      <template #action="{ record }">
        <a-space>
          <a-link @click="viewDetail(record)">详情</a-link>
          <a-link @click="editRecord(record)">编辑</a-link>
          <a-link @click="viewData(record)">监测数据</a-link>
        </a-space>
      </template>
    </GiTable>
  </GiPageLayout>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { Message } from '@arco-design/web-vue'
import type { TableColumnData } from '@arco-design/web-vue'

// 搜索表单
const searchForm = reactive({
  deviceName: '',
  location: '',
  status: '',
  page: 1,
  size: 10
})

// 查询条件配置
const queryFormColumns = [
  {
    field: 'deviceName',
    label: '设备名称',
    type: 'input' as const,
    props: {
      placeholder: '请输入设备名称'
    }
  },
  {
    field: 'location',
    label: '安装位置',
    type: 'input' as const,
    props: {
      placeholder: '请输入安装位置'
    }
  },
  {
    field: 'status',
    label: '设备状态',
    type: 'select' as const,
    props: {
      placeholder: '请选择设备状态',
      options: [
        { label: '正常运行', value: 'normal' },
        { label: '故障', value: 'fault' },
        { label: '维护中', value: 'maintenance' },
        { label: '离线', value: 'offline' }
      ]
    }
  }
]

// 表格列配置
const tableColumns: TableColumnData[] = [
  { title: '设备编号', dataIndex: 'deviceCode', width: 120 },
  { title: '设备名称', dataIndex: 'deviceName', width: 200, ellipsis: true, tooltip: true },
  { title: '型号规格', dataIndex: 'model', width: 150 },
  { title: '安装位置', dataIndex: 'location', width: 200, ellipsis: true, tooltip: true },
  { title: '安装日期', dataIndex: 'installDate', width: 120 },
  { title: '设备状态', dataIndex: 'status', slotName: 'status', width: 100 },
  { title: '最后检测时间', dataIndex: 'lastCheckTime', width: 160 },
  { title: '负责人', dataIndex: 'manager', width: 100 },
  { title: '联系电话', dataIndex: 'phone', width: 120 },
  { title: '备注', dataIndex: 'remark', width: 200, ellipsis: true, tooltip: true },
  { title: '操作', slotName: 'action', width: 180, fixed: 'right' }
]

// 数据状态
const loading = ref(false)
const dataList = ref([
  {
    id: 1,
    deviceCode: 'TM001',
    deviceName: '风电塔振动监测系统',
    model: 'VM-2000',
    location: '1号风机塔筒基础',
    installDate: '2023-06-15',
    status: 'normal',
    lastCheckTime: '2024-01-15 10:30:00',
    manager: '张工程师',
    phone: '13800138001',
    remark: '运行正常，数据传输稳定'
  },
  {
    id: 2,
    deviceCode: 'TM002',
    deviceName: '塔筒应力监测设备',
    model: 'SM-1500',
    location: '2号风机塔筒中部',
    installDate: '2023-07-10',
    status: 'fault',
    lastCheckTime: '2024-01-14 15:20:00',
    manager: '李工程师',
    phone: '13800138002',
    remark: '传感器故障，需要更换'
  },
  {
    id: 3,
    deviceCode: 'TM003',
    deviceName: '基础沉降监测系统',
    model: 'FM-800',
    location: '3号风机基础',
    installDate: '2023-08-05',
    status: 'maintenance',
    lastCheckTime: '2024-01-13 09:15:00',
    manager: '王工程师',
    phone: '13800138003',
    remark: '定期维护中'
  }
])

const pagination = reactive({
  current: 1,
  pageSize: 10,
  total: 3,
  showTotal: true,
  showPageSize: true
})

// 获取状态颜色
const getStatusColor = (status: string) => {
  const colorMap: Record<string, string> = {
    'normal': 'green',
    'fault': 'red',
    'maintenance': 'orange',
    'offline': 'gray'
  }
  return colorMap[status] || 'gray'
}

// 获取状态文本
const getStatusText = (status: string) => {
  const textMap: Record<string, string> = {
    'normal': '正常运行',
    'fault': '故障',
    'maintenance': '维护中',
    'offline': '离线'
  }
  return textMap[status] || status
}

// 搜索和重置
const search = async () => {
  loading.value = true
  setTimeout(() => {
    loading.value = false
  }, 1000)
}

const reset = () => {
  Object.assign(searchForm, {
    deviceName: '',
    location: '',
    status: '',
    page: 1,
    size: 10
  })
  pagination.current = 1
  search()
}

// 分页处理
const onPageChange = (page: number) => {
  searchForm.page = page
  pagination.current = page
  search()
}

const onPageSizeChange = (size: number) => {
  searchForm.size = size
  searchForm.page = 1
  pagination.pageSize = size
  pagination.current = 1
  search()
}

// 操作方法
const openAddModal = () => {
  Message.info('添加设备功能开发中...')
}

const viewDetail = (record: any) => {
  Message.info(`查看设备详情: ${record.deviceName}`)
}

const editRecord = (record: any) => {
  Message.info(`编辑设备: ${record.deviceName}`)
}

const viewData = (record: any) => {
  Message.info(`查看监测数据: ${record.deviceName}`)
}

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