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

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

const mapCompanyName = {
  '泓源': '泓源广诚',
  '百辰': '百辰荣达',
  '普润': '普润心堂&锦兴弘昌'
}

const columns = [
  {
    label: "部门",
    prop: "depart",
    cellRenderer({ row }) {
      const companyName = mapCompanyName[row.depart]
      return companyName ? companyName : row.depart
    }
  },
  {
    label: "本日销售额",
    prop: "sale_total",
    cellRenderer({ row }) {
      return unit2TenThousand(row.dayinfo.sale_total)
    }
  },
  {
    label: "本月金额",
    prop: "total_tips",
    align: 'center',
    children: [
      {
        label: "销售指标",
        prop: "total_tips",
        cellRenderer({ row }) {
          return unit2TenThousand(row.total_tips)
        }
      },
      {
        label: "净销售",
        prop: "monthNetSales",
        cellRenderer({ row }) {
          return unit2TenThousand(row.monthinfo.monthNetSales)
        }
      },
      {
        label: "净销售完成率",
        prop: "pure_month_sale",
        cellRenderer({ row }) {
          return row.monthinfo.monthProportion + '%'
        }
      }
    ]
  }
];

const contentConfig: ContentConfig = {
  title: "",
  superUserNoAction: false,
  handleData(data = []) {
    const tableData = data.map(({
      depart,
      msale_total,
      mth_total,
      sale_total,
      th_total,
      total_tips
    }) => {
      return {
        depart,
        total_tips,
        dayinfo: {
          sale_total,
          th_total
        },
        monthinfo: {
          msale_total,
          mth_total
        }
      }
    });

    const total = tableData.reduce((prev, current) => {
      const { total_tips = 0, day = 0, month = 0 } = current;
      return {
        total_tips: addition(total_tips, prev.total_tips),
        month: addition(month, prev.month),
        day: addition(day, prev.day),
      }
    }, {
      total_tips: 0,
      month: 0,
      day: 0
    })

    return tableData.map(({ depart, total_tips, dayinfo, monthinfo }) => {
      /* 月净销售 = 月销售 - 月退货 **/
      const monthNetSales = subtraction(monthinfo.msale_total, monthinfo.mth_total)
      /* 月经销售完成率 = 月净销售 / 销售指标 **/
      const monthProportion = multiplication(division(monthNetSales, total_tips), 100).toFixed(2)
      return {
        depart,
        total_tips,
        dayinfo: {
          ...dayinfo,
          /** 日销售额 = 日销售额 - 日退货额 */
          sale_total: subtraction(dayinfo.sale_total, dayinfo.th_total)
        },
        monthinfo: {
          monthNetSales,
          monthProportion: monthProportion
        },
        /* 占比 =  (当前部门销售额 / 各部门总销售额) / 100 **/
        proportion: multiplication(
          division(monthinfo.msale_total, total.month) || 0
          , 100) || 0
      }
    })
  },
  columns,
  apis: {
    httpList: (params) => httpDepartDayList({ ...params, companyNo: "GS2203161855277894" })
  }
};

export default contentConfig;
