<template>
  <div class="page">
    <section class="page-hero">
      <div>
        <h1>路灯资产</h1>
        <p>统一管理路灯设备、在线状态、设备状态和未处理报警。</p>
      </div>
      <div class="asset-stats">
        <span>总数 {{ total }}</span>
        <span>在线 {{ onlineTotal }}</span>
        <span>异常 {{ abnormalTotal }}</span>
      </div>
    </section>
    <div class="toolbar">
      <div class="filter-group">
        <el-input v-model="keyword" placeholder="搜索编号/名称" style="width: 240px" clearable @change="handleFilterChange" />
        <el-tree-select
          v-model="selectedRegionId"
          :data="regions"
          :props="{ label: 'name', children: 'children' }"
          node-key="id"
          check-strictly
          clearable
          placeholder="按区域筛选"
          style="width: 240px"
          @change="handleFilterChange"
        />
      </div>
    </div>
    <el-card class="modern-card" shadow="never">
      <el-table :data="lamps" v-loading="loading" class="modern-table">
        <el-table-column prop="code" label="编号" min-width="140" />
        <el-table-column prop="name" label="名称" min-width="160" />
        <el-table-column prop="region.name" label="区域" />
        <el-table-column label="在线状态">
          <template #default="{ row }">
            <el-tag :type="row.onlineStatus === 'ONLINE' ? 'success' : 'info'" effect="light">
              {{ onlineStatusText(row.onlineStatus) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="设备状态">
          <template #default="{ row }">
            <el-tag :type="row.status === 'NORMAL' ? 'success' : row.status === 'MAINTENANCE' ? 'warning' : 'danger'">
              {{ lampStatusText(row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="alarms.length" label="未处理报警" width="130" />
      </el-table>
      <el-pagination
        class="pagination"
        layout="total, sizes, prev, pager, next"
        :total="total"
        :current-page="page"
        :page-size="pageSize"
        :page-sizes="[10, 20, 50, 100]"
        @current-change="handlePageChange"
        @size-change="handleSizeChange"
      />
    </el-card>
    <el-dialog v-model="dialogVisible" title="新增路灯" width="520px">
      <el-form :model="form" label-width="90px">
        <el-form-item label="编号"><el-input v-model="form.code" /></el-form-item>
        <el-form-item label="名称"><el-input v-model="form.name" /></el-form-item>
        <el-form-item label="区域ID"><el-input v-model="form.regionId" placeholder="可在区域管理复制区县 ID" /></el-form-item>
        <el-form-item label="经度"><el-input v-model="form.longitude" /></el-form-item>
        <el-form-item label="纬度"><el-input v-model="form.latitude" /></el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="create">保存</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { onMounted, reactive, ref } from 'vue';
import { lampApi, regionApi } from '../../api/modules';
import { lampStatusText, onlineStatusText } from '../../utils/labels';

const lamps = ref<any[]>([]);
const regions = ref<any[]>([]);
const keyword = ref('');
const selectedRegionId = ref('');
const loading = ref(false);
const dialogVisible = ref(false);
const form = reactive({ code: '', name: '', regionId: '', longitude: '', latitude: '' });
const page = ref(1);
const pageSize = ref(20);
const total = ref(0);
const onlineTotal = ref(0);
const abnormalTotal = ref(0);

async function load() {
  loading.value = true;
  try {
    const result = await lampApi.list({
      keyword: keyword.value,
      regionId: selectedRegionId.value,
      page: page.value,
      pageSize: pageSize.value,
      excludeTerminal: '1',
    });
    lamps.value = result.items ?? [];
    total.value = result.total ?? 0;
    onlineTotal.value = result.onlineTotal ?? 0;
    abnormalTotal.value = result.abnormalTotal ?? 0;
  } finally {
    loading.value = false;
  }
}

function handlePageChange(value: number) {
  page.value = value;
  void load();
}

function handleFilterChange() {
  page.value = 1;
  void load();
}

function handleSizeChange(value: number) {
  pageSize.value = value;
  page.value = 1;
  void load();
}

async function create() {
  await lampApi.create({ ...form, longitude: Number(form.longitude), latitude: Number(form.latitude) });
  dialogVisible.value = false;
  await load();
}

onMounted(async () => {
  regions.value = (await regionApi.tree()) as any[];
  await load();
});
</script>

<style scoped>
.asset-stats {
  display: flex;
  gap: 10px;
}

.asset-stats span {
  padding: 10px 14px;
  border-radius: 999px;
  color: #1d4ed8;
  background: #eff6ff;
  font-weight: 600;
}

.filter-group {
  display: flex;
  gap: 10px;
}

.pagination {
  margin-top: 16px;
  justify-content: flex-end;
}
</style>
