<template>
  <div style="flex: 1">
    <Container :name="title" icon="cockpitItemIcon" size="operatingRevenueBg" topSize="middle">
      <div class="kpi-content" style="padding: 14px 16px; display: flex; width: 100%;">
        <div class="topItem-container" style="display: flex; gap: 8px; width: 100%;">
          <!-- 原片产量模块（传递整合了flag的yieldData） -->
          <div class="dashboard left">
            <div class="title">
              原片产量·吨
            </div>
            <div class="chart-wrap">
              <operatingSingleBar :detailData="yieldData"></operatingSingleBar>
            </div>
          </div>
          <!-- 拉引量模块（传递整合了flag的pullData） -->
          <div class="dashboard right">
            <div class="title">
              拉引量·吨
            </div>
            <div class="chart-wrap">
              <operatingSingleBar :detailData="pullData"></operatingSingleBar>
            </div>
          </div>
        </div>
      </div>
    </Container>
  </div>
</template>

<script>
import Container from './container.vue'
import operatingSingleBar from './operatingSingleBar.vue'

export default {
  name: 'ProductionDataAnalysis', // 语义化组件名
  components: { Container, operatingSingleBar },
  props: {
    // 产量/拉引量数据源（结构与原monthAnalysis一致）
    ytdAnalysis: {
      type: Array,
      // 默认值：原片产量、拉引量两个对象
      default: () => [
        { title: "原片产量", budget: 0, real: 0, rate: 0, diff: 0 },
        { title: "拉引量", budget: 0, real: 0, rate: 0, diff: 0 }
      ]
    },
    title: {
      type: String,
      default: '生产数据分析' // 默认标题
    },
    month: {
      type: String,
      default: ''
    },
  },
  data() {
    return {
      chart: null,
      // 初始化生产数据（包含flag字段）
      yieldData: { title: "原片产量", budget: 0, real: 0, rate: 0, diff: 0, flag: 0 },
      pullData: { title: "拉引量", budget: 0, real: 0, rate: 0, diff: 0, flag: 0 }
    }
  },
  watch: {
    // 监听生产数据源变化
    ytdAnalysis: {
      handler(newVal) {
        this.updateProductionData(newVal)
      },
      deep: true,
      immediate: true // 初始化立即执行
    }
  },
  mounted() {
    this.updateProductionData(this.ytdAnalysis)
  },
  methods: {
    // 达标标识判断（≥100返回1，<100返回0）
    getRateFlag(rate) {
      if (isNaN(rate) || rate === null || rate === undefined) return 0;
      return +(rate >= 100 || rate === 0); // + 号将布尔值转为数字（true→1，false→0）
    },

    // 处理生产数据
    updateProductionData(data) {
      // 数据兜底：确保是数组且长度≥2
      const validData = Array.isArray(data) && data.length >= 2
        ? data
        : this.$props.ytdAnalysis;

      // 提取原片产量（第0项）、拉引量（第1项）数据
      const yieldItem = validData[0] || { title: "原片产量", budget: 0, real: 0, rate: 0, diff: 0 };
      const pullItem = validData[1] || { title: "拉引量", budget: 0, real: 0, rate: 0, diff: 0 };

      // 整合flag字段到数据对象中
      this.yieldData = {
        ...yieldItem,
        flag: this.getRateFlag(yieldItem.rate)
      };
      this.pullData = {
        ...pullItem,
        flag: this.getRateFlag(pullItem.rate)
      };

      // 调试日志
      console.log('原片产量数据：', this.yieldData);
      console.log('拉引量数据：', this.pullData);
    }
  }
}
</script>

<style lang='scss' scoped>
.scroll-container {
  max-height: 210px;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 10px 0;

  &::-webkit-scrollbar {
    display: none;
  }

  scrollbar-width: none;
  -ms-overflow-style: none;
}

.topItem-container {
  display: flex;
  justify-content: space-between; // 双列均匀分布
}

.dashboard {
  flex: 1; // 双列等分宽度
  min-width: 300px; // 最小宽度限制
  height: 205px;
  background: #F9FCFF;
  padding: 16px 16px 0;
  margin: 0 4px;

  .title {
    height: 18px;
    font-family: PingFangSC, PingFang SC;
    font-weight: 400;
    font-size: 18px;
    color: #000000;
    line-height: 18px;
    letter-spacing: 2px;
    text-align: left;
    margin-bottom: 12px;
  }

  // 图表容器：适配高度
  .chart-wrap {
    width: 100%;
    height: calc(100% - 30px);
  }

  .number {
    display: flex;
    align-items: center;
    gap: 30px;
    height: 32px;
    font-family: YouSheBiaoTiHei;
    font-size: 32px;
    color: #0B58FF;
    line-height: 32px;
    letter-spacing: 2px;
    text-align: left;
  }

  .mom {
    width: 97px;
    height: 18px;
    font-family: PingFangSC, PingFang SC;
    font-weight: 400;
    font-size: 18px;
    color: #000000;
    line-height: 18px;
    letter-spacing: 1px;
    text-align: left;
  }
}

// 左右列间距优化
.dashboard.left {
  margin-left: 0;
}

.dashboard.right {
  margin-right: 0;
}
</style>
