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

import { httpCompanyDayList, 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: 'type',
  },
  {
    label: "日净销售额",
    prop: "day",
    cellRenderer({ row }) {
      return unit2TenThousand(row.day)
    }
  },
  {
    label: "月净销售额",
    prop: "month",
    cellRenderer({ row }) {
      return unit2TenThousand(row.month)
    }
  },
  {
    label: "本月毛利率",
    prop: "proportion",
    cellRenderer({ row }) {
      return Number(row.proportion || 0).toFixed(2) + '%'
    }
  }
];

const contentConfig: ContentConfig = {
  title: "",
  superUserNoAction: false,
  handleData(data = []) {
    return data.reduce((prev, current) => {
      const {
        mzy_sale_total,
        zy_sale_total,
        mchannel_sale_total,
        channel_sale_total
      } = current

      /*  月合计 = 月渠道 + 月直营 **/
      const monthTotal = addition(mchannel_sale_total, mzy_sale_total) || 0
      //console.log(mzy_sale_total,monthTotal)
      /* 直营月销售占比 =  月直营销售额 / 月合计销售额 **/
      const proportion_z = multiplication((division(mzy_sale_total, monthTotal) || 0), 100).toFixed(2)

      /* 渠道月销售占比 =  1 - 直营月销售占比 **/
      let proportion_q;
      if (Number(proportion_z) === 0) {
        const mod = multiplication((division(mchannel_sale_total, monthTotal) || 0), 100)
        proportion_q = mod !== 0 ? Number(100 - Number(proportion_z)).toFixed(2) : 0
      } else {
        proportion_q = Number(100 - Number(proportion_z)).toFixed(2)
      }


      return ([...prev, ...([{
        depart: current.depart,
        type: '直营',
        month: mzy_sale_total,
        day: zy_sale_total,
        proportion: proportion_z
      }, {
        depart: current.depart,
        type: '渠道',
        month: mchannel_sale_total,
        day: channel_sale_total,
        proportion: proportion_q
      }])])
    }, [])
  },
  columns,
  apis: {
    httpList: (params) => httpDepartDayList({ ...params, companyNo: "GS2203161855277894" })
  }
};

export default contentConfig;
