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

import { httpCompanyDayList, httpDepartDayList } from "/@/api/dashboard/grossMargin";
import { addition, createTotalItem, 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: "pure_day_sale",
    cellRenderer({ row }) {
      return unit2TenThousand(row.pure_day_sale)
    }
  },
  {
    label: "本日毛利润",
    prop: "pure_day_profit",
    cellRenderer({ row }) {
      return unit2TenThousand(row.pure_day_profit)
    }
  },
  {
    label: "本日毛利率",
    prop: "pure_day_interest_rate",
    cellRenderer({ row }) {
      return Number(row.pure_day_interest_rate || 0).toFixed(2) + '%'
    }
  },
  {
    label: "本月销售净额",
    prop: "pure_month_sale",
    cellRenderer({ row }) {
      return unit2TenThousand(row.pure_month_sale)
    }
  },
  {
    label: "本月指标达成率",
    prop: "month_completion_rate",
    cellRenderer({ row }) {
      return Number(row.month_completion_rate || 0).toFixed(2) + '%'
    }
  },
  {
    label: "本月销售指标",
    prop: "total_tips",
    cellRenderer({ row }) {
      return unit2TenThousand(row.total_tips)
    }
  },
  {
    label: "本月毛利润",
    prop: "pure_month_profit",
    cellRenderer({ row }) {
      return unit2TenThousand(row.pure_month_profit)
    }
  },
  {
    label: "本月毛利率",
    prop: "pure_month_interest_rate",
    cellRenderer({ row }) {
      return Number(row.pure_month_interest_rate || 0).toFixed(2) + '%'
    }
  }
];

const contentConfig: ContentConfig = {
  title: "",
  superUserNoAction: false,
  handleData(data = [], parmas = {}) {
    return new Promise(resolve => {
      // const tableData = data.map(tableData => {
      //   /* 本日净销售额 = 日销售额 - 日销售退货 **/
      //   const pure_day_sale = subtraction(tableItem.sale_total, tableItem.th_total)
      //   /* 本日净采购额 = 日采购额 - 日采购退货 **/
      //   const pure_day_purch = subtraction(tableItem.cgd_total, tableItem.cgd_th_total)
      //   /* 本日毛利润 = (本日净销售额 - 本日净采购额) **/
      //   const pure_day_profit = subtraction(pure_day_sale, pure_day_purch)
      //   /* 本日毛利率 = (本月毛利润 / 本月销售净额) * 100**/
      //   const pure_day_interest_rate = multiplication(division(pure_day_profit, pure_day_sale), 100)
      //   /* 本月净销售额 = 月销售额 - 月销售退货额 **/
      //   const pure_month_sale = subtraction(tableItem.msale_total, tableItem.mth_total)
      //   /* 本月净采购额 = 月采购额 - 月采购退货额 **/
      //   const pure_month_purch = subtraction(tableItem.mcgd_total, tableItem.mcgd_th_total)
      //   /* 本月指标达成率 = (本月销售净额 / 本月销售指标) * 100 **/
      //   const month_completion_rate = multiplication(division(pure_month_sale, tableItem.total_tips), 100)
      //   /* 本月毛利润 = (本月销售净额 - 本月采购净额) **/
      //   const pure_month_profit = subtraction(pure_month_sale, pure_month_purch)
      //   /* 本月毛利率 = (本月毛利润 / 本月销售净额) * 100**/
      //   const pure_month_interest_rate = multiplication(division(pure_month_profit, pure_month_sale), 100)
      //   return {
      //     total_tips: tableItem.total_tips,
      //     pure_day_purch,
      //     pure_month_purch,
      //     pure_day_interest_rate,
      //     depart: tableItem.depart,
      //     pure_day_sale,
      //     pure_day_profit,
      //     pure_month_sale,
      //     month_completion_rate,
      //     pure_month_profit,
      //     pure_month_interest_rate
      //   }
      // })

      httpCompanyDayList({ daytime: parmas.daytime || null }).then(result => {
        if (Number(result.code) === 0) {
          // DEMO:1
          // const addItem = result.data.reduce((prev, current) => ({
          //   saleDay: result.data[0].dayinfo.reduce((p, c) => addition(p, subtraction(c.sale_total, c.th_total)), 0),
          //   saleMonth: result.data[0].monthinfo.reduce((p, c) => addition(p, subtraction(c.msale_total, c.mth_total)), 0),
          //   purchDay: result.data[0].dayinfo.reduce((p, c) => addition(p, subtraction(c.cgd_total, c.cgd_th_total)), 0),
          //   purchMonth: result.data[0].monthinfo.reduce((p, c) => addition(p, subtraction(c.mcgd_total, c.mcgd_th_total)), 0),
          //   totalTips: current.total_tips
          // }), {
          //   saleDay: 0,
          //   saleMonth: 0,
          //   purchDay: 0,
          //   purchMonth: 0,
          //   totalTips: 0
          // })
          // tableData[1].total_tips = addition(tableData[1].total_tips, addItem.totalTips)
          // tableData[1].pure_day_sale = addition(tableData[1].pure_day_sale, addItem.saleDay)
          // tableData[1].pure_month_sale = addition(tableData[1].pure_month_sale, addItem.saleMonth)
          // tableData[1].pure_day_purch = addition(tableData[1].pure_day_purch, addItem.purchDay)
          // tableData[1].pure_month_purch = addition(tableData[1].pure_month_purch, addItem.purchMonth)
          // tableData[1].pure_day_profit = subtraction(tableData[1].pure_day_sale, tableData[1].pure_day_purch)
          // tableData[1].pure_month_profit = subtraction(tableData[1].pure_month_sale, tableData[1].pure_month_purch)
          // tableData[1].pure_month_interest_rate = multiplication(division(tableData[1].pure_month_profit, tableData[1].pure_month_sale), 100)
          // tableData[1].pure_day_interest_rate = multiplication(division(tableData[1].pure_day_profit, tableData[1].pure_day_sale), 100)
          // tableData[1].pure_day_interest_rate = multiplication(division(tableData[1].pure_month_profit, tableData[1].pure_month_sale), 100)

          //DEMO:2

          //合计三家公司的自营、渠道、供应商端数据
          const mapCompanySupplierSale = result.data.reduce((prev, current) => ({
            ...prev,
            [current.companyName]: {
              supplierSaleDay: subtraction(current.dayinfo[2].sale_total, current.dayinfo[2].th_total),
              supplierSaleMonth: subtraction(current.monthinfo[2].msale_total, current.monthinfo[2].mth_total),
              supplierPurchDay: subtraction(current.dayinfo[2].cgd_total, current.dayinfo[2].cgd_th_total),
              supplierPurchMonth: subtraction(current.monthinfo[2].mcgd_total, current.monthinfo[2].mcgd_th_total)
            }
          }), {})

          const tableData = data.map(tableItem => {
            const supplierSalekey = Object.keys(mapCompanySupplierSale).find(key => key.includes(tableItem.depart))

            /* 本日净销售额 = 日销售额 - 日销售退货 **/
            let pure_day_sale = subtraction(tableItem.sale_total, tableItem.th_total) as unknown as number
            /* 本日净采购额 = 日采购额 - 日采购退货 **/
            let pure_day_purch = subtraction(tableItem.cgd_total, tableItem.cgd_th_total) as unknown as number

            if (supplierSalekey) { // 百辰、普润、泓源 需要加上供应商端采购的数据
              pure_day_purch = addition(pure_day_purch, mapCompanySupplierSale[supplierSalekey].supplierPurchDay)
              pure_day_sale = addition(pure_day_sale, mapCompanySupplierSale[supplierSalekey].supplierSaleDay)
            }

            if (supplierSalekey === '北京普润心堂商贸有限公司') {  //普润的数据需要再加上锦兴的
              pure_day_purch = addition(pure_day_purch, mapCompanySupplierSale['北京锦兴弘昌科技有限公司'].supplierPurchDay)
              pure_day_sale = addition(pure_day_sale, mapCompanySupplierSale['北京锦兴弘昌科技有限公司'].supplierSaleDay)
            }

            /* 本日毛利润 = (本日净销售额 - 本日净采购额) **/
            const pure_day_profit = subtraction(pure_day_sale, pure_day_purch)
            /* 本日毛利率 = (本月毛利润 / 本月销售净额) * 100**/
            const pure_day_interest_rate = multiplication(division(pure_day_profit, pure_day_sale), 100)

            /* 本月净销售额 = 月销售额 - 月销售退货额 **/
            let pure_month_sale = subtraction(tableItem.msale_total, tableItem.mth_total) as unknown as number
            /* 本月净采购额 = 月采购额 - 月采购退货额 **/
            let pure_month_purch = subtraction(tableItem.mcgd_total, tableItem.mcgd_th_total) as unknown as number

            if (supplierSalekey) { // 百辰、普润、泓源 需要加上供应商端的数据
              pure_month_sale = addition(pure_month_sale, mapCompanySupplierSale[supplierSalekey].supplierSaleMonth)
              pure_month_purch = addition(pure_month_purch, mapCompanySupplierSale[supplierSalekey].supplierPurchMonth)
            }

            if (supplierSalekey === '北京普润心堂商贸有限公司') { //普润的数据需要再加上锦兴的
              pure_month_sale = addition(pure_month_sale, mapCompanySupplierSale['北京锦兴弘昌科技有限公司'].supplierSaleMonth)
              pure_month_purch = addition(pure_month_purch, mapCompanySupplierSale['北京锦兴弘昌科技有限公司'].supplierPurchMonth)
            }

            /* 本月指标达成率 = (本月销售净额 / 本月销售指标) * 100 **/
            const month_completion_rate = multiplication(division(pure_month_sale, tableItem.total_tips), 100)
            /* 本月毛利润 = (本月销售净额 - 本月采购净额) **/
            const pure_month_profit = subtraction(pure_month_sale, pure_month_purch)
            /* 本月毛利率 = (本月毛利润 / 本月销售净额) * 100**/
            const pure_month_interest_rate = multiplication(division(pure_month_profit, pure_month_sale), 100)

            return {
              total_tips: tableItem.total_tips,
              pure_day_purch,
              pure_month_purch,
              pure_day_interest_rate,
              depart: tableItem.depart,
              pure_day_sale,
              pure_day_profit,
              pure_month_sale,
              month_completion_rate,
              pure_month_profit,
              pure_month_interest_rate
            }
          })

          const totalItem = createTotalItem(tableData)
          totalItem.depart = "合计"
          totalItem.month_completion_rate = multiplication(division(totalItem.pure_month_sale, totalItem.total_tips), 100)
          totalItem.pure_month_interest_rate = multiplication(division(totalItem.pure_month_profit, totalItem.pure_month_sale), 100)
          totalItem.pure_day_interest_rate = multiplication(division(totalItem.pure_day_profit, totalItem.pure_day_sale), 100)
          resolve([...tableData, totalItem])
        }
      })
    })
  },
  columns,
  apis: {
    httpList: httpDepartDayList
  }
};

export default contentConfig;
