<template>
  <div class="rental-management">
    <!-- 搜索栏 -->
    <el-card class="filter-card">
      <el-form :model="queryParams" ref="queryRef" :inline="true" class="filter-form">
        <el-form-item label="房源编号" prop="propertyId">
          <el-input
              v-model="queryParams.propertyId"
              placeholder="请输入房源编号（如：PROP2024001）"
              clearable
              @keyup.enter="handleQuery"
          />
        </el-form-item>

        <el-form-item label="户型" prop="layout">
          <el-select
              v-model="queryParams.layout"
              placeholder="请选择户型"
              style="width: 180px;"
          >
            <el-option label="全部" value="" />
            <el-option label="一室一厅" value="一室一厅" />
            <el-option label="两室一厅" value="两室一厅" />
            <el-option label="两室两厅" value="两室两厅" />
            <el-option label="三室一厅" value="三室一厅" />
            <el-option label="三室两厅" value="三室两厅" />
            <el-option label="四室两厅" value="四室两厅" />
          </el-select>
        </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="properties" border>
        <el-table-column label="序号" type="index" align="center"   />
        <el-table-column label="房源编号" align="center" prop="propertyId"   />
        <el-table-column label="面积(㎡)" align="right" prop="area"   />
        <el-table-column label="户型" align="center" prop="layout"   />
        <el-table-column label="月租金(元)" align="right" prop="monthlyRent"   />
        <el-table-column label="租客信息" align="center" prop="tenant"   />
        <el-table-column label="合同起止日" align="center"  >
          <template #default="scope">
            {{ scope.row.contractStart }} - {{ scope.row.contractEnd }}
          </template>
        </el-table-column>
        <el-table-column label="押金状态" align="center" prop="depositStatus"   />
        <el-table-column label="历史租赁记录" align="left" prop="history" />
      </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 { getEstateManagementRentalDataList } from "@/api/estateManagement/estateManagement"; // 假设API路径

// 查询参数（两个条件：propertyId、layout）
const queryParams = reactive({
  propertyId: "", // 房源编号（精确查询）
  layout: "", // 户型（下拉选择）
  pageNum: 1,
  pageSize: 10,
  interfaceName: "租赁资产管家"
});

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

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

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

// 重置方法
const resetQuery = () => {
  queryParams.propertyId = "";
  queryParams.layout = "";
  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>
