<script setup lang="ts">
import { computed } from 'vue';
import type { TeamViewMode } from '@/views/personal-center/shared/team-dashboard';

defineOptions({ name: 'TeamContextPanel' });

interface Props {
  loading?: boolean;
  selectedLabel?: string;
  subordinateCount?: number;
}

const props = withDefaults(defineProps<Props>(), {
  loading: false,
  selectedLabel: '',
  subordinateCount: 0
});

const mode = defineModel<TeamViewMode>('mode', {
  required: true
});

const scopeOptions = computed(() => [
  { label: '个人视角', value: 'self' satisfies TeamViewMode },
  { label: '团队视角', value: 'team' satisfies TeamViewMode }
]);

const contextText = computed(() => {
  if (mode.value === 'self') {
    return '当前查看我自己的数据。';
  }

  if (props.selectedLabel) {
    return `当前范围：${props.selectedLabel}`;
  }

  return '当前查看团队数据。';
});
</script>

<template>
  <ElCard class="team-context-panel" body-class="team-context-panel__body">
    <div v-loading="props.loading" class="team-context-panel__layout">
      <div class="team-context-panel__controls">
        <ElSegmented v-model="mode" :options="scopeOptions" class="team-context-panel__segmented" />
      </div>

      <div class="team-context-panel__info">
        <div class="team-context-panel__info-main">
          <div class="team-context-panel__info-item">
            <span class="team-context-panel__info-label">当前范围</span>
            <strong class="team-context-panel__info-value">
              {{ props.selectedLabel || (mode === 'self' ? '我自己' : '--') }}
            </strong>
          </div>

          <div v-if="mode === 'team'" class="team-context-panel__info-item">
            <span class="team-context-panel__info-label">下属人数</span>
            <strong class="team-context-panel__info-value">{{ props.subordinateCount }}</strong>
          </div>
        </div>

        <p class="team-context-panel__info-desc">{{ contextText }}</p>
        <div v-if="$slots.default" class="team-context-panel__summary">
          <slot />
        </div>
      </div>
    </div>
  </ElCard>
</template>

<style scoped lang="scss">
.team-context-panel {
  border: 1px solid var(--el-border-color-light);
  background: var(--el-fill-color-blank);
  box-shadow: none;
}

:deep(.team-context-panel__body) {
  padding: 16px 18px;
}

.team-context-panel__layout {
  display: flex;
  align-items: flex-start;
  gap: 20px;
}

.team-context-panel__controls {
  flex-shrink: 0;
}

:deep(.team-context-panel__segmented) {
  padding: 6px;
  background: var(--el-fill-color-light);
  border-radius: 12px;
}

:deep(.team-context-panel__segmented .el-segmented__item) {
  min-width: 96px;
  min-height: 40px;
  padding: 0 22px;
  font-size: 14px;
}

.team-context-panel__info {
  flex: 1;
  min-width: 0;
  padding-left: 20px;
  border-left: 1px solid var(--el-border-color-lighter);
}

.team-context-panel__info-main {
  display: flex;
  flex-wrap: wrap;
  gap: 24px;
}

.team-context-panel__info-item {
  display: flex;
  align-items: baseline;
  gap: 10px;
  min-width: 180px;
}

.team-context-panel__info-label {
  color: var(--el-text-color-secondary);
  font-size: 12px;
  line-height: 1.5;
}

.team-context-panel__info-value {
  color: var(--el-text-color-primary);
  font-size: 15px;
  font-weight: 600;
  line-height: 1.5;
}

.team-context-panel__info-desc {
  margin: 10px 0 0;
  color: var(--el-text-color-regular);
  font-size: 13px;
  line-height: 1.6;
}

.team-context-panel__summary {
  margin-top: 14px;
}

@media (width <= 1200px) {
  .team-context-panel__layout {
    flex-direction: column;
    align-items: stretch;
  }

  .team-context-panel__info {
    padding-left: 0;
    padding-top: 14px;
    border-left: none;
    border-top: 1px solid var(--el-border-color-lighter);
  }
}
</style>
