<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%;">
          <!-- 管理费用模块 -->
          <div class="dashboard">
            <div class="title">
              管理费用·万元
            </div>
            <div class="chart-wrap">
              <operatingSingleBar :detailData="manageCostData"></operatingSingleBar>
            </div>
          </div>
          <!-- 销售费用模块 -->
          <div class="dashboard">
            <div class="title">
              销售费用·万元
            </div>
            <div class="chart-wrap">
              <operatingSingleBar :detailData="saleCostData"></operatingSingleBar>
            </div>
          </div>
          <!-- 财务费用模块 -->
          <div class="dashboard">
            <div class="title">
              财务费用·万元
            </div>
            <div class="chart-wrap">
              <operatingSingleBar :detailData="financeCostData"></operatingSingleBar>
            </div>
          </div>
        </div>
      </div>
    </Container>
  </div>
</template>

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

export default {
  name: 'CostAnalysis', // 语义化组件名
  components: { Container, operatingSingleBar },
  props: {
    // 费用数据：数组包含管理/销售/财务费用三个对象
    monthAnalysis: {
      type: Array,
      default: () => [
        { title: "管理费用", budget: 0, real: 0, rate: 0, diff: 0 },
        { 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 {
      // 初始化费用数据（包含flag字段）
      manageCostData: { title: "管理费用", budget: 0, real: 0, rate: 0, diff: 0, flag: 0 },
      saleCostData: { title: "销售费用", budget: 0, real: 0, rate: 0, diff: 0, flag: 0 },
      financeCostData: { title: "财务费用", budget: 0, real: 0, rate: 0, diff: 0, flag: 0 }
    }
  },
  watch: {
    monthAnalysis: {
      handler(newVal) {
        this.updateCostData(newVal)
      },
      deep: true,
      immediate: true // 初始化立即执行
    }
  },
  mounted() {
    this.updateCostData(this.monthAnalysis)
  },
  methods: {
    // 达标标识判断（≥100返回1，<100返回0）
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;
},

    // 处理费用数据
    updateCostData(data) {
      // 数据兜底：确保是数组且长度≥3
      const validData = Array.isArray(data) && data.length >= 3
        ? data
        : this.$props.monthAnalysis;

      // 提取三个费用项数据（兜底处理）
      const manageItem = validData[0] || { title: "管理费用", budget: 0, real: 0, rate: 0, diff: 0 };
      const saleItem = validData[1] || { title: "销售费用", budget: 0, real: 0, rate: 0, diff: 0 };
      const financeItem = validData[2] || { title: "财务费用", budget: 0, real: 0, rate: 0, diff: 0 };

      // 整合flag字段
      this.manageCostData = {
        ...manageItem,
        flag: this.getRateFlag(manageItem.rate, manageItem.real, manageItem.budget)
      };
      this.saleCostData = {
        ...saleItem,
        flag: this.getRateFlag(saleItem.rate, saleItem.real, saleItem.budget)
      };
      this.financeCostData = {
        ...financeItem,
        flag: this.getRateFlag(financeItem.rate, financeItem.real, financeItem.budget)
      };
      // 调试日志
      console.log('管理费用数据：', this.manageCostData);
      console.log('销售费用数据：', this.saleCostData);
      console.log('财务费用数据：', this.financeCostData);
    }
  }
}
</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: 240px; // 最小宽度，防止挤压
  max-width: 300px; // 最大宽度，限制过宽
  height: 205px;
  background: #F9FCFF;
  padding: 16px 0 0 10px;
  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;
    z-index: 1000;
  }
}
</style>
