<template>
  <view>
    <template v-if="list.length">
      <u-list>
        <!-- 表头 -->
        <u-list-item class="table-header">
          <view class="table-row">
            <view class="table-cell header-cell">小区名称</view>
            <view class="table-cell header-cell">联系电话</view>
            <view class="table-cell header-cell">等级</view>
            <view class="table-cell header-cell">物业费单价</view>
          </view>
        </u-list-item>
        <!-- 列表项 -->
        <u-list-item v-for="(item, index) in list" :key="index" @click="goToDetail(item)">
          <view class="table-row">
            <view class="table-cell">{{ item.name }}</view>
            <view class="table-cell">{{ item.tel || '暂无' }}</view>
            <view class="table-cell">{{ ['一级', '二级', '三级', '四级', '五级'][item.level] || '暂无' }}</view>
            <view class="table-cell">{{ item.feeItem ? item.feeItem + '元/㎡' : '暂无' }}</view>
          </view>
        </u-list-item>
      </u-list>
    </template>
    <u-empty v-else text="暂无小区信息"/>
    <custom-tabbar :current="1"/>
  </view>
</template>

<script>
import CustomTabbar from "@/components/customTabbar.vue";
import {communityListReq} from "@/api/common";

export default {
  name: "CommunityIndex",
  components: {CustomTabbar},
  data() {
    return {
      list: []
    }
  },
  methods: {
    async getList() {
      try {
        const {data} = await communityListReq()
        this.list = data
      } catch (error) {
        console.error('获取小区列表失败:', error)
        uni.showToast({
          title: '获取数据失败',
          icon: 'none'
        })
      }
    },
    goToDetail(item) {
      // 跳转到详情页，传递小区ID
      uni.navigateTo({
        url: `/pages/community/detail?id=${item.id}`
      })
    }
  },
  onLoad() {
    this.getList()
  }
}
</script>

<style scoped>
.table-header {
  background-color: #f5f5f5;
  font-weight: bold;
}

.table-row {
  display: flex;
  flex-direction: row;
  width: 100%;
  padding: 10px 0;
}

.table-cell {
  flex: 1;
  text-align: center;
  padding: 5px;
  font-size: 12px;
}

.header-cell {
  color: #333;
}
</style>