<template>
  <uni-popup ref="popup" type="center" :mask-click="true" @change="onPopupChange">
    <view class="quality-coverage-popup">
      <view class="popup-header">
        <text class="popup-title">质检覆盖率统计</text>
        <view class="popup-close" @click="close">
          <text class="close-icon">×</text>
        </view>
      </view>
      <scroll-view class="popup-content" scroll-y="true">
        <!-- 统计卡片 -->
        <view class="popup-stats-card">
          <view class="popup-stat-item">
            <text class="popup-stat-value">{{ coverageRate }}%</text>
            <text class="popup-stat-label">总体覆盖率</text>
          </view>
          <view class="popup-stat-item">
            <text class="popup-stat-value">{{ todayCoverage }}%</text>
            <text class="popup-stat-label">今日覆盖率</text>
          </view>
        </view>
        
        <!-- 图表区域 -->
        <view class="popup-chart-section">
          <text class="popup-section-title">覆盖率趋势图</text>
          <view class="popup-chart-placeholder">
            <text class="popup-placeholder-text">📊 图表区域</text>
            <text class="popup-placeholder-desc">此处将显示质检覆盖率趋势图表</text>
          </view>
        </view>
        
        <!-- 数据列表 -->
        <view class="popup-data-list">
          <text class="popup-section-title">详细数据</text>
          <view class="popup-list-item" v-for="(item, index) in dataList" :key="index">
            <view class="popup-item-left">
              <text class="popup-item-date">{{ item.date }}</text>
              <text class="popup-item-desc">{{ item.description }}</text>
            </view>
            <view class="popup-item-right">
              <text class="popup-item-rate">{{ item.rate }}%</text>
            </view>
          </view>
        </view>
      </scroll-view>
    </view>
  </uni-popup>
</template>

<script>
export default {
  name: 'QualityCoveragePopup',
  props: {
    openTrigger: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      coverageRate: '87.5',
      todayCoverage: '92.3',
      dataList: [
        { date: '2024-02-07', description: '今日质检覆盖率', rate: '92.3' },
        { date: '2024-02-06', description: '昨日质检覆盖率', rate: '89.1' },
        { date: '2024-02-05', description: '前日质检覆盖率', rate: '85.7' },
        { date: '2024-02-04', description: '工作日平均覆盖率', rate: '88.4' },
        { date: '2024-02-03', description: '周末覆盖率', rate: '91.2' }
      ]
    }
  },
  watch: {
    openTrigger(newVal) {
      // 当触发器变化时，自动打开弹窗
      if (newVal > 0) {
        this.$nextTick(() => {
          this.open();
        });
      }
    }
  },
  methods: {
    open() {
      // 安全地访问 refs，如果组件还未准备好则重试
      if (this.$refs && this.$refs.popup) {
        this.$refs.popup.open();
      } else {
        // 如果 refs 还未准备好，延迟重试
        setTimeout(() => {
          if (this.$refs && this.$refs.popup) {
            this.$refs.popup.open();
          }
        }, 100);
      }
    },
    close() {
      if (this.$refs && this.$refs.popup) {
        this.$refs.popup.close();
      }
    },
    onPopupChange(e) {
      if (!e.show) {
        this.$emit('close')
      }
    }
  }
}
</script>

<style scoped>
.quality-coverage-popup {
  width: 680rpx;
  max-height: 80vh;
  background-color: #FFFFFF;
  border-radius: 16rpx;
  display: flex;
  flex-direction: column;
  box-shadow: 0 8rpx 32rpx rgba(0, 0, 0, 0.2);
  overflow: hidden;
  position: relative;
}

.popup-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6rpx 32rpx;
  border-bottom: 1rpx solid #E0E0E0;
  flex-shrink: 0;
  background-color: #FFFFFF;
  box-sizing: border-box;
}

.popup-title {
  font-size: 24rpx;
  font-weight: 500;
  color: #333;
}

.popup-close {
  width: 40rpx;
  height: 40rpx;
  min-width: 40rpx;
  min-height: 40rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
  background-color: rgba(0, 0, 0, 0.05);
  border-radius: 50%;
  transition: background-color 0.2s;
}

.popup-close:active {
  background-color: rgba(0, 0, 0, 0.1);
}

.close-icon {
  font-size: 32rpx;
  color: #333;
  line-height: 1;
  display: block;
}

.popup-content {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  max-height: calc(80vh - 52rpx);
}

.popup-stats-card {
  display: flex;
  margin: 20rpx 30rpx;
  background: #f8f9fa;
  border-radius: 16rpx;
  padding: 40rpx 30rpx;
}

.popup-stat-item {
  flex: 1;
  text-align: center;
}

.popup-stat-item:not(:last-child) {
  border-right: 1rpx solid #e9ecef;
}

.popup-stat-value {
  display: block;
  font-size: 48rpx;
  font-weight: bold;
  color: #007aff;
  margin-bottom: 10rpx;
}

.popup-stat-label {
  display: block;
  font-size: 24rpx;
  color: #666666;
}

.popup-chart-section,
.popup-data-list {
  margin: 20rpx 30rpx;
  background: #f8f9fa;
  border-radius: 16rpx;
  overflow: hidden;
}

.popup-section-title {
  display: block;
  font-size: 32rpx;
  font-weight: bold;
  color: #333333;
  padding: 30rpx;
  border-bottom: 1rpx solid #e9ecef;
  background: #ffffff;
}

.popup-chart-placeholder {
  padding: 80rpx 30rpx;
  text-align: center;
  background: #ffffff;
}

.popup-placeholder-text {
  display: block;
  font-size: 48rpx;
  margin-bottom: 20rpx;
}

.popup-placeholder-desc {
  display: block;
  font-size: 28rpx;
  color: #666666;
}

.popup-list-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 30rpx;
  border-bottom: 1rpx solid #f0f0f0;
  background: #ffffff;
}

.popup-list-item:last-child {
  border-bottom: none;
}

.popup-item-left {
  flex: 1;
}

.popup-item-date {
  display: block;
  font-size: 28rpx;
  font-weight: bold;
  color: #333333;
  margin-bottom: 8rpx;
}

.popup-item-desc {
  display: block;
  font-size: 24rpx;
  color: #666666;
}

.popup-item-right {
  text-align: right;
}

.popup-item-rate {
  display: block;
  font-size: 28rpx;
  font-weight: bold;
  color: #007aff;
}
</style>

