/* 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",
    controller: true,
    cellRenderer({ row }) {
      return unit2TenThousand(row.wpay_fee)
    }
  },
  {
    label: "已开票",
    prop: "ainv_fee",
    controller: true,
    cellRenderer({ row }) {
      return unit2TenThousand(row.ainv_fee)
    }
  },
  {
    label: "已回款",
    prop: "apay_fee",
    controller: true,
    cellRenderer({ row }) {
      return unit2TenThousand(row.apay_fee)
    }
  },
  {
    label: "已开票-已回款",
    prop: "ainv_apay_fee",
    controller: true,
    cellRenderer({ row }) {
      return unit2TenThousand(row.ainv_apay_fee)
    }
  },
  // {
  //   label: "已开票未回款",
  //   prop: "wpay_ainv_fee",
  //   cellRenderer({ row }) {
  //     return createTooltip(unit2TenThousand(row.wpay_ainv_fee), row.wpay_ainv_fee, 70)
  //   }
  // },
  {
    label: "未开票",
    prop: "winv_fee",
    controller: true,
    cellRenderer({ row }) {
      return unit2TenThousand(row.winv_fee)
    }
  },
  {
    label: "待开票",
    prop: "inv_fee",
    controller: true,
    cellRenderer({ row }) {
      return unit2TenThousand(row.inv_fee)
    }
  },
  {
    label: "已发货未开票",
    prop: "winv_asend_fee",
    controller: true,
    cellRenderer({ row }) {
      return unit2TenThousand(row.winv_asend_fee)
    }
  },
  {
    label: "未发货",
    prop: "wpay_send_fee",
    controller: true,
    cellRenderer({ row }) {
      return unit2TenThousand(row.wpay_send_fee)
    }

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

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;
