<!-- 认证页面 -->
<template>
  <s-layout title="业主认证">
    <view class="check-page">
      <!-- 房屋列表 -->
      <scroll-view scroll-y class="house-list">
        <!-- 单个房屋项 -->
        <view
          v-for="(item, index) in state.houseList"
          :key="index"
          class="house-item"
          @tap="handleItemTap(item)"
        >
          <!-- 主要信息区 -->
          <view class="house-main">
            <view class="house-icon-wrapper">
              <image
                class="house-icon"
                src="/static/img/login_img.png"
                mode="aspectFit"
              />
            </view>
            <view class="house-info">
              <view class="house-name-row">
                <text class="house-name">{{ item.communityName }}</text>
                <text :class="['status-tag', getStatusClass(item.status)]">{{ getStatusText(item.status) }}</text>
              </view>
              <text class="house-address">{{ item.address }}</text>
            </view>
          </view>

          <!-- 操作区（已认证/已驳回显示） -->
          <view 
            v-if="item.status !== 0" 
            class="house-action"
            @tap.stop="handleItemTap(item)"
          >
            <view class="action-left">
              <image 
                class="action-icon" 
                :src="item.status === 1 ? '/static/img/yz-icon1.png' : '/static/img/yz-icon2.png'" 
                mode="aspectFit" 
              />
              <text class="action-text">{{ item.status === 1 ? '查看关联人员' : '查看原因' }}</text>
            </view>
            <image class="action-arrow-icon" src="/static/img/right-icon.png" mode="aspectFit" />
          </view>
        </view>

        <!-- 空状态 -->
        <view v-if="state.houseList.length === 0" class="empty-state">
          <text class="empty-text">暂无房屋信息</text>
        </view>
      </scroll-view>

      <!-- 底部按钮 -->
      <view class="bottom-btn-wrapper">
        <button class="submit-btn" @tap="goAddHouse">房屋不在此，去认证</button>
      </view>

      <!-- 驳回原因弹窗 - 使用全局 su-popup -->
      <su-popup
        :show="state.showRejectModal"
        type="center"
        round="24"
        :showClose="true"
        @close="closeRejectModal"
      >
        <view class="reject-modal-content">
          <view class="modal-header">
            <text class="modal-title">提示</text>
          </view>
          <view class="modal-body">
            <text class="reject-label">驳回原因：</text>
            <text class="reject-reason">{{ state.rejectReason }}</text>
          </view>
        </view>
      </su-popup>
    </view>
  </s-layout>
</template>

<script setup>
import { reactive } from 'vue';
import { onLoad } from '@dcloudio/uni-app';
import MemberHouseApi from '@/sheep/api/community/memberHouse';

// 页面状态
const state = reactive({
  houseList: [],
  showRejectModal: false,
  rejectReason: '',
});

// 状态映射（0-待审核, 1-已认证, 2-驳回）
const STATUS_MAP = {
  0: { text: '待审核', class: 'status-pending' },
  1: { text: '已认证', class: 'status-approved' },
  2: { text: '已驳回', class: 'status-rejected' },
};

// 获取状态文字
const getStatusText = (status) => {
  return STATUS_MAP[status]?.text || '未知';
};

// 获取状态样式类
const getStatusClass = (status) => {
  return STATUS_MAP[status]?.class || '';
};

// 获取我的房屋认证列表
const fetchHouseList = async () => {
  const { code, data } = await MemberHouseApi.getMyList();
  if (code === 0) {
    state.houseList = (data || []).map((item) => ({
      ...item,
      // 拼接完整地址
      address: `${item.buildingNo}${item.unitNo}${item.roomNo}`,
    }));
  }
};

onLoad(() => {
  fetchHouseList();
});

// 点击房屋项
const handleItemTap = (item) => {
  if (item.status === 1) {
    // 已认证 - 查看关联人员
    uni.navigateTo({
      url: '/pages/index/related-members'
    });
  } else if (item.status === 2) {
    // 已驳回 - 显示驳回原因弹窗
    state.rejectReason = item.rejectReason || '认证信息不完整，请重新提交';
    state.showRejectModal = true;
  }
};

// 关闭驳回弹窗
const closeRejectModal = () => {
  state.showRejectModal = false;
};

// 去添加房屋
const goAddHouse = () => {
  uni.navigateTo({
    url: '/pages/index/auth-form'
  });
};
</script>

<style lang="scss" scoped>
/* 页面容器 - 使用定位让按钮固定底部 */
.check-page {
  position: relative;
  background-color: #F5F5F5;
  min-height: 100%;
}

/* 房屋列表容器 - 减去底部按钮高度 */
.house-list {
  height: calc(100vh - 176rpx - 250rpx); /* 导航栏 + 底部按钮区域 */
  padding-bottom: 30rpx;
  padding-top: 30rpx;
}

/* 单个房屋项 - 圆角31rpx */
.house-item {
  background-color: #FFFFFF;
  border-radius: 31rpx;
  padding: 32rpx;
  /* 左右边距38rpx */
  margin: 0 38rpx 24rpx;
  box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.05);

  &:last-child {
    margin-bottom: 0;
  }
}

/* 主要信息区 */
.house-main {
  display: flex;
  align-items: flex-start;

  .house-icon-wrapper {
    width: 92rpx;
    height: 92rpx;
    margin-right: 26rpx;
    border-radius: 19rpx;
    flex-shrink: 0;

    .house-icon {
      width: 100%;
      height: 100%;
    }
  }

  .house-info {
    flex: 1;
    min-width: 0;

    .house-name-row {
      display: flex;
      align-items: center;
      justify-content: space-between;
      margin-bottom: 12rpx;

      .house-name {
        font-size: 32rpx;
        font-weight: 600;
        color: #333333;
      }

      .status-tag {
        font-size: 24rpx;
        padding: 6rpx 20rpx;
        border-radius: 8rpx;
        margin-left: 16rpx;
        flex-shrink: 0;

        &.status-pending {
          color: #FF6B35;
          background-color: rgba(255, 107, 53, 0.1);
        }

        &.status-approved {
          color: #07C160;
          background-color: rgba(7, 193, 96, 0.1);
        }

        &.status-rejected {
          color: #E74C3C;
          background-color: rgba(231, 76, 60, 0.1);
        }
      }
    }

    .house-address {
      font-size: 26rpx;
      color: #999999;
      line-height: 1.4;
    }
  }
}

/* 操作区 */
.house-action {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 24rpx;
  padding-top: 24rpx;
  border-top: 1rpx solid #F0F0F0;

  .action-left {
    display: flex;
    align-items: center;

    .action-icon {
      width: 36rpx;
      height: 36rpx;
      margin-right: 17rpx;
      opacity: 0.7;
    }

    .action-text {
        font-family: Source Han Sans SC, Source Han Sans SC;
        font-weight: 400;
        font-size: 27rpx;
        color: #666666;
        text-align: left;
        font-style: normal;
        text-transform: none;
    }
  }

  .action-arrow-icon {
    width: 28rpx;
    height: 28rpx;
    opacity: 0.6;
  }
}

/* 空状态 */
.empty-state {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 120rpx 0;

  .empty-text {
    font-size: 28rpx;
    color: #999999;
  }
}

/* 底部按钮 - 固定底部 */
.bottom-btn-wrapper {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 24rpx 38rpx;
  padding-bottom: calc(24rpx + env(safe-area-inset-bottom));
  background-color: #F5F5F5;

  .submit-btn {
    width: 100%;
    height: 96rpx;
    background: linear-gradient(135deg, #FF8A65 0%, #FF6B35 100%);
    border-radius: 48rpx;
    border: none;
    font-size: 32rpx;
    font-weight: 500;
    color: #FFFFFF;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 8rpx 24rpx rgba(255, 107, 53, 0.3);

    &::after {
      border: none;
    }

    &:active {
      opacity: 0.9;
      transform: scale(0.98);
    }
  }
}

/* 驳回原因弹窗内容 - 使用 su-popup 容器 */
.reject-modal-content {
  width: 560rpx;
  padding-bottom: 20rpx;

  .modal-header {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 32rpx 24rpx 16rpx;

    .modal-title {
      font-size: 34rpx;
      font-weight: 600;
      color: #333333;
    }
  }

  .modal-body {
    padding: 8rpx 28rpx 36rpx;
    display: flex;
    line-height: 1.6;

    .reject-label {
      font-size: 28rpx;
      color: #FF6B35;
      font-weight: 500;
      flex-shrink: 0;
    }

    .reject-reason {
      font-size: 28rpx;
      color: #666666;
      flex: 1;
    }
  }
}
</style>
