import { ContentConfig, BeforeRequestType } from "/@/components/PageContent";
import { createHttpRequset } from "../../../_http/createRequset";
import { isBeyondTime } from "../../../_utils";
import { ElMessage } from "element-plus";

const columns = [
  {
    prop: "supplier_name",
    label: "采购方",
    _slot_: "name"
  },
  {
    prop: "total_cgd",
    label: "采购订单总量"
  },
  {
    prop: "tag_1",
    label: "线上库存品采购单",
    width: "130px"
  },
  {
    prop: "tag_2",
    label: "线上非库存品采购单",
    width: "130px"
  },
  {
    prop: "tag_c",
    label: "ToC采购单"
  },
  {
    prop: "tag_b",
    label: "ToB采购单"
  },
  {
    prop: "tag_zx",
    label: "咨询采返采购单",
    width: "120px"
  }
];

const contentConfig: ContentConfig = {
  columns,
  root: true,
  notPagination: true,
  superUserNoAction: false,
  notNeedCompanyFilter: true,
  beforeRequestList(params: Record<string, string>) {
    const { start_date, end_date } = params;
    if (!start_date || !end_date) {
      ElMessage.warning("请选择一个时间区间 ");
      return BeforeRequestType.EMPTY;
    }

    const beyondTime = isBeyondTime({
      message: "时间区间不能超过 30 天",
      start: start_date,
      end: end_date,
      len: 30
    });

    return beyondTime ? BeforeRequestType.EMPTY : BeforeRequestType.NEXT;
  },
  handleData(data) {
    if (!data) return [];
    const list = Object.keys(data).reduce(
      (prev, key) => [...prev, data[key]],
      []
    );
    const totalIdx = list.findIndex(({ name }) => name === "总计");

    if (totalIdx !== -1) {
      const total = list.splice(totalIdx, 1);
      list.push(...total);
    }

    return list;
  },
  apis: {
    httpList: createHttpRequset("pom")
  }
};

export default contentConfig;
