/* eslint-disable prettier/prettier */
import { ContentConfig } from "/@/components/PageContent";
import { companyEveryMonth } 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: 'companyName',
    // cellRenderer({ row }) {
    //   if (row.companyName.indexOf('普润') === -1) {
    //     return mapCompany[row.companyName] || row.companyName
    //   }
    //   return '普润'
    // }

    cellRenderer({ row }) {
      if (row.companyName.indexOf('普润') === -1) {
        return mapCompany[row.companyName] || row.companyName
      }

      return h(ElPopover, { width: 220 }, {
        reference: () => h('div', null, [h(ElButton, { icon: useRenderIcon("information-line"), link: true }, {}), '普润']),
        default: () => '普润&锦兴&知事'
      })
    }
  },
  {
    label: '当日营业收入',
    cellRenderer({ row }) {
      return unit2TenThousand(row.sale_total)
    }
  },
  {
    label: '营收目标',
    cellRenderer({ row }) {
      return unit2TenThousand(row.total_tips)
    }
  },
  {
    label: '营业收入(净)',
    cellRenderer({ row }) {
      return h(ElPopover, { width: 220 }, {
        reference: () => h('div', null, [
          h(ElButton, { icon: useRenderIcon("information-line"), link: true }, {}),
          h('span', null, unit2TenThousand(row.msale_total))
        ]),
        default: () => h('div', {
          class: 'table-size'
        }, [
          h('p', null, `直营/自营: ${unit2TenThousand(row.info[0][0].monthinfo.msale_total)}`),
          h('p', null, `支付渠道: ${unit2TenThousand(row.info[0][1].monthinfo.msale_total)}`),
          h('p', null, `供应商端: ${unit2TenThousand(row.info[0][2].monthinfo.msale_total)}`)
        ])
      })
    }
  },
  {
    label: '营收完成%',
    cellRenderer({ row }) {
      return (String(row.completion_rate) === 'NaN' ? 0 : row.completion_rate) + "%"
    }
  },
  {
    label: '毛利目标',
    cellRenderer({ row }) {
      return unit2TenThousand(row.cost_tips)
    }
  },
  {
    label: '毛利完成%',
    cellRenderer({ row }) {
      return row.gross_completion_rate + '%'
    }
  },
  {
    label: '成本合计',
    cellRenderer({ row }) {
      return h(ElPopover, { width: 220 }, {
        reference: () => h('div', null, [
          h(ElButton, { icon: useRenderIcon("information-line"), link: true }, {}),
          h('span', null, unit2TenThousand(row.msale_total))
        ]),
        default: () => h('div', { class: 'table-size' }, [
          h('p', null, `直营/自营: ${unit2TenThousand(row.info[0][0].monthinfo.mcost_total)}`),
          h('p', null, `支付渠道: ${unit2TenThousand(row.info[0][1].monthinfo.mcost_total)}`),
          h('p', null, `供应商端: ${unit2TenThousand(row.info[0][2].monthinfo.mcost_total)}`),
          h('p', null, `物流费用: ${unit2TenThousand(row.info[0][3].monthinfo.mcost_total)}`)
        ])
      })
    }
  },
  {
    label: '实际毛利率',
    cellRenderer({ row }) {
      return unit2TenThousand(row.gross_sale_completion_rate) + "%"
    }
  }
];

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

    let tableData = [];

    (list || []).forEach(({ companyName, monthinfo, dayinfo, total_tips, cost_tips }) => {
      const mapResponseType = { '1': '自营', '2': '渠道', '3': '供应商端', '4': '物流费用' }
      const types = Object.keys(mapResponseType)
      let prev_sale_total = 0;
      let prev_msale_total = 0;
      let prev_cost_total = 0;
      let prev_mcost_total = 0;

      const item = types.map(type => {
        const _monthinfo = monthinfo.find(month => String(month.type) === type) || {}
        const _dayinfo = dayinfo.find(day => String(day.type) === type) || {}

        /** 月营业收入 = 月销售额 - 月退货额  */
        let msale_total = 0;
        if (Number(type) !== 4) {
          msale_total = subtraction(_monthinfo.msale_total, _monthinfo.mth_total) || 0
        }

        /** 日营业收入 = 日销售额 - 日退货额 */
        const sale_total = subtraction(_dayinfo.sale_total, _dayinfo.th_total) || 0;
        /** 日成本 = 日采购额 - 日退货额 */
        const cost_total = subtraction(_dayinfo.cgd_total, _dayinfo.cgd_th_total) || 0;

        /** 月成本 = 月采购额 - 月退货额 */
        let mcost_total = 0;

        if (Number(type) !== 4) {
          mcost_total = subtraction(_monthinfo.mcgd_total, _monthinfo.mcgd_th_total) || 0;
        } else {
          mcost_total = Number(_monthinfo.msale_total || 0).toFixed(2);
        }


        prev_sale_total = Number(addition(prev_sale_total, sale_total)).toFixed(2);
        prev_msale_total = Number(addition(prev_msale_total, msale_total)).toFixed(2);
        prev_cost_total = Number(addition(prev_cost_total, cost_total)).toFixed(2);
        prev_mcost_total = Number(addition(prev_mcost_total, mcost_total)).toFixed(2);


        return {
          type: mapResponseType[type],
          dayinfo: { ..._dayinfo, sale_total },
          monthinfo: { ...monthinfo, msale_total, mcost_total }
        }
      })

      const gross_completion = Number(subtraction(prev_msale_total, prev_mcost_total)).toFixed(2) //当月毛利完成 = 月营收 - 月成本


      tableData = [
        ...tableData,
        {
          companyName,  // 公司名称
          cost_tips, // 当月成本指标
          total_tips, // 当月营收目标
          info: [{ ...item }], // 直营..渠道 销售
          sale_total: prev_sale_total, // 当日营业收入
          cost_total: prev_cost_total, // 当日成本总额
          msale_total: prev_msale_total, // 当月营业收入
          mcost_total: prev_mcost_total,// 当月成本总额
          gross_completion, //当月毛利完成 = 月营收 - 月成本
          completion_rate: multiplication(division(prev_msale_total, total_tips), 100).toFixed(2), // 当月应收完成率
          gross_completion_rate: Number(cost_tips) === 0 ? 0 : multiplication(division(gross_completion, cost_tips) || 0, 100).toFixed(2), // 当月毛利完成率
          gross_sale_completion_rate: Number(multiplication(division(gross_completion, prev_msale_total) || 0, 100)).toFixed(2), // 本月毛利率 =  当月毛利完成 / 当月营业收入
        }
      ]

    })

    let i = ["北京百辰荣达国际科贸有限公司", '北京泓源广诚国际商贸有限公司', "北京普润心堂商贸有限公司"];
    const l = tableData.map(item => item.companyName);
    i = i.filter(item => l.includes(item));
    return i.map(item => tableData.find(l => l.companyName === item));
  },
  apis: {
    httpList: companyEveryMonth
  }
};

export default contentConfig;
