/* eslint-disable prettier/prettier */
import { ContentConfig } from "/@/components/PageContent";

import { httpCompanyDayList } from "/@/api/dashboard/grossMargin";
import { addition, division, multiplication, subtraction, unit2TenThousand } from "/@/utils/calc";

export const mapReponseType = {
  '1': '自营',
  '2': '渠道',
  '3': '供应商端'
}

export const columns = [
  {
    label: "业绩类型",
    prop: "type",
    cellRenderer({ row }) {
      return mapReponseType[row.type] || '--'
    }
  },
  {
    label: "本日销售额",
    prop: "sale_total",
    cellRenderer({ row }) {
      return unit2TenThousand(row.dayinfo.sale_total)
    }
  },
  {
    label: "本月销售额",
    prop: "month",
    cellRenderer({ row }) {
      return unit2TenThousand(row.monthinfo.msale_total)
    }
  },
  {
    label: "月占比",
    prop: "proportion",
    cellRenderer({ row }) {
      return Number(row.proportion || 0).toFixed(2) + '%'
    }
  }
];

const getDiffDay = function (date) {
  let totalDays, diffDate
  const myDate_1 = Date.parse(date)
  const myDate_2 = Date.parse('2023-04-17 00:00:00')
  // eslint-disable-next-line prefer-const
  diffDate = myDate_1 - myDate_2
  // eslint-disable-next-line prefer-const
  totalDays = Math.floor(diffDate / (1000 * 3600 * 24))
  return totalDays // 相差的天数
}

const contentConfig = (companyNo): ContentConfig => ({
  title: "",
  superUserNoAction: false,
  handleData(data = [], params = {}) {
    const isBeforeDate = params.daytime ? getDiffDay(params.daytime) < 0 && params.companyNo === "GS2302231323386950" : false

    const { monthinfo = [], dayinfo = [], total_tips = 0 } = data[0] || [];

    monthinfo[1] = {
      ...(isBeforeDate ? {
        msale_total: addition(monthinfo[1].msale_total, monthinfo[2].msale_total),
        mth_total: addition(monthinfo[1].mth_total, monthinfo[2].mth_total),
        type: '2'
      } : monthinfo[1])
    }

    monthinfo[0] = {
      ...(isBeforeDate ? {
        msale_total: 0,
        mth_total: 0,
        type: '1'
      } : monthinfo[0])
    }

    monthinfo[2] = {
      ...(isBeforeDate ? {
        msale_total: 0,
        mth_total: 0,
        type: '3'
      } : monthinfo[2])
    }

    const types = Object.keys(mapReponseType)


    const _total = {
      total_tips: Number(total_tips),
      day: dayinfo.reduce((prev, { sale_total = "0" }) => addition(prev, sale_total), 0),
      month: monthinfo.reduce((prev, { msale_total = "0" }) => addition(prev, msale_total), 0),
      monthReturn: monthinfo.reduce((prev, { mth_total = "0" }) => addition(prev, mth_total), 0),
    }

    const rows = types.map(type => {
      const _monthinfo = monthinfo.find(month => String(month.type) === type) || {}
      const _dayinfo = dayinfo.find(day => String(day.type) === type) || {}
      const msale_total = subtraction(_monthinfo.msale_total, _monthinfo.mth_total) || 0

      const total = subtraction(_total.month, _total.monthReturn)

      return {
        type,
        total_tips: _total.total_tips,
        dayinfo: {
          ..._dayinfo,
          /** 日销售额 = 日销售额 - 日退货额 */
          sale_total: subtraction(_dayinfo.sale_total, _dayinfo.th_total) || 0
        },
        monthinfo: {
          ..._monthinfo,
          /** 月销售额 = 月销售额 - 月退货额 */
          msale_total
        },
        /* 月占比 = (当前业绩类型月销售额 / 总月销售额) / 100 **/
        proportion: multiplication(
          division(msale_total, Number(total).toFixed(2)) || 0
          , 100).toFixed(2)
      }
    })
    return rows
  },
  columns,
  apis: {
    httpList: (parmas = {}) => httpCompanyDayList({ ...parmas, companyNo })
  }
});

export default contentConfig;
