import dayjs from "dayjs";

export const kpiMixins = {
  data() {
    return {
      isMobile: false,
      isZd: '',
      current:'2024-10',
      supplierId:'',
      supplierName:'',
      startMonthTime: '',
      startTime: '',
      endTime: '',
    }
  },
  methods: {
    applicateHandle() {
      if (window.parent) {
        const hasListener = window.parent.dispatchEvent(new Event('checkCloseDialog'));
        if (hasListener) {
          const data = {
            action: 'closeDialog',
            message: this.supplierId,
            // 其他需要传递的参数
          };
          window.parent.postMessage(data, '*');
        } else {
          window.history.back();
        }
      }
    },
    toOnlineHours(minutes) {
      let _hours = parseInt(minutes / 60);
      let _minutes = parseInt(minutes % 60);
      return _hours + '时' + _minutes + '分'
    },
    // 初始化获取当月日期
    initDate() {
      const today = dayjs(); // 获取当前日期
      const currentDay = today.date(); // 获取今天是几号（1-31）
      let targetMonth = today; // 默认目标月份是当前月
      if (currentDay === 1) {
        targetMonth = today.subtract(1, 'month'); // 上个月
      }
      this.current = targetMonth.format('YYYY-MM');
      this.startTime = targetMonth.startOf('month').format('YYYY-MM-DD HH:mm:ss');
      let endTime;
      if (targetMonth.isSame(today, 'month')) {
        endTime = today.subtract(1, 'day').endOf('day');
      } else {
        endTime = targetMonth.endOf('month');
      }
      this.endTime = endTime.format('YYYY-MM-DD HH:mm:ss');
      this.startMonthTime=this.getStartTimeFromEndTime(this.endTime)
    },
    //获取近四个月的开始时间
    getStartTimeFromEndTime(endTimeStr) {
      const startTime = dayjs(endTimeStr).subtract(3, 'month').startOf('month');
      return startTime.format('YYYY-MM-DD HH:mm:ss');
    },
    padZero(num) {
      return num < 10 ? `0${num}` : num;
    },
    checkMobile() {
      const userAgent = navigator.userAgent || navigator.vendor || window.opera;
      this.isMobile = /android|webos|iphone|ipad|ipod|blackberry|iemobile|opera mini/i.test(userAgent);
    },
    // 通用函数，用于处理百分比数据
    processPercentage(value) {
      value *= 100;
      if (value % 1 !== 0) {
        value = value.toFixed(2);
      }
      return value;
    },
    formatPercentage(value) {
      let result = value * 100;
      if (Number.isInteger(result)) {
        return result.toString() + '%';
      } else {
        return result.toFixed(2) + '%';
      }
    },
    // 格式化承接案件量数据
    formatCurrency(value) {
      if (!value) return '';
      let num = parseInt(value);
      return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
    },
    formatCurrency1(value) {
      if (!value) return ''; // 如果值为空，返回空字符串
      // 如果值已经包含逗号，直接返回原值
      if (value.toString().includes(',')) {
        return value;
      }
      // 否则，添加千分号
      let num = parseInt(value);
      if (isNaN(num)) return ''; // 如果转换失败，返回空字符串
      return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
    },
  }
}
