<template>
  <view class="header-container">
    <view class="header-content">
      <!-- 团队选择器 -->
      <view class="team-selector" @click="showTeamPicker = true">
        <text class="team-name">{{ currentTeamName }}</text>
        <image class="arrow-icon" src="/static/zhuanhua.svg" mode="aspectFit" />
      </view>

      <!-- 右侧操作按钮 -->
      <view class="header-actions">
        <view class="action-btn" @click="handleAddPatient">
          <image
            class="invite-icon"
            src="/static/work/qrcode.svg"
            mode="aspectFit"
          />
          <text class="action-text">邀请患者</text>
        </view>
        <view class="action-btn" @click="toTeams">
          <image
            class="invite-icon"
            src="/static/work/icon-team.svg"
            mode="aspectFit"
          />
          <text class="action-text">我的团队</text>
        </view>
      </view>
    </view>

    <!-- 标签页切换 -->
    <view class="tabs-container">
      <view
        class="tab-item"
        :class="{ active: activeTab === 'processing' }"
        @click="handleTabChange('processing')"
      >
        <text class="tab-text">处理中</text>
      </view>
      <view
        class="tab-item"
        :class="{ active: activeTab === 'finished' }"
        @click="handleTabChange('finished')"
      >
        <text class="tab-text">已结束</text>
      </view>
    </view>

    <!-- 团队选择弹窗 -->
    <view
      v-if="showTeamPicker"
      class="team-picker-overlay"
      @click="showTeamPicker = false"
    >
      <view class="team-picker-content" @click.stop>
        <view class="team-picker-header">
          <text class="picker-title">选择团队</text>
        </view>
        <scroll-view class="team-list" scroll-y>
          <view
            v-for="team in teamList"
            :key="team.teamId"
            class="team-item"
            :class="{ active: currentTeamId === team.teamId }"
            @click="selectTeam(team)"
          >
            <text class="team-item-name">{{ team.name }}</text>
            <text v-if="currentTeamId === team.teamId" class="check-icon">✓</text>
          </view>
        </scroll-view>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref, computed } from "vue";
import { storeToRefs } from "pinia";
import useTeamStore from "@/store/team.js";

// Props
const props = defineProps({
  activeTab: {
    type: String,
    default: "processing",
  },
});

// Emits
const emit = defineEmits(["update:activeTab", "teamChange", "addPatient"]);

// 获取团队信息
const teamStore = useTeamStore();
const { teams } = storeToRefs(teamStore);

// 团队相关状态
const showTeamPicker = ref(false);
const currentTeamId = ref(""); // 空字符串表示"全部会话消息"

// 团队列表（包含"全部会话消息"选项）
const teamList = computed(() => {
  const allOption = { teamId: "", name: "全部会话消息" };
  return [allOption, ...(teams.value || [])];
});

// 当前团队名称
const currentTeamName = computed(() => {
  if (!currentTeamId.value) return "全部会话消息";
  const team = teams.value.find((t) => t.teamId === currentTeamId.value);
  return team ? team.name : "全部会话消息";
});

// 选择团队
const selectTeam = (team) => {
  currentTeamId.value = team.teamId;
  showTeamPicker.value = false;
  console.log("切换到团队:", team.name);
  emit("teamChange", team.teamId);
};

// 切换标签页
const handleTabChange = (tab) => {
  if (props.activeTab === tab) return;
  emit("update:activeTab", tab);
};

// 添加患者
const handleAddPatient = () => {
  emit("addPatient");
};

function toTeams() {
  uni.navigateTo({ url: '/pages/work/team/list/team-list' });
}
</script>

<style scoped lang="scss">
.header-container {
  background-color: #fff;
  flex-shrink: 0;
}

.header-content {
  display: flex;
  align-items: center;
  padding: 16rpx 32rpx;
  border-bottom: 1rpx solid #f0f0f0;
}

.team-selector {
  flex: 1;
  display: flex;
  align-items: center;
  min-width: 0;
  cursor: pointer;

  &:active {
    opacity: 0.7;
  }
}

.team-name {
  font-size: 34rpx;
  font-weight: 500;
  color: #333;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.arrow-icon {
  width: 24rpx;
  height: 24rpx;
  margin-left: 8rpx;
  flex-shrink: 0;
  opacity: 0.6;
  filter: brightness(0.5);
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 16rpx;
  margin-left: 16rpx;
  flex-shrink: 0;
}

.action-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 6rpx 12rpx;
  //   background-color: #f5f5f5;
  border-radius: 8rpx;

  &:active {
    opacity: 0.7;
  }
}

.invite-icon {
  width: 32rpx;
  height: 32rpx;
  margin-bottom: 4rpx;
}

.action-text {
  font-size: 22rpx;
  color: #666;
  line-height: 1.4;
  text-align: center;
}

.tabs-container {
  display: flex;
  padding: 0 32rpx;
  gap: 48rpx;
}

.tab-item {
  position: relative;
  padding: 20rpx 0;
  cursor: pointer;

  &.active {
    .tab-text {
      color: #1890ff;
      font-weight: 500;
    }

    &::after {
      content: "";
      position: absolute;
      bottom: 0;
      left: 50%;
      transform: translateX(-50%);
      width: 48rpx;
      height: 4rpx;
      background-color: #1890ff;
      border-radius: 2rpx;
    }
  }
}

.tab-text {
  font-size: 30rpx;
  color: #666;
  transition: color 0.3s;
}

.team-picker-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  z-index: 1000;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding-top: 200rpx;
}

.team-picker-content {
  width: 600rpx;
  max-height: 800rpx;
  background-color: #fff;
  border-radius: 16rpx;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.team-picker-header {
  padding: 32rpx;
  border-bottom: 1rpx solid #f0f0f0;
  text-align: center;
}

.picker-title {
  font-size: 34rpx;
  font-weight: 500;
  color: #333;
}

.team-list {
  flex: 1;
  overflow-y: auto;
}

.team-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 32rpx;
  border-bottom: 1rpx solid #f0f0f0;
  cursor: pointer;

  &:active {
    background-color: #f5f5f5;
  }

  &.active {
    background-color: #e6f7ff;

    .team-item-name {
      color: #1890ff;
    }
  }

  &:last-child {
    border-bottom: none;
  }
}

.team-item-name {
  font-size: 32rpx;
  color: #333;
  flex: 1;
}

.check-icon {
  font-size: 34rpx;
  color: #1890ff;
  font-weight: bold;
}
</style>
