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

const columns = [
  {
    prop: "cat_1",
    label: "一级分类"
  },
  {
    prop: "cat_2",
    label: "二级分类"
  },
  {
    prop: "cat_3",
    label: "三级分类"
  },
  {
    prop: "tag_1",
    label: "库存品"
  },
  {
    prop: "tag_2",
    label: "非库存品"
  },
  {
    prop: "tag_3",
    label: "咨询采反"
  },
  {
    prop: "tag_supplier",
    label: "供应商发货"
  },
  {
    prop: "tag_person",
    label: "负责人发货"
  },
  {
    prop: "tag_logistics",
    label: "物流岗发货"
  }
];

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("oom")
  }
};

export default contentConfig;
