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

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

const columns = [
  {
    label: "月份",
    prop: "month"
  },
  {
    label: "销售净额",
    prop: "pure_sale",
    cellRenderer({ row }) {
      return unit2TenThousand(row.pure_sale)
    },
  },
  {
    label: "销售指标",
    prop: "total_tips",
    cellRenderer({ row }) {
      return unit2TenThousand(row.total_tips)
    },
  },
  {
    label: "指标达成率",
    prop: "completion_rate",
    cellRenderer({ row }) {
      return Number(row.completion_rate || 0).toFixed(2) + '%'
    }
  },
  {
    label: "毛利润",
    prop: "pure_profit",
    cellRenderer({ row }) {
      return unit2TenThousand(row.pure_profit)
    },
  },
  {
    label: "毛利率",
    prop: "pure_interest_rate",
    cellRenderer({ row }) {
      return Number(row.pure_interest_rate || 0).toFixed(2) + '%'
    },
  }
];


export function createCompanyMonthGrossMarginConfig(companyNo): ContentConfig {
  return {
    title: "",
    superUserNoAction: false,
    handleData(data = []) {
      const tableData = data.map(tableItem => {
        /* 销售净额 = 销售额 - 退货额 **/
        const pure_sale = subtraction(tableItem.sale_total, tableItem.th_total)
        /* 指标达成率 = 销售净额 / 销售指标 **/
        const completion_rate = multiplication(division(pure_sale, tableItem.total_tips), 100)

        /* 采购净额 = 采购额 - 采购退货额 **/
        const pure_purch = subtraction(tableItem.cgd_total, tableItem.cgd_th_total)
        /* 毛利润 = (本月销售净额 - 本月采购净额) **/
        const pure_profit = subtraction(pure_sale, pure_purch)
        /*  毛利率 = (本月毛利润 / 本月销售净额) * 100 **/
        const pure_interest_rate = multiplication(division(pure_profit, pure_sale) || 0, 100)

        return {
          month: tableItem.month,
          completion_rate,
          pure_interest_rate,
          pure_profit,
          pure_sale,
          pure_purch,
          total_tips: tableItem.total_tips
        }
      })

      const totalItem = createTotalItem(tableData)
      totalItem.month = "年度合计"
      /* 年度指标达成率 = (年度销售净额 / 年度销售指标) **/
      totalItem.completion_rate = multiplication(division(totalItem.pure_sale, totalItem.total_tips), 100)
      /* 年度毛利润 = 年度销售额 - 年度采购额 **/
      totalItem.pure_profit = subtraction(totalItem.pure_sale, totalItem.pure_purch)
      /* 年度毛利率 = (年度毛利润 / 年度销售额) **/
      totalItem.pure_interest_rate = multiplication(division(totalItem.pure_profit, totalItem.pure_sale), 100)

      return [...tableData, totalItem]
    },
    columns,
    apis: {
      httpList: () => httpCompanyMonthList({ companyNo })
    }
  }
}

// const contentConfig: ContentConfig = {
//   title: "",
//   superUserNoAction: false,
//   handleData(data = []) {
//     const tableData = data.map(tableItem => {
//       /* 销售净额 = 销售额 - 退货额 **/
//       const pure_sale = subtraction(tableItem.sale_total, tableItem.th_total)
//       /* 指标达成率 = 销售净额 / 销售指标 **/
//       const completion_rate = multiplication(division(pure_sale, tableItem.total_tips), 100)

//       /* 采购净额 = 采购额 - 采购退货额 **/
//       const pure_purch = subtraction(tableItem.cgd_total, tableItem.cgd_th_total)
//       /* 毛利润 = (本月销售净额 - 本月采购净额) **/
//       const pure_profit = subtraction(pure_sale, pure_purch)
//       /*  毛利率 = (本月毛利润 / 本月销售净额) * 100 **/
//       const pure_interest_rate = multiplication(division(pure_profit, pure_sale) || 0, 100)

//       return {
//         month: tableItem.month,
//         completion_rate,
//         pure_interest_rate,
//         pure_profit,
//         pure_sale,
//         pure_purch,
//         total_tips: tableItem.total_tips
//       }
//     })

//     const totalItem = createTotalItem(tableData)
//     totalItem.month = "年度合计"
//     /* 年度指标达成率 = (年度销售净额 / 年度销售指标) **/
//     totalItem.completion_rate = multiplication(division(totalItem.pure_sale, totalItem.total_tips), 100)
//     /* 年度毛利润 = 年度销售额 - 年度采购额 **/
//     totalItem.pure_profit = subtraction(totalItem.pure_sale, totalItem.pure_purch)
//     /* 年度毛利率 = (年度毛利润 / 年度销售额) **/
//     totalItem.pure_interest_rate = multiplication(division(totalItem.pure_profit, totalItem.pure_sale), 100)

//     return [...tableData, totalItem]
//   },
//   columns,
//   apis: {
//     httpList: httpDepartMonthList
//   }
// };

// export default contentConfig;
