<template>
  <view class="staff-manage-container">
    <!-- 下拉刷新提示 -->
    <view v-if="refreshing" class="refresh-tip-bar">
      <uni-icons type="loading" size="22" color="#ffb400" class="refresh-loading-icon" />
      <text class="refresh-tip-text">正在刷新...</text>
    </view>
    <!-- 顶部导航栏 -->
    <view class="navbar" :style="navbarStyle">
      <view class="nav-left" @tap="goBack">
        <uni-icons type="back" size="24" color="#222" />
      </view>
      <view class="nav-title">员工管理</view>
      <view class="nav-right">
        <!-- <uni-icons type="more-filled" size="24" color="#222" style="margin-right: 16rpx;" />
        <uni-icons type="scan" size="24" color="#222" /> -->
      </view>
    </view>
    <view class="staff-list" :style="{marginTop: navBarRealHeight + 'px'}">
      <view v-for="(staff, idx) in staffList" :key="idx" class="staff-card" @tap="goStaffDetail(staff)">
        <view class="staff-info-row">
          <text class="staff-label">姓名：</text>
          <text class="staff-value">{{ staff.name }}</text>
        </view>
        <view class="staff-info-row">
          <text class="staff-label">角色：</text>
          <text class="staff-value">{{ staff.role }}</text>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import pullRefreshMixin from '../mixins/pullRefreshMixin.js'
export default {
  mixins: [pullRefreshMixin],
  data() {
    return {
      staffList: [
        { name: '冯启彬', role: '质检员' },
        { name: '周琪', role: '质检员' },
        { name: '王凡玄', role: '质检员' },
        { name: '李世海', role: '质检员' },
        { name: '李娅', role: '质检员' },
        { name: '郑婷雅', role: '质检员' },
        { name: '冯思钰', role: '质检员' },
      ],
      statusBarHeight: 0,
      navBarRealHeight: 0,
      refreshing: false
    }
  },
  computed: {
    navbarStyle() {
      return `padding-top: ${this.statusBarHeight}px;`;
    }
  },
  onLoad() {
    uni.getSystemInfo({
      success: (res) => {
        this.statusBarHeight = res.statusBarHeight || 20;
      }
    });
    this.$nextTick(() => {
      uni.createSelectorQuery().select('.navbar').boundingClientRect(rect => {
        if (rect) {
          this.navBarRealHeight = rect.height;
        }
      }).exec();
    });
    // 获取员工列表
    this.fetchStaffList();
    // 监听刷新事件
    uni.$on('refreshStaffList', this.fetchStaffList);
  },
  onUnload() {
    uni.$off('refreshStaffList', this.fetchStaffList);
  },
  methods: {
    goBack() {
      uni.navigateBack();
    },
    goStaffDetail(staff) {
      uni.navigateTo({
        url: '/pages/manager/staff-detail',
        success: (res) => {
          res.eventChannel.emit('staffDetail', staff);
        }
      });
    },
    refreshData() {
      // TODO: 实现员工列表刷新逻辑，如重新请求接口
    },
    async onRefresh() {
      await this.refreshData && this.refreshData()
    },
    async fetchStaffList() {
      try {
        const res = await this.$api('getMyTeamList', { pageSize: 1000, current: 1 });
        if (res && res.code === 200 && res.result && Array.isArray(res.result.records)) {
          this.staffList = res.result.records
            .filter(user => user.role == 1 || user.role == 0 || user.role == 2)
            .map(user => ({
              id: user.id,
              name: user.nickName || '-',
              role: user.role == 1 ? '质检员' : user.role == 2 ? '管理员' : '用户'
            }));
        }
      } catch (e) {
        uni.showToast({ title: '获取员工列表失败', icon: 'none' });
      }
    },
  },
  onPullDownRefresh() {
    this.refreshing = true;
    Promise.resolve(this.fetchStaffList()).finally(() => {
      this.refreshing = false;
      uni.stopPullDownRefresh();
    });
  }
}
</script>

<style lang="scss" scoped>
.staff-manage-container {
  min-height: 100vh;
  background: #f7f7f7;
  padding-bottom: 40rpx;
}
.refresh-tip-bar {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 80rpx;
  background: #fffbe6;
  color: #ffb400;
  font-size: 30rpx;
  font-weight: 500;
  text-align: center;
  line-height: 80rpx;
  z-index: 9999;
  box-shadow: 0 2rpx 8rpx rgba(255, 156, 0, 0.03);
  display: flex;
  align-items: center;
  justify-content: center;
}
.refresh-loading-icon {
  margin-right: 16rpx;
}
.refresh-tip-text {
  font-size: 30rpx;
  color: #ffb400;
}
.navbar {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100rpx;
  background: #fff;
  z-index: 10;
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.03);
  padding: 0 32rpx;
  .nav-left {
    flex: 0 0 48rpx;
    display: flex;
    align-items: center;
    height: 100%;
  }
  .nav-title {
    flex: 1;
    text-align: center;
    font-size: 36rpx;
    font-weight: bold;
    color: #222;
    line-height: 100rpx;
  }
  .nav-right {
    flex: 0 0 80rpx;
    display: flex;
    align-items: center;
    justify-content: flex-end;
    height: 100%;
  }
}
.staff-list {
  margin-top: calc(100rpx + var(--status-bar-height));
  padding: 32rpx 0;
  min-height: calc(100vh - 100rpx - var(--status-bar-height));
  box-sizing: border-box;
}
.staff-card {
  background: #fff;
  border-radius: 40rpx;
  margin: 0 32rpx 32rpx 32rpx;
  padding: 40rpx 36rpx 36rpx 36rpx;
  box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.03);
}
.staff-info-row {
  display: flex;
  align-items: center;
  margin-bottom: 18rpx;
  .staff-label {
    font-size: 30rpx;
    color: #b3b3b3;
    width: 110rpx;
    font-weight: 400;
  }
  .staff-value {
    font-size: 32rpx;
    color: #222;
    font-weight: 500;
  }
}
</style> 