<!-- 关联人员页面 -->
<template>
  <s-layout title="关联人员">
    <view class="member-page">
      <scroll-view scroll-y class="member-list">
        <view
          v-for="(item, index) in memberList"
          :key="item.id || index"
          class="member-item"
        >
          <!-- 左侧头像 -->
          <image
            class="member-avatar"
            :src="item.facePhotoUrl || '/static/img/person.png'"
            mode="aspectFill"
          />

          <!-- 中间信息 -->
          <view class="member-info-wrap">
            <text class="member-name">{{ item.name }}</text>
            <text class="member-role">{{ item.relationDesc }}</text>
          </view>

          <!-- 右侧解除绑定（非业主显示） -->
          <text
            v-if="!item.isOwner"
            class="unbind-btn"
            @tap.stop="handleUnbind(item)"
          >解除绑定</text>
        </view>

        <!-- 空状态 -->
        <view v-if="memberList.length === 0" class="empty-state">
          <text class="empty-text">暂无关联人员</text>
        </view>
      </scroll-view>

      <!-- 底部返回按钮 -->
      <view class="bottom-btn-wrapper">
        <button class="back-btn" @tap="goBack">返回</button>
      </view>
    </view>
  </s-layout>
</template>

<script setup>
import { ref } from 'vue';
import { onLoad } from '@dcloudio/uni-app';
import MemberApi from '@/sheep/api/community/member';
import sheep from '@/sheep';

// 关联人员列表
const memberList = ref([]);
// 当前房屋ID
const houseId = ref(null);

// 页面加载
onLoad((options) => {
  // 优先从页面参数获取房屋ID，否则从用户状态获取
  houseId.value = options.houseId || sheep.$store('user').userInfo.currentHouseId;
  if (houseId.value) {
    loadMemberList();
  }
});

// 加载关联人员列表
async function loadMemberList() {
  const { code, data } = await MemberApi.getRelationList(houseId.value);
  if (code === 0 && Array.isArray(data)) {
    memberList.value = data;
  }
}

// 解除绑定
const handleUnbind = (item) => {
  uni.showModal({
    title: '提示',
    content: `确定要解除与 ${item.name} 的关联吗？`,
    success: async (res) => {
      if (res.confirm) {
        const { code, data } = await MemberApi.unbind(item.id);
        if (code === 0 && data) {
          uni.showToast({ title: '解除成功', icon: 'success' });
          loadMemberList();
        }
      }
    }
  });
};

// 返回上一页
const goBack = () => {
  uni.navigateBack();
};
</script>

<style lang="scss" scoped>
/* 页面容器 */
.member-page {
  background-color: #F5F5F5;
}

/* 列表区域 */
.member-list {
  height: calc(100vh - 176rpx - 150rpx);
  padding-top: 20rpx;
}

/* 单个成员项 */
.member-item {
  background-color: #FFFFFF;
  margin: 0 38rpx 24rpx;
  border-radius: 31rpx;
  padding: 28rpx 32rpx;
  display: flex;
  align-items: center;

  /* 左侧头像 */
  .member-avatar {
    width: 72rpx;
    height: 72rpx;
    border-radius: 50%;
    flex-shrink: 0;
    margin-right: 20rpx;
    background-color: #F5F5F5;
  }

  /* 信息区域 */
  .member-info-wrap {
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: center;
    overflow: hidden;
  }

  /* 姓名 */
  .member-name {
    font-size: 30rpx;
    color: #333333;
    font-weight: 500;
    line-height: 1.4;
  }

  /* 角色 */
  .member-role {
    font-size: 24rpx;
    color: #999999;
    margin-top: 4rpx;
    line-height: 1.4;
  }

  /* 解除绑定按钮 */
  .unbind-btn {
    font-size: 26rpx;
    color: #FF6B35;
    flex-shrink: 0;
    margin-left: 16rpx;

    &:active {
      opacity: 0.7;
    }
  }
}

/* 空状态 */
.empty-state {
  display: flex;
  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;

  .back-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);
    }
  }
}
</style>
