<!-- 物业人员页面 -->
<template>
  <s-layout title="物业人员" :bgStyle="{ backgroundColor:'#F8EDE8' }" navbar="inner" color="#333333">
    <view class="staff-page">
      <!-- 渐变背景装饰 -->
      <view class="gradient-bg"></view>

      <!-- 固定头部：统计卡片+分类Tab -->
      <view class="page-header">
        <!-- 统计卡片 -->
        <view class="stats-card">
          <!-- 小区名称 -->
          <view class="card-top">
            <view class="building-info">
              <text class="sicon-building building-icon"></text>
              <text class="building-name">{{ statsInfo.buildingName }}</text>
            </view>
            <view class="staff-badge">物业人员</view>
          </view>

          <!-- 总人数 -->
          <view class="total-row">
            <text class="total-label">总人数</text>
            <text class="total-count">{{ statsInfo.total }}<text class="total-unit">人</text></text>
          </view>

          <!-- 分类统计 -->
          <view class="category-row">
            <view 
              class="category-item" 
              v-for="(cat, index) in statsInfo.categories" 
              :key="index"
            >
              <text class="cat-name">{{ cat.name }}</text>
              <text class="cat-count">{{ cat.count }}</text>
            </view>
          </view>
        </view>

        <!-- 分类Tab栏 -->
        <scroll-view class="tab-scroll" scroll-x :show-scrollbar="false">
          <view class="tab-bar">
            <view 
              class="tab-item"
              :class="{ active: currentTab === tab.key }"
              v-for="(tab, index) in tabList"
              :key="index"
              @tap="currentTab = tab.key"
            >
              <text class="tab-text">{{ tab.label }}</text>
            </view>
          </view>
        </scroll-view>

        <!-- 人员信息标题 -->
        <view class="section-title">人员信息</view>
      </view>

      <!-- 人员列表 - 独立滚动区域 -->
      <scroll-view class="staff-scroll" scroll-y :style="{ height: scrollHeight + 'px' }">
        <view class="staff-list">
          <!-- 员工卡片 -->
          <view 
            class="staff-item"
            v-for="(item, index) in filteredStaffList"
            :key="index"
          >
            <!-- 头像 -->
            <image class="staff-avatar" :src="item.avatar" mode="aspectFill" />

            <!-- 信息区 -->
            <view class="staff-info">
              <!-- 姓名+职业 -->
              <view class="name-row">
                <text class="name-label">姓名：</text>
                <text class="name-text">{{ item.name }}</text>
                <text class="job-label">职业：</text>
                <text class="job-text">{{ item.job }}</text>
              </view>
              
              <!-- 电话 -->
              <view class="phone-row">
                <text class="phone-label">电话：</text>
                <text class="phone-text">{{ item.phone }}</text>
                <view class="call-btn" @tap="callPhone(item.phone)">
                  <text class="sicon-phone call-icon"></text>
                </view>
              </view>

              <!-- 职责描述 -->
              <view class="duty-row">
                <text class="duty-label">职责：</text>
                <text class="duty-text">{{ item.duty }}</text>
              </view>
            </view>
          </view>

          <!-- 空状态 -->
          <view class="empty-state" v-if="filteredStaffList.length === 0">
            <text class="empty-text">暂无相关人员</text>
          </view>
        </view>
      </scroll-view>
    </view>
  </s-layout>
</template>

<script setup>
import { ref, computed, onMounted, nextTick, getCurrentInstance } from 'vue';
import { onLoad } from '@dcloudio/uni-app';

// 当前选中的分类Tab
const currentTab = ref('security');

// 列表滚动高度
const scrollHeight = ref(0);

// Tab列表
const tabList = ref([
  { key: 'security', label: '安保类' },
  { key: 'cleaning', label: '保洁类' },
  { key: 'engineering', label: '工程类' },
  { key: 'admin', label: '行政管理类' },
  { key: 'finance', label: '财务类' }
]);

// 统计信息
const statsInfo = ref({
  buildingName: '融侨馨苑',
  total: 48,
  categories: [
    { name: '安保', count: 10 },
    { name: '保洁', count: 10 },
    { name: '维修', count: 10 },
    { name: '楼管', count: 10 },
    { name: '经理', count: 2 },
    { name: '客服', count: 10 }
  ]
});

// 人员列表数据
const staffList = ref([
  {
    id: 1,
    category: 'security',
    name: '吴仁贵',
    job: '安保主管',
    phone: '184****5263',
    duty: '人员调配与排班、根据项目需求制定安保人员排班计划，确保24小时无缝覆盖。',
    avatar: '/static/img/guest.png'
  },
  {
    id: 2,
    category: 'security',
    name: '李梓发',
    job: '保安',
    phone: '187****1005',
    duty: '定期巡查项目重点区域，检查安全设施（监控、消防器材、门禁系统）是否完好',
    avatar: '/static/img/login_img.png'
  },
  {
    id: 3,
    category: 'security',
    name: '冯启彬',
    job: '保安',
    phone: '187****1005',
    duty: '定期巡查项目重点区域，检查安全设施（监控、消防器材、门禁系统）是否完好',
    avatar: '/static/img/guest.png'
  }
]);

// 根据当前Tab筛选列表
const filteredStaffList = computed(() => {
  return staffList.value.filter(item => item.category === currentTab.value);
});

// 页面加载
onLoad(() => {
  loadStaffData();
});

// 渲染完成后计算scroll-view高度
onMounted(() => {
  const instance = getCurrentInstance();
  nextTick(() => {
    const sysInfo = uni.getSystemInfoSync();
    uni.createSelectorQuery()
      .in(instance)
      .select('.page-header')
      .boundingClientRect((rect) => {
        if (rect) {
          // scroll-view高度 = 屏幕可用高度 - 头部高度 - 头部距顶部距离
          scrollHeight.value = sysInfo.windowHeight - rect.height - rect.top;
        }
      })
      .exec();
  });
});

// 加载人员数据（TODO: 对接API）
function loadStaffData() {
  // TODO: 调用API获取物业人员列表
}

// 拨打电话
function callPhone(phone) {
  uni.makePhoneCall({
    phoneNumber: phone.replace(/\*/g, '') // 去掉脱敏星号
  });
}
</script>

<style lang="scss" scoped>
/* ==================== 页面容器 ==================== */
.staff-page {
  position: relative;
  min-height: 100vh;
}

/* ==================== 渐变背景 ==================== */
.gradient-bg {
  position: absolute;
  top: -176rpx;
  left: 0;
  right: 0;
  height: calc(100% + 176rpx);
  background: linear-gradient(180deg, #F8EDE8 0%, #FFFFFF 30%);
  z-index: -1;
}

/* ==================== 固定头部区域 ==================== */
.page-header {
  padding-bottom: 16rpx;
}

/* ==================== 统计卡片 ==================== */
.stats-card {
  margin: 24rpx 32rpx 24rpx;
  padding: 28rpx 32rpx 24rpx;
  background: linear-gradient(135deg, #EBF4FF 0%, #D6E7FF 100%);
  border-radius: 24rpx;

  /* 卡片顶部：小区名+标签 */
  .card-top {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 20rpx;
  }

  /* 小区信息 */
  .building-info {
    display: flex;
    align-items: center;

    .building-icon {
      font-size: 36rpx;
      color: #333333;
      margin-right: 12rpx;
    }

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

  /* 物业人员标签 */
  .staff-badge {
    background: linear-gradient(135deg, #5B9BFF 0%, #3D7BD9 100%);
    color: #FFFFFF;
    font-size: 26rpx;
    font-weight: 500;
    padding: 8rpx 24rpx;
    border-radius: 24rpx 0 24rpx 0;
  }

  /* 总人数行 */
  .total-row {
    display: flex;
    align-items: baseline;
    margin-bottom: 24rpx;

    .total-label {
      font-size: 26rpx;
      color: #666666;
      margin-right: 16rpx;
    }

    .total-count {
      font-size: 48rpx;
      font-weight: 700;
      color: #FA7E49;  /* 橙色主题 */

      .total-unit {
        font-size: 28rpx;
        font-weight: 400;
        color: #FA7E49;
        margin-left: 4rpx;
      }
    }
  }

  /* 分类统计行 */
  .category-row {
    display: flex;
    justify-content: space-between;

    .category-item {
      display: flex;
      flex-direction: column;
      align-items: center;

      .cat-name {
        font-size: 26rpx;
        color: #666666;
        margin-bottom: 8rpx;
      }

      .cat-count {
        font-size: 32rpx;
        font-weight: 600;
        color: #333333;
      }
    }
  }
}

/* ==================== 分类Tab栏 ==================== */
.tab-scroll {
  white-space: nowrap;
  margin: 0 0 8rpx;
}

.tab-bar {
  display: inline-flex;
  padding: 0 32rpx;

  .tab-item {
    padding: 20rpx 0;
    margin-right: 40rpx;
    position: relative;

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

    .tab-text {
      font-size: 28rpx;
      color: #999999;
      transition: all 0.2s;
    }

    /* 选中状态 */
    &.active {
      .tab-text {
        color: #FA7E49;  /* 橙色主题 */
        font-weight: 600;
      }

      /* 底部橙色指示条 */
      &::after {
        content: '';
        position: absolute;
        bottom: 0;
        left: 50%;
        transform: translateX(-50%);
        width: 48rpx;
        height: 4rpx;
        background: #FA7E49;
        border-radius: 2rpx;
      }
    }
  }
}

/* ==================== 人员信息标题 ==================== */
.section-title {
  padding: 16rpx 32rpx 20rpx;
  font-size: 32rpx;
  font-weight: 600;
  color: #333333;
}

/* ==================== 人员列表滚动区域 ==================== */
.staff-scroll {
  width: 100%;
}

/* ==================== 人员列表内容 ==================== */
.staff-list {
  padding: 0 32rpx 160rpx;
}

/* ==================== 单个员工卡片 ==================== */
.staff-item {
  display: flex;
  padding: 28rpx 0;

  /* 分割线（非第一项） */
  &:not(:last-child)::after {
    content: '';
    position: absolute;
    bottom: 0;
    left: 112rpx;
    right: 0;
    height: 1rpx;
    background-color: #F0F0F0;
  }

  /* 头像 */
  .staff-avatar {
    width: 96rpx;
    height: 96rpx;
    border-radius: 50%;
    flex-shrink: 0;
    background-color: #F0F0F0;
  }

  /* 信息区域 */
  .staff-info {
    flex: 1;
    margin-left: 24rpx;

    /* 姓名+职业行 */
    .name-row {
      display: flex;
      align-items: center;
      margin-bottom: 12rpx;

      .name-label,
      .job-label {
        font-size: 26rpx;
        color: #999999;
      }

      .name-text,
      .job-text {
        font-size: 28rpx;
        color: #333333;
        font-weight: 500;
        margin-right: 24rpx;
      }
    }

    /* 电话行 */
    .phone-row {
      display: flex;
      align-items: center;
      margin-bottom: 12rpx;

      .phone-label {
        font-size: 26rpx;
        color: #999999;
      }

      .phone-text {
        font-size: 28rpx;
        color: #333333;
        margin-right: 16rpx;
      }

      /* 拨打按钮 */
      .call-btn {
        width: 44rpx;
        height: 44rpx;
        border-radius: 50%;
        background-color: #FA7E49;
        display: flex;
        align-items: center;
        justify-content: center;

        .call-icon {
          font-size: 24rpx;
          color: #FFFFFF;
        }
      }
    }

    /* 职责描述行 */
    .duty-row {
      .duty-label {
        font-size: 26rpx;
        color: #999999;
      }

      .duty-text {
        font-size: 26rpx;
        color: #666666;
        line-height: 1.6;
      }
    }
  }
}

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

  .empty-text {
    font-size: 28rpx;
    color: #999999;
  }
}
</style>
