<template>
  <view class="page">
    <!-- #ifdef APP -->
    <statusBar></statusBar>
    <!-- #endif -->
    
    <!-- 导航栏 -->
    <uni-nav-bar 
      :fixed="true" 
      :statusBar="true" 
      title="AI销冠系统" 
      leftIcon="left" 
      @clickLeft="goBack"
      color="#333"
      backgroundColor="#FFFFFF">
    </uni-nav-bar>
    
    <view class="content">
      <!-- 功能分类展示 -->
      <scroll-view class="content-scroll" scroll-y="true" :style="{ top: computedContentTop || contentTop, bottom: contentBottom || '96rpx' }">
      <!-- 录音和接待统计 -->
      <view class="category-section two-column">
        <view class="category-header">
          <text class="category-icon">📊</text>
          <text class="category-title">录音和接待统计</text>
        </view>
        <view class="function-grid">
          <view class="function-item" @click="navigateToPage('recording_duration')">
            <view class="function-icon">⏱️</view>
            <text class="function-name">录音时长</text>
            <text class="function-desc">统计录音总时长</text>
          </view>
          <view class="function-item" @click="navigateToPage('customer_count')">
            <view class="function-icon">👥</view>
            <text class="function-name">接待客户数</text>
            <text class="function-desc">统计接待客户数量</text>
          </view>
        </view>
      </view>

      <!-- 质量管控 -->
      <view class="category-section two-column">
        <view class="category-header">
          <text class="category-icon">✅</text>
          <text class="category-title">质量管控</text>
        </view>
        <view class="function-grid">
          <view class="function-item" @click="navigateToPage('quality_coverage')">
            <view class="function-icon">🎯</view>
            <text class="function-name">质检覆盖率</text>
            <text class="function-desc">质检录音覆盖统计</text>
          </view>
          <view class="function-item" @click="navigateToPage('speech_rating')">
            <view class="function-icon">🗣️</view>
            <text class="function-name">员工话术评分</text>
            <text class="function-desc">员工沟通质量评分</text>
          </view>
        </view>
      </view>

      <!-- 销售分析 -->
      <view class="category-section sales-analysis">
        <view class="category-header">
          <text class="category-icon">📈</text>
          <text class="category-title">销售分析</text>
        </view>
        <view class="function-grid">
          <view class="function-item full-row-item" @click="navigateToPage('intent_level')">
            <view class="function-icon">🎯</view>
            <text class="function-name">客户意向分级</text>
            <text class="function-desc">客户购买意向分析</text>
          </view>
          <view class="function-item full-row-item" @click="navigateToPage('deal_attribution')">
            <view class="function-icon">💰</view>
            <text class="function-name">成交归因</text>
            <text class="function-desc">成交成功因素分析</text>
          </view>
          <view class="function-item full-row-item new-line-item" @click="navigateToPage('opening_rate')">
            <view class="function-icon">🚪</view>
            <text class="function-name">开口率</text>
            <text class="function-desc">客户主动开口统计</text>
          </view>
        </view>
      </view>

      <!-- 风险监控 -->
      <view class="category-section two-column">
        <view class="category-header">
          <text class="category-icon">⚠️</text>
          <text class="category-title">风险监控</text>
        </view>
        <view class="function-grid">
          <view class="function-item" @click="navigateToPage('over_commitment')">
            <view class="function-icon">🚨</view>
            <text class="function-name">过度承诺统计</text>
            <text class="function-desc">监控过度承诺情况</text>
          </view>
          <view class="function-item" @click="navigateToPage('red_line_touch')">
            <view class="function-icon">🔴</view>
            <text class="function-name">触及红线统计</text>
            <text class="function-desc">违规行为监控统计</text>
          </view>
        </view>
      </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
// #ifdef APP
import statusBar from "@/uni_modules/uni-nav-bar/components/uni-nav-bar/uni-status-bar";
// #endif

export default {
  // #ifdef APP
  components: {
    statusBar
  },
  // #endif
  data() {
    // 动态计算导航栏高度
    const systemInfo = uni.getSystemInfoSync();
    const statusBarHeight = systemInfo.statusBarHeight || 20; // 默认20px
    const navbarHeight = 44; // navbar高度44px
    // 转换为rpx：1px = 2rpx（在375px设计稿下）
    const statusBarHeightRpx = statusBarHeight * 2;
    const navbarHeightRpx = navbarHeight * 2;
    const totalNavbarHeight = statusBarHeightRpx + navbarHeightRpx;
    
    return {
      contentTop: totalNavbarHeight + 'rpx', // 内容区域距离顶部的距离
      contentBottom: '96rpx', // 内容区域距离底部的距离（默认值，会在 onReady 中更新）
    }
  },
  computed: {
    // 计算内容区域的位置（使用计算属性确保响应式）
    computedContentTop() {
      const systemInfo = uni.getSystemInfoSync();
      const statusBarHeight = systemInfo.statusBarHeight || 20;
      const navbarHeight = 44;
      const statusBarHeightRpx = statusBarHeight * 2;
      const navbarHeightRpx = navbarHeight * 2;
      const totalNavbarHeight = statusBarHeightRpx + navbarHeightRpx;
      return totalNavbarHeight + 'rpx';
    }
  },
  onLoad() {
    // 更新导航栏位置
    this.updateNavbarPosition();
  },
  onShow() {
    // 每次页面显示时都重新计算导航栏位置
    this.$nextTick(() => {
      this.updateNavbarPosition();
      setTimeout(() => {
        this.updateNavbarPosition();
      }, 50);
    });
  },
  onReady() {
    // 页面渲染完成后，查询实际导航栏高度并更新位置
    this.$nextTick(() => {
      // 查询导航栏实际高度
      const query = uni.createSelectorQuery().in(this);
      query.select('.uni-navbar__content').boundingClientRect((data) => {
        if (data && data.height) {
          // 使用实际高度（已经是包含状态栏的总高度）
          const actualHeight = data.height; // 实际导航栏高度（px）
          // 转换为rpx：1px = 2rpx（在375px设计稿下）
          const actualHeightRpx = actualHeight * 2;
          // 稍微减小一点，确保紧贴
          const adjustedHeight = Math.max(actualHeightRpx - 4, 0);
          this.$set(this, 'contentTop', adjustedHeight + 'rpx');
          console.log('[AIAnalysis] 使用实际导航栏高度:', actualHeight, 'px =', actualHeightRpx, 'rpx, 调整后:', adjustedHeight, 'rpx');
        } else {
          // 如果查询失败，使用默认计算并减小一点
          const systemInfo = uni.getSystemInfoSync();
          const statusBarHeight = systemInfo.statusBarHeight || 20;
          const navbarHeight = 44;
          const statusBarHeightRpx = statusBarHeight * 2;
          const navbarHeightRpx = navbarHeight * 2;
          const totalNavbarHeight = statusBarHeightRpx + navbarHeightRpx;
          const adjustedHeight = Math.max(totalNavbarHeight - 4, 0);
          this.$set(this, 'contentTop', adjustedHeight + 'rpx');
        }
      }).exec();
      
      // 查询底部 tabbar 实际高度
      const queryTabbar = uni.createSelectorQuery().in(this);
      queryTabbar.select('.tabbar').boundingClientRect((tabbarData) => {
        if (tabbarData && tabbarData.height) {
          // 使用实际高度
          const tabbarHeight = tabbarData.height; // tabbar 实际高度（px）
          const tabbarHeightRpx = tabbarHeight * 2;
          // 稍微减小一点，确保紧贴
          const adjustedBottom = Math.max(tabbarHeightRpx - 4, 0);
          this.$set(this, 'contentBottom', adjustedBottom + 'rpx');
          console.log('[AIAnalysis] 使用实际tabbar高度:', tabbarHeight, 'px =', tabbarHeightRpx, 'rpx, 调整后:', adjustedBottom, 'rpx');
        } else {
          // 如果查询失败，使用默认值
          this.$set(this, 'contentBottom', '96rpx');
        }
      }).exec();
      
      // 延迟再次更新，确保DOM已渲染
      setTimeout(() => {
        // 再次查询并更新导航栏
        const query2 = uni.createSelectorQuery().in(this);
        query2.select('.uni-navbar__content').boundingClientRect((data) => {
          if (data && data.height) {
            const actualHeight = data.height;
            const actualHeightRpx = actualHeight * 2;
            // 稍微减小一点，确保紧贴
            const adjustedHeight = Math.max(actualHeightRpx - 4, 0);
            this.$set(this, 'contentTop', adjustedHeight + 'rpx');
            console.log('[AIAnalysis] 延迟更新导航栏高度:', actualHeight, 'px =', actualHeightRpx, 'rpx, 调整后:', adjustedHeight, 'rpx');
          }
        }).exec();
        
        // 再次查询 tabbar 高度
        const queryTabbar2 = uni.createSelectorQuery().in(this);
        queryTabbar2.select('.tabbar').boundingClientRect((tabbarData) => {
          if (tabbarData && tabbarData.height) {
            const tabbarHeight = tabbarData.height;
            const tabbarHeightRpx = tabbarHeight * 2;
            const adjustedBottom = Math.max(tabbarHeightRpx - 4, 0);
            this.$set(this, 'contentBottom', adjustedBottom + 'rpx');
            console.log('[AIAnalysis] 延迟更新tabbar高度:', tabbarHeight, 'px =', tabbarHeightRpx, 'rpx, 调整后:', adjustedBottom, 'rpx');
          }
        }).exec();
      }, 200);
    });
  },
  methods: {
    // 计算并更新导航栏和内容区域的位置
    updateNavbarPosition() {
      const systemInfo = uni.getSystemInfoSync();
      const statusBarHeight = systemInfo.statusBarHeight || 20;
      const navbarHeight = 44;
      // 转换为rpx：1px = 2rpx（在375px设计稿下）
      const statusBarHeightRpx = statusBarHeight * 2;
      const navbarHeightRpx = navbarHeight * 2;
      const totalNavbarHeight = statusBarHeightRpx + navbarHeightRpx;
      
      const contentTop = totalNavbarHeight + 'rpx';
      
      this.$set(this, 'contentTop', contentTop);
      
      // #ifdef MP-WEIXIN
      this.$forceUpdate();
      // #endif
    },
    goBack() {
      // 检查页面栈，如果当前是第一个页面，则不能返回
      const pages = getCurrentPages();
      if (pages.length <= 1) {
        console.log('[AIAnalysis][goBack] 当前是第一个页面，无法返回');
        return;
      }
      uni.navigateBack({
        fail: (err) => {
          console.error('[AIAnalysis][goBack] 返回失败:', err);
        }
      });
    },
    navigateToPage(pageName) {
      const pagePath = `pages-subpackage/ai_features/${pageName}/${pageName}`
      uni.navigateTo({
        url: `/${pagePath}`,
        fail: (err) => {
          console.error('导航失败:', err)
          uni.showToast({
            title: '页面跳转失败',
            icon: 'none'
          })
        }
      })
    }
  }
}
</script>

<style scoped>
page {
  background-color: #F5F5F5;
}

.page {
  min-height: 100vh;
  background-color: #F5F5F5;
}

.content {
  padding-top: 0 !important;
  margin-top: 0 !important;
  position: relative;
  min-height: 100vh;
  /* 确保内容从顶部开始，不受占位符影响 */
  top: 0;
}

/* 隐藏导航栏占位区域，避免产生空白 */
::v-deep .uni-navbar__placeholder {
  display: none !important;
  height: 0 !important;
}

::v-deep .uni-navbar__placeholder-view {
  display: none !important;
  height: 0 !important;
}

/* 隐藏底部 tabBar 占位区域，避免产生空白（类似顶部导航栏的处理方式） */
/* 注意：只隐藏占位符，不隐藏实际的 tabbar */
::v-deep [class*="tabbar"][class*="placeholder"] {
  display: none !important;
  height: 0 !important;
}

/* 隐藏可能的页面底部安全区域占位符 */
::v-deep [class*="safe-area"][class*="bottom"],
::v-deep [class*="bottom"][class*="safe"] {
  display: none !important;
  height: 0 !important;
}

.content-scroll {
  position: absolute;
  left: 0;
  right: 0;
  top: 0; /* 先从顶部开始 */
  bottom: 0; /* 先设为0，通过查询实际高度动态设置 */
  /* top值通过内联样式动态设置，覆盖上面的 top: 0 */
  padding: 0 !important;
  padding-bottom: 0 !important;
  margin: 0 !important;
  box-sizing: border-box;
}

/* 确保scroll-view内部没有默认间距 */
.content-scroll > view {
  margin-top: 0;
  padding-top: 0;
  margin-bottom: 0;
  padding-bottom: 0;
}

.category-section {
  margin: 20rpx 30rpx;
  background: #FFFFFF;
  border-radius: 16rpx;
  overflow: hidden;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.04);
}

.category-section:first-child {
  margin-top: 0;
}

.category-section:last-child {
  margin-bottom: 0;
}

.category-header {
  display: flex;
  align-items: center;
  padding: 30rpx;
  background: #FFFFFF;
  border-bottom: 1rpx solid #F0F0F0;
}

.category-icon {
  font-size: 32rpx;
  margin-right: 20rpx;
}

.category-title {
  font-size: 32rpx;
  font-weight: 500;
  color: #333;
}

.function-grid {
  display: flex;
  flex-wrap: wrap;
  padding: 10rpx;
}

/* 两列布局的分类 */
.two-column .function-item {
  flex: 1;
  background: #FFFFFF;
  border-radius: 12rpx;
  padding: 24rpx 16rpx;
  margin: 0 5rpx;
  text-align: center;
  box-shadow: 0 2rpx 4rpx rgba(0, 0, 0, 0.04);
  transition: all 0.3s ease;
  border: 1rpx solid #F0F0F0;
}

/* 三列布局的分类 */
.three-column .function-item {
  width: 31%;
  background: #FFFFFF;
  border-radius: 12rpx;
  padding: 24rpx 12rpx;
  margin: 1%;
  text-align: center;
  box-shadow: 0 2rpx 4rpx rgba(0, 0, 0, 0.04);
  transition: all 0.3s ease;
  border: 1rpx solid #F0F0F0;
}

/* 销售分析：所有项目统一宽度（50%） */
.sales-analysis .function-item.full-row-item {
  flex: 1;
  width: calc(50% - 10rpx);
  background: #FFFFFF;
  border-radius: 12rpx;
  padding: 24rpx 16rpx;
  margin: 0 5rpx;
  text-align: center;
  box-shadow: 0 2rpx 4rpx rgba(0, 0, 0, 0.04);
  transition: all 0.3s ease;
  border: 1rpx solid #F0F0F0;
}

/* 销售分析：开口率单独一行，但保持50%宽度 */
.sales-analysis .function-item.new-line-item {
  flex-basis: 100%;
  width: calc(50% - 10rpx);
  margin-top: 10rpx;
}

.function-item:active {
  transform: scale(0.95);
  box-shadow: 0 1rpx 2rpx rgba(0, 0, 0, 0.1);
}

.function-icon {
  font-size: 48rpx;
  margin-bottom: 12rpx;
}

.function-name {
  display: block;
  font-size: 28rpx;
  font-weight: 500;
  color: #333;
  margin-bottom: 8rpx;
}

.function-desc {
  display: block;
  font-size: 24rpx;
  color: #666;
  line-height: 1.4;
}
</style>
