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

        <el-form-item label="费用类型" prop="feeType">
          <el-select
              v-model="queryParams.feeType"
              placeholder="请选择费用类型"
              style="width: 180px;"
          >
            <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="feeRecords" border>
        <el-table-column label="序号" type="index" align="center" />
        <el-table-column label="账单编号" align="center" prop="billId" />
        <el-table-column label="业主ID" align="center" prop="ownerId"  />
        <el-table-column label="费用类型" align="center" prop="feeType" />
        <el-table-column label="应收金额" align="right" prop="dueAmount" >
          <template #default="scope">
            ¥ {{ scope.row.dueAmount | numberFormat }}
          </template>
        </el-table-column>
        <el-table-column label="实收金额" align="right" prop="paidAmount" >
          <template #default="scope">
            ¥ {{ scope.row.paidAmount | numberFormat }}
          </template>
        </el-table-column>
        <el-table-column label="欠费状态" align="center" prop="arrearsStatus" >
          <template #default="scope">
            <el-tag :type="getStatusType(scope.row.arrearsStatus)">
              {{ scope.row.arrearsStatus }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="缴费期限" align="center" prop="dueDate" />
      </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 {getEstateManagementFeeDataList} from "@/api/estateManagement/estateManagement.js"; // 金额格式化工具

// 查询参数
const queryParams = reactive({
  billId: "", // 账单编号（支持精确查询）
  feeType: "", // 费用类型（物业费/水费/电费）
  pageNum: 1, // 当前页码
  pageSize: 10, // 每页条数
  interfaceName: "物业费用中心"
});

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

// 获取费用记录列表
const getList = async () => {
  loading.value = true;
  try {
    const response = await getEstateManagementFeeDataList(queryParams);
    feeRecords.value =  [];
    total.value =  0;
  } catch (error) {
    console.error("获取费用数据失败", error);
  } finally {
    loading.value = false;
  }
};

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

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

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

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

// 状态标签类型映射
const getStatusType = (status) => {
  if (status === "已结清") return "success";
  if (status === "部分缴纳") return "warning";
  if (status === "未缴纳") return "danger";
  return "info";
};

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

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

.el-table .cell {
  white-space: nowrap;
}

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