<template>
  <div class="owner-management">
    <!-- 搜索栏 -->
    <el-card class="filter-card">
      <el-form :model="queryParams" ref="queryRef" :inline="true" class="filter-form">
        <el-form-item label="业主姓名" prop="name">
          <el-input
              v-model="queryParams.name"
              placeholder="请输入业主姓名"
              clearable
              @keyup.enter="handleQuery"
          />
        </el-form-item>

        <el-form-item label="房号" prop="roomNo">
          <el-input
              v-model="queryParams.roomNo"
              placeholder="请输入房号（如：101）"
              clearable
              @keyup.enter="handleQuery"
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 数据表格 -->
    <el-card class="device-table">
      <el-table v-loading="loading" :data="owners" border>
        <el-table-column label="序号" type="index" align="center" width="180" />
        <el-table-column label="业主ID" align="center" prop="ownerId" width="120" />
        <el-table-column label="姓名" align="center" prop="name" width="120" />
        <el-table-column label="楼栋号" align="center" prop="buildingNo" width="180" />
        <el-table-column label="房号" align="center" prop="roomNo" width="120" />
        <el-table-column label="联系方式" align="center" prop="phone" width="150" />
        <el-table-column label="入住日期" align="center" prop="moveInDate" width="180" />
        <el-table-column label="紧急联系人" align="center" prop="emergencyContact" width="180" />
        <el-table-column label="车位号" align="center" prop="parkingSpace" width="180" />
        <el-table-column label="宠物登记信息" align="center" prop="petInfo" />
      </el-table>

      <!-- 分页器 -->
      <pagination
          v-show="total > 0"
          :total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
      />
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive } from "vue";
import { getEstateManagementList } from "@/api/estateManagement/estateManagement"; // 假设API路径

// 查询参数
const queryParams = reactive({
  name: "", // 业主姓名
  roomNo: "", // 房号
  pageNum: 1, // 当前页码
  pageSize: 10, // 每页条数
  interfaceName: "业主档案"
});

// 表格数据
const owners = ref([]);
const total = ref(0);
const loading = ref(false);

// 获取业主列表
const getList = async () => {
  loading.value = true;
  try {
    const response = await getEstateManagementList(queryParams);
    owners.value =  [];
    total.value =  0;
  } catch (error) {
    console.error("获取业主数据失败", error);
  } finally {
    loading.value = false;
  }
};

// 搜索方法
const handleQuery = () => {
  queryParams.pageNum = 1; // 重置页码
  getList();
};

// 重置方法
const resetQuery = () => {
  queryParams.name = "";
  queryParams.roomNo = "";
  handleQuery();
};

// 分页事件
const handleSizeChange = (newSize) => {
  queryParams.pageSize = newSize;
  getList();
};

const handleCurrentChange = (newPage) => {
  queryParams.pageNum = newPage;
  getList();
};

// 初始化加载
getList();
</script>

<style scoped>
.filter-card {
  margin-bottom: 20px;
}

.el-table .cell {
  white-space: normal; /* 允许单元格内容换行 */
}

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