/* eslint-disable prettier/prettier */
import { ContentConfig } from "/@/components/PageContent";
import { companyEvery } from "/@/api/dashboard/newResults";
import { addition, division, multiplication, subtraction, unit2TenThousand } from "/@/utils/calc";
import { h } from "vue";
import { ElButton, ElPopover } from "element-plus";
import { useRenderIcon } from "/@/components/ReIcon/src/hooks";

const mapCompany = {
  "北京百辰荣达国际科贸有限公司": "百辰",
  "北京泓源广诚国际商贸有限公司": "泓源",
  "北京万宇恒通国际科贸有限公司": "万宇",
  "北京锦兴弘昌科技有限公司": "锦兴",
  "北京普润心堂商贸有限公司": "普润",
  "北京知事文化产业发展有限公司": "知事",
  "北京泰文创供应链管理有限公司": "泰文创",
}


const columns = [
  {
    label: '月度',
    prop: 'month',
  },
  {
    label: '营收目标',
    prop: 'month',
    cellRenderer(scope) {
      return unit2TenThousand(scope.row.total_tips)
    }
  },
  {
    label: '营业收入(净)',
    prop: 'month',
    cellRenderer(scope) {
      return unit2TenThousand(scope.row.pure_sale)
    }
  },
  {
    label: '营收完成(%)',
    prop: 'month',
    cellRenderer(scope) {
      return Number(scope.row.completion_rate).toFixed(2) + "%"
    }
  },
  {
    label: '毛利目标',
    cellRenderer(scope) {
      return unit2TenThousand(scope.row.cost_tips)
    }
  },
  {
    label: '毛利完成',
    cellRenderer(scope) {
      return unit2TenThousand(scope.row.pure_profit)
    }
  },
  {
    label: '毛利完成%',
    cellRenderer(scope) {
      return Number(scope.row.cost_rate).toFixed(2) + "%"
    }
  },
];

const contentConfig: ContentConfig = {
  title: "",
  superUserNoAction: false,
  showTitle: true,
  responseCode: 0,
  columns,
  noRequiredInit: true,
  isRequestArray: true,
  handleData(data = []) {

    let 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 {
        pure_sale,
        pure_purch,
        pure_profit,
        completion_rate,
        pure_interest_rate,
        month: tableItem.month,
        cost_tips: tableItem.cost_tips,
        total_tips: tableItem.total_tips,
        cost_rate: Number(tableItem.cost_tips) === 0 ? 0 : multiplication(division(pure_profit, tableItem.cost_tips) || 0, 100).toFixed(2)
      }
    })


    const totalItem = tableData.reduce((prev, curr) => {
      return {
        pure_sale: Number(addition(prev.pure_sale, curr.pure_sale)).toFixed(2),
        pure_purch: Number(addition(prev.pure_purch, curr.pure_purch)).toFixed(2),
        pure_profit: Number(addition(prev.pure_profit, curr.pure_profit)).toFixed(2),
        completion_rate: Number(addition(prev.completion_rate, curr.completion_rate)).toFixed(2),
        pure_interest_rate: Number(addition(prev.pure_interest_rate, curr.pure_interest_rate)).toFixed(2),
        total_tips: Number(addition(prev.total_tips, curr.total_tips)).toFixed(2),
        cost_tips: Number(addition(prev.cost_tips, curr.cost_tips)).toFixed(2),
        cost_rate: Number(addition(prev.cost_rate, curr.cost_rate)).toFixed(2),
        month: '合计',
      }
    }, {
      pure_sale: 0,
      pure_purch: 0,
      pure_profit: 0,
      completion_rate: 0,
      pure_interest_rate: 0,
      month: '合计',
      cost_tips: 0,
      total_tips: 0,
      cost_rate: 0
    })

    totalItem.completion_rate = multiplication(division(totalItem.pure_sale, totalItem.total_tips) || 0, 100)
    totalItem.cost_rate = Number(totalItem.cost_tips) === 0 ? 0 : multiplication(division(totalItem.pure_profit, totalItem.cost_tips) || 0, 100).toFixed(2)
    return [...tableData, totalItem];
  },
  apis: {
    httpList: companyEvery
  }
};

export default contentConfig;
