<!-- 数据分析顶部导航栏组件 -->
<template>
  <view class="analytics-topbar">
    <view class="topbar-left">
      <!-- 仅窄屏且侧边栏未打开时显示菜单按钮 -->
      <view class="menu-icon" v-if="showMenuIcon" @click="handleMenu">
        <text class="icon">☰</text>
      </view>
      <view class="title-group">
        <text class="title">{{ title }}</text>
        <text class="subtitle">最后更新：{{ lastUpdateTime }}</text>
      </view>
    </view>
    <view class="topbar-right">
      <!-- 宽屏时显示的按钮 -->
      <view class="icon-btn-icon btn-visible" @click="handleRefresh">
        <text class="icon">🔄</text>
      </view>
      <view class="icon-btn-icon btn-visible" @click="handleSearch">
        <text class="icon">🔍</text>
      </view>
      <view class="icon-btn-icon notification btn-hidden" @click="handleNotification">
        <text class="icon">🔔</text>
        <view class="badge"></view>
      </view>
      <view class="icon-btn-icon btn-hidden" @click="handleFullscreen">
        <text class="icon">⛶</text>
      </view>
      <view class="icon-btn-icon btn-hidden" @click="handleMobile">
        <text class="icon">📱</text>
      </view>
      <view class="dropdown btn-visible" @click="handleDropdown">
        <text class="dropdown-text">crmeb demo</text>
        <text class="dropdown-arrow">▼</text>
      </view>
      <view class="icon-btn-icon btn-hidden" @click="handleSettings">
        <text class="icon">⚙️</text>
      </view>
      
      <!-- 更多按钮（窄屏时显示） -->
      <view class="more-btn" :class="{ active: showMoreMenu }" @click.stop="toggleMoreMenu">
        <text class="icon">⋯</text>
      </view>
    </view>
    
    <!-- 更多菜单下拉 -->
    <view class="more-menu" v-if="showMoreMenu" @click.stop>
      <view class="more-menu-item" @click="handleNotification">
        <text class="icon">🔔</text>
        <text>通知</text>
      </view>
      <view class="more-menu-item" @click="handleFullscreen">
        <text class="icon">⛶</text>
        <text>全屏</text>
      </view>
      <view class="more-menu-item" @click="handleMobile">
        <text class="icon">📱</text>
        <text>移动端</text>
      </view>
      <view class="more-menu-item" @click="handleSettings">
        <text class="icon">⚙️</text>
        <text>设置</text>
      </view>
    </view>
  </view>
</template>

<script lang="uts">
export default {
  props: {
    title: {
      type: String,
      default: '数据分析中心'
    },
    lastUpdateTime: {
      type: String,
      default: ''
    },
    // 由页面传入：当前侧边栏是否处于“打开/显示”状态（窄屏下用于隐藏菜单按钮）
    sidebarVisible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      showMoreMenu: false,
      isWideScreen: false
    }
  },
  computed: {
    showMenuIcon(): boolean {
      // 宽屏不显示；窄屏仅在侧边栏未打开时显示
      return !this.isWideScreen && !this.sidebarVisible
    }
  },
  onLoad() {
    this.checkScreenSize()
  },
  onShow() {
    this.checkScreenSize()
  },
  methods: {
    checkScreenSize() {
      const systemInfo = uni.getSystemInfoSync()
      const w = systemInfo.windowWidth || systemInfo.screenWidth
      // 与侧边栏一致：960px 以上视为宽屏
      this.isWideScreen = w >= 960
    },
    handleMenu() {
      this.$emit('menu-click')
    },
    handleRefresh() {
      this.$emit('refresh')
    },
    handleSearch() {
      this.$emit('search')
    },
    handleNotification() {
      this.showMoreMenu = false
      this.$emit('notification')
    },
    handleFullscreen() {
      this.showMoreMenu = false
      this.$emit('fullscreen')
    },
    handleMobile() {
      this.showMoreMenu = false
      this.$emit('mobile')
    },
    handleDropdown() {
      this.$emit('dropdown')
    },
    handleSettings() {
      this.showMoreMenu = false
      this.$emit('settings')
    },
    toggleMoreMenu() {
      this.showMoreMenu = !this.showMoreMenu
    }
  }
}
</script>

<style>
.analytics-topbar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  width: 100%;
  height: 64px;
  background: #ffffff;
  border-bottom: 1px solid rgba(0, 0, 0, 0.06);
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
  flex-wrap: nowrap;
  padding: 0 16px;
  z-index: 1000;
  box-sizing: border-box;
}

.topbar-left {
  display: flex;
  flex-direction: row;
  align-items: center;
  flex: 1;
  min-width: 0;
}

.menu-icon {
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  background: #f3f4f6;
  margin-right: 12px;
  flex-shrink: 0;
}

.menu-icon .icon {
  font-size: 18px;
  color: #333;
}

.title-group {
  display: flex;
  flex-direction: row;
  align-items: baseline;
  min-width: 0;
}

.title {
  font-size: 18px;
  font-weight: 700;
  color: #111;
  line-height: 1.4;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.subtitle {
  font-size: 12px;
  color: rgba(0, 0, 0, 0.55);
  line-height: 1.4;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  margin-left: 8px;
}

.topbar-right {
  display: flex;
  flex-direction: row;
  align-items: center;
  flex-shrink: 0;
}

.icon-btn-icon {
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  background: #f3f4f6;
  position: relative;
  margin-left: 8px;
}

.icon-btn-icon .icon {
  font-size: 18px;
  color: #333;
}

.icon-btn-icon.notification .badge {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 8px;
  height: 8px;
  background: #ef4444;
  border-radius: 50%;
  border: 2px solid #ffffff;
}

.dropdown {
  display: flex;
  align-items: center;
  padding: 8px 12px;
  border-radius: 8px;
  background: #f3f4f6;
  margin-left: 8px;
}

.dropdown-text {
  font-size: 14px;
  color: #333;
  font-weight: 500;
}

.dropdown-arrow {
  font-size: 10px;
  color: #666;
}

.more-btn {
  width: 40px;
  height: 40px;
  display: none;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  background: #f3f4f6;
  position: relative;
  margin-left: 8px;
}

.more-btn .icon {
  font-size: 20px;
  color: #333;
}

.more-menu {
  position: absolute;
  top: 100%;
  right: 16px;
  margin-top: 8px;
  background: #ffffff;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  padding: 8px 0;
  min-width: 160px;
  z-index: 1001;
}

.more-menu-item {
  display: flex;
  align-items: center;
  padding: 12px 16px;
}

.more-menu-item .icon {
  font-size: 18px;
}

.more-menu-item text {
  font-size: 14px;
  color: #333;
}

/* 响应式 */
@media screen and (max-width: 960px) {
  .btn-hidden {
    display: none !important;
  }
  
  .more-btn {
    display: flex !important;
  }
  
  .title,
  .subtitle {
    max-width: 200px;
  }
}
</style>
