<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;">
          <div class="dashboard">
            <div class="title">
              {{ month }}月完成率
            </div>
            <div class="number">
              <div class="yield">
                {{ formatRate(factoryData?.completeRate) }}%
              </div>
              <div class="mom">
                环比{{ formatRate(factoryData?.thb) }}%
                <img v-if="factoryData?.thb >= 0" class="arrow" src="../../../assets/img/topArrow.png" alt="上升">
                <img v-else class="arrow" src="../../../assets/img/downArrow.png" alt="下降">
              </div>
            </div>
            <div class="electricityGauge">
              <!-- 传递包含flag的factoryData给仪表盘组件 -->
              <electricityGauge id="month" :detailData="factoryData"></electricityGauge>
            </div>
          </div>
          <div class="line" style="padding: 0px;">
            <!-- 传递包含flag的factoryData给柱状图组件 -->
            <verticalBarChart :detailData="factoryData"></verticalBarChart>
          </div>
        </div>
      </div>
    </Container>
  </div>
</template>

<script>
import Container from './container.vue'
import electricityGauge from './electricityGauge.vue'
import verticalBarChart from './verticalBarChart.vue'
// 引入箭头图片（根据实际路径调整，若模板中直接用路径可注释）

export default {
  name: 'ProductionStatus',
  components: { Container, electricityGauge, verticalBarChart },
  props: {
    monData: {
      type: Object,
      default: () => ({})
    },
    title: {
      type: String,
      default: ''
    },
    month: {
      type: String,
      default: ''
    },
  },
  data() {
    return {
      chart: null,
    }
  },
  computed: {
    /**
     * 自动提取monData中的工厂数据，并新增flag字段
     */
    factoryData() {
      const factoryKeys = Object.keys(this.monData);
      if (factoryKeys.length === 0) {
        // 无数据时返回兜底对象（包含flag）
        return {
          completeRate: 0,
          diff: 0,
          real: 0,
          target: 0,
          thb: 0,
          flag: 0 // 兜底flag
        };
      }
      const firstKey = factoryKeys[0];
      const rawData = this.monData[firstKey];
      // 整合原始数据 + 计算flag
      return {
        completeRate: 0,
        diff: 0,
        real: 0,
        target: 0,
        thb: 0,
        ...rawData,
        flag: this.getRateFlag(rawData.completeRate, rawData.real, rawData.target) // 新增flag字段
      };
    }
  },
  methods: {
    /**
     * 格式化百分比数值：处理空值/非数字，兜底为0
     */
    formatRate(value) {
      if (isNaN(value) || value === null || value === undefined) {
        return 0;
      }
      return value;
    },
    /**
     * 判断完成率对应的flag值（<100为0，≥100为1）
     * @param {number} rate 完成率（原始值，如89代表89%）
     * @returns {0|1} flag值
     */
getRateFlag(rate, real, target) {
  if (isNaN(rate) || rate === null || rate === undefined) return 0;

  // 1. 完成率 >= 100 => 达标
  if (rate >= 100) return 1;

  // 2. 完成率 = 0 且 (目标值=0 或 实际值=目标值=0) => 达标
  if (rate === 0 && target === 0) return 1;

  // 其他情况 => 未达标
  return 0;
},
  }
}
</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;
}

.dashboard {
  width: 264px;
  height: 205px;
  background: #F9FCFF;
  padding: 16px 0 0 10px;
  .title {
    height: 18px;
    font-family: PingFangSC, PingFang SC;
    font-weight: 400;
    font-size: 18px;
    color: #000000;
    line-height: 18px;
    letter-spacing: 2px;
  }

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

  .mom {
    width: fit-content; // 自适应宽度，避免文字溢出
    height: 18px;
    font-family: PingFangSC, PingFang SC;
    font-weight: 400;
    font-size: 18px;
    color: #000000;
    line-height: 18px;
    letter-spacing: 1px;
    display: flex;
    align-items: center; // 箭头和文字垂直居中
    gap: 4px; // 文字和箭头间距
    z-index: 1000;
  }

  // 箭头样式优化
  .arrow {
    width: 16px;
    height: 16px;
    object-fit: contain;
  }
}

.line {
  width: 500px;
  height: 205px;
  background: #F9FCFF;
}
</style>
