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

import { addition, division } from "/@/utils/calc";
import { createTooltip } from "/@/utils/tootip";
import apis from "./apis";

function unit2TenThousand(value) {
  if (value === "NaN" || Number(value) === 0) return "0";
  if ((value < 1000 && value > 0) || (value < 0 && value > -1000)) return value;
  return division(value, 10000).toFixed(2) + "W";
}

const columns = [
  {
    label: "业务公司",
    prop: "depart",
    cellRenderer({ row }) {
      return row.depart === '总计' ? row.depart : row.depart.split("@")[1]
    }
  },
  {
    label: "应收账款",
    prop: "wpay_fee",
    cellRenderer({ row }) {
      return Number(row.wpay_fee).toFixed(2)
    }
  },
  {
    label: "已开票",
    prop: "ainv_fee",
    cellRenderer({ row }) {
      return Number(row.ainv_fee).toFixed(2)
    }
  },
  {
    label: "已回款",
    prop: "apay_fee",
    cellRenderer({ row }) {
      return Number(row.apay_fee).toFixed(2)
    }
  },
  {
    label: "已开票-已回款",
    prop: "ainv_apay_fee",
    cellRenderer({ row }) {
      return Number(row.ainv_apay_fee).toFixed(2)
    }
  },
  {
    label: "未开票",
    prop: "winv_fee",
    cellRenderer({ row }) {
      return Number(row.winv_fee).toFixed(2)
    }
  },
  {
    label: "待开票",
    prop: "inv_fee",
    cellRenderer({ row }) {
      return Number(row.inv_fee).toFixed(2)
    }
  },
  {
    label: "已发货未开票",
    prop: "winv_asend_fee",
    cellRenderer({ row }) {
      return Number(row.winv_asend_fee).toFixed(2)
    }
  },
  {
    label: "未发货",
    prop: "wpay_send_fee",
    cellRenderer({ row }) {
      return Number(row.wpay_send_fee).toFixed(2)
    }

  },
  {
    label: "超60天已发货未开票",
    prop: "winv_two_month",
    cellRenderer({ row }) {
      return Number(row.winv_two_month).toFixed(2)
    }
  },
  {
    label: "超180天应收账款",
    prop: "wpay_six_month",
    cellRenderer({ row }) {
      return Number(row.wpay_six_month).toFixed(2)
    }
  }
];

const contentConfig: ContentConfig = {
  columns,
  responseCode: 0,
  root: true,
  handleData(data = []) {
    return new Promise(resolve => {
      const totalItem = data.reduce((prev, current) => Object.keys(current).reduce((prev, currentKey) => ({
        ...prev,
        [currentKey]: currentKey !== 'depart' ? addition(current[currentKey] || 0, prev[currentKey] || 0) : '总计'
      }), prev), {})

      resolve([totalItem, ...data])
    })
  },
  tableRowClassName({ rowIndex }) {
    if (rowIndex === 0) {
      return "warning-row"
    }
  },
  superUserNoAction: false,
  apis: { httpList: apis.list }
};

export default contentConfig;
