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

import { httpDepartDayList, httpCompanyDayList } 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 = [], params = {}) {
    return new Promise(resolve => {
      httpCompanyDayList({ daytime: params.daytime }).then(result => {
        if (Number(result.code) === 0) {
          const mapCompanySupplierSale = result.data.reduce((prev, current) => ({
            ...prev,
            [current.companyName]: subtraction(current.monthinfo[2].msale_total, current.monthinfo[2].mth_total)
          }), {})

          const mapCompanySupplierSaleDay = result.data.reduce((prev, current) => ({
            ...prev,
            [current.companyName]: subtraction(current.dayinfo[2].sale_total, current.dayinfo[2].th_total)
          }), {}) 

          let tableData = data.map(({
            depart,
            msale_total,
            mth_total,
            sale_total,
            th_total,
            total_tips
          }) => ({
            depart,
            total_tips,
            dayinfo: { sale_total, th_total },
            monthinfo: { msale_total, mth_total }
          }));

          tableData = ["百辰", "泓源", "普润", "平台"].map(c => {
            return tableData.find(({ depart }) => depart === c)
          })

          this.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
          })

          tableData = tableData.map(({ depart, total_tips, dayinfo, monthinfo }) => {
            /* 月净销售 = 月销售 - 月退货 **/
            let monthNetSales = subtraction(monthinfo.msale_total, monthinfo.mth_total) as unknown as number
            let dayNetSales = subtraction(dayinfo.sale_total, dayinfo.th_total) as unknown as number
            const supplierSalekey = Object.keys(mapCompanySupplierSale).find(key => key.includes(depart))
            const supplierSaleDaykey = Object.keys(mapCompanySupplierSaleDay).find(key => key.includes(depart))
            if (supplierSalekey) { monthNetSales = addition(monthNetSales, mapCompanySupplierSale[supplierSalekey]) }
            if (supplierSaleDaykey) { dayNetSales = addition(dayNetSales, mapCompanySupplierSaleDay[supplierSaleDaykey]) }
            if (supplierSalekey === '北京普润心堂商贸有限公司') { monthNetSales = addition(monthNetSales, mapCompanySupplierSale['北京锦兴弘昌科技有限公司']) }
            if (supplierSaleDaykey === '北京普润心堂商贸有限公司') { dayNetSales = addition(dayNetSales, mapCompanySupplierSaleDay['北京锦兴弘昌科技有限公司']) }

            /* 月经销售完成率 = 月净销售 / 销售指标 **/
            const monthProportion = multiplication(division(monthNetSales, total_tips), 100).toFixed(2)
            return {
              depart,
              total_tips,
              dayinfo: {
                ...dayinfo,
                /** 日销售额 = 日销售额 - 日退货额 */
                sale_total: dayNetSales
              },
              monthinfo: { monthNetSales, monthProportion: monthProportion },
              /* 占比 =  (当前部门销售额 / 各部门总销售额) / 100 **/
              proportion: multiplication(division(monthinfo.msale_total, this.total.month) || 0, 100) || 0
            }
          })


          resolve(tableData)
        }
      })
    })
  },
  columns,
  apis: {
    httpList: (params) => httpDepartDayList({ ...params, companyNo: "GS2203161855277894" })
  }
};

export default contentConfig;
