/* eslint-disable prettier/prettier */
import { ContentConfig } from "/@/components/PageContent";
import { departmentEveryDay } 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: 'depart',
    cellRenderer({ row }) {
      return row.depart.split('@').join("");
    }
  },
  {
    label: '当日营业收入',
    cellRenderer({ row }) {
      return unit2TenThousand(row.dayinfo.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.monthinfo.monthNetSales))
        ]),
        default: () => h('div', { class: 'table-size' }, [
          h('p', null, `直营/自营: ${unit2TenThousand(row.currentMonthPure[0].zy)}`),
          h('p', null, `支付渠道: ${unit2TenThousand(row.currentMonthPure[0].qd)}`),
        ])
      })
    }
  },
  {
    label: '当月成本',
    cellRenderer({ row }) {
      return h(ElPopover, { width: 220 }, {
        reference: () => h('div', null, [
          h(ElButton, { icon: useRenderIcon("information-line"), link: true }, {}),
          h('span', null, unit2TenThousand(Number(addition(Number(addition(row.zy_cost, row.qd_cost)).toFixed(2), row.post_fee)).toFixed(2)))
        ]),
        default: () => h('div', { class: 'table-size' }, [
          h('p', null, `直营/自营: ${unit2TenThousand(row.zy_cost)}`),
          h('p', null, `渠道: ${unit2TenThousand(row.qd_cost)}`),
          h('p', null, `物流费用: ${unit2TenThousand(row.post_fee)}`),
        ])
      })
    }
  },
  {
    label: '当月毛利',
    cellRenderer({ row }) {
      return h(ElPopover, { width: 220 }, {
        reference: () => h('div', null, [
          h(ElButton, { icon: useRenderIcon("information-line"), link: true }, {}),
          h('span', null, unit2TenThousand(Number(subtraction(Number(addition(row.zy_gross, row.qd_gross)).toFixed(2), row.post_fee)).toFixed(2)))
        ]),
        default: () => h('div', { class: 'table-size' }, [
          h('p', null, `直营/自营: ${unit2TenThousand(row.zy_gross)}`),
          h('p', null, `渠道: ${unit2TenThousand(row.qd_gross)}`),
        ])
      })
    }
  },
];

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

    let list = (data || []).map(
      ({
        depart,
        msale_total,
        mth_total,
        sale_total,
        th_total,
        total_tips,
        mzy_sale_total,
        mchannel_sale_total,
        channel_cost_total,
        zy_cost_total,
        mzy_cost_total,
        mchannel_cost_total,
        mpost_fee: post_fee
      }) => ({
        depart,
        total_tips,
        mzy_sale_total,
        mchannel_sale_total,
        channel_cost_total,
        zy_cost_total,
        mchannel_cost_total,
        mzy_cost_total,
        post_fee,
        dayinfo: { sale_total, th_total },
        monthinfo: { msale_total, mth_total }
      }));

    list = list.filter(item => Number(item.monthinfo.msale_total) !== 0 || Number(item.post_fee) !== 0);

    const total = list.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 }
    );

    let mapDepart = list.map(({ depart }) => depart);

    list = mapDepart.map(d => list.find(({ depart }) => depart === d));

    const mapToDepartment = {
      百辰: "客服部@百辰",
      泓源: "网络部@泓源",
      普润: "	项目部@普润",
      平台: "	平台部@泰文创"
    };

    const companies = [
      { value: "GS2203161855277894", label: "平台公司: 万宇" },
      { value: "GS2302231125079621", label: "业务公司: 百辰" },
      { value: "GS2302231323386950", label: "业务公司: 泓源" },
      { value: "GS2302231124114965", label: "业务公司: 普润&锦兴&知事" },
      { value: "GS2402020907088731", label: "平台公司: 泰文创" }
    ]


    // const company = companies.find(
    //   item => item.value === this.companyNo
    // );

    list = list.map(
      ({
        depart,
        total_tips,
        dayinfo,
        monthinfo,
        mchannel_sale_total,
        mzy_sale_total,
        mchannel_cost_total,
        post_fee,
        mzy_cost_total
      }) => {
        // mzy_sale_total = Number(addition(Number(multiplication(mzy_sale_total,0.05)).toFixed(2),mzy_sale_total)).toFixed(2);
        // mchannel_sale_total = Number(addition(Number(multiplication(mchannel_sale_total,0.05)).toFixed(2),mchannel_sale_total)).toFixed(2);
        /* 月净销售 = 月销售 - 月退货 **/
        let monthNetSales = subtraction(
          monthinfo.msale_total,
          monthinfo.mth_total
        );
        // monthNetSales = Number(addition(Number(multiplication(monthNetSales,0.05)).toFixed(2),monthNetSales)).toFixed(2);

        /* 月经销售完成率 = 月净销售 / 销售指标 **/
        const monthProportion = multiplication(
          division(monthNetSales, total_tips),
          100
        ).toFixed(2);


        return {
          total_tips, // 营收目标
          zy_cost: mzy_cost_total, // 自营成本
          qd_cost: mchannel_cost_total, // 渠道成本
          post_fee: post_fee,
          depart: mapToDepartment[depart],
          // company: company.label,
          currentMonthPure: [
            {
              zy: mzy_sale_total,
              qd: mchannel_sale_total,
            }
          ], // 当月营业收入(净)
          zy_gross: Number(
            subtraction(mzy_sale_total, mzy_cost_total)
          ).toFixed(2), // 自营毛利  = 自营营收 - 自营成本

          qd_gross: Number(
            subtraction(mchannel_sale_total, mchannel_cost_total)
          ).toFixed(2), // 渠道毛利 = 渠道营收 - 渠道成本


          dayinfo: {
            ...dayinfo,
                /** 日销售额 = 日销售额 - 日退货额 */ sale_total: subtraction(
              dayinfo.sale_total,
              dayinfo.th_total
            )
          },
          /* 占比 =  (当前部门销售额 / 各部门总销售额) / 100 **/
          proportion:
            multiplication(
              division(monthinfo.msale_total, total.month) || 0,
              100
            ) || 0,
          monthinfo: { monthNetSales, monthProportion: monthProportion }
        };
      }
    );

    let l = ["百辰", "泓源", "普润", '泰文创'];
    const cl = list.map(item => item.depart);
    l = l.filter(item => cl.findIndex(i => i.indexOf(item)) !== -1);

    return l.map(item => list.find(i => i.depart.indexOf(item) !== -1)).filter(item => !!item);
  },
  apis: {
    httpList: departmentEveryDay
  }
};

export default contentConfig;
