<template>
  <view class="sub-page">
    <TopBar title="人员管理" show-back />

    <view class="px-4 pt-3">
      <!-- 筛选 -->
      <view class="filter-bar mb-3">
        <view
          v-for="status in filterStatuses"
          :key="status.value"
          class="filter-chip"
          :class="{ 'filter-chip-active': currentFilter === status.value }"
          @click="currentFilter = status.value"
        >
          {{ status.label }}
        </view>
      </view>

      <view v-if="loading" class="py-10 text-center">
        <text class="empty-text">加载中...</text>
      </view>

      <view v-else-if="filteredStaff.length === 0">
        <EmptyState message="暂无人员" />
      </view>

      <view v-else class="glass-card">
        <view
          v-for="staff in filteredStaff"
          :key="staff.staffId"
          class="list-row"
          hover-class="row-hover"
          :hover-start-time="0"
          :hover-stay-time="120"
          @click="showStaffDetail(staff)"
        >
          <view class="icon-chip mr-3">
            <text class="uni-icons uniui-person-filled chip-glyph"></text>
          </view>
          <view class="flex-1 min-w-0 mr-2">
            <text class="row-title truncate">{{ staff.name }}</text>
            <text class="row-sub truncate">{{ staff.phone }} | {{ staff.teamName || '未分配班组' }}</text>
          </view>
          <text class="status-tag flex-shrink-0 mr-1" :class="getStatusTagClass(staff.status)">
            {{ getStatusText(staff.status) }}
          </text>
          <text class="uni-icons uniui-arrowright row-arrow"></text>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { onPullDownRefresh } from '@dcloudio/uni-app'
import TopBar from '../../components/common/TopBar.vue'
import EmptyState from '../../components/common/EmptyState.vue'
import { getStaffList } from '../../api/staff'

const loading = ref(false)
const staffList = ref<any[]>([])
const currentFilter = ref('all')

const filterStatuses = [
  { value: 'all', label: '全部' },
  { value: 'on_duty', label: '在岗' },
  { value: 'working', label: '施工中' },
  { value: 'rest', label: '休息' },
]

const filteredStaff = computed(() => {
  if (currentFilter.value === 'all') return staffList.value
  return staffList.value.filter(s => s.status === currentFilter.value)
})

function getStatusText(status: string) {
  const map: Record<string, string> = {
    on_duty: '在岗',
    working: '施工中',
    rest: '休息',
  }
  return map[status] || status
}

function getStatusTagClass(status: string) {
  const map: Record<string, string> = {
    on_duty: 'status-tag-green',
    working: 'status-tag-blue',
    rest: 'status-tag-gold',
  }
  return map[status] || 'status-tag-gray'
}

function showStaffDetail(staff: any) {
  uni.showModal({
    title: staff.name,
    content: `电话: ${staff.phone}\n班组: ${staff.teamName || '未分配'}\n状态: ${getStatusText(staff.status)}`,
    showCancel: false
  })
}

onMounted(async () => {
  await fetchStaff()
})

async function fetchStaff() {
  loading.value = true
  try {
    const res = await getStaffList()
    staffList.value = res.rows || []
  } catch (error) {
    console.error('获取人员列表失败:', error)
  } finally {
    loading.value = false
  }
}

onPullDownRefresh(async () => {
  await fetchStaff()
  uni.stopPullDownRefresh()
})
</script>

<style scoped>
.sub-page {
  max-width: 430px;
  margin: 0 auto;
  min-height: 100vh;
  background-color: #f6fbf9;
  padding-bottom: 24px;
}

.glass-card {
  background-color: rgba(255, 255, 255, 0.85);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  border: 1px solid rgba(164, 216, 152, 0.35);
  border-radius: 24px;
  box-shadow: 0 18px 50px -12px rgba(54, 143, 111, 0.28);
  overflow: hidden;
  margin-bottom: 12px;
}

/* 筛选条：默认白底灰边，激活态绿色 */
.filter-bar {
  display: flex;
  gap: 8px;
  overflow-x: auto;
}

.filter-chip {
  padding: 6px 16px;
  border-radius: 999px;
  font-size: 13px;
  color: #6b7280;
  background-color: #ffffff;
  border: 1px solid #e5e7eb;
  flex-shrink: 0;
}

.filter-chip-active {
  color: #ffffff;
  font-weight: 600;
  background: linear-gradient(135deg, #368f6f 0%, #4ba98a 100%);
  border-color: transparent;
}

.list-row {
  display: flex;
  align-items: center;
  padding: 13px 16px;
  border-top: 1px solid rgba(164, 216, 152, 0.18);
}

.list-row:first-child {
  border-top: none;
}

.row-hover {
  background-color: rgba(164, 216, 152, 0.1);
}

.row-title {
  display: block;
  font-size: 14px;
  font-weight: 500;
  color: #1f2937;
}

.row-sub {
  display: block;
  margin-top: 3px;
  font-size: 12px;
  color: #9ca3af;
}

.row-arrow {
  font-size: 16px;
  color: #c4d0cb;
  flex-shrink: 0;
}

.status-tag {
  padding: 2px 10px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
}

.status-tag-green {
  color: #368f6f;
  background-color: rgba(164, 216, 152, 0.24);
}

.status-tag-blue {
  color: #2563eb;
  background-color: rgba(37, 99, 235, 0.1);
}

.status-tag-gold {
  color: #b7791f;
  background-color: rgba(251, 211, 141, 0.25);
}

.status-tag-gray {
  color: #6b7280;
  background-color: #f3f4f6;
}

.icon-chip {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: rgba(164, 216, 152, 0.2);
}

.chip-glyph {
  font-size: 20px;
  color: #368f6f;
}

.empty-text {
  font-size: 13px;
  color: #9ca3af;
}
</style>
