import { formatToDateTime } from '/@/utils/dateUtil';
import { ref } from 'vue';
import { getTrendData } from '/@/api/dashboard';

export function useDate() {
  const tenantDateValue = ref([]);
  const customerDateValue = ref([]);
  const tenantTrendList = ref([]);
  const customerTrendList = ref([]);
  const activeTenantIndex = ref(0);
  const activeCustomerIndex = ref(0);
  const TenantOrCustomerDateList = ref([
    { label: '30天', value: 2592000000 },
    { label: '最近三个月', value: 7776000000 },
    { label: '最近一年', value: 31536000000 },
  ]);

  // 租户趋势和客户趋势快速选择时间
  async function quickQueryTenantOrCustomerTime(
    index: number,
    value: number,
    flag: 'tenant' | 'customer'
  ) {
    const endTs = Date.now();
    if (flag === 'tenant') {
      if (activeTenantIndex.value === index) return;
      activeTenantIndex.value = index;
      tenantDateValue.value = [];
      const res = await getTrendData({
        startTs: endTs - value,
        endTs,
        interval: value === 2592000000 ? 86400000 : value === 7776000000 ? 172800000 : 2592000000,
        trend: 'TENANT_TREND',
      });
      tenantTrendList.value = res.map((item) => [item.ts, item.value]);
    } else {
      if (activeCustomerIndex.value === index) return;
      activeCustomerIndex.value = index;
      customerDateValue.value = [];
      const res = await getTrendData({
        startTs: endTs - value,
        endTs,
        interval: value === 2592000000 ? 86400000 : value === 7776000000 ? 172800000 : 2592000000,
        trend: 'CUSTOMER_TREND',
      });
      customerTrendList.value = res.map((item) => [item.ts, item.value]);
    }
  }

  // 获取选中的时间范围内的数据
  async function getDateData(startTs, endTs, trend: 'CUSTOMER_TREND' | 'TENANT_TREND', list) {
    // 计算时间间隔
    function computedInterval(startTs: number, endTs: number) {
      /**
       * 选择的时间间隔
       * <=1 2h
       * <=30 1day
       *  >30<90 2day
       *  >90 1month
       */
      let interval = 86400000;
      if (endTs - startTs <= 86400000) {
        interval = 7200000;
      } else if (endTs - startTs <= 2592000000) {
        interval = 86400000;
      } else if (endTs - startTs > 2592000000 && endTs - startTs < 7776000000) {
        interval = 172800000;
      } else if (endTs - startTs > 7776000000) {
        interval = 2592000000;
      }
      return interval;
    }
    startTs = parseInt(formatToDateTime(startTs, 'x')) - 86400000;
    endTs = parseInt(formatToDateTime(endTs, 'x'));
    const res = await getTrendData({
      startTs,
      endTs,
      interval: computedInterval(startTs, endTs),
      trend,
    });
    list.value = res.map((item) => [item.ts, item.value]);
  }

  // 租户选择日期
  function onDateTenantChange(_, dateString) {
    if (!_.length) return;
    const [startTs, endTs] = dateString;
    activeTenantIndex.value = -1;
    getDateData(startTs, endTs, 'TENANT_TREND', tenantTrendList);
  }
  // 客户趋势选择日期
  function onDateCustomerChange(_, dateString) {
    if (!_.length) return;
    const [startTs, endTs] = dateString;
    activeCustomerIndex.value = -1;
    getDateData(startTs, endTs, 'CUSTOMER_TREND', customerTrendList);
  }
  return {
    tenantDateValue,
    customerDateValue,
    tenantTrendList,
    customerTrendList,
    activeTenantIndex,
    activeCustomerIndex,
    TenantOrCustomerDateList,
    quickQueryTenantOrCustomerTime,
    onDateTenantChange,
    onDateCustomerChange,
  };
}
